Search in sources :

Example 21 with TransformerConfigurationException

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

the class DOMBuilder method write.

/**
     * Writes the document built by this builder into the given output stream.
     * This method is normally invoked only when the document is fully built.
     *
     * @param xml XML output stream
     * @throws IOException if the document could not be written
     */
public void write(OutputStream xml) throws IOException {
    try {
        Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult(xml));
    } catch (TransformerConfigurationException e) {
        IOException ioe = new IOException(e.getMessage());
        ioe.initCause(e);
        throw ioe;
    } catch (TransformerException e) {
        IOException ioe = new IOException(e.getMessage());
        ioe.initCause(e);
        throw ioe;
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 22 with TransformerConfigurationException

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

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

the class SerializingContentHandler method getSerializer.

/**
     * Creates a serializing content handler that writes to the given result.
     *
     * @param result serialization target
     * @return serializing content handler
     * @throws SAXException if the content handler could not be initialized
     */
public static DefaultHandler getSerializer(Result result) throws SAXException {
    try {
        TransformerHandler handler = FACTORY.newTransformerHandler();
        handler.setResult(result);
        // Specify the output properties to avoid surprises especially in
        // character encoding or the output method (might be html for some
        // documents!)
        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        if (NEEDS_XMLNS_ATTRIBUTES) {
            // so we need to do it explicitly with this wrapper
            return new SerializingContentHandler(handler);
        } else {
            return new DefaultContentHandler(handler);
        }
    } catch (TransformerConfigurationException e) {
        throw new SAXException("Failed to initialize XML serializer", e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException)

Example 24 with TransformerConfigurationException

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

use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.

the class DOMUtils method printXML.

/**
     * This prints the given DOM document to System.out with indentation and
     * utf-8 encoding.
     *
     * @param doc
     *            a DOM <code>Document</code>
     */
public static void printXML(Document doc) {
    try {
        // prepare the DOM document for writing
        Source source = new DOMSource(doc);
        // prepare the output
        Result result = new StreamResult(System.out);
        // write the DOM document to the file
        // get Transformer
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");
        // write to System.out
        xformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        // error generated during transformer configuration
        System.err.println(tce.getMessage());
        // use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null) {
            x = tce.getException();
        }
        x.printStackTrace();
    } catch (TransformerException te) {
        // error generated by the transformer
        System.err.println(te.getMessage());
        // use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null) {
            x = te.getException();
        }
        x.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

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