List of usage examples for org.jdom2 Element getChildren
public List<Element> getChildren()
List
of all the child elements nested directly (one level deep) within this element, as Element
objects. From source file:com.init.octo.schema.XSDAttribute.java
License:Open Source License
/** * This method builds the element definition * * @param root - the schema element that defines this element * @param cache - a list of pre-defined XML types */// ww w . j ava 2 s . c o m //log.debug("Build representation of an Attribute"); public boolean build(Element root, XSDCache cache, String parentName) { this.parentName = parentName; name = root.getAttributeValue(XSDSchema.NAME_ATT); ref = root.getAttributeValue(XSDSchema.REF_ATT); type = root.getAttributeValue(XSDSchema.TYPE_ATT); defaultAtt = root.getAttributeValue(XSDSchema.DEFAULT_ATT); use = root.getAttributeValue(XSDSchema.USE_ATT); if (use == null) { use = "optional"; } if (defaultAtt == null) { defaultAtt = ""; } if (notEmpty(name)) { cache.putAttribute(name, this); } if (notEmpty(ref)) { XSDAttribute refAtt = (XSDAttribute) cache.getAttribute(ref); if (refAtt == null) { log.warn("Referenced attribute not found [" + ref + "]"); return (true); } name = refAtt.getName(); use = refAtt.getUse(); defaultAtt = refAtt.getDefault(); type = refAtt.getType(); return (true); } for (Element child : root.getChildren()) { String childElementType = child.getName(); if (childElementType.equals(XSDSchema.ANNOTATION)) { annotation = child.getTextTrim(); } else if (childElementType.equals(XSDSchema.SIMPLETYPE)) { if (elementType != null) { throw new XmlSchemaException("Cannot define the type of an attribute more than once"); } elementType = new XSDElementTypeSimple(); elementType.build(child, cache, parentName); } } // end for all child elements of this <element> tag log.debug("Attribute built"); return true; }
From source file:com.init.octo.schema.XSDAttributeGroup.java
License:Open Source License
/** * This method builds the element definition * * @param root/*from ww w .j a v a2s.c o m*/ * - the schema element that defines this element * @param cache * - a list of pre-defined XML types */ public boolean build(Element root, XSDCache cache, String parentName) { log.debug("Build representation of an AttributeGroup"); id = root.getAttributeValue(XSDSchema.ID_ATT); name = root.getAttributeValue(XSDSchema.NAME_ATT); ref = root.getAttributeValue(XSDSchema.REF_ATT); if (name != null && name.equals("") == false) { cache.putAttributeGroup(name, this); } if (ref != null && ref.equals("") == false) { XSDAttributeGroup refAttGroup = (XSDAttributeGroup) cache.getAttributeGroup(ref); if (refAttGroup == null) { log.debug("Referenced attribute group not found [" + ref + "]"); return (true); } attributeList = refAttGroup.getAttributeList(); return (true); } log.debug("AttributeGroup <" + name + ">"); Element element; String elementName; attributeList = new ArrayList<XSDAttributeType>(); for (Iterator<?> i = (root.getChildren()).iterator(); i.hasNext();) { element = (Element) i.next(); elementName = element.getName(); log.debug("Child element <" + elementName + "> found"); /** process the child elements that define this element... **/ if (elementName.equals(XSDSchema.ANNOTATION)) { } else if (elementName.equals(XSDSchema.ATTRIBUTE)) { XSDAttribute attribute = new XSDAttribute(); if (attribute.build(element, cache, parentName) != true) { log.error("Error building an attribute object"); return (false); } attributeList.add(attribute); } else if (elementName.equals(XSDSchema.ATTRIBUTEGROUP)) { XSDAttributeGroup group = new XSDAttributeGroup(); if (group.build(element, cache, parentName) != true) { log.error("Error building a sub-attribute-group object"); return (false); } attributeList.add(group); } else { log.warn("Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of this <element> tag log.debug("Attribute built"); return (true); }
From source file:com.init.octo.schema.XSDChoice.java
License:Open Source License
/** * This method builds a choice element/*from ww w .ja v a2s . com*/ * @param root * - the choice element that defines this element * @param cache * - a list of pre-defined XML types */ public boolean build(Element root, XSDCache cache, String parentName) { log.debug("" + indent + ": " + "Build representation of a choice"); id = root.getAttributeValue(XSDSchema.ID_ATT); maxOccurs = root.getAttributeValue(XSDSchema.MAXOCCURS_ATT); minOccurs = root.getAttributeValue(XSDSchema.MINOCCURS_ATT); group = new LinkedList<XMLType>(); String elementName; for (Element element : root.getChildren()) { elementName = element.getName(); log.debug("" + indent + ": " + "Child element <" + elementName + "> found"); if (elementName.equals(XSDSchema.ANNOTATION)) { annotation = element.getTextTrim(); } else if (elementName.equals(XSDSchema.ELEMENT)) { log.debug("" + indent + ": " + "Adding element to the list of child elements"); XSDElement e = new XSDElement(indent + 1); if (e.build(element, cache, parentName) != true) { log.error("Error building the element"); return (false); } group.add(e); } else if (elementName.equals(XSDSchema.GROUP)) { log.debug("" + indent + ": " + "Adding group to the list of child elements"); XSDGroupType g = new XSDElementGroup(indent); if (g.build(element, cache, parentName) != true) { log.error("Error building a group"); return (false); } group.add(g); } else if (elementName.equals(XSDSchema.CHOICE)) { log.debug("" + indent + ": " + "Adding choice to the list of child elements"); XSDChoice c = new XSDChoice(indent); if (c.build(element, cache, parentName) != true) { log.error("Error building a choice"); return (false); } group.add(c); } else if (elementName.equals(XSDSchema.SEQUENCE)) { log.debug("" + indent + ": " + "Adding sequence to the list of child elements"); XSDSequence s = new XSDSequence(indent); // child elements all at the same level if (s.build(element, cache, parentName) != true) { log.debug("Error building a sequence"); return (false); } group.add(s); } else { log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of this <choice> tag log.debug("" + indent + ": " + "Choice built"); return (true); }
From source file:com.init.octo.schema.XSDElement.java
License:Open Source License
/** * This method builds the element definition * * @param root - the schema element that defines this element * @param cache - a list of pre-defined XML types *//*from w w w. java 2 s. c o m*/ public boolean build(Element root, XSDCache cache, String parentName) { log.debug("" + indent + ": " + "Build representation of an Element"); // root.getAttributeValue(XSDSchema.ID_ATT); //root.getAttributeValue(XSDSchema.BLOCK_ATT); //root.getAttributeValue(XSDSchema.FINAL_ATT); name = root.getAttributeValue(XSDSchema.NAME_ATT); ref = root.getAttributeValue(XSDSchema.REF_ATT); type = root.getAttributeValue(XSDSchema.TYPE_ATT); maxOccurs = root.getAttributeValue(XSDSchema.MAXOCCURS_ATT); minOccurs = root.getAttributeValue(XSDSchema.MINOCCURS_ATT); this.parentName = parentName; log.debug("" + indent + ": Element <" + name + ">"); /** Add the element definition to the element cache... **/ if (name != null && name.equals("") == false) { log.debug("Adding element [" + name + "] to the cache"); cache.putElement(name, this); } /** If this element is a reference to another full definition then get that definition... **/ if (ref != null && ref.equals("") == false) { name = ref; XSDElement refElement = (XSDElement) cache.getElement(ref); if (refElement == null) { log.debug("The reference element [" + ref + "] not in the cache..."); return (true); } log.debug("The reference element [" + ref + "] retrieved from the cache..."); /* Referenced element found so make this element look like it... */ elementType = refElement.getElementType(); if (maxOccurs == null) { maxOccurs = refElement.getMaxOccurs(); } if (minOccurs == null) { minOccurs = refElement.getMinOccurs(); } if (name == null) { name = refElement.getName(); } defaultOccurs(); return (true); } defaultOccurs(); /** if the type attribute has been specified then lookup its definition in the cache **/ if (type != null) { log.debug("" + indent + ": This element is of type <" + type + ">"); elementType = (XSDElementType) cache.getElementTypeComplex(type); if (elementType == null) { log.debug("No definition of type <" + type + "> found in type cache"); /** carry on, the type was probably an XSD standard type... **/ } else { cachedType = true; /** no sub definition allowed if the type has been specified **/ return (true); } } Element element; String elementName; for (Iterator<Element> i = (root.getChildren()).iterator(); i.hasNext();) { element = (Element) i.next(); elementName = element.getName(); log.debug("" + indent + ": " + "Child element <" + elementName + "> found"); /** process the child elements that define this element... **/ if (elementName.equals(XSDSchema.ANNOTATION)) { annotation = element.getTextTrim(); } else if (elementName.equals(XSDSchema.SIMPLETYPE)) { if (elementType != null) { log.error("Cannot define the type of an element more than once"); return (false); } elementType = new XSDElementTypeSimple(indent); if (elementType.build(element, cache, getFullName()) == false) { log.error("Error building a simpleType object"); return (false); } } else if (elementName.equals(XSDSchema.COMPLEXTYPE)) { if (elementType != null) { log.error("Cannot define the type of an element more than once"); return (false); } elementType = new XSDElementTypeComplex(indent); if (elementType.build(element, cache, getFullName()) == false) { log.error("Error building a complexType object"); return (false); } } else { log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of this <element> tag log.debug("" + indent + ": " + "Element built"); return (true); }
From source file:com.init.octo.schema.XSDElementTypeComplex.java
License:Open Source License
/** * This method builds a complex type//from w ww.j av a 2 s. c o m * * @param root - the complexType element that defines this element * @param cache - a list of pre-defined XML types */ public boolean build(Element root, XSDCache cache, String parentName) { log.debug("" + indent + ": " + "Build representation of a ComplexType"); /** get the complex types attributes **/ id = root.getAttributeValue(XSDSchema.ID_ATT); name = root.getAttributeValue(XSDSchema.NAME_ATT); abstractAtt = root.getAttributeValue(XSDSchema.ABSTRACT_ATT); mixed = root.getAttributeValue(XSDSchema.MIXED_ATT); block = root.getAttributeValue(XSDSchema.BLOCK_ATT); finalAtt = root.getAttributeValue(XSDSchema.FINAL_ATT); log.debug("" + indent + ": ComplexType <" + name + ">"); for (Element rootChild : root.getChildren()) { String elementName = rootChild.getName(); if (elementName.equals(XSDSchema.ANNOTATION)) { annotation = rootChild.getTextTrim(); } else if (elementName.equals(XSDSchema.SIMPLECONTENT)) { } else if (elementName.equals(XSDSchema.COMPLEXCONTENT)) { for (Element child : rootChild.getChildren()) { Attribute baseTypeAtt = child.getAttribute(XSDSchema.BASE_ATT); if (baseTypeAtt == null) { throw new XmlSchemaException("No base attribute for element [" + elementName + "]"); } XSDElementTypeComplex cachedObject = cache.getElementTypeComplex(baseTypeAtt.getValue()); if (cachedObject != null) { baseType = cachedObject; } processComplexTypeChildren(child, cache); } } else if (elementName.equals(XSDSchema.GROUP)) { if (elementGroup != null) { throw new XmlSchemaException("Cannot define a grouping element more than once"); } elementGroup = new XSDElementGroup(indent); elementGroup.build(rootChild, cache, parentName); } else if (elementName.equals(XSDSchema.ALL)) { if (elementGroup != null) { throw new XmlSchemaException("Cannot define a grouping element more than once"); } elementGroup = new XsdAll(indent); elementGroup.build(rootChild, cache, parentName); } else if (elementName.equals(XSDSchema.CHOICE)) { if (elementGroup != null) { throw new XmlSchemaException("Cannot define a grouping element more than once"); } elementGroup = new XSDChoice(indent); elementGroup.build(rootChild, cache, parentName); } else if (elementName.equals(XSDSchema.SEQUENCE)) { if (elementGroup != null) { log.error("Cannot define a grouping element more than once"); return (false); } elementGroup = new XSDSequence(indent); if (elementGroup.build(rootChild, cache, parentName) != true) { log.error("Error building a sequence object"); return (false); } } else if (elementName.equals(XSDSchema.ATTRIBUTE)) { XSDAttribute attribute = new XSDAttribute(); if (attribute.build(rootChild, cache, parentName) != true) { log.error("Error building an attribute object"); return (false); } attributeList.add(attribute); } else if (elementName.equals(XSDSchema.ATTRIBUTEGROUP)) { XSDAttributeGroup group = new XSDAttributeGroup(); if (group.build(rootChild, cache, parentName) != true) { log.error("Error building an attribute group object"); return (false); } attributeList.addAll(group.getAllAttributes()); } else { log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of this <element> tag log.debug("" + indent + ": " + "ComplexType built"); return (true); }
From source file:com.init.octo.schema.XSDElementTypeComplex.java
License:Open Source License
private void processComplexTypeChildren(Element child, XSDCache cache) { for (Element grandchild : child.getChildren()) { String grandchildName = grandchild.getName(); if (grandchildName.equals(XSDSchema.GROUP)) { baseTypeGroup = new XSDElementGroup(indent); if (baseTypeGroup.build(grandchild, cache, parentName) != true) { throw new XmlSchemaException("Error building a group object"); }/* w ww .j a v a 2 s .co m*/ } else if (grandchildName.equals(XSDSchema.ALL)) { baseTypeGroup = new XsdAll(indent); if (baseTypeGroup.build(grandchild, cache, parentName) != true) { throw new XmlSchemaException("Error building a ALL object"); } } else if (grandchildName.equals(XSDSchema.CHOICE)) { baseTypeGroup = new XSDChoice(indent); if (baseTypeGroup.build(grandchild, cache, parentName) != true) { throw new XmlSchemaException("Error building a choice object"); } } else if (grandchildName.equals(XSDSchema.SEQUENCE)) { baseTypeGroup = new XSDSequence(indent); if (baseTypeGroup.build(grandchild, cache, parentName) != true) { throw new XmlSchemaException("Error building a sequence object"); } } } }
From source file:com.init.octo.schema.XSDElementTypeSimple.java
License:Open Source License
/** * This method builds a simple type/*from w ww. j av a 2 s .c om*/ * * @param root * - the simpleType element that defines this element * @param cache * - a list of pre-defined XML types */ public boolean build(Element root, XSDCache cache, String parentName) { log.debug("" + indent + ": " + "Build representation of a SimpleType"); id = root.getAttributeValue(XSDSchema.ID_ATT); finalAtt = root.getAttributeValue(XSDSchema.FINAL_ATT); name = root.getAttributeValue(XSDSchema.NAME_ATT); for (Element child : root.getChildren()) { String childType = child.getName(); if (childType.equals(XSDSchema.ANNOTATION)) { annotation = child.getTextTrim(); break; } else if (childType.equals(XSDSchema.RESTRICTION)) { // @todo } else if (childType.equals(XSDSchema.LIST)) { // @todo } } return true; }
From source file:com.init.octo.schema.XSDGroupType.java
License:Open Source License
public boolean build(Element root, XSDCache cache, String parentName) { log.debug("" + indent + ": " + "Build representation of a group"); id = root.getAttributeValue(XSDSchema.ID_ATT); maxOccurs = root.getAttributeValue(XSDSchema.MAXOCCURS_ATT); minOccurs = root.getAttributeValue(XSDSchema.MINOCCURS_ATT); name = root.getAttributeValue(XSDSchema.NAME_ATT); ref = root.getAttributeValue(XSDSchema.REF_ATT); /* If this group is named then add it to the cache... */ /*/* w ww . ja v a 2s. c o m*/ if (name != null && name.equals("") == false) { cache.putGroup(name, this); } */ /* If this group is a reference then lookup the reference... */ /* if (ref != null && ref.equals("") == false) { List<XSDGroup> cachedGroup = cache.getGroup(ref); if (cachedGroup != null) { group return (true); } } */ group = new LinkedList<XMLType>(); Element element; String elementName; for (Iterator<?> i = (root.getChildren()).iterator(); i.hasNext();) { element = (Element) i.next(); elementName = element.getName(); log.debug("" + indent + ": " + "Child element <" + elementName + "> found"); /** process the child elements that define this element... **/ if (elementName.equals(XSDSchema.ANNOTATION)) { annotation = element.getTextTrim(); } else if (elementName.equals(XSDSchema.ALL)) { log.debug("" + indent + ": " + "Adding ALL to the list of child elements"); XsdAll a = new XsdAll(indent); // child elements will be at the same level if (a.build(element, cache, parentName) != true) { log.error("Error building a ALL element"); return (false); } group.add(a); } else if (elementName.equals(XSDSchema.CHOICE)) { log.debug("" + indent + ": " + "Adding choice to the list of child elements"); XSDChoice c = new XSDChoice(indent); // child elements will be // at the same level if (c.build(element, cache, parentName) != true) { log.error("Error building a choice"); return (false); } group.add(c); } else if (elementName.equals(XSDSchema.SEQUENCE)) { log.debug("" + indent + ": " + "Adding sequence to the list of child elements"); XSDSequence s = new XSDSequence(indent); if (s.build(element, cache, parentName) != true) { log.error("Error building a sequence"); return (false); } group.add(s); } else { log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of this <group> tag return (true); }
From source file:com.init.octo.schema.XSDSchema.java
License:Open Source License
private boolean privateBuild(String schemaFileName, Document document, boolean topLevel) throws Exception { int schemaFileNamesIdx = 0; String attributeValue;/*from w w w . j a v a2s . com*/ XSDElement thisDocRoot; // get the root element of the XML - this should be <schema>, or we wont process the document Element xsdRoot = document.getRootElement(); String elementName = xsdRoot.getName(); if (elementName.equals(SCHEMA) == false) { throw new Exception("The root element of the XML document is not <" + SCHEMA + ">"); } id = xsdRoot.getAttributeValue(XSDSchema.ID_ATT); targetNamespace = xsdRoot.getAttributeValue(XSDSchema.TARGNAMSP_ATT); try { cache.pushNamespaces(schemaFileName); } catch (Exception ex) { log.fatal("Exception caching namespaces: " + ex.toString()); return (false); } /** Go through the document and find any include or import elements... **/ for (Element element : xsdRoot.getChildren()) { elementName = element.getName(); if (elementName.equals(IMPORT) || elementName.equals(INCLUDE)) { attributeValue = element.getAttributeValue(SCHEMALOC_ATT); if (cache.schemaCached(attributeValue) == true) { log.debug("Schema already cached... ignoring"); continue; } log.debug(elementName + " schema [" + attributeValue + "]"); String importSchemaName = null; /* If the schema is relative to the current schema then work out the absolute name... */ if (attributeValue.startsWith("..") == true) { String[] currentSchemaPath = ((String) schemaFileNames.get(schemaFileNamesIdx)) .split("[/\\\\]"); String[] importSchemaFile = attributeValue.split("[/\\\\]"); int idx = 0; int n = 0; StringBuffer realImportName = new StringBuffer(); while (importSchemaFile[n].equals("..")) { idx++; n++; } for (n = 0; n < (currentSchemaPath.length - 1) - idx; n++) { realImportName.append(currentSchemaPath[n]); realImportName.append(File.separator); } for (n = idx; n < importSchemaFile.length; n++) { realImportName.append(importSchemaFile[n]); if (n < importSchemaFile.length - 1) { realImportName.append(File.separator); } } importSchemaName = realImportName.toString(); } else { /* schema in current directory... */ File sf = new File((String) schemaFileNames.get(schemaFileNamesIdx)); String dirName = sf.getParent(); importSchemaName = dirName + File.separator + attributeValue; } Document doc = builder.build(new FileReader(importSchemaName)); schemaFileNames.add(importSchemaName); schemaFileNamesIdx++; cache.putSchema(attributeValue, document); privateBuild(importSchemaName, doc, false); schemaFileNames.remove(schemaFileNamesIdx); schemaFileNamesIdx--; } } // do it twice... so that all predefined types are picked up, no matter what order they are defined in... // @todo I know it is clunky, and when I get time I will make it scan for types (and imports etc!) first then build the definition..... for (int x = 0; x < 2; x++) { /** iterate all of the child elements of schema **/ for (Element element : xsdRoot.getChildren()) { elementName = element.getName(); log.debug("" + indent + ": " + "Element <" + elementName + ">"); if (elementName.equals(ELEMENT)) { thisDocRoot = new XSDElement(indent); if (thisDocRoot.build(element, cache, "") != true) { log.fatal("Error processing the schema"); return (false); } if (topLevel == true) { topLevelElements.put(elementName, thisDocRoot); } } else if (elementName.equals(SIMPLETYPE)) { } else if (elementName.equals(COMPLEXTYPE)) { XSDElementTypeComplex complexType = new XSDElementTypeComplex(indent); if (complexType.build(element, cache, "") != true) { log.fatal("Error building a complex type"); throw new Exception("Error building a complex type"); } log.debug("Adding type <" + complexType.getName() + "> to type cache"); cache.putType(complexType.getName(), complexType); if (topLevel == true) { topLevelTypes.put(complexType.getName(), complexType); } } else if (elementName.equals(GROUP)) { XSDGroupType group = new XSDElementGroup(indent); if (group.build(element, cache, "") != true) { log.fatal("Error building a group"); return (false); } } else if (elementName.equals(ATTRIBUTEGROUP)) { XSDAttributeGroup attGroup = new XSDAttributeGroup(); if (attGroup.build(element, cache, "") != true) { log.warn("Error building an attribute group"); continue; } } else if (elementName.equals(ATTRIBUTE)) { XSDAttribute attribute = new XSDAttribute(); if (attribute.build(element, cache, "") != true) { log.warn("Error building an attribute object"); continue; } } else if (elementName.equals(IMPORT) || elementName.equals(INCLUDE)) { // do nothing - these have already been processed... } else { log.debug("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of the schema root } cache.popNamespaces(); return (true); }
From source file:com.init.octo.schema.XSDSchema.java
License:Open Source License
private void addElementToDocument(XSDElement schemaParent, Element documentParent, Element newChild) { /* If no schema defined or no children have been added to the parent then we can safely just add our child... */ if (schemaParent == null || documentParent.getChildren().size() == 0) { documentParent.addContent(newChild); } else {//from www . ja v a2s . co m List<XSDElement> xsdChildren = schemaParent.getChildren(); Object[] children = documentParent.getChildren().toArray(); Element child = null; String newChildName = newChild.getName(); boolean afterFlag = false; int childIdx = 0; for (childIdx = 0; childIdx < children.length && afterFlag == false; childIdx++) { child = (Element) children[childIdx]; if (child.getName().equals(newChildName) == false) { /* New child so check in XSD list of children to see if our new child goes before it... */ for (XSDElement el : xsdChildren) { /* If the new child name equals the XSD name then we can add after the current position... */ if (el.getName().equals(newChildName)) { afterFlag = true; childIdx--; break; } else if (el.getName().equals(child.getName())) { break; } } } } /* If the current child name equals the new child name then we need to go to the end of set of */ /* those children before we can add our new child, i.e. new A must go after AAA<here> */ while (childIdx < children.length - 1 && child.getName().equals(newChildName)) { childIdx++; child = (Element) children[childIdx]; } /* Now add the new child... */ if (childIdx < children.length) { documentParent.addContent(childIdx, newChild); } else { documentParent.addContent(newChild); } } }