首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

JaxB-运用举例

2013-04-20 
JaxB-应用举例预期的XML配置文件达到的效果如下:?xml version1.0 encodingUTF-8 standaloneyes?

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));

}

热点排行