Eclipse XSD 生成枚举类型的Schema的实例详解
EclipseXSD生成枚举类型的Schema的实例详解
前言:
因为网上关于EclipseXSD的中文资料比较少,而且关于EclipseXSD的范例代码也凤毛麟角,但是有的时候我们需要生成一个带枚举限定的简单类型的XSDSchema,比如下面的格式,
其中,
importorg.eclipse.xsd.XSDComplexTypeDefinition;
importorg.eclipse.xsd.XSDCompositor;
importorg.eclipse.xsd.XSDElementDeclaration;
importorg.eclipse.xsd.XSDEnumerationFacet;
importorg.eclipse.xsd.XSDFactory;
importorg.eclipse.xsd.XSDImport;
importorg.eclipse.xsd.XSDInclude;
importorg.eclipse.xsd.XSDModelGroup;
importorg.eclipse.xsd.XSDParticle;
importorg.eclipse.xsd.XSDRedefine;
importorg.eclipse.xsd.XSDSchema;
importorg.eclipse.xsd.XSDSchemaDirective;
importorg.eclipse.xsd.XSDSimpleTypeDefinition;
importorg.eclipse.xsd.util.XSDResourceImpl;
importorg.eclipse.xsd.util.XSDUtil;
importorg.junit.Test;
importorg.w3c.dom.Element;
publicclassEnumFacetTest{
protectedstaticXSDFactoryxsdFactory=XSDFactory.eINSTANCE;
privatevoidcreateAligementElement(XSDSimpleTypeDefinitionaligmentType){
String[]cellAligements={"RIGHT","MIDDLE","LEFT"};
for(inti=0;i
*/
@Test
publicvoidEnumFacetTest(){
StringtargeNameSpace="http://www.w3.org/2001/XMLSchema";
XSDSchemaxsdSchema=xsdFactory.createXSDSchema();
xsdSchema.setTargetNamespace(targeNameSpace);
xsdSchema.getQNamePrefixToNamespaceMap().put(null,"http://www.w3.org/2001/XMLSchema");
//1.1CreateComplextype:student
XSDComplexTypeDefinitioncomplexTypeDef=xsdFactory.createXSDComplexTypeDefinition();
complexTypeDef.setTargetNamespace(xsdSchema.getTargetNamespace());
complexTypeDef.setName("StudentType");
XSDParticlexsdParticle=xsdFactory.createXSDParticle();
XSDModelGroupxsdModuleGroup=xsdFactory.createXSDModelGroup();
xsdModuleGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
xsdParticle.setContent(xsdModuleGroup);
complexTypeDef.setContent(xsdParticle);
complexTypeDef.setContentType(xsdParticle);
xsdSchema.getContents().add(complexTypeDef);
//1.2Addelementforcomplextype
//1.2.1usernameelement
XSDParticlelocalXSDParticle=xsdFactory.createXSDParticle();
localXSDParticle.setMinOccurs(1);
localXSDParticle.setMaxOccurs(1);
XSDElementDeclarationlocalXSDElementDeclaration=xsdFactory.createXSDElementDeclaration();
localXSDElementDeclaration.setTargetNamespace(targeNameSpace);
localXSDElementDeclaration.setName("username");
XSDSchemalocalXSDSchema=XSDUtil.getSchemaForSchema("http://www.w3.org/2001/XMLSchema");
XSDSimpleTypeDefinitionlocalSimpleType=localXSDSchema.resolveSimpleTypeDefinition("string");
localXSDElementDeclaration.setTypeDefinition(localSimpleType);
localXSDParticle.setContent(localXSDElementDeclaration);
xsdModuleGroup.getContents().add(localXSDParticle);
//1.2.2passwordelement
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.1CreateSimpleTypewithXSDEnumerationFacet---------------
XSDSimpleTypeDefinitionxsdSimpleTypeDefinition=XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
XSDSimpleTypeDefinitionbaseTypeDefinition=xsdSchema.resolveSimpleTypeDefinitionURI("string");
xsdSimpleTypeDefinition.setBaseTypeDefinition(baseTypeDefinition);
xsdSimpleTypeDefinition.setName("AlignmentType");
createAligementElement(xsdSimpleTypeDefinition);
xsdSchema.getContents().add(xsdSimpleTypeDefinition);
//1.2.3.2CreateelementwithSimpleType--------------
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.CreateXSDElementDeclarationandattachedcomplextypetoXSDelement
XSDElementDeclarationxsdEelement=xsdFactory.createXSDElementDeclaration();
xsdEelement.setName("Student");
xsdEelement.setTypeDefinition(complexTypeDef);
xsdSchema.getContents().add(xsdEelement);
//3.PrintSchema
SchemaPrintService.printSchema(xsdSchema);
}
}
classSchemaPrintService{
/**
*printschematoconsole
*
*@paramxsdSchema
*/
publicstaticvoidprintSchema(XSDSchemaxsdSchema){
System.out.println("");
System.out
.println("");
xsdSchema.updateElement();
Elementelement=xsdSchema.getElement();
if(element!=null){
//Printtheserializationofthemodel.
XSDResourceImpl.serialize(System.out,element);
}
}
privatestaticvoidprintSchemaStart(XSDSchemaxsdSchema){
System.out.print("");
}
privatestaticvoidprintDirectives(Stringindent,XSDSchemaxsdSchema){
System.out.print(indent);
printSchemaStart(xsdSchema);
System.out.println();
if(!xsdSchema.getReferencingDirectives().isEmpty()){
System.out.println(indent+"");
for(XSDSchemaDirectivexsdSchemaDirective:xsdSchema
.getReferencingDirectives()){
XSDSchemareferencingSchema=xsdSchemaDirective.getSchema();
System.out.print(indent+"");
printSchemaStart(referencingSchema);
System.out.println();
System.out.print(indent+"");
if(xsdSchemaDirectiveinstanceofXSDImport){
XSDImportxsdImport=(XSDImport)xsdSchemaDirective;
System.out.print(" ");
System.out.println(indent+"");
}
System.out.println(indent+" ");
}
if(!xsdSchema.getIncorporatedVersions().isEmpty()){
System.out.println(indent+"");
for(XSDSchemaincorporatedVersion:xsdSchema
.getIncorporatedVersions()){
printDirectives(indent+"",incorporatedVersion);
}
System.out.println(indent+" ");
}
System.out.println(indent+"");
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!