Example usage for org.dom4j Node getNodeType

List of usage examples for org.dom4j Node getNodeType

Introduction

In this page you can find the example usage for org.dom4j Node getNodeType.

Prototype

short getNodeType();

Source Link

Document

Returns the code according to the type of node.

Usage

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

License:Open Source License

/**
 * Returns the response data object//from  ww  w  .java  2  s . com
 * 
 * @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:com.webslingerz.jpt.PageTemplateImpl.java

License:Open Source License

private void defaultContent(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        Interpreter beanShell, Stack<Map<String, Slot>> slotStack)
        throws SAXException, PageTemplateException, IOException {
    // Use default template content
    for (Iterator i = element.nodeIterator(); i.hasNext();) {
        Node node = (Node) i.next();
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            processElement((Element) node, contentHandler, lexicalHandler, beanShell, slotStack);
            break;

        case Node.TEXT_NODE:
            char[] text = Expression.evaluateText(node.getText().toString(), beanShell).toCharArray();
            contentHandler.characters(text, 0, text.length);
            break;

        case Node.COMMENT_NODE:
            char[] comment = node.getText().toCharArray();
            lexicalHandler.comment(comment, 0, comment.length);
            break;

        case Node.CDATA_SECTION_NODE:
            lexicalHandler.startCDATA();
            char[] cdata = node.getText().toCharArray();
            contentHandler.characters(cdata, 0, cdata.length);
            lexicalHandler.endCDATA();/*from   www .  ja va 2 s  .  co  m*/
            break;

        case Node.NAMESPACE_NODE:
            Namespace declared = (Namespace) node;
            // System.err.println( "Declared namespace: " +
            // declared.getPrefix() + ":" + declared.getURI() );
            namespaces.put(declared.getPrefix(), declared.getURI());
            // if ( declared.getURI().equals( TAL_NAMESPACE_URI ) ) {
            // this.talNamespacePrefix = declared.getPrefix();
            // }
            // else if (declared.getURI().equals( METAL_NAMESPACE_URI ) ) {
            // this.metalNamespacePrefix = declared.getPrefix();
            // }
            break;

        case Node.ATTRIBUTE_NODE:
            // Already handled
            break;

        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        default:
            // System.err.println( "WARNING: Node type not supported: " +
            // node.getNodeTypeName() );
        }
    }
}

From source file:com.zimbra.common.soap.Element.java

License:Open Source License

private static Element flattenDOM(org.dom4j.Element d4root, ElementFactory factory) {
    Element elt = factory.createElement(d4root.getQName());
    for (Iterator<?> it = d4root.attributeIterator(); it.hasNext();) {
        org.dom4j.Attribute d4attr = (org.dom4j.Attribute) it.next();
        elt.addAttribute(d4attr.getQualifiedName(), d4attr.getValue());
    }// w w  w . j  a  v a  2  s . com

    StringBuilder content = new StringBuilder();
    for (int i = 0, size = d4root.nodeCount(); i < size; i++) {
        org.dom4j.Node node = d4root.node(i);
        switch (node.getNodeType()) {
        case org.dom4j.Node.TEXT_NODE:
            content.append(node.getText());
            break;
        case org.dom4j.Node.ELEMENT_NODE:
            content.append(((org.dom4j.Element) node).asXML());
            break;
        }
    }
    return elt.setText(content.toString());
}

From source file:darks.orm.core.config.sqlmap.DMLConfigReader.java

License:Apache License

private void parseSqlTag(AbstractTag parent, Element el, String namesp) throws Exception {
    AbstractTag prevTag = null;/*from   ww w.  jav a  2s.  co m*/
    List<Node> list = el.content();
    Iterator<Node> it = list.iterator();
    while (it.hasNext()) {
        Node node = it.next();
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            Element childEl = (Element) node;
            prevTag = parseElementTag(parent, node, childEl, namesp, prevTag);
            break;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            String text = node.getText().replaceAll("\n|\t", " ").trim();
            if (!"".equals(text)) {
                TextTag tag = new TextTag(text, prevTag);
                parent.addChild(tag);
                prevTag = tag;
            }
            break;
        }
    }
}

From source file:dk.netarkivet.common.utils.XmlTree.java

License:Open Source License

/**
 * Initialise a node in an XML tree./*from w  w  w. j  a v  a2 s .  c o m*/
 *
 * @param n The XML node for this node
 * @param parser The parser that can convert a leaf node to a value of type T.
 * @throws ArgumentNotValid on null argument, or if n is not of type element or document.
 */
private XmlTree(Node n, ValueParser<T> parser) {
    ArgumentNotValid.checkNotNull(n, "Node n");
    ArgumentNotValid.checkNotNull(parser, "ValueParser<T> parser");
    if (n.getNodeType() == Node.DOCUMENT_NODE) {
        root = (Document) n;
        element = null;
    } else if (n.getNodeType() == Node.ELEMENT_NODE) {
        element = (Element) n;
        root = null;
    } else {
        throw new ArgumentNotValid("Invalid XML node type '" + n.getNodeTypeName() + "'");
    }
    this.parser = parser;
}

From source file:fr.gouv.culture.vitam.utils.XmlDom.java

License:Open Source License

/**
 * Recursively sets the namespace of the List and all children if the current namespace is match
 *///from w w  w  .  j  a  va2 s . c o  m
