Search in sources :

Example 36 with DOMResult

use of javax.xml.transform.dom.DOMResult in project camel by apache.

the class XmlConverter method toDOMNodeFromSAX.

@Converter
public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
    DOMResult result = new DOMResult();
    toResult(source, result);
    return result.getNode();
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) Converter(org.apache.camel.Converter)

Example 37 with DOMResult

use of javax.xml.transform.dom.DOMResult in project qi4j-sdk by Qi4j.

the class StaxValueDeserializer method readObjectTree.

@Override
protected Node readObjectTree(XMLEventReader input) throws Exception {
    XMLEvent peek = input.peek();
    if (peek.isStartElement() && "null".equals(peek.asStartElement().getName().getLocalPart())) {
        // <null>
        input.nextTag();
        // </null>
        input.nextTag();
        return null;
    }
    String elementBody = readElementBody(input);
    Transformer transformer = transformerFactory.newTransformer();
    DOMResult domResult = new DOMResult();
    transformer.transform(new StreamSource(new StringReader(elementBody)), domResult);
    return ((Document) domResult.getNode()).getDocumentElement();
}
Also used : Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) XMLEvent(javax.xml.stream.events.XMLEvent) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 38 with DOMResult

use of javax.xml.transform.dom.DOMResult in project robovm by robovm.

the class TransformerImpl method createSerializationHandler.

/**
     * Create a ContentHandler from a Result object and an OutputProperties.
     *
     * @param outputTarget Where the transform result should go,
     * should not be null.
     * @param format The OutputProperties object that will contain
     * instructions on how to serialize the output.
     *
     * @return A valid ContentHandler that will create the
     * result tree when it is fed SAX events.
     *
     * @throws TransformerException
     */
public SerializationHandler createSerializationHandler(Result outputTarget, OutputProperties format) throws TransformerException {
    SerializationHandler xoh;
    // If the Result object contains a Node, then create
    // a ContentHandler that will add nodes to the input node.
    org.w3c.dom.Node outputNode = null;
    if (outputTarget instanceof DOMResult) {
        outputNode = ((DOMResult) outputTarget).getNode();
        org.w3c.dom.Node nextSibling = ((DOMResult) outputTarget).getNextSibling();
        org.w3c.dom.Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument();
        } else {
            boolean isSecureProcessing = m_stylesheetRoot.isSecureProcessing();
            doc = org.apache.xml.utils.DOMHelper.createDocument(isSecureProcessing);
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            handler.setNextSibling(nextSibling);
        String encoding = format.getProperty(OutputKeys.ENCODING);
        xoh = new ToXMLSAXHandler(handler, (LexicalHandler) handler, encoding);
    } else if (outputTarget instanceof SAXResult) {
        ContentHandler handler = ((SAXResult) outputTarget).getHandler();
        if (null == handler)
            throw new IllegalArgumentException("handler can not be null for a SAXResult");
        LexicalHandler lexHandler;
        if (handler instanceof LexicalHandler)
            lexHandler = (LexicalHandler) handler;
        else
            lexHandler = null;
        String encoding = format.getProperty(OutputKeys.ENCODING);
        String method = format.getProperty(OutputKeys.METHOD);
        ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding);
        toXMLSAXHandler.setShouldOutputNSAttr(false);
        xoh = toXMLSAXHandler;
        String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
        String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM);
        if (systemID != null)
            xoh.setDoctypeSystem(systemID);
        if (publicID != null)
            xoh.setDoctypePublic(publicID);
        if (handler instanceof TransformerClient) {
            XalanTransformState state = new XalanTransformState();
            ((TransformerClient) handler).setTransformState(state);
            ((ToSAXHandler) xoh).setTransformState(state);
        }
    } else // result tree to either a stream or a writer.
    if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            SerializationHandler serializer = (SerializationHandler) SerializerFactory.getSerializer(format.getProperties());
            if (null != sresult.getWriter())
                serializer.setWriter(sresult.getWriter());
            else if (null != sresult.getOutputStream())
                serializer.setOutputStream(sresult.getOutputStream());
            else if (null != sresult.getSystemId()) {
                String fileURL = sresult.getSystemId();
                if (fileURL.startsWith("file:///")) {
                    if (fileURL.substring(8).indexOf(":") > 0)
                        fileURL = fileURL.substring(8);
                    else
                        fileURL = fileURL.substring(7);
                } else if (fileURL.startsWith("file:/")) {
                    if (fileURL.substring(6).indexOf(":") > 0)
                        fileURL = fileURL.substring(6);
                    else
                        fileURL = fileURL.substring(5);
                }
                m_outputStream = new java.io.FileOutputStream(fileURL);
                serializer.setOutputStream(m_outputStream);
                xoh = serializer;
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            // handler = serializer.asContentHandler();
            //  this.setSerializer(serializer);
            xoh = serializer;
        }//        }
         catch (IOException ioe) {
            throw new TransformerException(ioe);
        }
    } else {
        //"Can't transform to a Result of type "
        throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
    //+ outputTarget.getClass().getName()
    //+ "!");
    }
    // before we forget, lets make the created handler hold a reference
    // to the current TransformImpl object
    xoh.setTransformer(this);
    SourceLocator srcLocator = getStylesheet();
    xoh.setSourceLocator(srcLocator);
    return xoh;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SerializationHandler(org.apache.xml.serializer.SerializationHandler) DOMBuilder(org.apache.xml.utils.DOMBuilder) ContentHandler(org.xml.sax.ContentHandler) LexicalHandler(org.xml.sax.ext.LexicalHandler) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) SAXResult(javax.xml.transform.sax.SAXResult) SourceLocator(javax.xml.transform.SourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) XObject(org.apache.xpath.objects.XObject) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler)

