Search in sources :

Example 71 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project lucene-solr by apache.

the class TransformerProvider method getTemplates.

/** Return a Templates object for the given filename */
private Templates getTemplates(ResourceLoader loader, String filename, int cacheLifetimeSeconds) throws IOException {
    Templates result = null;
    lastFilename = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("compiling XSLT templates:" + filename);
        }
        final String fn = "xslt/" + filename;
        final TransformerFactory tFactory = TransformerFactory.newInstance();
        tFactory.setURIResolver(new SystemIdResolver(loader).asURIResolver());
        tFactory.setErrorListener(xmllog);
        final StreamSource src = new StreamSource(loader.openResource(fn), SystemIdResolver.createSystemIdFromResourceName(fn));
        try {
            result = tFactory.newTemplates(src);
        } finally {
            // some XML parsers are broken and don't close the byte stream (but they should according to spec)
            IOUtils.closeQuietly(src.getInputStream());
        }
    } catch (Exception e) {
        log.error(getClass().getName(), "newTemplates", e);
        throw new IOException("Unable to initialize Templates '" + filename + "'", e);
    }
    lastFilename = filename;
    lastTemplates = result;
    cacheExpiresTimeout = new TimeOut(cacheLifetimeSeconds, TimeUnit.SECONDS);
    return result;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) SystemIdResolver(org.apache.solr.util.SystemIdResolver) TimeOut(org.apache.solr.util.TimeOut) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Example 72 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project poi by apache.

the class OOXMLPrettyPrint method pretty.

private static void pretty(Document document, OutputStream outputStream, int indent) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    if (indent > 0) {
        // set properties to indent the resulting XML nicely
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
    }
    Result result = new StreamResult(outputStream);
    Source source = new DOMSource(document);
    transformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 73 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project jackrabbit by apache.

the class ClientSession method exportDocumentView.

/**
     * Exports the XML document view of the specified repository location
     * to the given XML content handler. This method first requests the
     * raw XML data from the remote session, and then uses an identity
     * transformation to feed the data to the given XML content handler.
     * Possible IO and transformer exceptions are thrown as SAXExceptions.
     *
     * {@inheritDoc}
     */
