Search in sources :

Example 1 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project hackpad by dropbox.

the class XmlNode method setProcessingInstructionName.

private void setProcessingInstructionName(String localName) {
    org.w3c.dom.ProcessingInstruction pi = (ProcessingInstruction) this.dom;
    //    We cannot set the node name; Document.renameNode() only supports elements and attributes.  So we replace it
    pi.getParentNode().replaceChild(pi, pi.getOwnerDocument().createProcessingInstruction(localName, pi.getData()));
}
Also used : ProcessingInstruction(org.w3c.dom.ProcessingInstruction) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 2 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project robovm by robovm.

the class XsltXPathConformanceTestSuite method emitNode.

private void emitNode(XmlSerializer serializer, Node node) throws IOException {
    if (node == null) {
        throw new UnsupportedOperationException("Cannot emit null nodes");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        serializer.startTag(element.getNamespaceURI(), element.getLocalName());
        emitAttributes(serializer, element);
        emitChildren(serializer, element);
        serializer.endTag(element.getNamespaceURI(), element.getLocalName());
    } else if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
        // TODO: is it okay to trim whitespace in general? This may cause
        //     false positives for elements like HTML's <pre> tag
        String trimmed = node.getTextContent().trim();
        if (trimmed.length() > 0) {
            serializer.text(trimmed);
        }
    } else if (node.getNodeType() == Node.DOCUMENT_NODE) {
        Document document = (Document) node;
        serializer.startDocument("UTF-8", true);
        emitNode(serializer, document.getDocumentElement());
        serializer.endDocument();
    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        ProcessingInstruction processingInstruction = (ProcessingInstruction) node;
        String data = processingInstruction.getData();
        String target = processingInstruction.getTarget();
        serializer.processingInstruction(target + " " + data);
    } else if (node.getNodeType() == Node.COMMENT_NODE) {
    // ignore!
    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
        EntityReference entityReference = (EntityReference) node;
        serializer.entityRef(entityReference.getNodeName());
    } else {
        throw new UnsupportedOperationException("Cannot emit " + node + " of type " + node.getNodeType());
    }
}
Also used : Element(org.w3c.dom.Element) EntityReference(org.w3c.dom.EntityReference) Document(org.w3c.dom.Document) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 3 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project robovm by robovm.

the class DOM3TreeWalker method serializePI.

/**
     * Serializes an ProcessingInstruction Node.
     * 
     * @param node The ProcessingInstruction Node to serialize
     */
protected void serializePI(ProcessingInstruction node) throws SAXException {
    ProcessingInstruction pi = node;
    String name = pi.getNodeName();
    // well-formed=true
    if ((fFeatures & WELLFORMED) != 0) {
        isPIWellFormed(node);
    }
    // apply the LSSerializer filter
    if (!applyFilter(node, NodeFilter.SHOW_PROCESSING_INSTRUCTION)) {
        return;
    }
    // String data = pi.getData();
    if (name.equals("xslt-next-is-raw")) {
        fNextIsRaw = true;
    } else {
        this.fSerializer.processingInstruction(name, pi.getData());
    }
}
Also used : ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 4 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project robovm by robovm.

the class TreeWalker method startNode.

/**
   * Start processing given node
   *
   *
   * @param node Node to process
   *
   * @throws org.xml.sax.SAXException
   */
protected void startNode(Node node) throws org.xml.sax.SAXException {
    if (m_contentHandler instanceof NodeConsumer) {
        ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
    }
    if (node instanceof Locator) {
        Locator loc = (Locator) node;
        m_locator.setColumnNumber(loc.getColumnNumber());
        m_locator.setLineNumber(loc.getLineNumber());
        m_locator.setPublicId(loc.getPublicId());
        m_locator.setSystemId(loc.getSystemId());
    } else {
        m_locator.setColumnNumber(0);
        m_locator.setLineNumber(0);
    }
    switch(node.getNodeType()) {
        case Node.COMMENT_NODE:
            {
                String data = ((Comment) node).getData();
                if (m_contentHandler instanceof LexicalHandler) {
                    LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
                    lh.comment(data.toCharArray(), 0, data.length());
                }
            }
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            // ??;
            break;
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            NamedNodeMap atts = ((Element) node).getAttributes();
            int nAttrs = atts.getLength();
            for (int i = 0; i < nAttrs; i++) {
                Node attr = atts.item(i);
                String attrName = attr.getNodeName();
                // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                    // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                    int index;
                    // Use "" instead of null, as Xerces likes "" for the 
                    // name of the default namespace.  Fix attributed 
                    // to "Steven Murray" <smurray@ebt.com>.
                    String prefix = (index = attrName.indexOf(":")) < 0 ? "" : attrName.substring(index + 1);
                    this.m_contentHandler.startPrefixMapping(prefix, attr.getNodeValue());
                }
            }
            // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));
            // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));
            String ns = m_dh.getNamespaceOfNode(node);
            if (null == ns)
                ns = "";
            this.m_contentHandler.startElement(ns, m_dh.getLocalNameOfNode(node), node.getNodeName(), new AttList(atts, m_dh));
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            {
                ProcessingInstruction pi = (ProcessingInstruction) node;
                String name = pi.getNodeName();
                // String data = pi.getData();
                if (name.equals("xslt-next-is-raw")) {
                    nextIsRaw = true;
                } else {
                    this.m_contentHandler.processingInstruction(pi.getNodeName(), pi.getData());
                }
            }
            break;
        case Node.CDATA_SECTION_NODE:
            {
                boolean isLexH = (m_contentHandler instanceof LexicalHandler);
                LexicalHandler lh = isLexH ? ((LexicalHandler) this.m_contentHandler) : null;
                if (isLexH) {
                    lh.startCDATA();
                }
                dispatachChars(node);
                {
                    if (isLexH) {
                        lh.endCDATA();
                    }
                }
            }
            break;
        case Node.TEXT_NODE:
            {
                if (nextIsRaw) {
                    nextIsRaw = false;
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
                    dispatachChars(node);
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
                } else {
                    dispatachChars(node);
                }
            }
            break;
        case Node.ENTITY_REFERENCE_NODE:
            {
                EntityReference eref = (EntityReference) node;
                if (m_contentHandler instanceof LexicalHandler) {
                    ((LexicalHandler) this.m_contentHandler).startEntity(eref.getNodeName());
                } else {
                // warning("Can not output entity to a pure SAX ContentHandler");
                }
            }
            break;
        default:
    }
}
Also used : Locator(org.xml.sax.Locator) NamedNodeMap(org.w3c.dom.NamedNodeMap) LexicalHandler(org.xml.sax.ext.LexicalHandler) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 5 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project robovm by robovm.

