本文整理汇总了Java中javax.xml.transform.sax.SAXResult.setHandler方法的典型用法代码示例。如果您正苦于以下问题:Java SAXResult.setHandler方法的具体用法?Java SAXResult.setHandler怎么用?Java SAXResult.setHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.sax.SAXResult
的用法示例。
在下文中一共展示了SAXResult.setHandler方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCheckElementContentWhitespace
import javax.xml.transform.sax.SAXResult; //导入方法依赖的package包/类
/**
* Test for the isIgnoringElementContentWhitespace and the
* setIgnoringElementContentWhitespace. The xml file has all kinds of
* whitespace,tab and newline characters, it uses the MyNSContentHandler
* which does not invoke the characters callback when this
* setIgnoringElementContentWhitespace is set to true.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckElementContentWhitespace() throws Exception {
String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
String outputFile = USER_DIR + "dbfactory02.out";
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
assertFalse(dbf.isIgnoringElementContentWhitespace());
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
assertFalse(eh.isErrorOccured());
DOMSource domSource = new DOMSource(doc);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
SAXResult saxResult = new SAXResult();
try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
saxResult.setHandler(handler);
transformer.transform(domSource, saxResult);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例2: setSAXConsumer
import javax.xml.transform.sax.SAXResult; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected void setSAXConsumer(final SAXConsumer consumer) {
TransformerHandler transformerHandler;
try {
transformerHandler = TRAX_FACTORY.newTransformerHandler(this.templates);
} catch (Exception e) {
throw new SetupException("Could not initialize transformer handler.", e);
}
if (this.parameters != null) {
final Transformer transformer = transformerHandler.getTransformer();
this.parameters.entrySet().forEach(entry -> {
final String name = entry.getKey();
// is valid XSLT parameter name
if (XSLT_PARAMETER_NAME_PATTERN.matcher(name).matches()) {
transformer.setParameter(name, entry.getValue());
}
});
}
final SAXResult result = new SAXResult();
result.setHandler(consumer);
// According to TrAX specs, all TransformerHandlers are LexicalHandlers
result.setLexicalHandler(consumer);
transformerHandler.setResult(result);
final SAXConsumerAdapter saxConsumerAdapter = new SAXConsumerAdapter();
saxConsumerAdapter.setContentHandler(transformerHandler);
super.setSAXConsumer(saxConsumerAdapter);
}
示例3: transform
import javax.xml.transform.sax.SAXResult; //导入方法依赖的package包/类
@Override
protected String transform(TransformerPool tp, Source source, Map parametervalues) throws TransformerException, IOException {
SAXResult result = new SAXResult();
XmlJsonWriter xjw = new XmlJsonWriter();
result.setHandler(xjw);
tp.transform(source, result, parametervalues);
return xjw.toString();
}
示例4: getSAXResult
import javax.xml.transform.sax.SAXResult; //导入方法依赖的package包/类
private Result getSAXResult() {
SAXResult saxResult = new SAXResult();
saxResult.setHandler(new DefaultHandler());
return saxResult;
}
示例5: getHandlerSAXResult
import javax.xml.transform.sax.SAXResult; //导入方法依赖的package包/类
private SAXResult getHandlerSAXResult() {
SAXResult res = new SAXResult();
MyContentHandler myContentHandler = new MyContentHandler(transOutputStream());
res.setHandler(myContentHandler);
return res;
}
示例6: getOutputHandler
import javax.xml.transform.sax.SAXResult; //导入方法依赖的package包/类
public ContentHandler getOutputHandler(OutputStream out) throws IOException
{
if (!isAvailable()) return null;
try
{
XSLTTransform xsltTransform = (XSLTTransform) transformerHolder.get();
if (xsltTransform == null)
{
xsltTransform = new XSLTTransform();
xsltTransform.setXslt(new InputSource(this.getClass()
.getResourceAsStream(xslt)));
transformerHolder.set(xsltTransform);
}
SAXResult sr = new SAXResult();
TransformerHandler th = xsltTransform.getContentHandler();
Transformer transformer = th.getTransformer();
if (transformParameters != null) {
for (Map.Entry<String, String> entry: transformParameters.entrySet()) {
transformer.setParameter(entry.getKey(), entry.getValue());
}
}
Properties p = OutputPropertiesFactory.getDefaultMethodProperties("xml");
// SAK-14388 - use the alternate XHTMLSerializer2 for Websphere environments
if ("websphere".equals(ServerConfigurationService.getString("servlet.container")))
{
// SAK-16712: null in java.util.Properties causes NullPointerException
if (getXalan270ContentHandler() != null )
{
outputProperties.put("{http://xml.apache.org/xalan}content-handler", getXalan270ContentHandler());
}
}
p.putAll(outputProperties);
/*
S_KEY_CONTENT_HANDLER:{http://xml.apache.org/xalan}content-handler
S_KEY_ENTITIES:{http://xml.apache.org/xalan}entities
S_KEY_INDENT_AMOUNT:{http://xml.apache.org/xalan}indent-amount
S_OMIT_META_TAG:{http://xml.apache.org/xalan}omit-meta-tag
S_USE_URL_ESCAPING:{http://xml.apache.org/xalan}use-url-escaping
*/
Serializer s = SerializerFactory.getSerializer(p);
s.setOutputStream(out);
sr.setHandler(s.asContentHandler());
th.setResult(sr);
return th;
}
catch (Exception ex)
{
throw new RuntimeException("Failed to create Content Handler", ex); //$NON-NLS-1$
/*
* String stackTrace = null; try { StringWriter exw = new
* StringWriter(); PrintWriter pw = new PrintWriter(exw);
* log.error(ex.getMessage(), ex); stackTrace = exw.toString(); } catch
* (Exception ex2) { stackTrace =
* MessageFormat.format(defaultStackTrace, new Object[] {
* ex.getMessage() }); } out.write(MessageFormat.format(errorFormat,
* new Object[] { ex.getMessage(), stackTrace }));
*/
}
}