Example usage for org.dom4j Node ATTRIBUTE_NODE

List of usage examples for org.dom4j Node ATTRIBUTE_NODE

Introduction

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

Prototype

short ATTRIBUTE_NODE

To view the source code for org.dom4j Node ATTRIBUTE_NODE.

Click Source Link

Document

Matches elements nodes

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.j  av a2s  . c o 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.common.xml.XmlWriter.java

License:Apache License

protected void writeAttribute(Attribute attribute) throws IOException {
    writer.write(" ");
    writer.write(attribute.getQualifiedName());
    writer.write("=");

    char quote = format.getAttributeQuoteCharacter();
    writer.write(quote);// w w w  .j a  v a 2s.  co m

    writeEscapeAttributeEntities(attribute.getValue());

    writer.write(quote);
    lastOutputNodeType = Node.ATTRIBUTE_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;//from www .jav a  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: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  www. ja v  a2s . c o 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.cladonia.xml.ExchangerXMLWriter.java

License:Open Source License

protected void writeAttribute(Attribute attribute) throws IOException {
    StringBuffer buffer = new StringBuffer();
    XAttribute a = (XAttribute) attribute;

    buffer.append(attribute.getQualifiedName());
    buffer.append("=");

    String value = attribute.getValue();
    boolean hasQuote = value.indexOf("\"") != -1;

    if (hasQuote) {
        buffer.append("'");
        buffer.append(value);/* www  . j  a va2 s .  com*/
        buffer.append("'");
    } else {
        buffer.append("\"");
        buffer.append(value);
        buffer.append("\"");
    }

    String attr = buffer.toString();

    //      System.out.println( "Wrap ["+isWrapText()+", "+getMaxLineLength()+", "+((PositionedWriter)writer).getColumn()+", "+attr.length()+"]");

    if (getMaxLineLength() != -1
            && (((PositionedWriter) writer).getColumn() + attr.length() + 1) >= getMaxLineLength()) {
        //         System.out.println( "Wrapping ... ["+attr+"]");
        ++indentLevel;

        format.setNewlines(true);
        format.setIndent(indentString);

        writePrintln();
        indent();

        --indentLevel;

        format.setNewlines(false);
        format.setIndent("");
    } else {
        writer.write(" ");
    }

    a.setAttributeStartPosition(((PositionedWriter) writer).getPosition());

    writer.write(attr);

    a.setAttributeEndPosition(((PositionedWriter) writer).getPosition());

    lastOutputNodeType = Node.ATTRIBUTE_NODE;
}

From source file:com.cladonia.xml.helper.Helper.java

License:Open Source License

private void setElementInternal(Node baseNode, ElementInformation ei) {
    try {//www  .  ja  v a2s. c  om
        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)/*  ww  w.  j a va 2s .  c o  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  w w  .  j av  a2s.co  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.xml.XMLFormatter.java

License:Mozilla Public License

/** Writes the attributes of the given element
  *//from  www .ja v a 2s  . c  om
  */
//    protected void writeAttributes( Element element ) throws IOException {
//       if (DEBUG) System.out.println( "XMLFormatter.writeAttributes( "+element+")");
//       if (indentMixed) {
//          super.writeAttributes( element);
//       } else {

// I do not yet handle the case where the same prefix maps to
// two different URIs. For attributes on the same element
// this is illegal; but as yet we don't throw an exception
// if someone tries to do this
//           for ( int i = 0, size = element.attributeCount(); i < size; i++ ) {
//               Attribute attribute = element.attribute(i);
//               Namespace ns = attribute.getNamespace();
//               if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
//                   String prefix = ns.getPrefix();
//                   String uri = namespaceStack.getURI(prefix);
//                   if (!ns.getURI().equals(uri)) { // output a new namespace declaration
//                       writeNamespace(ns);
//                       namespaceStack.push(ns);
//                   }
//               }
//
//               writer.write(" ");
//               writer.write(attribute.getQualifiedName());
//               writer.write("=\"");
//               writeEscapeAttributeEntities(attribute.getValue());
//               writer.write("\"");
//           }
////       }
//    }

protected void writeAttribute(Attribute attribute) throws IOException {
    XAttribute a = (XAttribute) attribute;

    //       if ( attributeOnNewLine) {
    //          ++indentLevel;
    //
    //          format.setNewlines( true);
    //          format.setIndent( indentString);
    //          
    //           writePrintln();
    //           indent();
    //       } else {
    writer.write(" ");
    //       }

    writer.write(attribute.getQualifiedName());
    writer.write("=");

    String value = attribute.getValue();

    if (format.isTrimText() && value != null && value.trim().length() > 0) {
        StringTokenizer tokenizer = new StringTokenizer(value);
        StringBuffer buffer = new StringBuffer();
        boolean first = true;

        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();

            if (!first) {
                buffer.append(" ");
            }

            buffer.append(token);
            first = false;
        }

        value = buffer.toString();
    }

    boolean hasQuote = value.indexOf("\"") != -1;

    if (hasQuote) {
        writer.write("'");
        writeEscapeAttributeEntities(value);
        writer.write("'");
    } else {
        writer.write("\"");
        writeEscapeAttributeEntities(value);
        writer.write("\"");
    }

    lastOutputNodeType = Node.ATTRIBUTE_NODE;

    // reset...
    //       if ( attributeOnNewLine) {
    //          --indentLevel;
    //
    //          format.setNewlines( false);
    //          format.setIndent( "");
    //       }
}

From source file:com.flaptor.util.parser.HtmlParser.java

License:Apache License

@SuppressWarnings("unchecked")
private void removeNamespace(List list) {
    if (null != list) {
        for (Node node : (List<Node>) list) {
            if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                ((Attribute) node).setNamespace(Namespace.NO_NAMESPACE);
            } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                removeNamespace((Element) node);
            }//from  w  w w  .  j  av  a 2 s. c om
        }
    }
}