Added an arg to sloppyPrint() so XML escapes can be toggled

This commit is contained in:
Vhati 2017-12-31 05:53:01 -05:00
parent 30840f0480
commit fa8ac1f6d1
2 changed files with 18 additions and 11 deletions

View file

@ -327,7 +327,7 @@ public class ModUtilities {
ByteArrayOutputStream tmpData = new ByteArrayOutputStream(); ByteArrayOutputStream tmpData = new ByteArrayOutputStream();
Writer writer = new EOLWriter( new OutputStreamWriter( tmpData, encoder ), "\r\n" ); Writer writer = new EOLWriter( new OutputStreamWriter( tmpData, encoder ), "\r\n" );
SloppyXMLOutputProcessor.sloppyPrint( mergedDoc, writer, encoding ); SloppyXMLOutputProcessor.sloppyPrint( mergedDoc, writer, encoding, false );
writer.flush(); writer.flush();
InputStream result = new ByteArrayInputStream( tmpData.toByteArray() ); InputStream result = new ByteArrayInputStream( tmpData.toByteArray() );
@ -369,7 +369,7 @@ public class ModUtilities {
ByteArrayOutputStream tmpData = new ByteArrayOutputStream(); ByteArrayOutputStream tmpData = new ByteArrayOutputStream();
Writer writer = new EOLWriter( new OutputStreamWriter( tmpData, encoder ), "\r\n" ); Writer writer = new EOLWriter( new OutputStreamWriter( tmpData, encoder ), "\r\n" );
SloppyXMLOutputProcessor.sloppyPrint( doc, writer, encoding ); SloppyXMLOutputProcessor.sloppyPrint( doc, writer, encoding, false );
writer.flush(); writer.flush();
InputStream result = new ByteArrayInputStream( tmpData.toByteArray() ); InputStream result = new ByteArrayInputStream( tmpData.toByteArray() );

View file

@ -154,12 +154,17 @@ public class SloppyXMLOutputProcessor extends AbstractXMLOutputProcessor {
* is encoding bytes to match. If encoding is null, the default * is encoding bytes to match. If encoding is null, the default
* is "UTF-8". * is "UTF-8".
* *
* There will be no XML character entity escaping, which would have allowed * If XML character entity escaping is allowed, otherwise unmappable
* otherwise unmappable characters to print without errors. * 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!? * LineEndings will be CR-LF. Except for comments!?
*
* @param doc
* @param writer
* @param disableEscaping
*/ */
public static void sloppyPrint( Document doc, Writer writer, String encoding ) throws IOException { public static void sloppyPrint( Document doc, Writer writer, String encoding, boolean allowEscaping ) throws IOException {
Format format = Format.getPrettyFormat(); Format format = Format.getPrettyFormat();
format.setTextMode( Format.TextMode.PRESERVE ); // Permit leading/trailing space. format.setTextMode( Format.TextMode.PRESERVE ); // Permit leading/trailing space.
format.setExpandEmptyElements( false ); format.setExpandEmptyElements( false );
@ -171,12 +176,14 @@ public class SloppyXMLOutputProcessor extends AbstractXMLOutputProcessor {
format.setEncoding( encoding ); format.setEncoding( encoding );
} }
format.setEscapeStrategy(new EscapeStrategy() { if ( !allowEscaping ) {
@Override format.setEscapeStrategy(new EscapeStrategy() {
public boolean shouldEscape( char ch ) { @Override
return false; public boolean shouldEscape( char ch ) {
} return false;
}); }
});
}
XMLOutputter outputter = new XMLOutputter( format, new SloppyXMLOutputProcessor() ); XMLOutputter outputter = new XMLOutputter( format, new SloppyXMLOutputProcessor() );
outputter.output( doc, writer ); outputter.output( doc, writer );