use of javax.xml.transform.sax.SAXResult in project webservices-axiom by apache.
the class TestGetSAXResultWithDTD method runTest.
@Override
protected void runTest() throws Throwable {
OMElement root = metaFactory.getOMFactory().createOMElement("root", null);
SAXResult result = root.getSAXResult();
LexicalHandler lexicalHandler = result.getLexicalHandler();
ContentHandler contentHandler = result.getHandler();
contentHandler.startDocument();
lexicalHandler.startDTD("test", null, "my.dtd");
lexicalHandler.endDTD();
contentHandler.startElement("", "test", "test", new AttributesImpl());
contentHandler.endElement("", "test", "test");
contentHandler.endDocument();
OMNode child = root.getFirstOMChild();
assertTrue(child instanceof OMElement);
assertEquals("test", ((OMElement) child).getLocalName());
}
use of javax.xml.transform.sax.SAXResult in project asciidoctor-fopub by asciidoctor.
the class InputHandler method renderTo.
/**
* Generate a document, given an initialized Fop object
* @param userAgent the user agent
* @param outputFormat the output format to generate (MIME type, see MimeConstants)
* @param out the output stream to write the generated output to (may be null if not applicable)
* @throws FOPException in case of an error during processing
*/
public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) throws FOPException {
String baseURL = null;
try {
baseURL = new File(sourcefile.getAbsolutePath()).getParentFile().toURI().toURL().toExternalForm();
} catch (Exception e) {
baseURL = "";
}
FopFactory factory = new FopFactoryBuilder(URI.create(baseURL)).build();
Fop fop;
if (out != null) {
fop = factory.newFop(outputFormat, userAgent, out);
} else {
fop = factory.newFop(outputFormat, userAgent);
}
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
transformTo(res);
}
use of javax.xml.transform.sax.SAXResult in project j2objc by google.
the class TransformerIdentityImpl method createResultContentHandler.
/**
* Create a result ContentHandler from a Result object, based
* on the current OutputProperties.
*
* @param outputTarget Where the transform result should go,
* should not be null.
*
* @return A valid ContentHandler that will create the
* result tree when it is fed SAX events.
*
* @throws TransformerException
*/
private void createResultContentHandler(Result outputTarget) throws TransformerException {
if (outputTarget instanceof SAXResult) {
SAXResult saxResult = (SAXResult) outputTarget;
m_resultContentHandler = saxResult.getHandler();
m_resultLexicalHandler = saxResult.getLexicalHandler();
if (m_resultContentHandler instanceof Serializer) {
// Dubious but needed, I think.
m_serializer = (Serializer) m_resultContentHandler;
}
} else if (outputTarget instanceof DOMResult) {
DOMResult domResult = (DOMResult) outputTarget;
Node outputNode = domResult.getNode();
Node nextSibling = domResult.getNextSibling();
Document doc;
short type;
if (null != outputNode) {
type = outputNode.getNodeType();
doc = (Node.DOCUMENT_NODE == type) ? (Document) outputNode : outputNode.getOwnerDocument();
} else {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
if (m_isSecureProcessing) {
try {
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (ParserConfigurationException pce) {
}
}
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument();
} catch (ParserConfigurationException pce) {
throw new TransformerException(pce);
}
outputNode = doc;
type = outputNode.getNodeType();
((DOMResult) outputTarget).setNode(outputNode);
}
DOMBuilder domBuilder = (Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
if (nextSibling != null)
domBuilder.setNextSibling(nextSibling);
m_resultContentHandler = domBuilder;
m_resultLexicalHandler = domBuilder;
} else if (outputTarget instanceof StreamResult) {
StreamResult sresult = (StreamResult) outputTarget;
try {
Serializer serializer = SerializerFactory.getSerializer(m_outputFormat.getProperties());
m_serializer = serializer;
if (null != sresult.getWriter())
serializer.setWriter(sresult.getWriter());
else if (null != sresult.getOutputStream())
serializer.setOutputStream(sresult.getOutputStream());
else if (null != sresult.getSystemId()) {
String fileURL = sresult.getSystemId();
if (fileURL.startsWith("file:///")) {
if (fileURL.substring(8).indexOf(":") > 0) {
fileURL = fileURL.substring(8);
} else {
fileURL = fileURL.substring(7);
}
} else if (fileURL.startsWith("file:/")) {
if (fileURL.substring(6).indexOf(":") > 0) {
fileURL = fileURL.substring(6);
} else {
fileURL = fileURL.substring(5);
}
}
m_outputStream = new java.io.FileOutputStream(fileURL);
serializer.setOutputStream(m_outputStream);
} else
//"No output specified!");
throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
m_resultContentHandler = serializer.asContentHandler();
} catch (IOException ioe) {
throw new TransformerException(ioe);
}
} else {
//"Can't transform to a Result of type "
throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
// + outputTarget.getClass().getName()
// + "!");
}
if (m_resultContentHandler instanceof DTDHandler)
m_resultDTDHandler = (DTDHandler) m_resultContentHandler;
if (m_resultContentHandler instanceof DeclHandler)
m_resultDeclHandler = (DeclHandler) m_resultContentHandler;
if (m_resultContentHandler instanceof LexicalHandler)
m_resultLexicalHandler = (LexicalHandler) m_resultContentHandler;
}
use of javax.xml.transform.sax.SAXResult in project j2objc by google.
the class TransformerImpl method createSerializationHandler.
/**
* Create a ContentHandler from a Result object and an OutputProperties.
*
* @param outputTarget Where the transform result should go,
* should not be null.
* @param format The OutputProperties object that will contain
* instructions on how to serialize the output.
*
* @return A valid ContentHandler that will create the
* result tree when it is fed SAX events.
*
* @throws TransformerException
*/
public SerializationHandler createSerializationHandler(Result outputTarget, OutputProperties format) throws TransformerException {
SerializationHandler xoh;
// If the Result object contains a Node, then create
// a ContentHandler that will add nodes to the input node.
org.w3c.dom.Node outputNode = null;
if (outputTarget instanceof DOMResult) {
outputNode = ((DOMResult) outputTarget).getNode();
org.w3c.dom.Node nextSibling = ((DOMResult) outputTarget).getNextSibling();
org.w3c.dom.Document doc;
short type;
if (null != outputNode) {
type = outputNode.getNodeType();
doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument();
} else {
boolean isSecureProcessing = m_stylesheetRoot.isSecureProcessing();
doc = org.apache.xml.utils.DOMHelper.createDocument(isSecureProcessing);
outputNode = doc;
type = outputNode.getNodeType();
((DOMResult) outputTarget).setNode(outputNode);
}
DOMBuilder handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
if (nextSibling != null)
handler.setNextSibling(nextSibling);
String encoding = format.getProperty(OutputKeys.ENCODING);
xoh = new ToXMLSAXHandler(handler, (LexicalHandler) handler, encoding);
} else if (outputTarget instanceof SAXResult) {
ContentHandler handler = ((SAXResult) outputTarget).getHandler();
if (null == handler)
throw new IllegalArgumentException("handler can not be null for a SAXResult");
LexicalHandler lexHandler;
if (handler instanceof LexicalHandler)
lexHandler = (LexicalHandler) handler;
else
lexHandler = null;
String encoding = format.getProperty(OutputKeys.ENCODING);
String method = format.getProperty(OutputKeys.METHOD);
ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding);
toXMLSAXHandler.setShouldOutputNSAttr(false);
xoh = toXMLSAXHandler;
String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM);
if (systemID != null)
xoh.setDoctypeSystem(systemID);
if (publicID != null)
xoh.setDoctypePublic(publicID);
if (handler instanceof TransformerClient) {
XalanTransformState state = new XalanTransformState();
((TransformerClient) handler).setTransformState(state);
((ToSAXHandler) xoh).setTransformState(state);
}
} else // result tree to either a stream or a writer.
if (outputTarget instanceof StreamResult) {
StreamResult sresult = (StreamResult) outputTarget;
try {
SerializationHandler serializer = (SerializationHandler) SerializerFactory.getSerializer(format.getProperties());
if (null != sresult.getWriter())
serializer.setWriter(sresult.getWriter());
else if (null != sresult.getOutputStream())
serializer.setOutputStream(sresult.getOutputStream());
else if (null != sresult.getSystemId()) {
String fileURL = sresult.getSystemId();
if (fileURL.startsWith("file:///")) {
if (fileURL.substring(8).indexOf(":") > 0)
fileURL = fileURL.substring(8);
else
fileURL = fileURL.substring(7);
} else if (fileURL.startsWith("file:/")) {
if (fileURL.substring(6).indexOf(":") > 0)
fileURL = fileURL.substring(6);
else
fileURL = fileURL.substring(5);
}
m_outputStream = new java.io.FileOutputStream(fileURL);
serializer.setOutputStream(m_outputStream);
xoh = serializer;
} else
//"No output specified!");
throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
// handler = serializer.asContentHandler();
// this.setSerializer(serializer);
xoh = serializer;
}// }
catch (IOException ioe) {
throw new TransformerException(ioe);
}
} else {
//"Can't transform to a Result of type "
throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
//+ outputTarget.getClass().getName()
//+ "!");
}
// before we forget, lets make the created handler hold a reference
// to the current TransformImpl object
xoh.setTransformer(this);
SourceLocator srcLocator = getStylesheet();
xoh.setSourceLocator(srcLocator);
return xoh;
}
use of javax.xml.transform.sax.SAXResult in project robovm by robovm.
the class TransformerImpl method createSerializationHandler.
/**
* Create a ContentHandler from a Result object and an OutputProperties.
*
* @param outputTarget Where the transform result should go,
* should not be null.
* @param format The OutputProperties object that will contain
* instructions on how to serialize the output.
*
* @return A valid ContentHandler that will create the
* result tree when it is fed SAX events.
*
* @throws TransformerException
*/
public SerializationHandler createSerializationHandler(Result outputTarget, OutputProperties format) throws TransformerException {
SerializationHandler xoh;
// If the Result object contains a Node, then create
// a ContentHandler that will add nodes to the input node.
org.w3c.dom.Node outputNode = null;
if (outputTarget instanceof DOMResult) {
outputNode = ((DOMResult) outputTarget).getNode();
org.w3c.dom.Node nextSibling = ((DOMResult) outputTarget).getNextSibling();
org.w3c.dom.Document doc;
short type;
if (null != outputNode) {
type = outputNode.getNodeType();
doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument();
} else {
boolean isSecureProcessing = m_stylesheetRoot.isSecureProcessing();
doc = org.apache.xml.utils.DOMHelper.createDocument(isSecureProcessing);
outputNode = doc;
type = outputNode.getNodeType();
((DOMResult) outputTarget).setNode(outputNode);
}
DOMBuilder handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
if (nextSibling != null)
handler.setNextSibling(nextSibling);
String encoding = format.getProperty(OutputKeys.ENCODING);
xoh = new ToXMLSAXHandler(handler, (LexicalHandler) handler, encoding);
} else if (outputTarget instanceof SAXResult) {
ContentHandler handler = ((SAXResult) outputTarget).getHandler();
if (null == handler)
throw new IllegalArgumentException("handler can not be null for a SAXResult");
LexicalHandler lexHandler;
if (handler instanceof LexicalHandler)
lexHandler = (LexicalHandler) handler;
else
lexHandler = null;
String encoding = format.getProperty(OutputKeys.ENCODING);
String method = format.getProperty(OutputKeys.METHOD);
ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding);
toXMLSAXHandler.setShouldOutputNSAttr(false);
xoh = toXMLSAXHandler;
String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM);
if (systemID != null)
xoh.setDoctypeSystem(systemID);
if (publicID != null)
xoh.setDoctypePublic(publicID);
if (handler instanceof TransformerClient) {
XalanTransformState state = new XalanTransformState();
((TransformerClient) handler).setTransformState(state);
((ToSAXHandler) xoh).setTransformState(state);
}
} else // result tree to either a stream or a writer.
if (outputTarget instanceof StreamResult) {
StreamResult sresult = (StreamResult) outputTarget;
try {
SerializationHandler serializer = (SerializationHandler) SerializerFactory.getSerializer(format.getProperties());
if (null != sresult.getWriter())
serializer.setWriter(sresult.getWriter());
else if (null != sresult.getOutputStream())
serializer.setOutputStream(sresult.getOutputStream());
else if (null != sresult.getSystemId()) {
String fileURL = sresult.getSystemId();
if (fileURL.startsWith("file:///")) {
if (fileURL.substring(8).indexOf(":") > 0)
fileURL = fileURL.substring(8);
else
fileURL = fileURL.substring(7);
} else if (fileURL.startsWith("file:/")) {
if (fileURL.substring(6).indexOf(":") > 0)
fileURL = fileURL.substring(6);
else
fileURL = fileURL.substring(5);
}
m_outputStream = new java.io.FileOutputStream(fileURL);
serializer.setOutputStream(m_outputStream);
xoh = serializer;
} else
//"No output specified!");
throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
// handler = serializer.asContentHandler();
// this.setSerializer(serializer);
xoh = serializer;
}// }
catch (IOException ioe) {
throw new TransformerException(ioe);
}
} else {
//"Can't transform to a Result of type "
throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
//+ outputTarget.getClass().getName()
//+ "!");
}
// before we forget, lets make the created handler hold a reference
// to the current TransformImpl object
xoh.setTransformer(this);
SourceLocator srcLocator = getStylesheet();
xoh.setSourceLocator(srcLocator);
return xoh;
}
Aggregations