/** Creates the ExecutableTask from the Schema tree. */ private void createResult(XSSchema schema) { execTask = new ExecutableTask(); // Get the "task" xs:element. XSElementDecl task = schema.getElementDecl("task"); // We know it's an xs:complexType. XSComplexType taskType = task.getType().asComplexType(); // Get the xs:sequence of elements of the anonymous xs:complexType. // A model group can be xs:all, xs:choice or xs:sequence. XSParticle taskParticle = taskType.getContentType().asParticle(); XSModelGroup taskSequence = taskParticle.getTerm().asModelGroup(); // Get the direct children in the xs:sequence. XSParticle taskElemParticles[] = taskSequence.getChildren(); for (XSParticle taskElemParticle : taskElemParticles) { XSElementDecl taskElem = taskElemParticle.getTerm().asElementDecl(); if (taskElem.getName().equals("execInfo")) { // Displayable info for the UI. // We have the "execInfo" element. ExecutableInfo execInfo = new ExecutableInfo(); // We know it's an xs:complexType. XSComplexType infoType = taskElem.getType().asComplexType(); XSParticle infoParticle = infoType.getContentType().asParticle(); XSModelGroup infoSequence = infoParticle.getTerm().asModelGroup(); XSParticle infoElemParticles[] = infoSequence.getChildren(); for (XSParticle infoElemParticle : infoElemParticles) { XSElementDecl infoElem = infoElemParticle.getTerm().asElementDecl(); XSSimpleType simpleType = infoElem.getType().asSimpleType(); XSRestrictionSimpleType restrictionSimple = simpleType.asRestriction(); XSFacet facet = restrictionSimple.getDeclaredFacets().iterator().next(); switch (infoElem.getName()) { case "name": execInfo.setExecName(facet.getValue().toString()); break; case "type": execInfo.setExecType(facet.getValue().toString()); break; case "description": execInfo.setExecDescription(facet.getValue().toString()); break; } } execTask.setExecInfo(execInfo); } else { XSType taskElemType = taskElem.getType(); if (taskElemType.isSimpleType()) { // We do not check constraints. They will be validated later. ExecutableParameter param = new ExecutableParameter(); param.setParamName(taskElem.getName()); execTask.addParameter(param); } else if (taskElemType.isComplexType()) { // Refers to inputFile and outputFile. // No need to check these since they are always the same. } } } }
public static void main(String args[]) { if (args.length != 1) { System.out.println("usage: java xerces.ParseXSD xsdfilepath"); System.exit(1); } System.out.println("Going to parse schema " + args[0]); // get DOM implementation DOMImplementationAS domImpl = (DOMImplementationAS) ASDOMImplementationImpl.getDOMImplementation(); // create a new parser DOMASBuilder parser = domImpl.createDOMASBuilder(); ASModel asmodel = null; try { asmodel = parser.parseASURI(args[0]); } catch (Exception e) { e.printStackTrace(); System.exit(1); } ASModelImpl model = (ASModelImpl) asmodel; Vector models = model.getInternalASModels(); SchemaGrammar[] grammars = new SchemaGrammar[models.size()]; for (int i = 0; i < models.size(); i++) { grammars[i] = ((ASModelImpl) models.elementAt(i)).getGrammar(); } XSModel xs = new XSModelImpl(grammars); XSNamedMap elemmap = xs.getComponents(XSConstants.ELEMENT_DECLARATION); // assume the first top element is what we want XSElementDecl topElem = (XSElementDecl) elemmap.item(0); System.out.println("Top Element is: " + topElem.getName()); echo(topElem.getAnnotationAttrs()); XSComplexTypeDefinition ct = (XSComplexTypeDefinition) topElem.getTypeDefinition(); // attributes XSObjectList attruses = ct.getAttributeUses(); if (null != attruses) { for (int i = 0; i < attruses.getLength(); i++) { XSAttributeUse attruse = (XSAttributeUse) attruses.item(i); XSAttributeDeclaration attr = attruse.getAttrDeclaration(); System.out.println("has an attribute: " + attr.getName()); echo(attr.getAnnotationAttrs()); } } XSParticle particle = ct.getParticle(); XSTerm term = particle.getTerm(); XSModelGroup topGroup = (XSModelGroup) term; // traverse the group XSObjectList objs = topGroup.getParticles(); for (int i = 0; i < objs.getLength(); i++) { XSObject obj = objs.item(i); XSParticle p = (XSParticle) obj; XSTerm t = p.getTerm(); // we got nested group if (t instanceof XSModelGroup) { System.out.println("has a group"); XSModelGroup g = (XSModelGroup) t; echo(g.getAnnotationAttrs()); } if (t instanceof XSElementDeclaration) { XSElementDeclaration e = (XSElementDeclaration) t; XSTypeDefinition et = e.getTypeDefinition(); System.out.println("has an element: " + e.getName()); if (et.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { System.out.println(e.getName() + " is complex type."); echo(e.getAnnotationAttrs()); } else { System.out.println(e.getName() + " is simple type."); echo(e.getAnnotationAttrs()); } } } }