本文整理汇总了Java中org.apache.xml.serializer.Serializer.setWriter方法的典型用法代码示例。如果您正苦于以下问题:Java Serializer.setWriter方法的具体用法?Java Serializer.setWriter怎么用?Java Serializer.setWriter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xml.serializer.Serializer
的用法示例。
在下文中一共展示了Serializer.setWriter方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
/**
* This function serializes the generated Schematron schema to the given
* directory. The document name is derived from the name of the GML
* application schema. Serialization takes place only if at least one
* rule has been generated.
* @param outputDirectory
*/
public void write(String outputDirectory) {
if (printed || !assertion) {
return;
}
// Choose serialization parameters
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
// outputFormat.setProperty("encoding", model.characterEncoding());
outputFormat.setProperty("encoding", "UTF-8");
// Do the actual writing
try {
OutputStream fout = new FileOutputStream(outputDirectory + "/" + pi.xsdDocument() + "_SchematronSchema.xml");
OutputStreamWriter outputXML = new OutputStreamWriter(fout, outputFormat.getProperty("encoding"));
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(document);
outputXML.close();
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
// Indicate we did it to avoid doing it again
printed = true;
}
示例2: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public void write() {
if (printed)
return;
if (diagnosticsOnly)
return;
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "no");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"0");
outputFormat.setProperty("encoding", "UTF-8");
try {
File file = new File(
outputDirectory + "/" + pi.name() + " Mapping Table.xml");
FileWriter outputXML = new FileWriter(file);
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(document);
outputXML.close();
result.addResult(getTargetName(), outputDirectory,
pi.name() + " Mapping Table.xml", pi.targetNamespace());
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
printed = true;
}
示例3: print
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
private void print(Document document, String filenameSuffix) {
String outFileName = outputFilename + filenameSuffix;
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
outputFormat.setProperty("encoding", "UTF-8");
/*
* Uses OutputStreamWriter instead of FileWriter to set character
* encoding (see doc in Serializer.setWriter and FileWriter)
*/
File res = new File(outputDirectoryFile, outFileName);
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(res), "UTF-8"))) {
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(writer);
serializer.asDOMSerializer().serialize(document);
result.addResult(getTargetName(), outputDirectory, outFileName,
null);
} catch (IOException ioe) {
result.addError(this, 3, outFileName, ioe.getMessage());
}
}
示例4: toFile
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public void toFile(String filename) {
if (document == null) {
return;
}
try {
root.setAttribute("config", options.configFile);
root.setAttribute("end", (new Date()).toString());
// check that directory exists and create it if necessary
File f = new File(filename);
f = f.getParentFile();
if (f != null && !f.exists()) {
FileUtils.forceMkdir(f);
}
BufferedWriter outputXML = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filename), "UTF-8"));
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(document);
outputXML.close();
String xsltfileName = options.parameter("xsltFile");
if (xsltfileName != null && !xsltfileName.isEmpty()) {
StreamSource xsltSource;
if (xsltfileName.toLowerCase().startsWith("http")) {
// get xslt via URL
URL url = new URL(xsltfileName);
URLConnection urlConnection = url.openConnection();
xsltSource = new StreamSource(
urlConnection.getInputStream());
} else {
InputStream stream = getClass()
.getResourceAsStream("/xslt/result.xsl");
if (stream == null) {
// get it from the file system
File xsltFile = new File(xsltfileName);
if (!xsltFile.canRead()) {
throw new Exception(
"Cannot read " + xsltFile.getName());
}
xsltSource = new StreamSource(xsltFile);
} else {
// get it from the JAR file
xsltSource = new StreamSource(stream);
}
}
File outHTML = new File(filename.replace(".xml", ".html"));
if (outHTML.exists())
outHTML.delete();
BufferedWriter outputHTML = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outHTML), "UTF-8"));;
if (xsltSource != null) {
Source xmlSource = new DOMSource(document);
Result res = new StreamResult(outputHTML);
TransformerFactory transFact = TransformerFactory
.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, res);
/*
* Apparently, the following is necessary to close streams
* appropriately when running ShapeChange in a separate
* process that was spawned by another process (in the given
* case, a server application):
*/
xsltSource.getInputStream().close();
outputHTML.close();
}
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
示例5: writeAll
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
@Override
public void writeAll(ShapeChangeResult r) {
if (printed || diagnosticsOnly) {
return;
}
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
outputFormat.setProperty("encoding", "UTF-8");
/*
* Uses OutputStreamWriter instead of FileWriter to set character
* encoding (see doc in Serializer.setWriter and FileWriter)
*/
try {
File repXsd = new File(outputDirectoryFile, outputFilename);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(repXsd), "UTF-8"));
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(writer);
serializer.asDOMSerializer().serialize(document);
writer.close();
r.addResult(getTargetName(), outputDirectory, outputFilename,
schemaTargetNamespace);
} catch (IOException ioe) {
r.addError(this, 3, outputFilename, ioe.getMessage());
}
printed = true;
}
示例6: printFile
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
/** Dump XML Schema file */
public void printFile(Properties outputFormat) throws Exception {
if (printed) {
return;
}
Node anchor = rootAnnotation;
Element e;
Collections.sort(includes);
for (Iterator<String> i = includes.iterator(); i.hasNext();) {
e = document.createElementNS(Options.W3C_XML_SCHEMA, "include");
addAttribute(e, "schemaLocation", i.next());
if (anchor == null) {
root.insertBefore(e, root.getFirstChild());
} else {
root.insertBefore(e, anchor.getNextSibling());
}
anchor = e;
}
String s;
String loc;
Collections.sort(imports);
for (Iterator<String> i = imports.iterator(); i.hasNext();) {
e = document.createElementNS(Options.W3C_XML_SCHEMA, "import");
s = i.next();
addAttribute(e, "namespace", s);
loc = options.schemaLocationOfNamespace(s);
if (loc != null) {
addAttribute(e, "schemaLocation", loc);
}
if (anchor == null) {
root.insertBefore(e, root.getFirstChild());
} else {
root.insertBefore(e, anchor.getNextSibling());
}
anchor = e;
}
// Check whether we can use the given output directory
File outputDirectoryFile = new File(outputDirectory);
boolean exi = outputDirectoryFile.exists();
if (!exi) {
outputDirectoryFile.mkdirs();
exi = outputDirectoryFile.exists();
}
boolean dir = outputDirectoryFile.isDirectory();
boolean wrt = outputDirectoryFile.canWrite();
boolean rea = outputDirectoryFile.canRead();
if (!exi || !dir || !wrt || !rea) {
result.addFatalError(this, 12, outputDirectory);
throw new ShapeChangeAbortException();
}
/*
* Uses OutputStreamWriter instead of FileWriter to set character
* encoding (see doc in Serializer.setWriter and FileWriter)
*/
try {
String fname = outputDirectory + "/" + name;
new File(fname).getCanonicalPath();
OutputStream fout = new FileOutputStream(fname);
OutputStream bout = new BufferedOutputStream(fout);
OutputStreamWriter outputXML = new OutputStreamWriter(bout,
outputFormat.getProperty("encoding"));
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(document);
outputXML.close();
} catch (IOException ioe) {
result.addError(null, 171, name);
}
printed = true;
}
示例7: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
/**
* @see de.interactive_instruments.ShapeChange.Target.Target#write()
*/
public void write() {
if (printed) {
return;
}
if (diagnosticsOnly) {
return;
}
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
outputFormat.setProperty("encoding", "UTF-8");
try {
FileWriter outputXML;
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
for (String className : documentMap.keySet()) {
Document doc = documentMap.get(className);
if (doc != null) {
outputXML = new FileWriter(
outputDirectory + "/" + className + ".xml");
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(doc);
outputXML.close();
result.addResult(getTargetName(), outputDirectory,
className + ".xml", null);
}
}
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
printed = true;
}
示例8: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
@Override
public void write() {
if (printed || diagnosticsOnly) {
return;
}
addImports();
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
outputFormat.setProperty("encoding", "UTF-8");
/*
* Uses OutputStreamWriter instead of FileWriter to set character
* encoding (see doc in Serializer.setWriter and FileWriter)
*/
try {
File repXsd = new File(outputDirectoryFile, outputFilename);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(repXsd), "UTF-8"));
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(writer);
serializer.asDOMSerializer().serialize(document);
writer.close();
result.addResult(getTargetName(), outputDirectory, outputFilename,
schemaPi.targetNamespace());
} catch (IOException ioe) {
result.addError(null, 171, outputFilename);
}
printed = true;
}
示例9: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public void write() {
if (printed) {
return;
}
if (diagnosticsOnly)
return;
if (aborted)
return;
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
outputFormat.setProperty("encoding", "UTF-8");
try {
File file = new File(outputDirectory + "/index." + pi.xmlns()
+ ".definitions.xml");
FileWriter outputXML = new FileWriter(file);
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(document);
outputXML.close();
result.addResult(getTargetName(), outputDirectory, "index." + pi.xmlns() + ".definitions.xml", pi.targetNamespace());
if (!schema) {
for (Iterator<ClassInfo> i = model.classes(pi).iterator(); i.hasNext();) {
ClassInfo ci = i.next();
Document cDocument = documentMap.get(ci.id());
if (cDocument != null) {
outputXML = new FileWriter(outputDirectory + "/" + ci.name() + ".definitions.xml");
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(cDocument);
outputXML.close();
result.addResult(getTargetName(), outputDirectory, ci.name() + ".definitions.xml", ci.qname());
}
}
}
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
printed = true;
}
示例10: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public void write() {
if (printed) {
return;
}
if (diagnosticsOnly) {
return;
}
try {
Properties outputFormat = OutputPropertiesFactory.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
outputFormat.setProperty("encoding", "UTF-8");
Serializer serializer = SerializerFactory.getSerializer(outputFormat);
for (Iterator<ClassInfo> i = model.classes(pi).iterator(); i.hasNext();) {
ClassInfo ci = i.next();
Document cDocument = documentMap.get(ci.id());
if (cDocument != null) {
String dir = options.parameter(this.getClass().getName(),"outputDirectory");
if (dir==null)
dir = options.parameter("outputDirectory");
if (dir==null)
dir = options.parameter(".");
File outDir = new File(dir);
if(!outDir.exists())
outDir.mkdirs();
/*
* SO: Used OutputStreamWriter instead of FileWriter to set character encoding
* (see doc in Serializer.setWriter and FileWriter)
*/
OutputStream fout= new FileOutputStream(dir + "/" + ci.name() + ".xml");
OutputStreamWriter outputXML = new OutputStreamWriter(fout, outputFormat.getProperty("encoding"));
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(cDocument);
outputXML.close();
result.addResult(getTargetName(), dir, ci.name()+".xml", ci.qname());
}
}
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
printed = true;
}
示例11: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public void write() {
if (printed) {
return;
}
if (diagnosticsOnly) {
return;
}
try {
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty(
"{http://xml.apache.org/xalan}indent-amount", "2");
outputFormat.setProperty("encoding", "UTF-8");
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
for (Iterator<ClassInfo> i = model.classes(pi).iterator(); i
.hasNext();) {
ClassInfo ci = i.next();
Document cDocument = documentMap.get(ci.id());
if (cDocument != null) {
String dir = options.parameter(this.getClass().getName(),
"outputDirectory");
if (dir == null)
dir = options.parameter("outputDirectory");
if (dir == null)
dir = options.parameter(".");
File outDir = new File(dir);
if (!outDir.exists())
outDir.mkdirs();
/*
* SO: Used OutputStreamWriter instead of FileWriter to set
* character encoding (see doc in Serializer.setWriter and
* FileWriter)
*/
OutputStream fout = new FileOutputStream(
dir + "/" + ci.name() + ".xml");
OutputStreamWriter outputXML = new OutputStreamWriter(fout,
outputFormat.getProperty("encoding"));
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(cDocument);
outputXML.close();
result.addResult(getTargetName(), dir, ci.name() + ".xml",
ci.qname());
}
}
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
printed = true;
}
示例12: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
public void write() {
if (error || printed)
return;
// Check whether we can use the given output directory
File outputDirectoryFile = new File(outputDirectory);
boolean exi = outputDirectoryFile.exists();
if (!exi) {
outputDirectoryFile.mkdirs();
exi = outputDirectoryFile.exists();
}
boolean dir = outputDirectoryFile.isDirectory();
boolean wrt = outputDirectoryFile.canWrite();
boolean rea = outputDirectoryFile.canRead();
if (!exi || !dir || !wrt || !rea) {
result.addFatalError(this, 12, outputDirectory);
return;
}
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
outputFormat.setProperty("encoding", encoding);
String fileName = pi.name().replace("/", "_").replace(" ", "_")
+ ".rdf";
try {
OutputStream fout = new FileOutputStream(
outputDirectory + "/" + fileName);
OutputStream bout = new BufferedOutputStream(fout);
OutputStreamWriter outputXML = new OutputStreamWriter(bout,
outputFormat.getProperty("encoding"));
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(document);
outputXML.close();
result.addResult(getTargetName(), outputDirectory, fileName,
pi.targetNamespace() + "#");
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
printed = true;
}
示例13: write
import org.apache.xml.serializer.Serializer; //导入方法依赖的package包/类
@Override
public void write() {
if (printed || !atLeastOneSchematronRuleExists) {
return;
}
// Choose serialization parameters
Properties outputFormat = OutputPropertiesFactory
.getDefaultMethodProperties("xml");
outputFormat.setProperty("indent", "yes");
outputFormat.setProperty("{http://xml.apache.org/xalan}indent-amount",
"2");
// outputFormat.setProperty("encoding", model.characterEncoding());
outputFormat.setProperty("encoding", "UTF-8");
// Do the actual writing
try {
String fileName = schema.name().replace("/", "_").replace(" ", "_")
+ "_SchematronSchema.xml";
OutputStream fout = new FileOutputStream(outputDirectory + "/"
+ fileName);
OutputStreamWriter outputXML = new OutputStreamWriter(fout,
outputFormat.getProperty("encoding"));
Serializer serializer = SerializerFactory
.getSerializer(outputFormat);
serializer.setWriter(outputXML);
serializer.asDOMSerializer().serialize(document);
outputXML.close();
} catch (Exception e) {
String m = e.getMessage();
if (m != null) {
result.addError(m);
}
e.printStackTrace(System.err);
}
// Indicate we did it to avoid doing it again
printed = true;
}
示例14: 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);
}
}
}