List of usage examples for org.jdom2 Element getName
public String getName()
From source file:com.init.octo.schema.XSDChoice.java
License:Open Source License
/** * This method builds a choice element/*from ww w . j a v a2 s.c o m*/ * @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 .j a va2s . com*/ 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 w w.ja v 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"); }/*from w w w .j av a2s. c o 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 w w . j a va2 s . co m * * @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... */ /*/*from w w w . jav a2 s . 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 va 2s . c o m*/ 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 w w w.j av a 2 s .c o 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); } } }
From source file:com.init.octo.schema.XSDSchema.java
License:Open Source License
/** wow * //from w w w. ja v a2 s .c o m * @param parent * @param childName * @return */ private Element findElement(Element parent, String childName) { List<Element> children = parent.getChildren(); ListIterator<?> it = children.listIterator(); /** We need to go backwards through the list... **/ while (it.hasNext()) { it.next(); } while (it.hasPrevious()) { Element child = (Element) it.previous(); if (child.getName().equals(childName)) { return (child); } } return (null); }
From source file:com.init.octo.schema.XSDSchema.java
License:Open Source License
/** * /*from ww w .j a v a2 s. co m*/ * @param elementSpec * @param schemaElement * @param element */ private void addMandatoryElementToDocument(XSDElement schemaElement, String elementSpec, Element element) { String[] eSpec = elementSpec.split("\\."); if (eSpec[0].equals(element.getName()) == false) { log.warn("The parent element [" + element.getName() + "] does not match the element spec [" + elementSpec + "]"); return; } int idx = 1; while (idx < eSpec.length) { if (isAttribute(eSpec[idx])) { String attname = eSpec[idx].substring(1, eSpec[idx].length()); Attribute att = element.getAttribute(attname); if (att == null) { /** Mandatory attribute does not exist so add it.. **/ element.setAttribute(attname, schemaElement.getDefaultAttValue(attname)); } return; } StringBuffer tmp = new StringBuffer(); for (int i = idx; i < eSpec.length; i++) { if (tmp.length() != 0) { tmp.append("."); } tmp.append(eSpec[i]); } Iterator<?> it = element.getChildren(eSpec[idx]).iterator(); /** * If the document does not have the specified child element and we * are at the end **/ /** * of the chain then this is a mandatory element that needs to be * added... **/ if (it.hasNext() == false && idx == eSpec.length - 1) { Element newel = new Element(eSpec[idx]); addElementToDocument(schemaElement, element, newel); return; } /** * If the document does not have the specified child element and * that element is **/ /** just part of the chain, then no need to carry on.. **/ if (it.hasNext() == false) { return; } /** * If the document does have the specified child element and we are * at the end of the chain then every thing is ok, so just return **/ if (idx == eSpec.length - 1) { return; } else { /** We have to follow every one of the elements down the tree.. **/ while (it.hasNext()) { addMandatoryElementToDocument(schemaElement, tmp.toString(), (Element) it.next()); } return; } } }