Example 39 with DOMResult

use of javax.xml.transform.dom.DOMResult in project dbeaver by serge-rider.

the class DBDDocumentXML method updateDocument.

@Override
public void updateDocument(@NotNull DBRProgressMonitor monitor, @NotNull InputStream stream, String encoding) throws DBException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMResult output = new DOMResult();
        transformer.transform(new StreamSource(new InputStreamReader(stream, encoding)), output);
        document = (Document) output.getNode();
        modified = true;
    } catch (Exception e) {
        throw new DBException("Error transforming XML document", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) DBException(org.jkiss.dbeaver.DBException)

Example 40 with DOMResult

use of javax.xml.transform.dom.DOMResult in project nokogiri by sparklemotion.

the class XsltStylesheet method transform.

@JRubyMethod(rest = true, required = 1, optional = 2)
public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.getRuntime();
    argumentTypeCheck(runtime, args[0]);
    NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
    DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
    final DOMResult result;
    String stringResult = null;
    try {
        // DOMResult
        result = tryXsltTransformation(context, args, domSource, elistener);
        if (result.getNode().getFirstChild() == null) {
            // StreamResult
            stringResult = retryXsltTransformation(context, args, domSource, elistener);
        }
    } catch (TransformerConfigurationException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    } catch (TransformerException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    } catch (IOException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    }
    switch(elistener.getErrorType()) {
        case ERROR:
        case FATAL:
            throw runtime.newRuntimeError(elistener.getErrorMessage());
        case WARNING:
        default:
    }
    if (stringResult == null) {
        return createDocumentFromDomResult(context, runtime, result);
    } else {
        return createDocumentFromString(context, runtime, stringResult);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) RubyString(org.jruby.RubyString) IOException(java.io.IOException) Ruby(org.jruby.Ruby) TransformerException(javax.xml.transform.TransformerException) NokogiriXsltErrorListener(nokogiri.internals.NokogiriXsltErrorListener) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

DOMResult (javax.xml.transform.dom.DOMResult)61 Document (org.w3c.dom.Document)33 DOMSource (javax.xml.transform.dom.DOMSource)24 Transformer (javax.xml.transform.Transformer)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)17 TransformerException (javax.xml.transform.TransformerException)14 IOException (java.io.IOException)13 InputSource (org.xml.sax.InputSource)13 StreamSource (javax.xml.transform.stream.StreamSource)12 StringReader (java.io.StringReader)11 SAXResult (javax.xml.transform.sax.SAXResult)10 Element (org.w3c.dom.Element)10 Node (org.w3c.dom.Node)10 SAXSource (javax.xml.transform.sax.SAXSource)9 StreamResult (javax.xml.transform.stream.StreamResult)9 Test (org.junit.Test)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 Source (javax.xml.transform.Source)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 InputStream (java.io.InputStream)6