`
christ_man
  • 浏览: 5583 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JaxB-应用举例

 
阅读更多

预期的XML配置文件达到的效果如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ns2:xmlSourceGroup xmlns:ns2="http://www.yonyou.com.cn/search-metadata">

    <sourceGroups>

        <sourceGroup securityPlugin="sdfsdf" description="sdfsdf" name="sfsdf" id="sfdsd">

            <sources>

                <source description="哈哈哈" name="testsource" id="han">

                    <parameters/>

                    <fields>

                        <field fieldType="Boolean" strategy="text_whitespace" name="sdf"/>

                    </fields>

                </source>

            </sources>

        </sourceGroup>

    </sourceGroups>

</ns2:xmlSourceGroup>

 

 

@XmlAccessorType(XmlAccessType.FIELD)

@XmlAccessorOrder(XmlAccessOrder.UNDEFINED)

@XmlType(propOrder = { "name", "strategy", "isKey", "isACL", "pinYinType",

"dataOrigin", "fieldType", "compoundType", "isSuggest" })

public class Field implements IField ,Serializable {

 

private static final long serialVersionUID = -3018559388328279993L;

 

@XmlAttribute

private String name;

 

@XmlValue

private String dataOrigin;

 

@XmlAttribute

private String strategy;

 

@XmlAttribute

private Boolean isACL;

 

@XmlAttribute

private String pinYinType;

 

@XmlAttribute

private String fieldType;

 

@XmlAttribute

private Boolean isKey;

 

@XmlAttribute

private String compoundType;

 

@XmlAttribute

private Boolean isSuggest;

 

@Override

public String getDataOrigin() {

return dataOrigin;

}

 

@Override

public String getName() {

return name;

}

 

@Override

public String getStrategy() {

return strategy;

}

 

@Override

public Boolean isACL() {

if (isACL == null) {

return Boolean.FALSE;

}

return isACL;

}

 

@Override

public String getPinYinType() {

if (pinYinType == null)

return PinYinType.Nonsupport.toString();

return pinYinType;

}

 

public void setACL(Boolean isACL) {

this.isACL = isACL ? Boolean.TRUE : null;

}

 

public void setDataOrigin(String dataOrigin) {

this.dataOrigin = dataOrigin;

}

 

public void setName(String name) {

this.name = name;

}

 

public void setStrategy(String strategy) {

this.strategy = strategy;

}

 

public void setPinYinType(String pinYinType) {

this.pinYinType = pinYinType;

}

 

public String getFieldType() {

return fieldType;

}

 

public void setFieldType(String fieldType) {

this.fieldType = fieldType;

}

 

@Override

public Boolean isKey() {

if (isKey == null) {

return Boolean.FALSE;

}

return isKey;

}

 

public void setKey(Boolean isKey) {

this.isKey = isKey ? Boolean.TRUE : null;

 

}

 

@Override

public String getCompoundType() {

return compoundType;

}

 

public void setCompoundType(String compoundType) {

this.compoundType = compoundType;

}

 

@Override

public Boolean isSuggest() {

if (isSuggest == null) {

return Boolean.FALSE;

}

return isSuggest;

}

 

public void setSuggest(Boolean isSuggest) {

this.isSuggest = isSuggest ? Boolean.TRUE : null;

}

 

@Override

public int hashCode() {

return getName().hashCode() ^ 1234321 ;

}

 

@Override

public boolean equals(Object obj) {

if(obj == null){

return false;

}

if(obj instanceof Field){

Field anotherField = (Field) obj;

if(this.getName().equals(anotherField.getName())){

return true;

}

}

return false;

}

 

}

 

 

@XmlAccessorType(XmlAccessType.FIELD)

@XmlAccessorOrder(XmlAccessOrder.UNDEFINED)

public final class Source implements ISource ,Serializable {

 

/**

*/

private static final long serialVersionUID = 8365332767467941142L;

 

@XmlAttribute

private String id;

 

@XmlAttribute

private String name;

 

@XmlAttribute

private String description;

 

@XmlElementWrapper(name = "parameters")

@XmlElement(name = "parameter")

private List<Parameter> parameters;

 

@XmlElementWrapper(name = "fields")

@XmlElement(name = "field")

private List<Field> fields;

 

@Override

public String getId() {

return id;

}

 

@Override

public String getName() {

return name;

}

 

@Override

public String getDescription() {

return description;

}

 

@Override

public List<Parameter> getParameters() {

return parameters;

}

 

public Parameter getParameter(String paramName){

if(parameters == null)

return null;

for(Parameter param : parameters){

if(param.getName().equals(paramName))

return param;

}

return null;

}

 

@Override

public List<Field> getFields() {

return fields;

}

 

public void setFields(List<Field> fields) {

this.fields = fields;

}

 

public void setId(String id) {

this.id = id;

}

 

public void setName(String name) {

this.name = name;

}

 

public void setDescription(String description) {

this.description = description;

}

 

public void setParameters(List<Parameter> parameters) {

this.parameters = parameters;

}

 

@Override

public int hashCode() {

return id.hashCode();

}

 

@Override

public boolean equals(Object obj) {

if(obj == null)

return false ;

if(obj instanceof Source)

{

Source anObject = (Source)obj ;

if(this.getId().equals(anObject.getId()))

return true;

}

return false;

}

 

}

 

 

 

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType

public class SourceGroup implements ISourceGroup,Serializable {

 

/**

*/

private static final long serialVersionUID = -8087962860520149779L;

 

@XmlAttribute

private String id;

 

@XmlAttribute

private String name;

 

@XmlAttribute

private String description;

 

@XmlAttribute

private String sourceType;

 

@XmlAttribute

private String securityPlugin;

 

@XmlElementWrapper(name = "sources")

@XmlElement(name = "source")

private List<Source> sources;

 

@Override

public String getDescription() {

return description;

}

 

@Override

public String getId() {

return id;

}

 

@Override

public String getName() {

return name;

}

 

public String getSecurityPlugin() {

return securityPlugin;

}

 

public List<Source> getSources() {

return sources;

}

 

public String getSourceType() {

return sourceType;

}

 

public void setDescription(String description) {

this.description = description;

}

 

public void setId(String id) {

this.id = id;

}

 

public void setName(String name) {

this.name = name;

}

 

public void setSecurityPlugin(String securityPlugin) {

this.securityPlugin = securityPlugin;

}

 

public void setSources(List<Source> sources) {

this.sources = sources;

}

 

public void setSourceType(String sourceType) {

this.sourceType = sourceType;

}

 

@Override

public int hashCode() {

return id.hashCode();

}

 

public Source getSource(String sourceId) {

if (sourceId == null)

return null;

for (Source src : sources) {

if (src.getId().equals(sourceId))

return src;

}

return null;

}

 

public void removeSource(String sourceID) {

Source removeSource = getSource(sourceID);

sources.remove(removeSource);

}

 

@Override

public boolean equals(Object obj) {

if (obj == null)

return false;

if (obj instanceof SourceGroup) {

SourceGroup anObject = (SourceGroup) obj;

if (this.getId().equals(anObject.getId())) {

return true;

}

}

return false;

}

 

}

 

 

最终的与配置文件相对应的JavaBean如下:

@XmlRootElement(name = "xmlSourceGroup")

@XmlType

public class XMLSourceGroup implements Serializable {

 

private static final long serialVersionUID = 1865617884435489731L;

 

@XmlElementWrapper(name = "sourceGroups")

@XmlElement(name = "sourceGroup")

List<SourceGroup> sourceGroups;

 

public List<SourceGroup> getSourceGroups() {

return sourceGroups;

}

 

public void setSourceGroups(List<SourceGroup> sourceGroups) {

this.sourceGroups = sourceGroups;

}

 

public void addGroup(SourceGroup group) {

if (group == null)

return;

if (sourceGroups == null) {

sourceGroups = new ArrayList<SourceGroup>();

}

sourceGroups.add(group);

}

 

public void addGroups(List<SourceGroup> addGroups) {

if (addGroups == null)

return;

if (sourceGroups == null) {

sourceGroups = new ArrayList<SourceGroup>();

}

 

for (SourceGroup group : addGroups) {

if (!sourceGroups.contains(group)) {

sourceGroups.add(group);

}

}

}

 

public void removeGroup(SourceGroup group) {

if (sourceGroups == null)

return;

sourceGroups.remove(group);

}

 

public void removeGroup(String groupID) {

SourceGroup group = getSourceGroup(groupID);

removeGroup(group);

}

 

public void addSource(Source source, String group) {

SourceGroup srcGroup = getSourceGroup(group);

List<Source> sources = srcGroup.getSources();

if (sources == null) {

sources = new ArrayList<Source>();

}

sources.add(source);

srcGroup.setSources(sources);

}

 

public void removeSource(Source source, String group) {

SourceGroup srcGroup = getSourceGroup(group);

List<Source> sources = srcGroup.getSources();

if (sources == null) {

return;

}

sources.remove(source);

}

 

public void removeSource(String groupID, String sourceID) {

SourceGroup srcGroup = getSourceGroup(groupID);

if (srcGroup == null)

return;

srcGroup.removeSource(sourceID);

}

 

public SourceGroup getSourceGroup(String groupID) {

if (sourceGroups == null)

return null;

for (SourceGroup group : sourceGroups) {

if (group.getId().equals(groupID)) {

return group;

}

}

return null;

}

 

public String[] getGroupIds() {

if (sourceGroups == null)

return null;

List<String> ids = new ArrayList<String>();

for (SourceGroup group : sourceGroups) {

ids.add(group.getId());

}

return ids.toArray(new String[0]);

}

}

 

 

 

读取逻辑:

      private static void loadGroupConfig() {

JAXBContext jc;

Unmarshaller unmarshaller = null;

try {

jc = initJAXBContext(XMLSourceType.class);

unmarshaller = jc.createUnmarshaller();

} catch (JAXBException e) {

throw new RuntimeException("load " + IMetaDataResource.SOURCE_GROUP

+ " error, init unmarshaller failed!", e);

}

String ncHome = RuntimeEnv.getInstance().getNCHome();

File configFile = new File(ncHome, IMetaDataResource.SOURCE_GROUP_FILE);

XMLSourceGroup defaultGroup = null;

xmlSourceGroup = new XMLSourceGroup();

if (configFile.exists()) {

try {

defaultGroup = (XMLSourceGroup) unmarshaller

.unmarshal(configFile);

xmlSourceGroup.addGroups(defaultGroup.getSourceGroups());

} catch (Exception e) {

AnteLogger.error(IMetaDataResource.SOURCE_GROUP + " load failed!",

e);

}

}

FileResource[] moduleGroupFile = new ModulesFileResourceLoader(

new File(ncHome, "modules"))

.getResource("config\\searchconfig\\source-group-config.xml");

for (FileResource resource : moduleGroupFile) {

try {

XMLSourceGroup moduleGroup = (XMLSourceGroup) unmarshaller

.unmarshal(resource.getFile());

xmlSourceGroup.addGroups(moduleGroup.getSourceGroups());

} catch (JAXBException e) {

try {

AnteLogger.error("parse search source group ["

+ resource.getFile().getCanonicalPath() + "]"

+ e.getMessage());

} catch (IOException e1) {

// none to do ;

}

}

}

 

}

 

写入逻辑:

        public static void saveToLocal(XMLSourceGroup xmlSourceGroup)

throws Exception {

JAXBContext jc;

Marshaller marshaller = null;

try {

jc = initJAXBContext(XMLSourceGroup.class);

marshaller = jc.createMarshaller();

} catch (JAXBException e) {

throw new RuntimeException("save " + IMetaDataResource.SOURCE_GROUP

+ " error, init marshaller failed!", e);

}

String ncHome = RuntimeEnv.getInstance().getNCHome();

File configFile = new File(ncHome, IMetaDataResource.SOURCE_GROUP_FILE);

if (!configFile.canWrite()) {

configFile.setWritable(true);

}

marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(xmlSourceGroup, new FileOutputStream(configFile));

}

分享到:
评论

相关推荐

    javax.rar(jaxb-impl-2.3.0、jaxb-core-2.3.0、jaxb-api-2.3.0)

    标题中的"jaxb-impl-2.3.0、jaxb-core-2.3.0、jaxb-api-2.3.0"是JAXB的不同组件版本号,它们在处理XML到Java对象之间的转换时起到关键作用。在描述中提到的“Maven项目中缺少jaxb-api的异常报错”,通常指的是在运行...

    jaxb-api-2.1.jar 和 jaxb-impl-2.1.8.jar

    `jaxb-api-2.1.jar` 和 `jaxb-impl-2.1.8.jar` 是Java应用程序中用于XML绑定(Java Architecture for XML Binding,简称JAXB)的重要库文件。JAXB是Java SE和Java EE平台的标准部分,它提供了一种将XML文档与Java...

    jaxb-api-2.3.1-API文档-中文版.zip

    赠送jar包:jaxb-api-2.3.1.jar; 赠送原API文档:jaxb-api-2.3.1-javadoc.jar; 赠送源代码:jaxb-api-2.3.1-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.3.1.pom; 包含翻译后的API文档:jaxb-api-2.3.1-...

    jaxb-runtime-2.3.5-API文档-中英对照版.zip

    赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...

    jaxb-runtime-2.3.5-API文档-中文版.zip

    赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...

    jackson-module-jaxb-annotations-2.7.8-API文档-中英对照版.zip

    赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...

    JAXB2 jaxb-api.jar jaxb-xjc.jar jaxb-impl.jar activation.jar

    1. **jaxb-api.jar**:这是JAXB2的主要API接口定义,包含了所有的注解和接口,如`@XmlRootElement`、`@XmlElement`等,以及用于转换的核心类,如`Unmarshaller`和`Marshaller`。这个jar文件提供了与XML绑定的基本...

    jaxb-svg11-1.0.2-API文档-中文版.zip

    赠送jar包:jaxb-svg11-1.0.2.jar; 赠送原API文档:jaxb-svg11-1.0.2-javadoc.jar; 赠送源代码:jaxb-svg11-1.0.2-sources.jar; 赠送Maven依赖信息文件:jaxb-svg11-1.0.2.pom; 包含翻译后的API文档:jaxb-svg11...

    activation.jar jaxb1-impl.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jsr173_1.0

    5. **jaxb-xjc.jar**:这个库包含了XJC工具,它是JAXB编译器,可以将XML Schema转换为对应的Java源代码。通过XJC,开发者可以直接从XML Schema生成Java类,从而简化了处理XML数据的工作。 6. **jsr173_1.0_api.jar*...

    jaxb-api.jar.jaxws-api.zip_ jaxb-api.jar_cxf_jax-ws.jar_jaxb-api

    标题提到的"jaxb-api.jar.jaxws-api.zip_ jaxb-api.jar_cxf_jax-ws.jar_jaxb-api",是针对这个问题提供的一组支持包。 **JAXB(Java Architecture for XML Binding)** 是Java平台的一个标准,它允许开发者将XML文...

    jaxb-core-2.3.0.jar

    有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-core-2.3.0.jar

    jaxb-api jaxb-impl jar

    `jaxb-impl.jar`包含了编译器工具,如`JAXBCompiler`,以及运行时支持,使得开发者能够在应用程序中直接使用JAXB服务。 在实际开发中,当处理XML数据时,我们首先会导入`jaxb-api.jar`,然后根据需求选择是否引入`...

    jaxb-impl.jar jaxb-api.jar jsr173_1.0_api.jar

    `jaxb-api.jar`则包含了JAXB的公共API,这是开发人员在编写JAXB应用程序时需要导入的库。它定义了如`javax.xml.bind.annotation`和`javax.xml.bind`等包,提供了注解和API,使得开发者能够声明哪些Java类和属性应该...

    jaxb-api-2.2 jaxb-impl

    在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...

    jaxb-core-2.2.10-b140310.1920-API文档-中文版.zip

    赠送jar包:jaxb-core-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-core-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-core-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-core-...

    jaxb-impl-2.2.10-b140310.1920-API文档-中文版.zip

    赠送jar包:jaxb-impl-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-impl-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-impl-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-impl-...

    jaxb-impl-2.3.0.jar

    有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-impl-2.3.0.jar

    jersey-media-jaxb-2.22.2-API文档-中英对照版.zip

    赠送jar包:jersey-media-jaxb-2.22.2.jar; 赠送原API文档:jersey-media-jaxb-2.22.2-javadoc.jar; 赠送源代码:jersey-media-jaxb-2.22.2-sources.jar; 赠送Maven依赖信息文件:jersey-media-jaxb-2.22.2.pom;...

    jaxb-impl-2.1.13.jar

    jaxb-impl-2.1.13.jar

    jaxb-core-2.3.0.1.jar

    jaxb-core-2.3.0.1.jar

Global site tag (gtag.js) - Google Analytics