Search in sources :

Example 56 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project cxf by apache.

the class StaxUtils method createXMLStreamWriter.

public static XMLStreamWriter createXMLStreamWriter(Result r) {
    if (r instanceof DOMResult) {
        // use our own DOM writer to avoid issues with Sun's
        // version that doesn't support getNamespaceContext
        DOMResult dr = (DOMResult) r;
        Node nd = dr.getNode();
        if (nd instanceof Document) {
            return new W3CDOMStreamWriter((Document) nd);
        } else if (nd instanceof Element) {
            return new W3CDOMStreamWriter((Element) nd);
        } else if (nd instanceof DocumentFragment) {
            return new W3CDOMStreamWriter((DocumentFragment) nd);
        }
    }
    XMLOutputFactory factory = getXMLOutputFactory();
    try {
        return factory.createXMLStreamWriter(r);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Cant' create XMLStreamWriter", e);
    } finally {
        returnXMLOutputFactory(factory);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) Node(org.w3c.dom.Node) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 57 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project cxf by apache.

the class StaxUtils method readDocElement.

public static Node readDocElement(Document doc, Node parent, XMLEvent ev, StreamToDOMContext context) throws XMLStreamException {
    switch(ev.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
            {
                context.incrementCount();
                Element e;
                StartElement startElem = ev.asStartElement();
                QName name = startElem.getName();
                if (!StringUtils.isEmpty(name.getPrefix())) {
                    e = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart());
                } else {
                    e = doc.createElementNS(name.getNamespaceURI(), name.getLocalPart());
                }
                e = (Element) parent.appendChild(e);
                if (context.isRecordLoc()) {
                    context.setRecordLoc(addLocation(doc, e, startElem.getLocation(), context.isRecordLoc()));
                }
                if (context.isRepairing() && !isDeclared(e, name.getNamespaceURI(), name.getPrefix())) {
                    declare(e, name.getNamespaceURI(), name.getPrefix());
                }
                context.pushToStack(parent);
                if (context.isThreshold() && innerElementLevelThreshold != -1 && context.getStackSize() >= innerElementLevelThreshold) {
                    throw new DepthExceededStaxException("reach the innerElementLevelThreshold:" + innerElementLevelThreshold);
                }
                if (context.isThreshold() && innerElementCountThreshold != -1 && context.getCount() >= innerElementCountThreshold) {
                    throw new DepthExceededStaxException("reach the innerElementCountThreshold:" + innerElementCountThreshold);
                }
                parent = e;
                break;
            }
        case XMLStreamConstants.END_ELEMENT:
            if (context.isStackEmpty()) {
                return parent;
            }
            parent = context.popFromStack();
            if (parent instanceof Document || parent instanceof DocumentFragment) {
                return parent;
            }
            break;
        case XMLStreamConstants.NAMESPACE:
            Namespace ns = (Namespace) ev;
            declare((Element) parent, ns.getNamespaceURI(), ns.getPrefix());
            break;
        case XMLStreamConstants.ATTRIBUTE:
            Attribute at = (Attribute) ev;
            QName qname = at.getName();
            String attName = qname.getLocalPart();
            String attPrefix = qname.getPrefix();
            if (attPrefix != null && attPrefix.length() > 0) {
                attName = attPrefix + ":" + attName;
            }
            Attr attr = doc.createAttributeNS(qname.getNamespaceURI(), attName);
            attr.setValue(at.getValue());
            ((Element) parent).setAttributeNode(attr);
            break;
        case XMLStreamConstants.CHARACTERS:
            if (parent != null) {
                Characters characters = ev.asCharacters();
                context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createTextNode(characters.getData())), characters.getLocation(), context.isRecordLoc()));
            }
            break;
        case XMLStreamConstants.COMMENT:
            if (parent != null) {
                parent.appendChild(doc.createComment(((javax.xml.stream.events.Comment) ev).getText()));
            }
            break;
        case XMLStreamConstants.CDATA:
            Characters characters = ev.asCharacters();
            context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createCDATASection(characters.getData())), characters.getLocation(), context.isRecordLoc()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(((ProcessingInstruction) ev).getTarget(), ((ProcessingInstruction) ev).getData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            javax.xml.stream.events.EntityReference er = (javax.xml.stream.events.EntityReference) ev;
            parent.appendChild(doc.createEntityReference(er.getName()));
            break;
        default:
            break;
    }
    return parent;
}
Also used : Comment(org.w3c.dom.Comment) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) Characters(javax.xml.stream.events.Characters) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) Namespace(javax.xml.stream.events.Namespace) Attr(org.w3c.dom.Attr) StartElement(javax.xml.stream.events.StartElement) EntityReference(org.w3c.dom.EntityReference) DocumentFragment(org.w3c.dom.DocumentFragment) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Aggregations

DocumentFragment (org.w3c.dom.DocumentFragment)57 Document (org.w3c.dom.Document)27 Element (org.w3c.dom.Element)24 Node (org.w3c.dom.Node)20 NodeList (org.w3c.dom.NodeList)17 JAXBElement (javax.xml.bind.JAXBElement)8 Marshaller (javax.xml.bind.Marshaller)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 DOMException (org.w3c.dom.DOMException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 JAXBException (javax.xml.bind.JAXBException)4 DOMFragmentParser (org.cyberneko.html.parsers.DOMFragmentParser)4 Text (org.w3c.dom.Text)4 SAXException (org.xml.sax.SAXException)4 MalformedURLException (java.net.MalformedURLException)3 LinkedHashMap (java.util.LinkedHashMap)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3