本文整理汇总了Java中org.apache.xml.serializer.Serializer.setOutputStream方法的典型用法代码示例。如果您正苦于以下问题:Java Serializer.setOutputStream方法的具体用法?Java Serializer.setOutputStream怎么用?Java Serializer.setOutputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xml.serializer.Serializer
的用法示例。
在下文中一共展示了Serializer.setOutputStream方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createContentHandler
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
/**
* @param outputStream The output
* @return A content handler
* @throws IOException When there's an XML error
*/
private ContentHandler createContentHandler(OutputStream outputStream) throws IOException {
Properties outputProperties = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML);
outputProperties.setProperty("indent", "yes");
outputProperties.setProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
outputProperties.setProperty(OutputPropertiesFactory.S_KEY_LINE_SEPARATOR, "\n");
Serializer serializer = SerializerFactory.getSerializer(outputProperties);
serializer.setOutputStream(outputStream);
return serializer.asContentHandler();
}
示例2: testPipe
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
@Test
public void testPipe()
throws TransformerException, TransformerConfigurationException,
SAXException, IOException
{
// Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
// Determine whether the TransformerFactory supports The use uf SAXSource
// and SAXResult
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
{
// Cast the TransformerFactory to SAXTransformerFactory.
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Create a TransformerHandler for each stylesheet.
TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo1.xsl")));
TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo2.xsl")));
TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo3.xsl")));
// Create an XMLReader.
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(tHandler1);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
tHandler1.setResult(new SAXResult(tHandler2));
tHandler2.setResult(new SAXResult(tHandler3));
// transformer3 outputs SAX events to the serializer.
java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
Serializer serializer = SerializerFactory.getSerializer(xmlProps);
serializer.setOutputStream(System.out);
tHandler3.setResult(new SAXResult(serializer.asContentHandler()));
// Parse the XML input document. The input ContentHandler and output ContentHandler
// work in separate threads to optimize performance.
reader.parse(new InputSource(PipeTest.class.getResourceAsStream("foo.xml")));
}
}
示例3: testPipe
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
@Test
public void testPipe()
throws TransformerException, TransformerConfigurationException,
SAXException, IOException
{
// Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
// Determine whether the TransformerFactory supports The use uf SAXSource
// and SAXResult
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
{
// Cast the TransformerFactory to SAXTransformerFactory.
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Create a TransformerHandler for each stylesheet.
TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(Pipe.class.getResourceAsStream("foo1.xsl")));
TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(Pipe.class.getResourceAsStream("foo2.xsl")));
TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource(Pipe.class.getResourceAsStream("foo3.xsl")));
// Create an XMLReader.
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(tHandler1);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
tHandler1.setResult(new SAXResult(tHandler2));
tHandler2.setResult(new SAXResult(tHandler3));
// transformer3 outputs SAX events to the serializer.
java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
Serializer serializer = SerializerFactory.getSerializer(xmlProps);
serializer.setOutputStream(System.out);
tHandler3.setResult(new SAXResult(serializer.asContentHandler()));
// Parse the XML input document. The input ContentHandler and output ContentHandler
// work in separate threads to optimize performance.
reader.parse(new InputSource(Pipe.class.getResourceAsStream("foo.xml")));
}
}
示例4: main
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public static void main(String[] args)
throws TransformerException, TransformerConfigurationException,
SAXException, IOException
{
// Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
// Determine whether the TransformerFactory supports The use uf SAXSource
// and SAXResult
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
// Cast the TransformerFactory to SAXTransformerFactory.
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Create an XMLFilter for each stylesheet.
XMLFilter xmlFilter = saxTFactory.newXMLFilter(new StreamSource("C:\\Users\\Gian\\Desktop\\config.xsl"));
// Create an XMLReader.
XMLReader reader = XMLReaderFactory.createXMLReader();
// xmlFilter uses the XMLReader as its reader.
xmlFilter.setParent(reader);
// xmlFilter3 outputs SAX events to the serializer.
java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
FileOutputStream fileOutputStream = null;
File file = null;
try {
file = new File("C:\\Users\\Gian\\Desktop\\target.profile");
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
Serializer serializer = SerializerFactory.getSerializer(xmlProps);
serializer.setOutputStream(fileOutputStream);
xmlFilter.setContentHandler(serializer.asContentHandler());
xmlFilter.parse(new InputSource("C:\\Users\\Gian\\Desktop\\Enterprise - Area Channel Sales Manager.profile"));
} catch(IOException ex){
} finally {
fileOutputStream.close();
}
}
}
示例5: testPipeUsingTemplate
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
@Test public void testPipeUsingTemplate(){
try{
// Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
// Determine whether the TransformerFactory supports The use uf SAXSource
// and SAXResult
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)){
// Cast the TransformerFactory to SAXTransformerFactory.
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Get Relevant StyleSheets
URIResolver resolver = new WidgetURITestResolver();
saxTFactory.setURIResolver(resolver);
/*
Document edlStyle,templateStyle;
edlStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml"));
// Get Template Name
XPathFactory xPathFactory=XPathFactory.newInstance();
XPath xPath=xPathFactory.newXPath();
Node node=(Node)xPath.evaluate("//use[0]",edlStyle,XPathConstants.NODE);
//Node node=nodes.item(0);
if(node!=null){
if(node.hasAttributes()){
Node testAttri=node.getAttributes().getNamedItem("name");
if(testAttri!=null){
templateName=testAttri.getNodeValue();
}else{
//set default template as widgets
templateName="widgets";
}
}
}
System.out.println(templateName);
*/
//templateStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("widgets_pipeTest.xml"));
// Create a TransformerHandler for each stylesheet.
TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml")));
// TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("trans.xml")));
//TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource("foo3.xsl"));
// Create an XMLReader.
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(tHandler1);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
//tHandler1.setResult(new SAXResult(tHandler2));
//tHandler2.setResult(new SAXResult(tHandler3));
// transformer3 outputs SAX events to the serializer.
java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
Serializer serializer = SerializerFactory.getSerializer(xmlProps);
FileOutputStream transformedXML=new FileOutputStream("c://file.xml");
serializer.setOutputStream(transformedXML);
tHandler1.setResult(new SAXResult(serializer.asContentHandler()));
// Parse the XML input document. The input ContentHandler and output ContentHandler
// work in separate threads to optimize performance.
reader.parse(new InputSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop.xml")));
}
}catch(Exception ex){
ex.printStackTrace();
}
}
示例6: getOutputHandler
import org.apache.xml.serializer.Serializer; //导入方法依赖的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 }));
*/
}
}
示例7: switchSerializerIfHTML
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
/**
* Switch to HTML serializer if element is HTML
*
*
* @param transformer Non-null transformer instance
* @param ns Namespace URI of the element
* @param localName Local part of name of element
*
* @throws TransformerException
*/
public static void switchSerializerIfHTML(
TransformerImpl transformer, String ns, String localName)
throws TransformerException
{
if (null == transformer)
return;
if (((null == ns) || (ns.length() == 0))
&& localName.equalsIgnoreCase("html"))
{
// System.out.println("transformer.getOutputPropertyNoDefault(OutputKeys.METHOD): "+
// transformer.getOutputPropertyNoDefault(OutputKeys.METHOD));
// Access at level of hashtable to see if the method has been set.
if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD))
return;
// Getting the output properties this way won't cause a clone of
// the properties.
Properties prevProperties = transformer.getOutputFormat().getProperties();
// We have to make sure we get an output properties with the proper
// defaults for the HTML method. The easiest way to do this is to
// have the OutputProperties class do it.
OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
htmlOutputProperties.copyFrom(prevProperties, true);
Properties htmlProperties = htmlOutputProperties.getProperties();
try
{
// Serializer oldSerializer = transformer.getSerializer();
Serializer oldSerializer = null;
if (null != oldSerializer)
{
Serializer serializer =
SerializerFactory.getSerializer(htmlProperties);
Writer writer = oldSerializer.getWriter();
if (null != writer)
serializer.setWriter(writer);
else
{
OutputStream os = oldSerializer.getOutputStream();
if (null != os)
serializer.setOutputStream(os);
}
// transformer.setSerializer(serializer);
ContentHandler ch = serializer.asContentHandler();
transformer.setContentHandler(ch);
}
}
catch (java.io.IOException e)
{
throw new TransformerException(e);
}
}
}
示例8: extractMorph
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public static void extractMorph(ExtractionProperties extractProps)
throws TransformerException, TransformerConfigurationException,
SAXException, IOException, JDOMException {
System.out.println("Extracting morph:");
System.out.println("Generating morph.xml");
TransformerFactory tFactory = TransformerFactory.newInstance();
File morphFile = new File(new File(extractProps.destDir), "morph.xml");
File tempFile = new File(new File(extractProps.tempDir), "temp.xml");
if (tFactory.getFeature(SAXSource.FEATURE)
&& tFactory.getFeature(SAXResult.FEATURE)) {
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
ArrayList<XMLFilter> filterChain = new ArrayList<XMLFilter>();
ArrayList<String> xslChain = new ArrayList<String>();
if (extractProps.macroSpecs.length() > 0) {
}
addTransforms(xslChain, extractProps.macroSpecs);
for (String xslFile : xslChain)
filterChain.add(saxTFactory.newXMLFilter(ExtractGrammar
.getSource(xslFile)));
// Create an XMLReader and set first xsl transform to that.
XMLReader reader = XMLReaderFactory.createXMLReader();
XMLFilter xmlFilter0 = filterChain.get(0);
xmlFilter0.setParent(reader);
//Create chain of xsl transforms
// Create an XMLFilter for each stylesheet.
for (int i = 1; i < filterChain.size(); i++) {
XMLFilter xmlFilterPrev = filterChain.get(i - 1);
XMLFilter xmlFilterCurr = filterChain.get(i);
xmlFilterCurr.setParent(xmlFilterPrev);
}
XMLFilter xmlFilter = filterChain.get(filterChain.size() - 1);
java.util.Properties xmlProps = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
xmlProps.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
Serializer serializer = SerializerFactory.getSerializer(xmlProps);
serializer.setOutputStream(new FileOutputStream(morphFile));
//XMLFilter xmlFilter = xmlFilter2;
//XMLFilter xmlFilter = xmlFilter3;
xmlFilter.setContentHandler(serializer.asContentHandler());
xmlFilter.parse(new InputSource(tempFile.getPath()));
}
//Deleting the temporary lex file
//tempFile.delete();
}