Search in sources :

Example 51 with TransformerConfigurationException

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

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 52 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException 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)

Example 53 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.

the class XSLTComponentTest method getTransformer.

protected Transformer getTransformer(String file) {
    final InputStream stylesheetStream = this.getClass().getResourceAsStream(file);
    ;
    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    try {
        return transformerFactory.newTransformer(new StreamSource(stylesheetStream));
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to create Transformer for stylesheet: " + file, e);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource)

Example 54 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.

the class XmlUtilitiesImpl method toString.

public static String toString(Node node) {
    final Transformer identityTransformer;
    try {
        identityTransformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to create identity transformer to serialize Node to String", e);
    }
    identityTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    final StringWriter outputWriter = new StringWriter();
    final StreamResult outputTarget = new StreamResult(outputWriter);
    final DOMSource xmlSource = new DOMSource(node);
    try {
        identityTransformer.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to convert Node to String using Transformer", e);
    }
    return outputWriter.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 55 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.

the class TemplatesBuilder method loadResource.

/* (non-Javadoc)
     * @see org.apereo.portal.utils.cache.resource.ResourceBuilder#buildResource(org.springframework.core.io.Resource, java.io.InputStream)
     */
@Override
public LoadedResource<Templates> loadResource(Resource resource) throws IOException {
    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    if (this.transformerAttributes != null) {
        for (final Map.Entry<String, Object> attributeEntry : this.transformerAttributes.entrySet()) {
            transformerFactory.setAttribute(attributeEntry.getKey(), attributeEntry.getValue());
        }
    }
    final ResourceTrackingURIResolver uriResolver = new ResourceTrackingURIResolver(this.resourceLoader);
    transformerFactory.setURIResolver(uriResolver);
    final URI uri = resource.getURI();
    final String systemId = uri.toString();
    final InputStream stream = resource.getInputStream();
    final Templates templates;
    try {
        final StreamSource source = new StreamSource(stream, systemId);
        templates = transformerFactory.newTemplates(source);
    } catch (TransformerConfigurationException e) {
        throw new IOException("Failed to parse stream into Templates", e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
    final Map<Resource, Long> resolvedResources = uriResolver.getResolvedResources();
    return new LoadedResourceImpl<Templates>(templates, resolvedResources);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Resource(org.springframework.core.io.Resource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)93 TransformerException (javax.xml.transform.TransformerException)62 Transformer (javax.xml.transform.Transformer)52 StreamResult (javax.xml.transform.stream.StreamResult)49 DOMSource (javax.xml.transform.dom.DOMSource)42 IOException (java.io.IOException)35 TransformerFactory (javax.xml.transform.TransformerFactory)33 StreamSource (javax.xml.transform.stream.StreamSource)23 SAXException (org.xml.sax.SAXException)21 StringWriter (java.io.StringWriter)17 Source (javax.xml.transform.Source)16 TransformerHandler (javax.xml.transform.sax.TransformerHandler)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Document (org.w3c.dom.Document)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 InputStream (java.io.InputStream)10 Node (org.w3c.dom.Node)10 File (java.io.File)9 Result (javax.xml.transform.Result)9