public void exportDocumentView(String path, ContentHandler handler, boolean binaryAsLink, boolean noRecurse) throws SAXException, RepositoryException {
    try {
        byte[] xml = remote.exportDocumentView(path, binaryAsLink, noRecurse);
        Source source = new StreamSource(new ByteArrayInputStream(xml));
        Result result = new SAXResult(handler);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
    } catch (RemoteException ex) {
        throw new RemoteRepositoryException(ex);
    } catch (IOException ex) {
        throw new SAXException(ex);
    } catch (TransformerConfigurationException ex) {
        throw new SAXException(ex);
    } catch (TransformerException ex) {
        throw new SAXException(ex);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteException(java.rmi.RemoteException) TransformerException(javax.xml.transform.TransformerException)

Example 74 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project jackrabbit by apache.

the class RepositoryConfig method internalCreateWorkspaceConfig.

/**
     * Creates a new workspace configuration with the specified name and the
     * specified workspace <code>template</.
     * <p>
     * This method creates a workspace configuration subdirectory,
     * copies the workspace configuration template into it, and finally
     * adds the created workspace configuration to the repository.
     * The initialized workspace configuration object is returned to
     * the caller.
     *
     * @param name workspace name
     * @param template the workspace template
     * @param configContent optional stringbuffer that will have the content
     *        of workspace configuration file written in
     * @return created workspace configuration
     * @throws ConfigurationException if creating the workspace configuration
     *                                failed
     */
private synchronized WorkspaceConfig internalCreateWorkspaceConfig(String name, Element template, StringBuffer configContent) throws ConfigurationException {
    // The physical workspace home directory on disk (TODO encode name?)
    File directory = new File(workspaceDirectory, name);
    // or cannot be created
    if (!directory.mkdir()) {
        if (directory.exists()) {
            throw new ConfigurationException("Workspace directory already exists: " + name);
        } else {
            throw new ConfigurationException("Failed to create workspace directory: " + name);
        }
    }
    FileSystem virtualFS;
    if (workspaceConfigDirectory != null) {
        // virtual repository file system
        try {
            virtualFS = fsf.getFileSystem();
        } catch (RepositoryException e) {
            throw new ConfigurationException("File system configuration error", e);
        }
    } else {
        // workspace configurations are maintained on disk
        virtualFS = null;
    }
    try {
        Writer configWriter;
        // get a writer for the workspace configuration file
        if (virtualFS != null) {
            // a configuration directoy had been specified; create workspace
            // configuration in virtual repository file system rather than
            // on disk
            String configDir = workspaceConfigDirectory + FileSystem.SEPARATOR + name;
            String configFile = configDir + FileSystem.SEPARATOR + WORKSPACE_XML;
            try {
                // Create the directory
                virtualFS.createFolder(configDir);
                configWriter = new OutputStreamWriter(virtualFS.getOutputStream(configFile));
            } catch (FileSystemException e) {
                throw new ConfigurationException("failed to create workspace configuration at path " + configFile, e);
            }
        } else {
            File file = new File(directory, WORKSPACE_XML);
            try {
                configWriter = new FileWriter(file);
            } catch (IOException e) {
                throw new ConfigurationException("failed to create workspace configuration at path " + file.getPath(), e);
            }
        }
        // the configuration writer.
        try {
            template.setAttribute("name", name);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            if (configContent == null) {
                transformer.transform(new DOMSource(template), new StreamResult(configWriter));
            } else {
                StringWriter writer = new StringWriter();
                transformer.transform(new DOMSource(template), new StreamResult(writer));
                String s = writer.getBuffer().toString();
                configWriter.write(s);
                configContent.append(s);
            }
        } catch (IOException e) {
            throw new ConfigurationException("Cannot create a workspace configuration file", e);
        } catch (TransformerConfigurationException e) {
            throw new ConfigurationException("Cannot create a workspace configuration writer", e);
        } catch (TransformerException e) {
            throw new ConfigurationException("Cannot create a workspace configuration file", e);
        } finally {
            IOUtils.closeQuietly(configWriter);
        }
        // Load the created workspace configuration.
        WorkspaceConfig wc;
        if (virtualFS != null) {
            String configDir = workspaceConfigDirectory + FileSystem.SEPARATOR + name;
            wc = loadWorkspaceConfig(virtualFS, configDir);
        } else {
            wc = loadWorkspaceConfig(directory);
        }
        if (wc != null) {
            addWorkspaceConfig(wc);
            return wc;
        } else {
            throw new ConfigurationException("Failed to load the created configuration for workspace " + name + ".");
        }
    } finally {
        try {
            if (virtualFS != null) {
                virtualFS.close();
            }
        } catch (FileSystemException ignore) {
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileWriter(java.io.FileWriter) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) StringWriter(java.io.StringWriter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileSystem(org.apache.jackrabbit.core.fs.FileSystem) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 75 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project jackrabbit by apache.

the class ExportDocViewTest method readDocument.

/**
     * Reads a DOM document from the given XML stream.
     *
     * @param xml XML stream
     * @return DOM document
     * @throws RepositoryException if the document could not be read
     */
private Document readDocument(InputStream xml) throws RepositoryException {
    try {
        StreamSource source = new StreamSource(xml);
        DOMResult result = new DOMResult();
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return (Document) result.getNode();
    } catch (TransformerException e) {
        throw new RepositoryException("Unable to read xml file", e);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamSource(javax.xml.transform.stream.StreamSource) RepositoryException(javax.jcr.RepositoryException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)257 Transformer (javax.xml.transform.Transformer)221 StreamResult (javax.xml.transform.stream.StreamResult)198 DOMSource (javax.xml.transform.dom.DOMSource)157 TransformerException (javax.xml.transform.TransformerException)86 StringWriter (java.io.StringWriter)77 StreamSource (javax.xml.transform.stream.StreamSource)77 Document (org.w3c.dom.Document)67 Source (javax.xml.transform.Source)56 IOException (java.io.IOException)55 File (java.io.File)47 DocumentBuilder (javax.xml.parsers.DocumentBuilder)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 Element (org.w3c.dom.Element)36 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35 Result (javax.xml.transform.Result)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 StringReader (java.io.StringReader)28