Using Org.eclipse.xsd.XSDEnumerationFacet to generate schema for enumerated types

Source: Internet
Author: User

Because there is less information about Eclipse XSD on the web, and the sample code for Eclipse XSD is rare, sometimes we need to generate an XSD Schema with an enumeration-qualified simple type, such as the following format,

<?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>


Among them, <simpletype name= "Alignmenttype" > Represents aa simple type with enumeration qualification. So how should it be generated? See the code below for reference.

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" Targetnames Pace= "Http://www.w3.org/2001/XMLSchema" > <complextype name= "Studenttype" > <sequence> <element m  axoccurs= "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"/> </sequen ce> </complexType> <simpletype name= "Alignmenttype" > <restriction base= "string" > <enumer ation value= "right"/> <enumeration value= "middle"/> <enumeration value= "left"/> </restricti on> </simpleType> <element name= "Student" type= "Studenttype"/></schema> */@Test Public V OID 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: Studentxsdcomplextypedefinition 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 elementxsdparticle 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 Elementlocalxsdparticle = 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 Elementxsdelementdeclaration xsdeelement=xsdfactory.createxsdelementdeclaration (); XsdEelement.setName ("Student" ); Xsdeelement.settypedefinition (complextypedef); Xsdschema.getcontents (). Add (xsdeelement);//3.Print        Schemaschemaprintservice.printschema (Xsdschema);  }}class Schemaprintservice {/** * Print Schema to console * * @param xsdschema */public static void Printschema (Xsdschema Xsdschema) {System.out.println ("<!--===== Schema composition =====");p rintdirectives ("", 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=\"); 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);p Rintschemastart ( 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 + "");p Rintschemastart (ReferenciNgschema); System.out.println (); System.out.print (Indent + ""); if (xsdschemadirective instanceof xsdimport) {xsdimport Xsdimport = (xsdimport) Xsdsch emadirective; 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>");}}




Using Org.eclipse.xsd.XSDEnumerationFacet to generate schema for enumerated types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.