本文整理汇总了Java中javax.xml.transform.sax.TransformerHandler类的典型用法代码示例。如果您正苦于以下问题:Java TransformerHandler类的具体用法?Java TransformerHandler怎么用?Java TransformerHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransformerHandler类属于javax.xml.transform.sax包,在下文中一共展示了TransformerHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testcase08
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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: createTransformerHandler
import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
public static TransformerHandler createTransformerHandler(final ErrorListener errorListener,
final boolean indentOutput) throws TransformerConfigurationException {
final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactoryFactory.newInstance();
if (errorListener != null) {
stf.setErrorListener(errorListener);
}
final TransformerHandler th = stf.newTransformerHandler();
final Transformer transformer = th.getTransformer();
setCommonOutputProperties(transformer, indentOutput);
if (errorListener != null) {
transformer.setErrorListener(errorListener);
}
return th;
}
示例3: marshal
import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
public static <T> String marshal(final JAXBContext context, final T obj, final XmlAdapter<?, ?>... adapters) {
if (obj == null) {
return null;
}
try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Marshaller marshaller = context.createMarshaller();
if (adapters != null) {
for (final XmlAdapter<?, ?> adapter : adapters) {
marshaller.setAdapter(adapter);
}
}
final TransformerHandler transformerHandler = XMLUtil.createTransformerHandler(true);
transformerHandler.setResult(new StreamResult(out));
marshaller.marshal(obj, transformerHandler);
return out.toString(String.valueOf(StandardCharsets.UTF_8));
} catch (final Throwable t) {
throw new RuntimeException(t.getMessage(), t);
}
}
示例4: getSerializer
import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
@Override
public TransformerHandler getSerializer()
{
try
{
SAXTransformerFactory transformerFactory = buildTransformerFactory();
configure(transformerFactory);
TransformerHandler transformer = buildTransformer(transformerFactory);
configure(transformer.getTransformer());
return transformer;
}
catch (TransformerConfigurationException ex)
{
throw new FluentXmlConfigurationException(ex);
}
}
示例5: buildSingleTransformerPipeline
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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);
}
}
示例6: toSAXResult
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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);
}
}
示例7: identityTransform
import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
* Performs identity transformation.
*/
public static <T extends Result>
T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException {
if (src instanceof StreamSource) {
// work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
// is not turned on by default
StreamSource ssrc = (StreamSource) src;
TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler();
th.setResult(result);
XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader();
reader.setContentHandler(th);
reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
reader.parse(toInputSource(ssrc));
} else {
newTransformer().transform(src, result);
}
return result;
}
示例8: identityTransform
import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
* Performs identity transformation.
* @param <T>
* @param src
* @param result
* @return
* @throws javax.xml.transform.TransformerException
* @throws java.io.IOException
* @throws org.xml.sax.SAXException
* @throws javax.xml.parsers.ParserConfigurationException
*/
public static <T extends Result> T identityTransform(Source src, T result)
throws TransformerException, SAXException, ParserConfigurationException, IOException {
if (src instanceof StreamSource) {
// work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
// is not turned on by default
StreamSource ssrc = (StreamSource) src;
TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler();
th.setResult(result);
XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader();
reader.setContentHandler(th);
reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
reader.parse(toInputSource(ssrc));
} else {
newTransformer().transform(src, result);
}
return result;
}
示例9: testXMLStackOverflowBug
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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");
}
}
示例10: test
import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
@Test
public void test() throws Exception {
TransformerHandler th = ((SAXTransformerFactory) TransformerFactory.newInstance()).newTransformerHandler();
DOMResult result = new DOMResult();
th.setResult(result);
th.startDocument();
th.startElement("", "root", "root", new AttributesImpl());
th.characters(new char[0], 0, 0);
th.endElement("", "root", "root");
th.endDocument();
// there's no point in having empty text --- we should remove it
Assert.assertEquals(0, ((Document) result.getNode()).getDocumentElement().getChildNodes().getLength());
}
示例11: testcase01
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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));
}
示例12: testcase02
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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));
}
示例13: testcase03
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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));
}
示例14: testcase09
import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
* Test newTransformerHandler with a Template Handler along with a relative
* URI in the style-sheet file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase09() throws Exception {
String outputFile = USER_DIR + "saxtf009.out";
String goldFile = GOLDEN_DIR + "saxtf009GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
thandler.setSystemId("file:///" + XML_DIR);
reader.setContentHandler(thandler);
reader.parse(XSLT_INCL_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));
}
示例15: getTransformerHandler
import javax.xml.transform.sax.TransformerHandler; //导入依赖的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());
}