Eclipse XSD 生成枚举类型的Schema的实例详解
程序员文章站
2022-08-06 19:05:39
eclipse xsd 生成枚举类型的schema的实例详解
前言:
因为网上关于eclipse xsd的中文资料比较少,而且关于eclipse xsd的范例代码也凤毛...
eclipse xsd 生成枚举类型的schema的实例详解
前言:
因为网上关于eclipse xsd的中文资料比较少,而且关于eclipse xsd的范例代码也凤毛麟角,但是有的时候我们需要生成一个带枚举限定的简单类型的xsd schema,比如下面的格式,
<?xml version="1.0" encoding="utf-8"?><schema xmlns="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.w3.org/2001/xmlschema"> <complextype name="studenttype"> <sequence> <element maxoccurs="1" minoccurs="1" name="username" type="string"/> <element maxoccurs="1" minoccurs="1" name="password" type="string"/> <element maxoccurs="1" minoccurs="1" name="alignment" type="alignmenttype"/> </sequence> </complextype> <simpletype name="alignmenttype"> <restriction base="string"> <enumeration value="right"/> <enumeration value="middle"/> <enumeration value="left"/> </restriction> </simpletype> <element name="student" type="studenttype"/> </schema>
其中, <simpletype name="alignmenttype"> 代表的就是一个带枚举限定的简单类型。那么应该如何生成呢?请见参考下面的代码。
import org.eclipse.xsd.xsdcomplextypedefinition; import org.eclipse.xsd.xsdcompositor; import org.eclipse.xsd.xsdelementdeclaration; import org.eclipse.xsd.xsdenumerationfacet; import org.eclipse.xsd.xsdfactory; import org.eclipse.xsd.xsdimport; import org.eclipse.xsd.xsdinclude; import org.eclipse.xsd.xsdmodelgroup; import org.eclipse.xsd.xsdparticle; import org.eclipse.xsd.xsdredefine; import org.eclipse.xsd.xsdschema; import org.eclipse.xsd.xsdschemadirective; import org.eclipse.xsd.xsdsimpletypedefinition; import org.eclipse.xsd.util.xsdresourceimpl; import org.eclipse.xsd.util.xsdutil; import org.junit.test; import org.w3c.dom.element; public class enumfacettest { protected static xsdfactory xsdfactory = xsdfactory.einstance; private void createaligementelement(xsdsimpletypedefinition aligmenttype){ string[] cellaligements={"right","middle","left"}; for(int i=0;i<cellaligements.length;i++){ xsdenumerationfacet alenum=xsdfactory.einstance.createxsdenumerationfacet(); alenum.setlexicalvalue(cellaligements[i]); //aligmenttype.getfacets().add(alenum); aligmenttype.getfacetcontents().add(alenum); } } /** <?xml version="1.0" encoding="utf-8"?><schema xmlns="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.w3.org/2001/xmlschema"> <complextype name="studenttype"> <sequence> <element maxoccurs="1" minoccurs="1" name="username" type="string"/> <element maxoccurs="1" minoccurs="1" name="password" type="string"/> <element maxoccurs="1" minoccurs="1" name="alignment" type="alignmenttype"/> </sequence> </complextype> <simpletype name="alignmenttype"> <restriction base="string"> <enumeration value="right"/> <enumeration value="middle"/> <enumeration value="left"/> </restriction> </simpletype> <element name="student" type="studenttype"/> </schema> */ @test public void enumfacettest() { string targenamespace="http://www.w3.org/2001/xmlschema"; xsdschema xsdschema=xsdfactory.createxsdschema(); xsdschema.settargetnamespace(targenamespace); xsdschema.getqnameprefixtonamespacemap().put(null, "http://www.w3.org/2001/xmlschema"); //1.1 create complex type:student xsdcomplextypedefinition complextypedef = xsdfactory.createxsdcomplextypedefinition(); complextypedef.settargetnamespace(xsdschema.gettargetnamespace()); complextypedef.setname("studenttype"); xsdparticle xsdparticle=xsdfactory.createxsdparticle(); xsdmodelgroup xsdmodulegroup=xsdfactory.createxsdmodelgroup(); xsdmodulegroup.setcompositor(xsdcompositor.sequence_literal); xsdparticle.setcontent(xsdmodulegroup); complextypedef.setcontent(xsdparticle); complextypedef.setcontenttype(xsdparticle); xsdschema.getcontents().add(complextypedef); //1.2 add element for complex type //1.2.1 username element xsdparticle localxsdparticle = xsdfactory.createxsdparticle(); localxsdparticle.setminoccurs(1); localxsdparticle.setmaxoccurs(1); xsdelementdeclaration localxsdelementdeclaration = xsdfactory.createxsdelementdeclaration(); localxsdelementdeclaration.settargetnamespace(targenamespace); localxsdelementdeclaration.setname("username"); xsdschema localxsdschema = xsdutil.getschemaforschema("http://www.w3.org/2001/xmlschema"); xsdsimpletypedefinition localsimpletype=localxsdschema.resolvesimpletypedefinition("string"); localxsdelementdeclaration.settypedefinition(localsimpletype); localxsdparticle.setcontent(localxsdelementdeclaration); xsdmodulegroup.getcontents().add(localxsdparticle); //1.2.2 password element localxsdparticle = xsdfactory.createxsdparticle(); localxsdparticle.setminoccurs(1); localxsdparticle.setmaxoccurs(1); localxsdelementdeclaration = xsdfactory.createxsdelementdeclaration(); localxsdelementdeclaration.settargetnamespace(targenamespace); localxsdelementdeclaration.setname("password"); localxsdschema = xsdutil.getschemaforschema("http://www.w3.org/2001/xmlschema"); localsimpletype=localxsdschema.resolvesimpletypedefinition("string"); localxsdelementdeclaration.settypedefinition(localsimpletype); localxsdparticle.setcontent(localxsdelementdeclaration); xsdmodulegroup.getcontents().add(localxsdparticle); //1.2.3.1 create simple type with xsdenumerationfacet--------------- xsdsimpletypedefinition xsdsimpletypedefinition = xsdfactory.einstance.createxsdsimpletypedefinition(); xsdsimpletypedefinition basetypedefinition = xsdschema.resolvesimpletypedefinitionuri("string"); xsdsimpletypedefinition.setbasetypedefinition(basetypedefinition); xsdsimpletypedefinition.setname("alignmenttype"); createaligementelement(xsdsimpletypedefinition); xsdschema.getcontents().add(xsdsimpletypedefinition); //1.2.3.2 create element with simple type -------------- localxsdparticle = xsdfactory.createxsdparticle(); localxsdparticle.setminoccurs(1); localxsdparticle.setmaxoccurs(1); localxsdelementdeclaration = xsdfactory.createxsdelementdeclaration(); localxsdelementdeclaration.settargetnamespace(targenamespace); localxsdelementdeclaration.setname("alignment"); localxsdschema = xsdutil.getschemaforschema("http://www.w3.org/2001/xmlschema"); localxsdelementdeclaration.settypedefinition(xsdsimpletypedefinition); localxsdparticle.setcontent(localxsdelementdeclaration); xsdmodulegroup.getcontents().add(localxsdparticle); //2.create xsdelementdeclaration and attached complex type to xsd element xsdelementdeclaration xsdeelement=xsdfactory.createxsdelementdeclaration(); xsdeelement.setname("student"); xsdeelement.settypedefinition(complextypedef); xsdschema.getcontents().add(xsdeelement); //3.print schema schemaprintservice.printschema(xsdschema); } } class schemaprintservice { /** * print schema to console * * @param xsdschema */ public static void printschema(xsdschema xsdschema) { system.out.println("<!-- ===== schema composition ====="); printdirectives(" ", xsdschema); system.out.println("-->"); system.out .println("<!-- [ " + xsdschema.getschemalocation() + " ] -->"); xsdschema.updateelement(); element element = xsdschema.getelement(); if (element != null) { // print the serialization of the model. xsdresourceimpl.serialize(system.out, element); } } private static void printschemastart(xsdschema xsdschema) { system.out.print("<schema targetnamespace=\""); if (xsdschema.gettargetnamespace() != null) { system.out.print(xsdschema.gettargetnamespace()); } system.out.print("\" schemalocation=\""); if (xsdschema.getschemalocation() != null) { system.out.print(xsdschema.getschemalocation()); } system.out.print("\">"); } private static void printdirectives(string indent, xsdschema xsdschema) { system.out.print(indent); printschemastart(xsdschema); system.out.println(); if (!xsdschema.getreferencingdirectives().isempty()) { system.out.println(indent + " <referencingdirectives>"); for (xsdschemadirective xsdschemadirective : xsdschema .getreferencingdirectives()) { xsdschema referencingschema = xsdschemadirective.getschema(); system.out.print(indent + " "); printschemastart(referencingschema); system.out.println(); system.out.print(indent + " "); if (xsdschemadirective instanceof xsdimport) { xsdimport xsdimport = (xsdimport) xsdschemadirective; system.out.print("<import namespace=\""); if (xsdimport.getnamespace() != null) { system.out.print(xsdimport.getnamespace()); } system.out.print("\" schemalocation=\""); } else if (xsdschemadirective instanceof xsdredefine) { system.out.print("<redefine schemalocation=\""); } else if (xsdschemadirective instanceof xsdinclude) { system.out.print("<include schemalocation=\""); } if (xsdschemadirective.getschemalocation() != null) { system.out.print(xsdschemadirective.getschemalocation()); } system.out.println("\"/>"); system.out.println(indent + " </schema>"); } system.out.println(indent + " </referencingdirectives>"); } if (!xsdschema.getincorporatedversions().isempty()) { system.out.println(indent + " <incorporatedversions>"); for (xsdschema incorporatedversion : xsdschema .getincorporatedversions()) { printdirectives(indent + " ", incorporatedversion); } system.out.println(indent + " </incorporatedversions>"); } system.out.println(indent + "</schema>"); } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Hibernate 修改数据的实例详解
下一篇: 忆江南茶叶,江南忆,最忆是杭州