Example usage for javax.xml.transform.sax SAXSource setSystemId

List of usage examples for javax.xml.transform.sax SAXSource setSystemId

Introduction

In this page you can find the example usage for javax.xml.transform.sax SAXSource setSystemId.

Prototype

@Override
public void setSystemId(String systemId) 

Source Link

Document

Set the system identifier for this Source.

Usage

From source file:org.callimachusproject.xml.XdmNodeFactory.java

private XdmNode parse(InputSource isource) throws SAXException {
    // Make sure the builder uses our entity resolver
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setEntityResolver(xmlResolver);
    SAXSource source = new SAXSource(reader, isource);
    if (isource.getSystemId() != null) {
        source.setSystemId(isource.getSystemId());
    }//  w  w w . j a  v a2  s .c  o  m
    return parse(source);
}

From source file:net.sourceforge.vulcan.spring.SpringProjectDomBuilder.java

@Override
protected Transformer createTransformer(String format) throws NoSuchTransformFormatException {
    if (!transformResources.containsKey(format)) {
        throw new NoSuchTransformFormatException();
    }//from www.j av  a2s  .c  o  m

    final Resource resource = applicationContext.getResource(transformResources.get(format));

    try {
        final SAXSource source = new SAXSource(XMLReaderFactory.createXMLReader(),
                new InputSource(resource.getInputStream()));

        try {
            // Try to tell the xsl where it is for the sake of xsl:include
            source.setSystemId(resource.getFile().getAbsolutePath());
        } catch (FileNotFoundException ignore) {
            // Not all resources are backed by files.
        }

        return transformerFactory.newTransformer(source);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new RuntimeException(e);
    }

}

From source file:com.google.code.docbook4j.renderer.BaseRenderer.java

public InputStream render() throws Docbook4JException {

    assertNotNull(xmlResource, "Value of the xml source should be not null!");

    FileObject xsltResult = null;
    FileObject xmlSourceFileObject = null;
    FileObject xslSourceFileObject = null;
    FileObject userConfigXmlSourceFileObject = null;

    try {//w  w  w  .  j  av a 2  s  .c  om

        xmlSourceFileObject = FileObjectUtils.resolveFile(xmlResource);
        if (xslResource != null) {
            xslSourceFileObject = FileObjectUtils.resolveFile(xslResource);
        } else {
            xslSourceFileObject = getDefaultXslStylesheet();
        }

        if (userConfigXmlResource != null) {
            userConfigXmlSourceFileObject = FileObjectUtils.resolveFile(userConfigXmlResource);
        }

        SAXParserFactory factory = createParserFactory();
        final XMLReader reader = factory.newSAXParser().getXMLReader();

        EntityResolver resolver = new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {

                log.debug("Resolving file {}", systemId);

                FileObject inc = FileObjectUtils.resolveFile(systemId);
                return new InputSource(inc.getContent().getInputStream());
            }
        };

        // prepare xml sax source
        ExpressionEvaluatingXMLReader piReader = new ExpressionEvaluatingXMLReader(reader, vars);
        piReader.setEntityResolver(resolver);

        SAXSource source = new SAXSource(piReader,
                new InputSource(xmlSourceFileObject.getContent().getInputStream()));
        source.setSystemId(xmlSourceFileObject.getURL().toExternalForm());

        // prepare xslt result
        xsltResult = FileObjectUtils.resolveFile("tmp://" + UUID.randomUUID().toString());
        xsltResult.createFile();

        // create transofrmer and do transformation
        final Transformer transformer = createTransformer(xmlSourceFileObject, xslSourceFileObject);
        transformer.transform(source, new StreamResult(xsltResult.getContent().getOutputStream()));

        // do post processing
        FileObject target = postProcess(xmlSourceFileObject, xslSourceFileObject, xsltResult,
                userConfigXmlSourceFileObject);

        FileObjectUtils.closeFileObjectQuietly(xsltResult);
        FileObjectUtils.closeFileObjectQuietly(target);
        return target.getContent().getInputStream();

    } catch (FileSystemException e) {
        throw new Docbook4JException("Error transofrming xml!", e);
    } catch (SAXException e) {
        throw new Docbook4JException("Error transofrming xml!", e);
    } catch (ParserConfigurationException e) {
        throw new Docbook4JException("Error transofrming xml!", e);
    } catch (TransformerException e) {
        throw new Docbook4JException("Error transofrming xml!", e);
    } catch (IOException e) {
        throw new Docbook4JException("Error transofrming xml !", e);
    } finally {
        FileObjectUtils.closeFileObjectQuietly(xmlSourceFileObject);
        FileObjectUtils.closeFileObjectQuietly(xslSourceFileObject);
    }

}

