/** * Process a "group" element. * * @param element The "group" element. * @todo processGroup, processElement and processAttributeGroup are similar and may benefit from a * bit of refactoring and code re-use. */ protected void processGroup(Element element) { String ref = element.getAttributeValue("ref"); if (ref != null) { // We have a group reference. println("Parsing group reference to '" + ref + "'"); ElementGroupReference reference = scope.getElementGroupReference(ref); // Add the group reference to the enclosing group list. elementGroupList.add(reference); // Nothing else to do. return; } // Ensure that the element has a name. String name = element.getAttributeValue("name"); if (name == null) { throw new IllegalStateException("Group does not have a name"); } println("Parsing group definition for '" + name + "'"); // Create a new Group object in the current scope, this will // throw an exception if there is a clash. ElementGroup definition = scope.addElementGroup(name); // Process the attributes. List attributes = element.getAttributes(); for (Iterator i = attributes.iterator(); i.hasNext(); ) { Attribute attribute = (Attribute) i.next(); Namespace attributeNamespace = attribute.getNamespace(); String attributeName = attribute.getName(); if (attributeNamespace == xsdNamespace || attributeNamespace == Namespace.NO_NAMESPACE) { } else { throw new IllegalStateException("Unknown attribute namespace '" + attributeNamespace + "'"); } } // Add the group definition to the list of groups. elementGroupList.add(definition); // Push the group definition object onto the stack. schemaObjects.push(definition); // Push a new scope. System.out.println("NEW SCOPE FOR GROUP " + name); scope = new Scope(scope, "Group " + name); // Get the AttributesStructure. AttributesStructure attributesStructure = definition.getAttributesStructure(); // Push a new list of Attributes. List oldAttributeList = attributeList; attributeList = attributesStructure.getAttributes(); // Push a new list of AttributeGroups. List oldAttributeGroupList = attributeGroupList; attributeGroupList = attributesStructure.getAttributeGroups(); // Process the contents of the group. parseContent(element); // Pop the list of AttributeGroups. attributeGroupList = oldAttributeGroupList; // Pop the list of Attributes. attributeList = oldAttributeList; // Pop the scope. scope = scope.getEnclosingScope(); System.out.println("POPPING SCOPE FOR GROUP " + name); // Pop the group definition object from the stack. schemaObjects.pop(); }
/** * Process an "attributeGroup" element. * * @param element The "attributeGroup" element. */ protected void processAttributeGroup(Element element) { // Check to see whether this is an attribute group reference first. String ref = element.getAttributeValue("ref"); if (ref != null) { println("Parsing attribute group reference to '" + ref + "'"); AttributeGroupReference reference = scope.getAttributeGroupReference(ref); // Add the attributeGroup reference to the enclosing attributeGroup list. attributeGroupList.add(reference); // Nothing else to do. return; } // Ensure that the element has a name. String name = element.getAttributeValue("name"); if (name == null) { throw new IllegalStateException("AttributeGroup does not have a name"); } println("Parsing attribute group definition for '" + name + "'"); // Create a new AttributeGroupDefinition object in the current scope, this // will throw an exception if there is a clash. AttributeGroupDefinition definition = scope.addAttributeGroupDefinition(name); // Process the attributes. List attributes = element.getAttributes(); for (Iterator i = attributes.iterator(); i.hasNext(); ) { Attribute attribute = (Attribute) i.next(); Namespace attributeNamespace = attribute.getNamespace(); String attributeName = attribute.getName(); if (attributeNamespace == xsdNamespace || attributeNamespace == Namespace.NO_NAMESPACE) { if ("name".equals(attributeName)) { // Don't do anything else. } else { throw new IllegalStateException("Unknown schema attribute '" + attributeName + "'"); } } else { throw new IllegalStateException("Unknown attribute namespace '" + attributeNamespace + "'"); } } // Add the attribute group definition to the list of attribute groups. attributeGroupList.add(definition); // Push the attribute group definition object onto the stack. schemaObjects.push(definition); // Push a new scope. scope = new Scope(scope, "Attribute group " + name); // Get the AttributesStructure. AttributesStructure attributesStructure = definition.getAttributesStructure(); // Push a new list of Attributes. List oldAttributeList = attributeList; attributeList = attributesStructure.getAttributes(); // Push a new list of AttributeGroups. List oldAttributeGroupList = attributeGroupList; attributeGroupList = attributesStructure.getAttributeGroups(); // Process the contents of the attribute group. parseContent(element); // Pop the list of AttributeGroups. attributeGroupList = oldAttributeGroupList; // Pop the list of Attributes. attributeList = oldAttributeList; // Pop the scope. scope = scope.getEnclosingScope(); // Pop the attribute group definition object from the stack. schemaObjects.pop(); }
/** * Process a "complexType" element. * * @param element The "complexType" element. */ protected void processComplexType(Element element) { println("Parsing complexType"); // might not be named String name = element.getAttributeValue("name"); ComplexType complexType; if (name == null) { // Anonymous complex type so must be inside an element. complexType = scope.createComplexType(null); // If we are defining an element then store the complex type into the // element definition. SchemaObject current = getCurrentObject(); if (current instanceof ElementDefinition) { ElementDefinition definition = (ElementDefinition) current; definition.setComplexType(complexType); } } else { complexType = scope.addComplexType(name); complexTypeList.add(complexType); } if ("true".equals(element.getAttributeValue("mixed"))) { complexType.setMixed(true); } // Element is empty unless we find anything in the body of this element // to change out mind. complexType.setEmpty(true); // Push the complex type object onto the stack. schemaObjects.push(complexType); scope = new Scope(scope, "ComplexType " + name); // Get the AttributesStructure. AttributesStructure attributesStructure = complexType.getAttributesStructure(); // Push a new list of Attributes. List oldAttributeList = attributeList; attributeList = attributesStructure.getAttributes(); // Push a new list of AttributeGroups. List oldAttributeGroupList = attributeGroupList; attributeGroupList = attributesStructure.getAttributeGroups(); // Push a new list of Elements. List oldElementList = elementList; elementList = new ArrayList(); // Parse the content of the complex type. parseContent(element); // Pop the list of Elements. elementList = oldElementList; // Pop the list of AttributeGroups. attributeGroupList = oldAttributeGroupList; // Pop the list of Attributes. attributeList = oldAttributeList; scope = scope.getEnclosingScope(); // Pop the complex type object from the stack. schemaObjects.pop(); }
/** * Parse the specified element. * * @param element The element to parse. */ public void parse(Element element) { Namespace namespace = element.getNamespace(); String name = element.getName(); if (namespace != xsdNamespace && namespace.getURI().equals("http://www.w3.org/2001/XMLSchema")) { /* println ("Overriding xsdNamespace " + xsdNamespace + " with " + namespace); */ xsdNamespace = namespace; } if (namespace == xsdNamespace) { if ("annotation".equals(name)) { processAnnotation(element); } else if ("appinfo".equals(name)) { processAppInfo(element); } else if ("attribute".equals(name)) { processAttribute(element); } else if ("attributeGroup".equals(name)) { processAttributeGroup(element); } else if ("choice".equals(name)) { processChoice(element); } else if ("complexType".equals(name)) { processComplexType(element); } else if ("documentation".equals(name)) { processDocumentation(element); } else if ("element".equals(name)) { processElement(element); } else if ("group".equals(name)) { processGroup(element); } else if ("schema".equals(name)) { processSchema(element); } else if ("sequence".equals(name)) { processSequence(element); } else if ("simpleType".equals(name)) { processSimpleType(element); } else if ("all".equals(name)) { processAll(element); } else if ("complexContent".equals(name)) { processAll(element); } else if ("any".equals(name)) { System.out.println("Ignoring 'any'"); } else if ("unique".equals(name)) { System.out.println("Ignoring 'unique'"); } else if ("include".equals(name)) { System.out.println("Ignoring 'include'"); } else if ("simpleContent".equals(name)) { System.out.println("Ignoring 'simpleContent'"); } else if ("extension".equals(name)) { String base = element.getAttributeValue("base"); if (base != null) { ComplexType complexType = (ComplexType) scope.getComplexType(base); AttributesStructure structure = complexType.getAttributesStructure(); attributeGroupList.addAll(structure.getAttributeGroups()); attributeList.addAll(structure.getAttributes()); } processAll(element); // System.out.println("Ignoring 'extension'"); } else if ("import".equals(name)) { System.out.println("Ignoring 'import'"); } else { throw new IllegalArgumentException("Unhandled element " + name); } } else { throw new IllegalArgumentException("Unhandled namespace " + namespace); } }