Example usage for org.dom4j.dom DOMNodeHelper asDOMElement

List of usage examples for org.dom4j.dom DOMNodeHelper asDOMElement

Introduction

In this page you can find the example usage for org.dom4j.dom DOMNodeHelper asDOMElement.

Prototype

public static org.w3c.dom.Element asDOMElement(Node element) 

Source Link

Usage

From source file:com.stgmastek.core.util.XMLUtil.java

License:Open Source License

/**
 * Returns the response data object/* w ww  .j a va 2 s.co  m*/
 * 
 * @param responseString
 *         The response string.
 * 
 * @return List of Element instances from data tag.
 * 
 * @throws Exception 
 */
public static List<org.w3c.dom.Element> getData(String responseString) throws Exception {

    DocumentFactory factory = new org.dom4j.dom.DOMDocumentFactory();
    SAXReader reader = new SAXReader(factory);
    List<org.w3c.dom.Element> elements = new ArrayList<org.w3c.dom.Element>();
    try {
        Document responseDocument = reader.read(new StringReader(responseString));
        org.dom4j.Node dataNode = responseDocument.selectSingleNode(DATA_XPATH);

        if ((dataNode != null) && (dataNode.getNodeType() == Node.ELEMENT_NODE)) {
            Element dataElement = (Element) dataNode;
            for (Iterator<?> nodeIterator = dataElement.elementIterator(); nodeIterator.hasNext();) {
                Element objectNode = (Element) nodeIterator.next();
                elements.add(DOMNodeHelper.asDOMElement(objectNode));
            }
        }

    } catch (DocumentException e) {
        throw new Exception("Invalid response", e);
    }
    return elements;
}

From source file:net.unicon.warlock.fac.xml.XmlWarlockFactory.java

License:Open Source License

public IScreen parseScreen(Element e, Element[] reference) throws WarlockException {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }/* www. j ava 2s  .co  m*/
    if (reference == null) {
        String msg = "Argument 'reference' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    // Resolve the References.
    List ref = e.selectNodes("//include");
    Iterator it = ref.iterator();
    while (it.hasNext()) {

        Element incl = (Element) it.next();

        // Read the library handle.
        Attribute h = incl.attribute("handle");
        if (h == null) {
            String msg = "Element <include> is missing required attribute " + "'handle'.";
            throw new XmlFormatException(msg);
        }
        String handle = h.getValue();

        // Read the xpath expression.
        Attribute x = incl.attribute("xpath");
        if (x == null) {
            String msg = "Element <include> is missing required attribute " + "'xpath'.";
            throw new XmlFormatException(msg);
        }
        String xpath = x.getValue();

        // Find the relevent library.
        Element lib = null;
        for (int i = 0; i < reference.length; i++) {
            if (reference[i].valueOf("@handle").equals(handle)) {
                lib = reference[i];
                break;
            }
        }
        if (lib == null) {
            String msg = "Unable to locate the specified reference " + "library:  " + handle;
            throw new WarlockException(msg);
        }

        // Replace the node(s).
        org.w3c.dom.Element dIncl = DOMNodeHelper.asDOMElement(incl);
        Iterator targets = lib.selectNodes(xpath).iterator();
        while (targets.hasNext()) {
            Element elm = (Element) targets.next();
            org.w3c.dom.Element dElm = DOMNodeHelper.asDOMElement(elm);
            org.w3c.dom.Element dImp = (org.w3c.dom.Element) dIncl.getOwnerDocument().importNode(dElm, true);
            dIncl.getParentNode().insertBefore(dImp, dIncl);
        }
        dIncl.getParentNode().removeChild(dIncl);

    }

    return AbstractWarlockFactory.ScreenImpl.parse(e, this);

}