本文整理汇总了Java中javax.xml.transform.sax.SAXTransformerFactory.newTransformerHandler方法的典型用法代码示例。如果您正苦于以下问题:Java SAXTransformerFactory.newTransformerHandler方法的具体用法?Java SAXTransformerFactory.newTransformerHandler怎么用?Java SAXTransformerFactory.newTransformerHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.sax.SAXTransformerFactory
的用法示例。
在下文中一共展示了SAXTransformerFactory.newTransformerHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testcase08
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Test newTransformerHandler with a Template Handler.
*
* @throws Exception If any errors occur.
*/
public void testcase08() throws Exception {
String outputFile = USER_DIR + "saxtf008.out";
String goldFile = GOLDEN_DIR + "saxtf008GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
reader.setContentHandler(thandler);
reader.parse(XSLT_FILE);
TransformerHandler tfhandler
= saxTFactory.newTransformerHandler(thandler.getTemplates());
Result result = new StreamResult(fos);
tfhandler.setResult(result);
reader.setContentHandler(tfhandler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例2: convertWordDocumentIntoHtml
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Converts a .docx document into HTML markup. This code
* is based on <a href="http://stackoverflow.com/a/9053258/313554">this StackOverflow</a> answer.
*
* @param wordDocument The converted .docx document.
* @return
*/
public ConvertedDocumentDTO convertWordDocumentIntoHtml(MultipartFile wordDocument) {
LOGGER.info("Converting word document: {} into HTML", wordDocument.getOriginalFilename());
try {
InputStream input = wordDocument.getInputStream();
Parser parser = new OOXMLParser();
StringWriter sw = new StringWriter();
SAXTransformerFactory factory = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8");
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(new StreamResult(sw));
Metadata metadata = new Metadata();
metadata.add(Metadata.CONTENT_TYPE, "text/html;charset=utf-8");
parser.parse(input, handler, metadata, new ParseContext());
return new ConvertedDocumentDTO(wordDocument.getOriginalFilename(), sw.toString());
}
catch (IOException | SAXException | TransformerException | TikaException ex) {
LOGGER.error("Conversion failed because an exception was thrown", ex);
throw new DocumentConversionException(ex.getMessage(), ex);
}
}
示例3: buildSingleTransformerPipeline
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private Result buildSingleTransformerPipeline(Result result)
{
try
{
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
if (result != null)
{
transformerHandler.setResult(result);
}
return new SAXResult(transformerHandler);
}
catch (TransformerConfigurationException ex)
{
throw new FluentXmlConfigurationException(ex);
}
}
示例4: toSAXResult
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private SAXResult toSAXResult(Result result)
{
if (result instanceof SAXResult)
{
return (SAXResult) result;
}
try
{
SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler transformerHandler = transformerFactory.newTransformerHandler();
transformerHandler.setResult(result);
return new SAXResult(transformerHandler);
}
catch (TransformerConfigurationException ex)
{
throw new FluentXmlConfigurationException(ex);
}
}
示例5: SchemaToXML
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public SchemaToXML(Schema schema, StreamResult streamResult)
{
final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
try
{
xmlOut = stf.newTransformerHandler();
}
catch (TransformerConfigurationException error)
{
throw new RuntimeException("Unable to create TransformerHandler.", error);
}
final Transformer t = xmlOut.getTransformer();
try
{
t.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
}
catch (final IllegalArgumentException e)
{
// It was worth a try
}
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.ENCODING, SchemaComparator.CHAR_SET);
xmlOut.setResult(streamResult);
this.schema = schema;
}
示例6: getTransformerHandler
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
@Override
protected TransformerHandler getTransformerHandler(String xslFileName) throws SAXException, ParserConfigurationException,
TransformerConfigurationException, IOException {
SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
factory.setURIResolver(uriResolver);
TemplatesHandler templatesHandler = factory.newTemplatesHandler();
SAXParserFactory pFactory = SAXParserFactory.newInstance();
pFactory.setNamespaceAware(true);
XMLReader xmlreader = pFactory.newSAXParser().getXMLReader();
// create the stylesheet input source
InputSource xslSrc = new InputSource(xslFileName);
xslSrc.setSystemId(filenameToURL(xslFileName));
// hook up the templates handler as the xsl content handler
xmlreader.setContentHandler(templatesHandler);
// call parse on the xsl input source
xmlreader.parse(xslSrc);
// extract the Templates object created from the xsl input source
return factory.newTransformerHandler(templatesHandler.getTemplates());
}
示例7: testXMLStackOverflowBug
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
@Test
public final void testXMLStackOverflowBug() throws TransformerConfigurationException, IOException, SAXException {
try {
SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler ser = stf.newTransformerHandler();
ser.setResult(new StreamResult(System.out));
StringBuilder sb = new StringBuilder(4096);
for (int x = 4096; x > 0; x--) {
sb.append((char) x);
}
ser.characters(sb.toString().toCharArray(), 0, sb.toString().toCharArray().length);
ser.endDocument();
} catch (StackOverflowError se) {
se.printStackTrace();
Assert.fail("StackOverflow");
}
}
示例8: testcase01
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a transformer handler which write out the
* result to output file. Test verifies output file is same as golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase01() throws Exception {
String outputFile = USER_DIR + "saxtf001.out";
String goldFile = GOLDEN_DIR + "saxtf001GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler handler = saxTFactory.newTransformerHandler(new StreamSource(XSLT_FILE));
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例9: testcase02
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a content handler which write out the result
* to output file. Test verifies output file is same as golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase02() throws Exception {
String outputFile = USER_DIR + "saxtf002.out";
String goldFile = GOLDEN_DIR + "saxtf002GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile);
FileInputStream fis = new FileInputStream(XSLT_FILE)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
SAXSource ss = new SAXSource();
ss.setInputSource(new InputSource(fis));
TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例10: testcase03
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is
* namespace awareness, DocumentBuilder parse xslt file as DOMSource.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase03() throws Exception {
String outputFile = USER_DIR + "saxtf003.out";
String goldFile = GOLDEN_DIR + "saxtf003GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XSLT_FILE));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例11: JDependProcessorXML2
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Constructs a <code>JDepend</code> instance with the specified writer.
* Creates a new instance of JDependProcessorXML
*
*
* @param writer Writer.
*/
public JDependProcessorXML2(PrintWriter writer) {
super(writer);
formatter = NumberFormat.getInstance(Locale.ENGLISH);
formatter.setMaximumFractionDigits(2);
try {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.INDENT,"yes");
// use getWriter as transform result destination
hd.setResult(new StreamResult(this.getWriter()) );
// create the XmlEmitter
this.xmlEmitter = new XmlEmitter( hd );
} catch (TransformerConfigurationException tce) {
throw new XmlEmitterException(tce);
}
}
示例12: SimpleXMLWriter
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Builds a {@link SimpleXMLWriter} for the given display.
*
* @param display
* the display to build the writer for (not null).
* @throws TransformerConfigurationException
* if for some reason the TransformerHandler cannot be created.
*/
public SimpleXMLWriter(final Display display) throws TransformerConfigurationException {
Check.notNull(display, "display"); //$NON-NLS-1$
final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
transformerHandler = transformerFactory.newTransformerHandler();
transformer = transformerHandler.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$
// TODO Reference DTD or schema.
transformerHandler.setResult(new StreamResult(display.getPrintStream()));
}
示例13: getTransformer
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private static Result getTransformer(Result result) throws Exception {
SAXTransformerFactory fact = (SAXTransformerFactory) TransformerFactory.newInstance();
String xsl = "/" + PersistenceUnitDefinition.class.getPackage().getName().replace(
".", "/") + "/persistence.xsl";
TransformerHandler handler;
try (InputStream xsls = PersistenceUnitDefinition.class.getClassLoader().getResourceAsStream(xsl)) {
// Create the final pipeline.
handler = fact.newTransformerHandler(new StreamSource(xsls));
handler.setResult(result);
String preTransform = System.getProperty("osgi.jpa.transform");
if (preTransform != null) {
// Construct the transformation pipeline.
try (FileInputStream pf = new FileInputStream(preTransform)) {
TransformerHandler preTransformer = fact.newTransformerHandler(new StreamSource(pf));
preTransformer.setResult(new SAXResult(handler));
handler = preTransformer;
}
}
}
return new SAXResult(handler);
}
示例14: serialize
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Serializes a property list into a Cocoa XML Property List document.
* @param propertyList property list.
* @param file destination.
* @param comments comments to insert into document.
* @throws SAXException if exception during serialization.
* @throws TransformerConfigurationException if exception creating serializer.
*/
public static void serialize(final Map propertyList,
final List comments,
final File file)
throws IOException, SAXException,
TransformerConfigurationException {
SAXTransformerFactory sf = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler handler = sf.newTransformerHandler();
FileOutputStream os = new FileOutputStream(file);
StreamResult result = new StreamResult(os);
handler.setResult(result);
handler.startDocument();
for(Iterator iter = comments.iterator(); iter.hasNext();) {
char[] comment = String.valueOf(iter.next()).toCharArray();
handler.comment(comment, 0, comment.length);
}
AttributesImpl attributes = new AttributesImpl();
handler.startElement(null, "plist", "plist", attributes);
serializeMap(propertyList, handler);
handler.endElement(null, "plist", "plist");
handler.endDocument();
}
示例15: write
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public void write( Writer out )
{
try
{
StreamResult streamResult = new StreamResult( out );
SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory
.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty( OutputKeys.ENCODING, "ISO-8859-1" );
serializer.setOutputProperty( OutputKeys.INDENT, "yes" );
serializer.setOutputProperty( OutputKeys.METHOD, "xml" );
serializer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4" );
hd.setResult( streamResult );
// hd.startDocument();
write( hd );
hd.endDocument();
}
catch( Exception e )
{
e.printStackTrace();
}
}