Java Code Examples for org.dom4j.Element.getParent()

The following are Jave code examples for showing how to use getParent() of the org.dom4j.Element class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: joai-project   File: MdeNode.java   View Source Code Vote up 6 votes
/**
 *  Insert an HTML comment into the html source.
 *
 * @param  e  NOT YET DOCUMENTED
 * @param  s  NOT YET DOCUMENTED
 */
protected void insertHtmlComment(Element e, String s) {
	prtln("\ninsertHtmlComment()");
	Element comment = df.createElement("st__htmlComment");
	comment.setText(s);
	Element parent = e.getParent();

	if (parent != null) {
		List children = parent.elements();
		int index = children.indexOf(e);
		children.add(index, comment);
	}
	else {
		// prtlnErr("PARENT NOT FOUND");
	}
}
 
Example 2
Project: joai-project   File: StructureWalker.java   View Source Code Vote up 5 votes
/**
 *  Process an exention element of a DerivedContent model. For extensions (used
 *  in Simple and ComplexContent type definitions, we create a dummy element of
 *  the extension's base type. The dummy element has an attribute of
 *  "extension" so it can be identified and handled properly when it is fed
 *  back into processSchemaElement and expanded as if it were an element of the
 *  baseType.<p>
 *
 *  Finaly the attributes (if any) are processed as if they were defined in the
 *  parent of the parent element, which is the enclosing ComplexType.
 *
 * @param  e              The schema extention element being processed
 * @param  parent         The instanceDoc parent of the extention (a simple or
 *      complexContent element)
 * @exception  Exception  NOT YET DOCUMENTED
 */
private void processExtension(Element e, Element parent) throws Exception {

	String baseType = e.attributeValue("base");
	String parentName = XPathUtils.getNodeName(getPath(parent));

	prtln("\n processExtension() handling extension (baseType: " + baseType + ")", 1);
	prtln(pp(e));

	// create dummy element
	//  embed namespace information into the dummy element so it is available to "isBuiltInType"
	QName qname = df.createQName("element", e.getNamespace());
	Element extElement = df.createElement(qname);

	extElement.addAttribute("type", baseType);
	extElement.addAttribute("extension", "true");
	extElement.addAttribute("name", parentName);

	prtln("\t ... extn element: " + extElement.asXML() + "\n", 1);

	/*
	 *  // print out some DEBUGGING info
	 *  if (parentName.equals("catalog")) {
	 *  prtln("parent name = " + parentName, 1);
	 *  prtln("base = " + baseType, 1);
	 *  prtln("dummy extension element: " + extElement.asXML(), 1);
	 *  }
	 */
	// this (or something like it must be here to catch the extension attributes?
	processSchemaElement(extElement, parent);

	// take care of the attributes of this extention
	for (Iterator i = e.elementIterator(); i.hasNext(); ) {
		Element grandParent = parent.getParent();
		Element childAttribute = (Element) i.next();
		processSchemaElement(childAttribute, parent);
	}
}
 
Example 3
Project: joai-project   File: DcsDataRecord.java   View Source Code Vote up 5 votes
/**
 *  Gets the historyElement attribute of the DcsDataRecord object
 *
 * @param  changeDate  NOT YET DOCUMENTED
 * @return             The historyElement value
 */
private Element getHistoryElement(String changeDate) {
	List dateElements = doc.selectNodes("//dcsDataRecord/statusEntries/statusEntry/changeDate");
	if (dateElements != null && dateElements.size() > 0)
		for (Iterator i = dateElements.iterator(); i.hasNext(); ) {
			Element dateElement = (Element) i.next();
			if (dateElement.getText().equals(changeDate))
				return dateElement.getParent();
		}
	return null;
}
 
Example 4
Project: ats-framework   File: XmlText.java   View Source Code Vote up 4 votes
/**
 * Replace a XML element.
 *
 * @param xpath XPath , pointing to a XML element
 * @param object the new XML element
 * @return this instance
 * @throws XMLException
 */
