本文整理了Java中org.jdom2.output.Format.setLineSeparator()
方法的一些代码示例,展示了Format.setLineSeparator()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Format.setLineSeparator()
方法的具体详情如下:
包路径:org.jdom2.output.Format
类名称:Format
方法名:setLineSeparator
[英]This will set the newline separator (lineSeparator
). The default is \r\n
. To make it output the system default line ending string, call setLineSeparator(System.getProperty("line.separator"))
.
To output "UNIX-style" documents, call setLineSeparator("\n")
. To output "Mac-style" documents, call setLineSeparator("\r")
. DOS-style documents use CR-LF ("\r\n"), which is the default.
Note that this only applies to newlines generated by the outputter. All XML parsers are required to 'normalize' all the combinations of line seperators to just '\n'. As a result, if any JDOM component has an end-of-line-like value (e.g. '\r') in it then that value must be the result of an escaped value in the XML source document 
or a value explicitly set with one of the Text value setters. Values in JDOM content that were explicitly set to be '\r' will always be escaped on XML Output.
The actual newline separator itself though can be set with this method. Any internal newlines in Text output will be represented by this end-of-line sequence. For example, the following code:
Text txt = new Text("\r\n");
XMLOutputter xout = new XMLOutputter();
String result = xout.outputString(txt);
will produce the literal String sequence " \r\n" because the original \r is escaped to be 
and the original \n is replaced with the JDOM default Line Separator "\r\n".
If the format's "indent" property is null (as is the default for the Raw and Compact formats), then this value only effects the newlines written after the declaration and doctype, as well as any newlines embedded within existing text content.
Setting the indent to be null will disable end-of-line processing for any formatting, but will not affect substitution of embedded \n. Setting this value to null or the empty string will disable all end-of-line modifications.
[中]这将设置换行符(lineSeparator
)。默认值为\r\n
。要使其输出系统默认的行结束字符串,请调用setLineSeparator(System.getProperty("line.separator"))
。
要输出“UNIX风格”文档,请调用setLineSeparator("\n")
。要输出“Mac风格”文档,请调用setLineSeparator("\r")
。DOS样式的文档使用默认的CR-LF(“\r\n”)。
请注意,这仅适用于输出程序生成的换行符。所有XML解析器都需要将行分隔符的所有组合“规范化”为“\n”。因此,如果任何JDOM组件中都有一个类似于行尾的值(例如“\r”),那么该值必须是XML源文档
中转义值的结果,或者是使用其中一个文本值设置器显式设置的值。JDOM内容中显式设置为“\r”的值将始终在XML输出时转义。
但是,实际的换行符本身可以使用此方法设置。文本输出中的任何内部换行符都将由该行尾序列表示。例如,以下代码:
Text txt = new Text("\r\n");
XMLOutputter xout = new XMLOutputter();
String result = xout.outputString(txt);
将生成文本字符串序列“
;\r\n”,因为原始\r转义为
,并且原始\n将替换为JDOM默认的行分隔符“\r\n”。
如果格式的“缩进”属性为空(这是原始格式和压缩格式的默认值),则此值仅影响声明和doctype之后写入的换行,以及现有文本内容中嵌入的任何换行。
将缩进设置为null将禁用任何格式的行尾处理,但不会影响嵌入的\n替换。将此值设置为null或空字符串将禁用所有行尾修改。
代码示例来源:origin: gocd/gocd
private static XMLOutputter xmlOutputer() {
Format format = Format.getPrettyFormat().setEncoding("utf-8").setLineSeparator("\n");
return new XMLOutputter(format);
}
代码示例来源:origin: org.jdom/jdom
/**
* This will set the newline separator sequence.
* <p>
* This method differes from {@link #setLineSeparator(String)} slightly in
* that, to disable end-of-line processing you should call:
* <pre>
* Format.setLinewSeparator(LineSeparator.NONE);
* </pre>
*
* @see #setLineSeparator(String) for comprehensive notes.
*
* @param separator {@link LineSeparator} line separator to us
* @return a pointer to this Format for chaining
* @since JDOM2
*/
public Format setLineSeparator(LineSeparator separator) {
return setLineSeparator(separator == null ?
STANDARD_LINE_SEPARATOR :
separator.value());
}
代码示例来源:origin: Unidata/thredds
public NcMLWriter() {
this.namespace = ncmlDefaultNamespace;
this.xmlFormat = Format.getPrettyFormat().setLineSeparator(LineSeparator.UNIX);
this.writeVariablesPredicate = writeMetadataVariablesPredicate;
}
代码示例来源:origin: jpos/jPOS
p.println("");
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
out.getFormat().setLineSeparator(System.lineSeparator());
try {
out.output((Element) value, p);
代码示例来源:origin: org.apache.maven.plugins/maven-shade-plugin
/**
* Method write
*
* @param project
* @param writer
* @param document
*/
public void write( Model project, Document document, OutputStreamWriter writer )
throws java.io.IOException
{
Format format = Format.getRawFormat();
format.setEncoding( writer.getEncoding() ).setLineSeparator( System.getProperty( "line.separator" ) );
write( project, document, writer, format );
} // -- void write(Model, Document, OutputStreamWriter)
代码示例来源:origin: ssaring/sportstracker
/**
* Writes the specified JDOM element to the XML file specified by filename.
* The file will use UTF-8 encoding and has 4-space indentation.
*
* @param eRoot root element of the XML document
* @param filename filename of the XML file to create
* @throws IOException
*/
public static void writeXMLFile(Element eRoot, String filename) throws IOException {
Document document = new Document(eRoot);
XMLOutputter outputter = new XMLOutputter();
Format format = Format.getPrettyFormat();
format.setLineSeparator(System.getProperty("line.separator"));
format.setIndent(" ");
outputter.setFormat(format);
// FileWriter can't be used here, because default encoding on Win32 isn't UTF-8
try (OutputStreamWriter osWriter = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8")) {
outputter.output(document, osWriter);
osWriter.flush();
}
}
}
代码示例来源:origin: eu.fbk.pikes/pikes-naflib
/**
* Returns a string containing the XML content of a KAFDocument object.
*/
static String kafToStr(KAFDocument kaf) {
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setLineSeparator(LineSeparator.UNIX));
// out.getFormat().setTextMode(Format.TextMode.PRESERVE);
Document jdom = KAFToDOM(kaf);
return out.outputString(jdom);
}
代码示例来源:origin: org.opentcs/opentcs-kernel-extension-rmi-services
docFormat.setLineSeparator(System.getProperty("line.separator"));
XMLOutputter outputter = new XMLOutputter(docFormat);
outputter.output(new Document(rootElement), writer);
代码示例来源:origin: com.github.ixa-ehu/kaflib-naf
/** Returns a string containing the XML content of a KAFDocument object. */
static String kafToStr(KAFDocument kaf) {
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setLineSeparator(LineSeparator.UNIX).setTextMode(Format.TextMode.TRIM_FULL_WHITE));
Document jdom = KAFToDOM(kaf);
return out.outputString(jdom);
}
代码示例来源:origin: io.wcm.tooling.commons/io.wcm.tooling.commons.crx-packmgr-helper
private void writeXmlWithExcludes(InputStream inputStream, OutputStream outputStream, Set<String> namespacePrefixes)
throws IOException, JDOMException {
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(inputStream);
Set<String> namespacePrefixesActuallyUsed = new HashSet<>();
applyXmlExcludes(doc.getRootElement(), "", namespacePrefixesActuallyUsed);
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()
.setIndent(" ")
.setLineSeparator(LineSeparator.UNIX));
outputter.setXMLOutputProcessor(new OneAttributePerLineXmlProcessor(namespacePrefixes, namespacePrefixesActuallyUsed));
outputter.output(doc, outputStream);
outputStream.flush();
}
代码示例来源:origin: movsim/movsim
/**
* Writes the internal xml after validation to file.
*
* @param localDoc
* the local doc
* @param outFilename
* the output file name
*/
public static void writeInternalXmlToFile(Document localDoc, String outFilename) {
final PrintWriter writer = FileUtils.getWriter(outFilename);
final XMLOutputter outputter = new XMLOutputter();
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setLineSeparator("\n");
outputter.setFormat(format);
outputter.setFormat(format);
try {
LOG.info(" write internal xml after validation to file \"" + outFilename + "\"");
outputter.output(localDoc, writer);
} catch (final Exception e) {
e.printStackTrace();
}
}
代码示例来源:origin: org.apache.jspwiki/jspwiki-main
/**
* {@inheritDoc}
*/
public String getString()
throws IOException
{
m_document.setContext( m_context );
CustomXMLOutputProcessor processor = new CustomXMLOutputProcessor();
XMLOutputter output = new XMLOutputter(processor);
StringWriter out = new StringWriter();
Format fmt = Format.getRawFormat();
fmt.setExpandEmptyElements( false );
fmt.setLineSeparator( LINEBREAK );
output.setFormat( fmt );
output.outputElementContent( m_document.getRootElement(), out );
String result = out.toString();
return result;
}
}
代码示例来源:origin: org.apache.jspwiki/jspwiki-main
/**
* {@inheritDoc}
*/
public String getString()
throws IOException
{
Element rootElement = m_document.getRootElement();
processChildren( rootElement );
m_document.setContext( m_context );
XMLOutputter output = new XMLOutputter();
StringWriter out = new StringWriter();
Format fmt = Format.getRawFormat();
fmt.setExpandEmptyElements( false );
fmt.setLineSeparator( LINEBREAK );
output.setFormat( fmt );
output.outputElementContent( m_document.getRootElement(), out );
return out.toString();
}
}
代码示例来源:origin: com.atlassian.maven.plugins/maven-jgitflow-plugin
format.setLineSeparator( ls );
XMLOutputter out = new XMLOutputter( format );
out.output( document.getRootElement(), writer );
代码示例来源:origin: org.codehaus.izpack/izpack-util
prettyFormatter.setLineSeparator(System.getProperty("line.separator"));
XMLOutputter sortie = new XMLOutputter(prettyFormatter);
代码示例来源:origin: org.apache.maven.plugins/maven-shade-plugin
/**
* Method write
*
* @param project
* @param stream
* @param document
* @deprecated
*/
public void write( Model project, Document document, OutputStream stream )
throws java.io.IOException
{
updateModel( project, "project", new Counter( 0 ), document.getRootElement() );
XMLOutputter outputter = new XMLOutputter();
Format format = Format.getPrettyFormat();
format.setIndent( " " ).setLineSeparator( System.getProperty( "line.separator" ) );
outputter.setFormat( format );
outputter.output( document, stream );
} // -- void write(Model, Document, OutputStream)
代码示例来源:origin: org.codehaus.izpack/izpack-util
prettyFormatter.setLineSeparator(System.getProperty("line.separator"));
XMLOutputter sortie = new XMLOutputter(prettyFormatter);
代码示例来源:origin: pwm-project/pwm
final Format format = Format.getRawFormat();
format.setTextMode( Format.TextMode.PRESERVE );
format.setLineSeparator( "" );
outputter.setFormat( format );
outputter.output( doc, writer );
代码示例来源:origin: Unidata/thredds
static public void prettyPrint() throws IOException {
org.jdom2.Document doc;
try {
SAXBuilder builder = new SAXBuilder();
doc = builder.build("C:/docs/bufr/wmo/Code-FlagTables-11-2007.xml");
Format pretty = Format.getPrettyFormat();
String sep = pretty.getLineSeparator();
String ind = pretty.getIndent();
String mine = "\r\n";
pretty.setLineSeparator(mine);
// wierd - cant pretty print ??!!
XMLOutputter fmt = new XMLOutputter(pretty);
Writer pw = new FileWriter("C:/docs/bufr/wmo/wordNice.txt");
fmt.output(doc, pw);
} catch (JDOMException e) {
throw new IOException(e.getMessage());
}
}
代码示例来源:origin: Vhati/Slipstream-Mod-Manager
format.setOmitDeclaration( false );
format.setIndent( "\t" );
format.setLineSeparator( LineSeparator.CRNL );
内容来源于网络,如有侵权,请联系作者删除!