org.jdom.Comment Java Examples

The following examples show how to use org.jdom.Comment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JDOMStreamReader.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
protected int nextChild() {
    int currentChild = getCurrentFrame().getCurrentChild();
    currentChild++;
    getCurrentFrame().setCurrentChild(currentChild);
    this.content = getCurrentElement().getContent(currentChild);

    if (content instanceof Text) {
        return CHARACTERS;
    } else if (content instanceof Element) {
        setupNamespaces((Element)content);
        return START_ELEMENT;
    } else if (content instanceof Comment) {
        return CHARACTERS;
    } else if (content instanceof EntityRef) {
        return ENTITY_REFERENCE;
    }

    throw new IllegalStateException();
}
 
Example #2
Source File: JDOMNodePointer.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
/**
 * Get the parent of the specified node.
 * @param node to check
 * @return parent Element
 */
private static Element nodeParent(Object node) {
    if (node instanceof Element) {
        Object parent = ((Element) node).getParent();
        return parent instanceof Element ? (Element) parent : null;
    }
    if (node instanceof Text) {
        return (Element) ((Text) node).getParent();
    }
    if (node instanceof CDATA) {
        return (Element) ((CDATA) node).getParent();
    }
    if (node instanceof ProcessingInstruction) {
        return (Element) ((ProcessingInstruction) node).getParent();
    }
    if (node instanceof Comment) {
        return (Element) ((Comment) node).getParent();
    }
    return null;
}
 
Example #3
Source File: JDOMNodePointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Add the specified content to this element.
 * @param content List
 */
private void addContent(List content) {
    Element element = (Element) node;
    int count = content.size();

    for (int i = 0; i < count; i++) {
        Object child = content.get(i);
        if (child instanceof Element) {
            child = ((Element) child).clone();
            element.addContent((Element) child);
        }
        else if (child instanceof Text) {
            child = ((Text) child).clone();
            element.addContent((Text) child);
        }
        else if (node instanceof CDATA) {
            child = ((CDATA) child).clone();
            element.addContent((CDATA) child);
        }
        else if (node instanceof ProcessingInstruction) {
            child = ((ProcessingInstruction) child).clone();
            element.addContent((ProcessingInstruction) child);
        }
        else if (node instanceof Comment) {
            child = ((Comment) child).clone();
            element.addContent((Comment) child);
        }
    }
}
 
Example #4
Source File: JDOMStreamWriter.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void writeComment(String value) throws XMLStreamException {
    currentNode.addContent(new Comment(value));
}
 
Example #5
Source File: StaxSerializer.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException {
    // need to check if the namespace is declared before we write the
    // start element because that will put the namespace in the context.
    String elPrefix = e.getNamespacePrefix();
    String elUri = e.getNamespaceURI();

    String boundPrefix = writer.getPrefix(elUri);
    boolean writeElementNS = false;
    if (boundPrefix == null || !elPrefix.equals(boundPrefix)) {
        writeElementNS = true;
    }

    writer.writeStartElement(elPrefix, e.getName(), elUri);

    List<?> namespaces = e.getAdditionalNamespaces();
    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext();) {
        Namespace ns = (Namespace)itr.next();

        String prefix = ns.getPrefix();
        String uri = ns.getURI();

        writer.setPrefix(prefix, uri);
        writer.writeNamespace(prefix, uri);

        if (elUri.equals(uri) && elPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    for (Iterator<?> itr = e.getAttributes().iterator(); itr.hasNext();) {
        Attribute attr = (Attribute)itr.next();
        String attPrefix = attr.getNamespacePrefix();
        String attUri = attr.getNamespaceURI();

        if (attUri == null || attUri.isEmpty()) {
            writer.writeAttribute(attr.getName(), attr.getValue());
        } else {
            writer.writeAttribute(attPrefix, attUri, attr.getName(), attr.getValue());

            if (!isDeclared(writer, attPrefix, attUri)) {
                if (elUri.equals(attUri) && elPrefix.equals(attPrefix)) {
                    if (writeElementNS) {
                        writer.setPrefix(attPrefix, attUri);
                        writer.writeNamespace(attPrefix, attUri);
                        writeElementNS = false;
                    }
                } else {
                    writer.setPrefix(attPrefix, attUri);
                    writer.writeNamespace(attPrefix, attUri);
                }
            }
        }
    }

    if (writeElementNS) {
        if (elPrefix == null || elPrefix.length() == 0) {
            writer.writeDefaultNamespace(elUri);
        } else {
            writer.writeNamespace(elPrefix, elUri);
        }
    }

    for (Iterator<?> itr = e.getContent().iterator(); itr.hasNext();) {
        Content n = (Content)itr.next();
        if (n instanceof CDATA) {
            writer.writeCData(n.getValue());
        } else if (n instanceof Text) {
            writer.writeCharacters(((Text)n).getText());
        } else if (n instanceof Element) {
            writeElement((Element)n, writer);
        } else if (n instanceof Comment) {
            writer.writeComment(n.getValue());
        } else if (n instanceof EntityRef) {
            // EntityRef ref = (EntityRef) n;
            // writer.writeEntityRef(ref.)
        }
    }

    writer.writeEndElement();
}
 
Example #6
Source File: JDOMNodePointer.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * Execute test against node on behalf of pointer.
 * @param pointer Pointer
 * @param node to test
 * @param test to execute
 * @return true if node passes test
 */
public static boolean testNode(
    NodePointer pointer,
    Object node,
    NodeTest test) {
    if (test == null) {
        return true;
    }
    if (test instanceof NodeNameTest) {
        if (!(node instanceof Element)) {
            return false;
        }

        NodeNameTest nodeNameTest = (NodeNameTest) test;
        QName testName = nodeNameTest.getNodeName();
        String namespaceURI = nodeNameTest.getNamespaceURI();
        boolean wildcard = nodeNameTest.isWildcard();
        String testPrefix = testName.getPrefix();
        if (wildcard && testPrefix == null) {
            return true;
        }
        if (wildcard
            || testName.getName()
                    .equals(JDOMNodePointer.getLocalName(node))) {
            String nodeNS = JDOMNodePointer.getNamespaceURI(node);
            return equalStrings(namespaceURI, nodeNS) || nodeNS == null
                    && equalStrings(testPrefix, getPrefix(node));
        }
        return false;
    }
    if (test instanceof NodeTypeTest) {
        switch (((NodeTypeTest) test).getNodeType()) {
            case Compiler.NODE_TYPE_NODE :
                return true;
            case Compiler.NODE_TYPE_TEXT :
                return (node instanceof Text) || (node instanceof CDATA);
            case Compiler.NODE_TYPE_COMMENT :
                return node instanceof Comment;
            case Compiler.NODE_TYPE_PI :
                return node instanceof ProcessingInstruction;
            default:
                return false;
        }
    }
    if (test instanceof ProcessingInstructionTest && node instanceof ProcessingInstruction) {
        String testPI = ((ProcessingInstructionTest) test).getTarget();
        String nodePI = ((ProcessingInstruction) node).getTarget();
        return testPI.equals(nodePI);
    }
    return false;
}