Search in sources :

Example 6 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project nokogiri by sparklemotion.

the class XmlRelaxng method getSchema.

private Schema getSchema(Source source, ThreadContext context) {
    InputStream is;
    VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
    if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        is = ss.getInputStream();
    } else {
        //if (this.source instanceof DOMSource)
        DOMSource ds = (DOMSource) source;
        StringWriter xmlAsWriter = new StringWriter();
        StreamResult result = new StreamResult(xmlAsWriter);
        try {
            TransformerFactory.newInstance().newTransformer().transform(ds, result);
        } catch (TransformerConfigurationException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        } catch (TransformerException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
        try {
            is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
    }
    try {
        return factory.compileSchema(is);
    } catch (VerifierConfigurationException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 7 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project gocd by gocd.

the class XsltStylesheet method parse_stylesheet_doc.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    Ruby runtime = context.getRuntime();
    ensureFirstArgIsDocument(runtime, args[0]);
    XmlDocument xmlDoc = (XmlDocument) args[0];
    ensureDocumentHasNoError(context, xmlDoc);
    Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
    XsltStylesheet xslt = (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass) klazz);
    try {
        xslt.init(args[1], doc);
    } catch (TransformerConfigurationException ex) {
        throw runtime.newRuntimeError("could not parse xslt stylesheet");
    }
    return xslt;
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) Document(org.w3c.dom.Document) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 8 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project gocd by gocd.

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());
    DOMResult result = null;
    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)

Example 9 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project j2objc by google.

the class TransformerFactoryImpl method processFromNode.

public javax.xml.transform.Templates processFromNode(Node node) throws TransformerConfigurationException {
    try {
        TemplatesHandler builder = newTemplatesHandler();
        TreeWalker walker = new TreeWalker(builder, new org.apache.xml.utils.DOM2Helper(), builder.getSystemId());
        walker.traverse(node);
        return builder.getTemplates();
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            // se.printStackTrace();
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se);
        //"processFromNode failed", se);
        }
    } catch (TransformerConfigurationException tce) {
        // Assume it's already been reported to the error listener.
        throw tce;
    }/* catch (TransformerException tce)
    {
      // Assume it's already been reported to the error listener.
      throw new TransformerConfigurationException(tce.getMessage(), tce);
    }*/
     catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            //"processFromNode failed",
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e);
        //e);
        }
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TreeWalker(org.apache.xml.utils.TreeWalker) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Example 10 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project j2objc by google.

the class TransformerFactoryImpl method newTransformerHandler.

/**
   * Get a TransformerHandler object that can process SAX
   * ContentHandler events into a Result, based on the Templates argument.
   *
   * @param templates The source of the transformation instructions.
   *
   * @return TransformerHandler ready to transform SAX events.
   * @throws TransformerConfigurationException
   */
public TransformerHandler newTransformerHandler(Templates templates) throws TransformerConfigurationException {
    try {
        TransformerImpl transformer = (TransformerImpl) templates.newTransformer();
        transformer.setURIResolver(m_uriResolver);
        TransformerHandler th = (TransformerHandler) transformer.getInputContentHandler(true);
        return th;
    } catch (TransformerConfigurationException ex) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(ex);
                return null;
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        }
        throw ex;
    }
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)78 TransformerException (javax.xml.transform.TransformerException)52 Transformer (javax.xml.transform.Transformer)42 StreamResult (javax.xml.transform.stream.StreamResult)37 DOMSource (javax.xml.transform.dom.DOMSource)34 IOException (java.io.IOException)30 TransformerFactory (javax.xml.transform.TransformerFactory)25 StreamSource (javax.xml.transform.stream.StreamSource)20 SAXException (org.xml.sax.SAXException)17 StringWriter (java.io.StringWriter)15 Source (javax.xml.transform.Source)12 Document (org.w3c.dom.Document)11 InputStream (java.io.InputStream)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 TransformerHandler (javax.xml.transform.sax.TransformerHandler)9 Node (org.w3c.dom.Node)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 DocumentBuilder (javax.xml.parsers.DocumentBuilder)7 Result (javax.xml.transform.Result)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6