Search in sources :

Example 91 with TransformerException

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

the class GRDDLTransformer method transform.

public Graph transform(final Document inputDoc, TempGraphFactory graphFactory) throws GrddlTransformException {
    final DocumentResult result = new DocumentResult();
    try {
        xsltTransformerRegistry.transformWithCorrectClassLoader(key, new XSLTTransformerExecutor<Void>() {

            @Override
            public Void execute(Transformer transformer) throws TransformerException {
                DocumentSource source = new DocumentSource(inputDoc);
                transformer.transform(source, result);
                return null;
            }
        });
        // TODO: likely need to optimize with some sort of streaming document reader here
        Graph graph = graphFactory.createTempGraph();
        graph.addTriplesFromRDFXMLAbbrev(new StringReader(result.getDocument().asXML()));
        return graph;
    } catch (ShineRuntimeException e) {
        LOGGER.error("Could not convert to a graph. The document was: \n{}", result.getDocument().asXML(), e);
        throw e;
    } catch (TransformerException e) {
        LOGGER.warn("Could not perform grddl transform. The document was: \n{}", result.getDocument().asXML(), e);
        throw new GrddlTransformException(e);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Graph(com.thoughtworks.studios.shine.semweb.Graph) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) DocumentResult(org.dom4j.io.DocumentResult) DocumentSource(org.dom4j.io.DocumentSource) StringReader(java.io.StringReader) TransformerException(javax.xml.transform.TransformerException)

Example 92 with TransformerException

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

the class NUnitRDFizer method importFile.

public Graph importFile(final String parentURI, Document document) throws GrddlTransformException {
    final DocumentResult result = new DocumentResult();
    final DocumentSource source = new DocumentSource(document);
    try {
        return xsltTransformerRegistry.transformWithCorrectClassLoader(XSLTTransformerRegistry.XUNIT_NUNIT_TO_JUNIT_XSL, new XSLTTransformerExecutor<Graph>() {

            @Override
            public Graph execute(Transformer transformer) throws TransformerException, GrddlTransformException {
                transformer.transform(source, result);
                return jUnitRDFizer.importFile(parentURI, result.getDocument());
            }
        });
    } catch (TransformerException e) {
        throw new ShineRuntimeException(e);
    }
}
Also used : Graph(com.thoughtworks.studios.shine.semweb.Graph) Transformer(javax.xml.transform.Transformer) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) DocumentResult(org.dom4j.io.DocumentResult) DocumentSource(org.dom4j.io.DocumentSource) GrddlTransformException(com.thoughtworks.studios.shine.semweb.grddl.GrddlTransformException) TransformerException(javax.xml.transform.TransformerException)

Example 93 with TransformerException

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

the class AbstractDom4jExporter method marshal.

/* (non-Javadoc)
     * @see org.springframework.oxm.Marshaller#marshal(java.lang.Object, javax.xml.transform.Result)
     */
