本文整理匯總了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 }));
*/
}
}