use of javax.xml.transform.sax.TransformerHandler in project jackrabbit by apache.
the class SerializingContentHandler method needsXmlnsAttributes.
/**
* Probes the available XML serializer for xmlns support. Used to set
* the value of the {@link #NEEDS_XMLNS_ATTRIBUTES} flag.
*
* @return whether the XML serializer needs explicit xmlns attributes
*/
private static boolean needsXmlnsAttributes() {
try {
StringWriter writer = new StringWriter();
TransformerHandler probe = FACTORY.newTransformerHandler();
probe.setResult(new StreamResult(writer));
probe.startDocument();
probe.startPrefixMapping("p", "uri");
probe.startElement("uri", "e", "p:e", new AttributesImpl());
probe.endElement("uri", "e", "p:e");
probe.endPrefixMapping("p");
probe.endDocument();
return writer.toString().indexOf("xmlns") == -1;
} catch (Exception e) {
throw new UnsupportedOperationException("XML serialization fails");
}
}
use of javax.xml.transform.sax.TransformerHandler 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.sax.TransformerHandler in project jackrabbit by apache.
the class ResultHelper method needsXmlnsAttributes.
/**
* Probes the available XML serializer for xmlns support. Used to set
* the value of the {@link #NEEDS_XMLNS_ATTRIBUTES} flag.
*
* @return whether the XML serializer needs explicit xmlns attributes
*/
private static boolean needsXmlnsAttributes() {
try {
StringWriter writer = new StringWriter();
TransformerHandler probe = FACTORY.newTransformerHandler();
probe.setResult(new StreamResult(writer));
probe.startDocument();
probe.startPrefixMapping("p", "uri");
probe.startElement("uri", "e", "p:e", new AttributesImpl());
probe.endElement("uri", "e", "p:e");
probe.endPrefixMapping("p");
probe.endDocument();
return writer.toString().indexOf("xmlns") == -1;
} catch (Exception e) {
throw new UnsupportedOperationException("XML serialization fails");
}
}
use of javax.xml.transform.sax.TransformerHandler in project jackrabbit by apache.
the class ResultHelper method getResult.
/**
* In case the underlying XML library doesn't properly handle xmlns attributes
* this method creates new content handler dealing with the misbehavior and
* returns an new instance of SAXResult. Otherwise the passed result
* is returned back.
*
* @param result
* @return A instance of <code>Result</code> that properly handles xmlns attributes.
* @throws SAXException
*/
public static Result getResult(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, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
if (NEEDS_XMLNS_ATTRIBUTES) {
// so we need to do it explicitly with this wrapper
return new SAXResult(new SerializingContentHandler(handler));
} else {
return result;
}
} catch (TransformerConfigurationException e) {
throw new SAXException("Failed to initialize XML serializer", e);
}
}
use of javax.xml.transform.sax.TransformerHandler in project jackrabbit-oak by apache.
the class HtmlRepresentation method startResponse.
private XHTMLContentHandler startResponse(HttpServletResponse response, String title) throws IOException {
try {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
Transformer transformer = handler.getTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
handler.setResult(new StreamResult(response.getOutputStream()));
Metadata metadata = new Metadata();
metadata.set(Metadata.TITLE, title);
return new XHTMLContentHandler(handler, metadata);
} catch (TransformerConfigurationException e) {
throw new IOException(e);
}
}
Aggregations