@Override
public void marshal(Object graph, Result result) throws IOException, XmlMappingException {
    @SuppressWarnings("unchecked") final Tuple<String, Element> data = (Tuple<String, Element>) graph;
    final Transformer transformer;
    try {
        transformer = this.xmlUtilities.getIdentityTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to load identity Transformer", e);
    }
    // Setup the transformer to pretty-print the output
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(new DocumentSource(data.second), result);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to write Element to Result for: " + data.first, e);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DocumentSource(org.dom4j.io.DocumentSource) Element(org.dom4j.Element) Tuple(org.apereo.portal.utils.Tuple) TransformerException(javax.xml.transform.TransformerException)

Example 94 with TransformerException

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

the class XSLTComponent method getEventReader.

/* (non-Javadoc)
     * @see org.apereo.portal.rendering.StAXPipelineComponent#getXmlStreamReader(java.lang.Object, java.lang.Object)
     */
@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
    final PipelineEventReader<XMLEventReader, XMLEvent> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
    final Transformer transformer = this.transformerSource.getTransformer(request, response);
    // Setup a URIResolver based on the current resource loader
    transformer.setURIResolver(this.uriResolver);
    // Configure the Transformer via injected class
    if (this.xsltParameterSource != null) {
        final Map<String, Object> transformerParameters = this.xsltParameterSource.getParameters(request, response);
        if (transformerParameters != null) {
            this.logger.debug("{} - Setting Transformer Parameters: ", this.beanName, transformerParameters);
            for (final Map.Entry<String, Object> transformerParametersEntry : transformerParameters.entrySet()) {
                final String name = transformerParametersEntry.getKey();
                final Object value = transformerParametersEntry.getValue();
                if (value != null) {
                    transformer.setParameter(name, value);
                }
            }
        }
        final Properties outputProperties = this.xsltParameterSource.getOutputProperties(request, response);
        if (outputProperties != null) {
            this.logger.debug("{} - Setting Transformer Output Properties: ", this.beanName, outputProperties);
            transformer.setOutputProperties(outputProperties);
        }
    }
    // The event reader from the previous component in the pipeline
    final XMLEventReader eventReader = pipelineEventReader.getEventReader();
    // Wrap the event reader in a stream reader to avoid a JDK bug
    final XMLStreamReader streamReader;
    try {
        streamReader = new FixedXMLEventStreamReader(eventReader);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLStreamReader from XMLEventReader", e);
    }
    final Source xmlReaderSource = new StAXSource(streamReader);
    // Setup logging for the transform
    transformer.setErrorListener(this.errorListener);
    // Transform to a SAX ContentHandler to avoid JDK bug:
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6775588
    final XMLEventBufferWriter eventWriterBuffer = new XMLEventBufferWriter();
    final ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriterBuffer);
    contentHandler.setDocumentLocator(new LocatorImpl());
    final SAXResult outputTarget = new SAXResult(contentHandler);
    try {
        this.logger.debug("{} - Begining XML Transformation", this.beanName);
        transformer.transform(xmlReaderSource, outputTarget);
        this.logger.debug("{} - XML Transformation complete", this.beanName);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to transform document", e);
    }
    final String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
    final List<XMLEvent> eventBuffer = eventWriterBuffer.getEventBuffer();
    final XMLEventReader outputEventReader = new XMLEventBufferReader(eventBuffer.listIterator());
    final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
    final PipelineEventReaderImpl<XMLEventReader, XMLEvent> pipelineEventReaderImpl = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(outputEventReader, outputProperties);
    pipelineEventReaderImpl.setOutputProperty(OutputKeys.MEDIA_TYPE, mediaType);
    return pipelineEventReaderImpl;
}
Also used : XMLEventBufferWriter(org.apereo.portal.xml.stream.XMLEventBufferWriter) Transformer(javax.xml.transform.Transformer) XMLStreamReader(javax.xml.stream.XMLStreamReader) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) Properties(java.util.Properties) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) ContentHandler(org.xml.sax.ContentHandler) XMLEventReader(javax.xml.stream.XMLEventReader) LocatorImpl(org.xml.sax.helpers.LocatorImpl) TransformerException(javax.xml.transform.TransformerException) StAXSource(javax.xml.transform.stax.StAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) SAXResult(javax.xml.transform.sax.SAXResult) XMLEventBufferReader(org.apereo.portal.xml.stream.XMLEventBufferReader) PipelineEventReaderImpl(org.apereo.portal.rendering.PipelineEventReaderImpl) XMLEvent(javax.xml.stream.events.XMLEvent) Map(java.util.Map)

Example 95 with TransformerException

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

the class ResourceLoaderURIResolver method resolveResource.

/**
 * Resolve the requested {@link Resource}
 */
protected Resource resolveResource(String href, String base) throws TransformerException {
    final Resource resolvedResource;
    if (base != null) {
        final Resource baseResource = this.resourceLoader.getResource(base);
        try {
            resolvedResource = baseResource.createRelative(href);
        } catch (IOException e) {
            throw new TransformerException("Failed to find '" + href + "' relative to: " + baseResource, e);
        }
        this.logger.debug("Created resource {} for href: {} and base: {}", new Object[] { resolvedResource, href, baseResource });
    } else {
        resolvedResource = this.resourceLoader.getResource(href);
        this.logger.debug("Created resource {} for href: {}", resolvedResource, href);
    }
    return resolvedResource;
}
Also used : Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerException (javax.xml.transform.TransformerException)786 Transformer (javax.xml.transform.Transformer)351 StreamResult (javax.xml.transform.stream.StreamResult)345 DOMSource (javax.xml.transform.dom.DOMSource)298 IOException (java.io.IOException)266 TransformerFactory (javax.xml.transform.TransformerFactory)175 Document (org.w3c.dom.Document)155 StringWriter (java.io.StringWriter)152 SAXException (org.xml.sax.SAXException)152 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)150 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)127 Source (javax.xml.transform.Source)96 StreamSource (javax.xml.transform.stream.StreamSource)90 Element (org.w3c.dom.Element)87 DocumentBuilder (javax.xml.parsers.DocumentBuilder)77 File (java.io.File)70 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)63 Node (org.w3c.dom.Node)62 ByteArrayOutputStream (java.io.ByteArrayOutputStream)58 ErrorListener (javax.xml.transform.ErrorListener)55