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:architecture.common.xml.XmlWriter.java

License:Apache License

protected void writeNode(Node node) throws IOException {
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writeElement((Element) node);
        break;//from   ww  w  .  jav a2 s  .  co  m
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeNodeText(node);
        // write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        // write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:architecture.ee.util.xml.XmlWriter.java

License:Apache License

protected void writeNode(Node node) throws IOException {
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writeElement((Element) node);
        break;/* www.  j a  va 2s. co  m*/
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeNodeText(node);
        //write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        //write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:bixo.examples.webmining.AnalyzeHtml.java

License:Apache License

private String getAttributeFromNode(Node node, String attribute) {
    String attributeValue = null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) node;
        attributeValue = e.attributeValue(attribute);
    }/*from www .j ava 2  s.  co  m*/
    return (attributeValue == null ? "" : attributeValue);
}

From source file:com.christophermrossi.jpt.PageTemplateImpl.java

License:Open Source License

private void defaultContent(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        Interpreter beanShell, Stack 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 = node.getText().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  w  w  w . ja  v  a 2  s .c  om*/
            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.cladonia.xml.helper.Helper.java

License:Open Source License

private void setElementInternal(Node baseNode, ElementInformation ei) {
    try {/*from   www . java  2 s .  co  m*/
        XElement node = null;

        if (baseNode.getNodeType() == org.dom4j.Node.ATTRIBUTE_NODE) {
            node = (XElement) baseNode.getParent();
        } else if (baseNode.getNodeType() == org.dom4j.Node.ELEMENT_NODE) {
            node = (XElement) baseNode;
        }

        SchemaElement element = null;

        if (ei != null) {

            if (ei instanceof SchemaElement) {
                element = (SchemaElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (element != null) {
                    setText(type, element.getType());

                    Vector modAtts = element.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            SchemaAttribute sa = (SchemaAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = element.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            SchemaElement sa = (SchemaElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else if (ei instanceof DTDElement) {
                DTDElement dtdElement = (DTDElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (dtdElement != null) {
                    setText(type, dtdElement.getType());

                    Vector modAtts = dtdElement.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            DTDAttribute sa = (DTDAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = dtdElement.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            DTDElement sa = (DTDElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else if (ei instanceof RNGElement) {
                RNGElement rngElement = (RNGElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (rngElement != null) {
                    setText(type, rngElement.getType());

                    Vector modAtts = rngElement.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            RNGAttribute sa = (RNGAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = rngElement.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            RNGElement sa = (RNGElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else {

            }
        } else {
            //System.out.println("one is null");
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeMixedNode(Node node) throws IOException {
    if (DEBUG)/*from  ww w .ja  v  a2 s .  co  m*/
        System.out.println("XMLFormatter.writeMixedNode( " + node + ")");
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writeMixedElement((Element) node);
        break;
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeString(node.getText());
        //write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        //write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writePreservedNode(Node node) throws IOException {
    if (DEBUG)/*from w ww . j a va2 s. c  o m*/
        System.out.println("XMLFormatter.writeMixedNode( " + node + ")");
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writePreservedElement((Element) node);
        break;
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeString(node.getText());
        //write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        //write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:com.cladonia.xngreditor.actions.OpenBrowserAction.java

License:Open Source License

/**
 * Method to check if the document contains a processing intruction
 * which specifies a stylesheet. If it cannot find one declared at the beginning
 * it will walk the tree to see can it find one.
 * //from w  w  w.j a v  a 2 s .c  o  m
 * @param document
 * @return boolean true or false
 */
public boolean checkForStylesheetPI(ExchangerDocument document) {
    XDocument doc = document.getDocument();
    for (int cnt = 0; cnt < doc.nodeCount(); ++cnt) {
        Node n = doc.node(cnt);
        if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            FlyweightProcessingInstruction pi = (FlyweightProcessingInstruction) n;
            if (pi.getTarget().equalsIgnoreCase(XML_STYLESHEET)) {
                return (true);
            }
        }

    }
    return (treeWalk(document.getRoot()));

}

From source file:com.cladonia.xngreditor.actions.OpenBrowserAction.java

License:Open Source License

/**
 * walks the tree to see if it can find a processing instruction 
 * relating to a stylesheet// ww w  . j a v a2s . co  m
 * @param element
 * @return boolean true or false
 */
public boolean treeWalk(XElement element) {
    for (int i = 0, size = element.nodeCount(); i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Element) {
            treeWalk((XElement) node);
        } else {

            if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
                FlyweightProcessingInstruction pi = (FlyweightProcessingInstruction) node;
                if (pi.getTarget().equalsIgnoreCase(XML_STYLESHEET)) {
                    return (true);
                }
            }
        }
    }
    return (false);
}

From source file:com.devoteam.srit.xmlloader.core.utils.XMLElementTextOnlyParser.java

License:Open Source License

public List<Element> replace(Element element, ParameterPool variables) throws Exception {
    List<Element> result = new LinkedList();
    result.add(element.createCopy());// ww  w  .j a  va 2  s. c  om
    element = result.get(0);

    Iterator nodesIterator = element.nodeIterator();
    HashMap<Node, Node> nodesToReplace = new HashMap<Node, Node>();

    boolean alreadyNext = false;

    while (nodesIterator.hasNext()) {
        Node node = null;
        if (!alreadyNext) {
            node = (Node) nodesIterator.next();
        }

        Node lastTextNode = null;
        String lastTextNodeText = "";
        alreadyNext = false;

        //
        // We put all successive TEXT Nodes into one node ( there is some fragmentation i don't understand )
        //
        while (null != node && node.getNodeType() == Node.TEXT_NODE) {
            alreadyNext = true;
            lastTextNode = (Text) node;
            lastTextNodeText += lastTextNode.getText();

            // this node will be deleted later ... if not overwritten in this hashmap
            nodesToReplace.put(lastTextNode, null);
            if (nodesIterator.hasNext()) {
                node = (Node) nodesIterator.next();
            } else {
                node = null;
            }
        }

        //
        // We process normally the CDATA Nodes
        // 
        if (null != node && node.getNodeType() == Node.CDATA_SECTION_NODE) {
            lastTextNode = (Node) node;
            lastTextNodeText = lastTextNode.getText();
        }

        //
        // We do nothing for the other type Nodes
        // 
        if (null == lastTextNode) {
            continue;
        }

        lastTextNode.setText(lastTextNodeText);

        //
        // Now that we have only one, complete, TEXT node or one CDATA node to proceed
        //
        Node textNode = (Node) lastTextNode;

        String text = textNode.getText();
        String out = "";

        int endOfLine;
        String line;

        //
        // Transform all \r\n, in \n
        //
        //text = Utils.replaceNoRegex(text, "\r", "");

        while (text.length() > 0) {
            //
            // Read a line
            //
            endOfLine = text.indexOf("\n");

            if (endOfLine == -1) {
                line = text;
                text = "";
            } else {
                line = text.substring(0, endOfLine + 1);
                text = text.substring(endOfLine + 1);
            }

            //
            // Replace line if it contains at least a variable
            //
            if (Parameter.containsParameter(line)) {
                List<String> results = variables.parse(line);

                for (String s : results) {
                    out += s;
                }
            } else {
                out += line;
            }
        }

        //
        // Set new text into new AVP
        //
        Text newTextNode = new DefaultText(out);
        nodesToReplace.put(textNode, newTextNode);
    }

    for (Node key : nodesToReplace.keySet()) {
        DefaultElementInterface.replaceNode((DefaultElement) element, key, nodesToReplace.get(key));
    }

    if (result.size() != 1) {
        throw new ExecutionException("Size of result for XMLElementTextOnlyParser should be 1");
    }

    return result;
}