@PublicAtsApi
public XmlText replace(
                        String xpath,
                        Object object ) throws XMLException {

    if (StringUtils.isNullOrEmpty(xpath)) {
        throw new XMLException("Null/empty xpath is not allowed.");
    }

    if (object == null) {
        throw new XMLException("Null object is not allowed for replacement."
                               + "If you want to remove existing XML element, use XMLText.remove().");
    }

    Element newElement = null;

    if (object instanceof XmlText) {
        newElement = ((XmlText) object).root;
    }

    if (object instanceof String) {
        if (StringUtils.isNullOrEmpty((String) object)) {

            throw new XMLException("Null/empty String object is not allowed for replacement."
                                   + "If you want to remove existing XML element, use XMLText.remove().");

        }
        newElement = new XmlText((String) object).root;
    }

    if (newElement == null) {
        throw new XMLException("Given object for replacing an existing one is from invallid class instance. "
                               + "Use String or XMLText instances only.");
    }

    Element oldElement = findElement(xpath);

    if (oldElement != null) {

        if (oldElement.isRootElement()) {
            throw new XMLException("You cannot replace the root element of the XML document.");
        }

        Element parent = oldElement.getParent();
        if (parent != null) {
            parent.elements().set(parent.elements().indexOf(oldElement), newElement);
        } else {
            throw new XMLException("Parent for element with xpath '" + xpath + "' could not be found.");
        }

    } else {
        throw new XMLException("'" + xpath + "' is not a valid path");
    }

    return this;
}
 
Example 5
Project: joai-project   File: SchemaReader.java   View Source Code Vote up 4 votes
/**
	 *  Gets the typeDefs attribute of the SchemaReader object
	 *
	 * @param  typeSpec       NOT YET DOCUMENTED
	 * @exception  Exception  NOT YET DOCUMENTED
	 */
	private void getTypeDefs(String typeSpec) throws Exception {
		// prtln ("getTypeDefs: " + typeSpec + " (" + this.source.toString() + ")");
		if (doc == null) {
			return;
		}
		List list = doc.selectNodes("//" + NamespaceRegistry.makeQualifiedName (xsdPrefix, typeSpec));

		for (Iterator iter = list.iterator(); iter.hasNext(); ) {
			Element element = (Element) iter.next();
			String typeName = element.attributeValue("name");
			boolean isInlineTypeDef = false;
			
			// type definitions without a name attribute are inline definitions
			if (typeName == null) {
				Element parent = element.getParent();
				if (parent == null) {
					prtlnErr("WARNING: parent not found for in-line typeDef\n" + element.asXML());
					continue;
				}
				String parentName = parent.attributeValue("name");
				typeName = definitionMiner.getInlineTypeName(parentName);

				// handle union inlines differently than others
				if (isUnionDataType (parent))
					addMemberType (parent, typeName);
				else
					parent.addAttribute("type", typeName);
				
				// parent.addAttribute("type", typeName);
				element.addAttribute("name", typeName);
				isInlineTypeDef = true;
			}

			GenericType typeDef = null;
			try {
				if (typeSpec == "complexType") {
					typeDef = new ComplexType(element, source.toString(), targetNamespace, this);
					typeDef.setInline(isInlineTypeDef);
				}
				else if (typeSpec == "simpleType") {
					typeDef = new SimpleType(element, source.toString(), targetNamespace, this);
					typeDef.setInline(isInlineTypeDef);
				}
				else
					throw new Exception("unknown typeSpec: " + typeSpec);
	
				if (typeDef != null) {
					try {
						definitionMiner.addGlobalDef(typeName, typeDef, targetNamespace);
					} catch (Exception e) {
						prtlnErr(e.getMessage());
					}
				}
			} catch (Throwable t) {
				prtlnErr ("WARNING: not able to create typeDef for this element:" + Dom4jUtils.prettyPrint(element));
/* 				prtlnErr ("\tReason: " + t.getMessage());
				t.printStackTrace(); */
			}
		}
	}