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;
}
}
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);
}
}
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);
}
}
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) {
}
}
}
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();
}
}
Aggregations