Search in sources :

Example 11 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project webservices-axiom by apache.

the class DocumentImpl method importNode.

public Node importNode(Node importedNode, boolean deep) throws DOMException {
    short type = importedNode.getNodeType();
    Node newNode = null;
    switch(type) {
        case Node.ELEMENT_NODE:
            {
                Element newElement;
                if (importedNode.getLocalName() == null) {
                    newElement = this.createElement(importedNode.getNodeName());
                } else {
                    String ns = importedNode.getNamespaceURI();
                    ns = (ns != null) ? ns.intern() : null;
                    newElement = createElementNS(ns, importedNode.getNodeName());
                }
                // Copy element's attributes, if any.
                NamedNodeMap sourceAttrs = importedNode.getAttributes();
                if (sourceAttrs != null) {
                    int length = sourceAttrs.getLength();
                    for (int index = 0; index < length; index++) {
                        ((ElementImpl) newElement).coreAppendAttribute((AttrImpl) importNode(sourceAttrs.item(index), true));
                    }
                }
                newNode = newElement;
                break;
            }
        case Node.ATTRIBUTE_NODE:
            {
                if (importedNode.getLocalName() == null) {
                    newNode = createAttribute(importedNode.getNodeName());
                } else {
                    String ns = importedNode.getNamespaceURI();
                    ns = (ns != null) ? ns.intern() : null;
                    newNode = createAttributeNS(ns, importedNode.getNodeName());
                }
                ((Attr) newNode).setValue(importedNode.getNodeValue());
                break;
            }
        case Node.TEXT_NODE:
            {
                newNode = createTextNode(importedNode.getNodeValue());
                break;
            }
        case Node.COMMENT_NODE:
            {
                newNode = createComment(importedNode.getNodeValue());
                break;
            }
        case Node.DOCUMENT_FRAGMENT_NODE:
            {
                newNode = createDocumentFragment();
                // No name, kids carry value
                break;
            }
        case Node.CDATA_SECTION_NODE:
            newNode = createCDATASection(importedNode.getNodeValue());
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            {
                ProcessingInstruction pi = (ProcessingInstruction) importedNode;
                newNode = createProcessingInstruction(pi.getTarget(), pi.getData());
                break;
            }
        case Node.ENTITY_REFERENCE_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.NOTATION_NODE:
            throw new UnsupportedOperationException("TODO : Implement handling of org.w3c.dom.Node type == " + type);
        // Can't import document nodes
        case Node.DOCUMENT_NODE:
        default:
            throw newDOMException(DOMException.NOT_SUPPORTED_ERR);
    }
    // If deep, replicate and attach the kids.
    if (deep && !(importedNode instanceof Attr)) {
        for (Node srckid = importedNode.getFirstChild(); srckid != null; srckid = srckid.getNextSibling()) {
            newNode.appendChild(importNode(srckid, true));
        }
    }
    return newNode;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ProcessingInstruction(org.w3c.dom.ProcessingInstruction) Attr(org.w3c.dom.Attr)

Example 12 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project j2objc by google.

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)

Example 13 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project j2objc by google.

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 14 with ProcessingInstruction

use of org.w3c.dom.ProcessingInstruction in project j2objc by google.

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 15 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 (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:
            Element elem_node = (Element) node;
            {
                // Make sure the namespace node
                // for the element itself is declared
                // to the ContentHandler
                String uri = elem_node.getNamespaceURI();
                if (uri != null) {
                    String prefix = elem_node.getPrefix();
                    if (prefix == null)
                        prefix = "";
                    this.m_contentHandler.startPrefixMapping(prefix, uri);
                }
            }
            NamedNodeMap atts = elem_node.getAttributes();
            int nAttrs = atts.getLength();
            // each attribute is declared to the ContentHandler
            for (int i = 0; i < nAttrs; i++) {
                final Node attr = atts.item(i);
                final String attrName = attr.getNodeName();
                final int colon = attrName.indexOf(':');
                final String prefix;
                // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                    // to "Steven Murray" <smurray@ebt.com>.
                    if (colon < 0)
                        prefix = "";
                    else
                        prefix = attrName.substring(colon + 1);
                    this.m_contentHandler.startPrefixMapping(prefix, attr.getNodeValue());
                } else if (colon > 0) {
                    prefix = attrName.substring(0, colon);
                    String uri = attr.getNamespaceURI();
                    if (uri != null)
                        this.m_contentHandler.startPrefixMapping(prefix, uri);
                }
            }
            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) AttList(org.apache.xml.serializer.utils.AttList) 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)

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