private final static void setNamespaces(List<?> l, Namespace ns) {
    Node n = null;
    for (int i = 0; i < l.size(); i++) {
        n = (Node) l.get(i);

        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            Namespace namespace = ((Attribute) n).getNamespace();
            if (!namespace.equals(ns)) {
                ((Attribute) n).setNamespace(ns);
            }
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Namespace namespace = ((Element) n).getNamespace();
            if (!namespace.equals(ns)) {
                if (ns.equals(Namespace.NO_NAMESPACE)) {
                    ((Element) n).remove(namespace);
                }
                setNamespaces((Element) n, ns);
            }
        }
    }
}

From source file:gov.nih.nci.caarray.upgrade.SingleConnectionHibernateHelper.java

License:BSD License

/**
 * @param connection/* w w  w .  jav  a 2s.c  om*/
 */
public void initialize(Connection connection) {
    HibernateSingleConnectionProvider.setConnection(connection);

    InputStream configurationStream = FixIlluminaGenotypingCsvDesignProbeNamesMigrator.class
            .getResourceAsStream("/hibernate.cfg.xml");
    SAXReader reader = new SAXReader();
    reader.setEntityResolver(new org.hibernate.util.DTDEntityResolver());
    Document configurationDocument = null;
    try {
        configurationDocument = reader.read(configurationStream);
    } catch (DocumentException e) {
        throw new UnhandledException(e);
    }
    Node sessionFactoryNode = configurationDocument
            .selectSingleNode("/hibernate-configuration/session-factory");

    Iterator<?> iter = ((Branch) sessionFactoryNode).nodeIterator();
    while (iter.hasNext()) {
        Node currentNode = (Node) iter.next();
        if (currentNode.getNodeType() == Node.ELEMENT_NODE && !currentNode.getName().equals("mapping")) {
            iter.remove();
        }
    }

    DOMWriter domWriter = new DOMWriter();
    org.w3c.dom.Document document = null;
    try {
        document = domWriter.write(configurationDocument);
    } catch (DocumentException e) {
        throw new UnhandledException(e);
    }

    configuration = new AnnotationConfiguration();
    configuration.setProperty(Environment.CONNECTION_PROVIDER,
            "gov.nih.nci.caarray.upgrade.HibernateSingleConnectionProvider");

    configuration.configure(document);

    configuration.setProperty(Environment.TRANSACTION_STRATEGY,
            "org.hibernate.transaction.JDBCTransactionFactory");
    configuration.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
    configuration.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
    configuration.getProperties().remove(Environment.TRANSACTION_MANAGER_STRATEGY);
    configuration.setNamingStrategy(new NamingStrategy());

    sessionFactory = configuration.buildSessionFactory();
}

From source file:io.mashin.oep.hpdl.XMLReadUtils.java

License:Open Source License

public static HashMap<String, Point> graphicalInfoFrom(Document document) {
    HashMap<String, Point> graphicalInfoMap = new HashMap<String, Point>();
    try {// w w w. ja va 2s.c  o  m
        SAXReader reader = new SAXReader();
        Pattern p = Pattern.compile("\\s*<workflow>.*</workflow>\\s*", Pattern.DOTALL);

        @SuppressWarnings("unchecked")
        Iterator<Node> iter = document.nodeIterator();
        while (iter.hasNext()) {

            Node xmlNode = iter.next();
            if (xmlNode.getNodeType() == Node.COMMENT_NODE) {

                String graphicalInfo = xmlNode.getText();
                if (p.matcher(graphicalInfo).find()) {

                    Element graphicalElement = reader.read(new StringReader(graphicalInfo)).getRootElement();
                    @SuppressWarnings("unchecked")
                    Iterator<Node> gIter = graphicalElement.nodeIterator();

                    while (gIter.hasNext()) {
                        Node gNode = gIter.next();
                        if (gNode.getName() != null && gNode.getName().equals("node")) {
                            graphicalInfoMap.put(gNode.valueOf("@name"),
                                    new Point(Integer.parseInt(gNode.valueOf("@x")),
                                            Integer.parseInt(gNode.valueOf("@y"))));
                        }
                    }
                    break;

                }

            }

        }
    } catch (DocumentException ex) {
        ex.printStackTrace();
    }
    return graphicalInfoMap;
}

From source file:io.mashin.oep.hpdl.XMLReadUtils.java

License:Open Source License

public static List<Node> nodesList(Element rootElement) {
    List<Node> list = new ArrayList<Node>();
    @SuppressWarnings("unchecked")
    Iterator<Node> iter = rootElement.nodeIterator();
    while (iter.hasNext()) {
        Node node = iter.next();
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getName() != null && !node.getName().isEmpty()) {
            switch (node.getName()) {
            case "start":
            case "end":
            case "decision":
            case "fork":
            case "join":
            case "kill":
            case "action":
                list.add(node);/*from  w  w w. j a  va2s.c  o m*/
            }
        }
    }
    return list;
}

From source file:net.dontdrinkandroot.lastfm.api.CheckImplementationStatus.java

License:Apache License

/**
 * Recursively sets the namespace of the List and all children if the current namespace is match
 *///from   w  w  w . j  a v a  2s . c  o m
public static void setNamespaces(final List<Node> l, final Namespace ns) {

    Node n = null;
    for (int i = 0; i < l.size(); i++) {
        n = l.get(i);
        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            ((Attribute) n).setNamespace(ns);
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            CheckImplementationStatus.setNamespaces((Element) n, ns);
        }
    }
}