the class DocumentImpl method shallowCopy.

/**
     * Returns a shallow copy of the given node. If the node is an element node,
     * its attributes are always copied.
     *
     * @param node a node belonging to any document or DOM implementation.
     * @param operation the operation type to use when notifying user data
     *     handlers of copied element attributes. It is the caller's
     *     responsibility to notify user data handlers of the returned node.
     * @return a new node whose document is this document and whose DOM
     *     implementation is this DOM implementation.
     */
private NodeImpl shallowCopy(short operation, Node node) {
    switch(node.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            AttrImpl attr = (AttrImpl) node;
            AttrImpl attrCopy;
            if (attr.namespaceAware) {
                attrCopy = createAttributeNS(attr.getNamespaceURI(), attr.getLocalName());
                attrCopy.setPrefix(attr.getPrefix());
            } else {
                attrCopy = createAttribute(attr.getName());
            }
            attrCopy.setNodeValue(attr.getValue());
            return attrCopy;
        case Node.CDATA_SECTION_NODE:
            return createCDATASection(((CharacterData) node).getData());
        case Node.COMMENT_NODE:
            return createComment(((Comment) node).getData());
        case Node.DOCUMENT_FRAGMENT_NODE:
            return createDocumentFragment();
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot copy node of type " + node.getNodeType());
        case Node.ELEMENT_NODE:
            ElementImpl element = (ElementImpl) node;
            ElementImpl elementCopy;
            if (element.namespaceAware) {
                elementCopy = createElementNS(element.getNamespaceURI(), element.getLocalName());
                elementCopy.setPrefix(element.getPrefix());
            } else {
                elementCopy = createElement(element.getTagName());
            }
            NamedNodeMap attributes = element.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                AttrImpl elementAttr = (AttrImpl) attributes.item(i);
                AttrImpl elementAttrCopy = (AttrImpl) shallowCopy(operation, elementAttr);
                notifyUserDataHandlers(operation, elementAttr, elementAttrCopy);
                if (elementAttr.namespaceAware) {
                    elementCopy.setAttributeNodeNS(elementAttrCopy);
                } else {
                    elementCopy.setAttributeNode(elementAttrCopy);
                }
            }
            return elementCopy;
        case Node.ENTITY_NODE:
        case Node.NOTATION_NODE:
            // TODO: implement this when we support these node types
            throw new UnsupportedOperationException();
        case Node.ENTITY_REFERENCE_NODE:
            /*
             * When we support entities in the doctype, this will need to
             * behave differently for clones vs. imports. Clones copy
             * entities by value, copying the referenced subtree from the
             * original document. Imports copy entities by reference,
             * possibly referring to a different subtree in the new
             * document.
             */
            return createEntityReference(node.getNodeName());
        case Node.PROCESSING_INSTRUCTION_NODE:
            ProcessingInstruction pi = (ProcessingInstruction) node;
            return createProcessingInstruction(pi.getTarget(), pi.getData());
        case Node.TEXT_NODE:
            return createTextNode(((Text) node).getData());
        default:
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unsupported node type " + node.getNodeType());
    }
}
Also used : DOMException(org.w3c.dom.DOMException) NamedNodeMap(org.w3c.dom.NamedNodeMap) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Aggregations

ProcessingInstruction (org.w3c.dom.ProcessingInstruction)20 Element (org.w3c.dom.Element)10 NamedNodeMap (org.w3c.dom.NamedNodeMap)9 Node (org.w3c.dom.Node)8 Document (org.w3c.dom.Document)7 EntityReference (org.w3c.dom.EntityReference)6 Locator (org.xml.sax.Locator)4 LexicalHandler (org.xml.sax.ext.LexicalHandler)4 DOMException (org.w3c.dom.DOMException)3 DocumentType (org.w3c.dom.DocumentType)3 AttList (org.apache.xml.serializer.utils.AttList)2 Attr (org.w3c.dom.Attr)2 Comment (org.w3c.dom.Comment)2 NodeList (org.w3c.dom.NodeList)2 CDATASection (org.w3c.dom.CDATASection)1 DOMImplementation (org.w3c.dom.DOMImplementation)1 Text (org.w3c.dom.Text)1