Search in sources :

Example 1 with NokogiriXsltErrorListener

use of nokogiri.internals.NokogiriXsltErrorListener 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 2 with NokogiriXsltErrorListener

use of nokogiri.internals.NokogiriXsltErrorListener in project gocd by gocd.

the class XsltStylesheet method init.

private void init(IRubyObject stylesheet, Document document) throws TransformerConfigurationException {
    // either RubyString or RubyFile
    this.stylesheet = stylesheet;
    if (factory == null)
        factory = TransformerFactory.newInstance();
    NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
    factory.setErrorListener(elistener);
    sheet = factory.newTemplates(new DOMSource(document));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) NokogiriXsltErrorListener(nokogiri.internals.NokogiriXsltErrorListener)

Example 3 with NokogiriXsltErrorListener

use of nokogiri.internals.NokogiriXsltErrorListener in project nokogiri by sparklemotion.

the class XsltStylesheet method init.

private void init(IRubyObject stylesheet, Document document) throws TransformerConfigurationException {
    // either RubyString or RubyFile
    this.stylesheet = stylesheet;
    if (factory == null) {
        factory = TransformerFactory.newInstance();
    }
    NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
    factory.setErrorListener(elistener);
    sheet = factory.newTemplates(new DOMSource(document));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) NokogiriXsltErrorListener(nokogiri.internals.NokogiriXsltErrorListener)

Example 4 with NokogiriXsltErrorListener

use of nokogiri.internals.NokogiriXsltErrorListener 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

DOMSource (javax.xml.transform.dom.DOMSource)4 NokogiriXsltErrorListener (nokogiri.internals.NokogiriXsltErrorListener)4 IOException (java.io.IOException)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 DOMResult (javax.xml.transform.dom.DOMResult)2 Ruby (org.jruby.Ruby)2 RubyString (org.jruby.RubyString)2 JRubyMethod (org.jruby.anno.JRubyMethod)2