本文整理汇总了Java中org.jdom2.output.Format.setOmitDeclaration方法的典型用法代码示例。如果您正苦于以下问题:Java Format.setOmitDeclaration方法的具体用法?Java Format.setOmitDeclaration怎么用?Java Format.setOmitDeclaration使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jdom2.output.Format
的用法示例。
在下文中一共展示了Format.setOmitDeclaration方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sloppyPrint
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Creates an outputter and writes an XML tree.
*
* The encoding argument here only sets an attribute in the
* XML declaration. It's up to the caller to ensure the writer
* is encoding bytes to match. If encoding is null, the default
* is "UTF-8".
*
* If XML character entity escaping is allowed, otherwise unmappable
* characters may be written without errors. If disabled, an
* UnmappableCharacterException will make their presence obvious and fatal.
*
* LineEndings will be CR-LF. Except for comments!?
*
* @param doc
* @param writer
* @param disableEscaping
*/
public static void sloppyPrint( Document doc, Writer writer, String encoding, boolean allowEscaping ) throws IOException {
Format format = Format.getPrettyFormat();
format.setTextMode( Format.TextMode.PRESERVE ); // Permit leading/trailing space.
format.setExpandEmptyElements( false );
format.setOmitDeclaration( false );
format.setIndent( "\t" );
format.setLineSeparator( LineSeparator.CRNL );
if ( encoding != null ) {
format.setEncoding( encoding );
}
if ( !allowEscaping ) {
format.setEscapeStrategy(new EscapeStrategy() {
@Override
public boolean shouldEscape( char ch ) {
return false;
}
});
}
XMLOutputter outputter = new XMLOutputter( format, new SloppyXMLOutputProcessor() );
outputter.output( doc, writer );
}
示例2: saveDocument
import org.jdom2.output.Format; //导入方法依赖的package包/类
private void saveDocument(String path, String filename, XML mode, Document document) {
if (path != null) {
filename = fileJoin(path, filename);
}
if (dryRun) {
stderr("Writing %s\n", filename);
return;
}
Format prettyFormat = Format.getPrettyFormat();
prettyFormat.setOmitDeclaration(mode == XML.NO_DECLARATION);
XMLOutputter outputter = new XMLOutputter(prettyFormat);
try (Writer writer = new FileWriter(filename)) {
outputter.output(document, writer);
} catch (IOException e) {
stderr("%s exception writing %s\n", e.getClass().getSimpleName(), filename);
}
}
示例3: saveToTempFile
import org.jdom2.output.Format; //导入方法依赖的package包/类
private File saveToTempFile(Element diff) throws IOException {
Format format = Format.getRawFormat();
format.setOmitDeclaration(true);
XMLOutputter outputter = new XMLOutputter(format);
File temp = File.createTempFile("xml-patch-diff-", ".xml");
try (FileOutputStream os = new FileOutputStream(temp)) {
outputter.output(diff, os);
}
Log.debug("created temp file: " + temp.getAbsolutePath());
return temp;
}
示例4: getTransformedXml
import org.jdom2.output.Format; //导入方法依赖的package包/类
public static String getTransformedXml(String inputXml, String stylesheetXml, String xmlFormat, boolean omitXmlDeclaration) {
StringWriter writer = new StringWriter();
SAXBuilder builder = new SAXBuilder();
builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
builder.setFeature("http://xml.org/sax/features/validation", false);
try {
Document inputDoc = builder.build(new StringReader(inputXml));
StringReader reader = new StringReader(stylesheetXml);
XSLTransformer transformer = new XSLTransformer(reader);
Document outputDoc = transformer.transform(inputDoc);
XMLOutputter xmlOutput = new XMLOutputter();
Format format = null;
if (xmlFormat.equals(COMPACT_FORMAT)) {
format = Format.getCompactFormat();
} else if (xmlFormat.equals(RAW_FORMAT)) {
format = Format.getRawFormat();
} else {
format = Format.getPrettyFormat();
}
format.setOmitDeclaration(omitXmlDeclaration);
xmlOutput.setFormat(format);
xmlOutput.output(outputDoc, writer);
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
return writer.toString();
}
示例5: sloppyPrint
import org.jdom2.output.Format; //导入方法依赖的package包/类
/**
* Creates an outputter and writes an XML tree.
*
* The encoding argument here only sets an attribute in the
* XML declaration. It's up to the caller to ensure the writer
* is encoding bytes to match. If encoding is null, the default
* is "UTF-8".
*
* LineEndings will be CR-LF. Except for comments!?
*/
public static void sloppyPrint( Document doc, Writer writer, String encoding ) throws IOException
{
Format format = Format.getPrettyFormat();
format.setExpandEmptyElements( false );
format.setOmitDeclaration( false );
format.setIndent( "\t" );
format.setLineSeparator( LineSeparator.CRNL );
if ( encoding != null ) format.setEncoding( encoding );
XMLOutputter outputter = new XMLOutputter( format, new SloppyXMLOutputProcessor() );
outputter.output( doc, writer );
}
示例6: patch
import org.jdom2.output.Format; //导入方法依赖的package包/类
private static Set<String> patch(List<Element> diffs, File srcdir, File destdir, File tempdir) throws Exception {
Set<String> outputFilePaths = new HashSet<>();
for (Element diff : diffs) {
if (diff.getAttribute("file") == null) {
throw new IllegalArgumentException("diff element missing 'file' attribute");
}
String srcfilePath = diff.getAttributeValue("file");
File fileToPatch = new File(srcfilePath);
if (fileToPatch.isAbsolute()) {
throw new IllegalArgumentException("not a relative path: " + srcfilePath);
}
outputFilePaths.add(srcfilePath);
File alreadyInTempDir = new File(tempdir, srcfilePath);
fileToPatch = alreadyInTempDir.exists() ? alreadyInTempDir : new File(srcdir, srcfilePath);
Log.info("patching " + srcfilePath + " [from " + fileToPatch.getAbsolutePath() + "]");
diff.removeAttribute("file");
Format format = Format.getRawFormat();
format.setOmitDeclaration(true);
XMLOutputter outputter = new XMLOutputter(format);
String s = outputter.outputString(diff);
InputStream diffStream = new ByteArrayInputStream(s.getBytes("UTF-8"));
InputStream inputStream = new FileInputStream(fileToPatch);
File outputFile = File.createTempFile("xmlpatch", ".xml");
OutputStream outputStream = new FileOutputStream(outputFile);
Patcher.patch(inputStream, diffStream, outputStream);
inputStream.close();
outputStream.close();
File outputTemp = new File(tempdir, srcfilePath);
File tempParentDir = outputTemp.getParentFile();
if (!tempParentDir.exists() && !tempParentDir.mkdirs()) {
throw new IOException("failed to create directory: " + tempParentDir.getAbsolutePath());
}
IoHelper.move(outputFile, outputTemp);
}
return outputFilePaths;
}