Eclipse XSD 生成枚舉類型的Schema的實例詳解
eclipse xsd 生成枚舉類型的schema的實例詳解
前言:
因為網(wǎng)上關于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>"); } }
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
- jsp+servlet實現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設計的實現(xiàn)方法
- 將properties文件的配置設置為整個Web應用的全局變量實現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎實踐
- JavaServlet的文件上傳和下載實現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法