From source file:org.ojbc.util.camel.processor.accesscontrol.XacmlRequestTransformer.java

private SAXSource getSaxSourceForXsl(String xslFileStringContent, String xslUrlExtFormForSystemId) {

    try {//  w  ww .j  a v  a  2 s.  c  o m

        InputSource inputSource = new InputSource(xslFileStringContent);
        inputSource.setEncoding(CharEncoding.UTF_8);

        SAXSource saxInputSource = new SAXSource(inputSource);

        //need to setSystemId because xsl needs this set in order for to <import> to know where to look to load relative paths
        saxInputSource.setSystemId(xslUrlExtFormForSystemId);

        return saxInputSource;

    } catch (Exception e) {
        throw new RuntimeException("Unable to read XSL: " + xslFileStringContent, e);
    }
}

From source file:org.ojbc.web.portal.services.SearchResultConverter.java

private SAXSource createSourceAndSetSystemId(org.springframework.core.io.Resource inputStream) {
    try {//from   w  ww  . ja  v a 2 s .  co m
        SAXSource inputSource;
        inputSource = createSource(inputStream.getInputStream());
        //need to setSystemId because xsl needs this set in order for to <import> to know where to look to load relative paths
        inputSource.setSystemId(inputStream.getURL().toExternalForm());
        return inputSource;
    } catch (Exception e) {
        throw new RuntimeException("Unable to read XML/XSL", e);
    }
}

From source file:org.orbeon.oxf.processor.EmailProcessor.java

public static SAXSource getSAXSource(Processor processor, PipelineContext pipelineContext, String href,
        String base, String contentType) {
    try {//  w w w .ja v  a 2s .  c  om
        // There are two cases:
        // 1. We read the source as SAX
        //    o This is required when reading from a processor input; in this case, we behave like
        //      the inline case
        //    o When reading from another type of URI, the resource could be in theory any type
        //      of file.
        // 2. We don't read the source as SAX
        //    o It is particularly useful to support this when resources are to be used as binary
        //      attachments such as images.
        //    o Here, we consider that the source can be XML, text/html, text/*,
        //      or binary. We do not handle reading Base64-encoded files. We leverage the URL
        //      generator to obtain the content in XML format.
        XMLReader xmlReader;
        {
            String inputName = ProcessorImpl.getProcessorInputSchemeInputName(href);
            if (inputName != null) {
                // Resolve to input of current processor
                xmlReader = new ProcessorOutputXMLReader(pipelineContext,
                        processor.getInputByName(inputName).getOutput());
            } else {
                // Resolve to regular URI
                Processor urlGenerator = (contentType == null)
                        ? new URLGenerator(URLFactory.createURL(base, href))
                        : new URLGenerator(URLFactory.createURL(base, href), contentType, true);
                xmlReader = new ProcessorOutputXMLReader(pipelineContext,
                        urlGenerator.createOutput(ProcessorImpl.OUTPUT_DATA));
            }
        }

        // Return SAX Source based on XML Reader
        SAXSource saxSource = new SAXSource(xmlReader, new InputSource());
        saxSource.setSystemId(href);
        return saxSource;
    } catch (IOException e) {
        throw new OXFException(e);
    }
}