[Fix] Always use \n in XmlHelper.writeDocument() using RemoveCRFilterWriter

This commit is contained in:
Robert von Burg 2019-09-04 12:37:30 +02:00
parent a81808d821
commit e21be1a386
1 changed files with 14 additions and 6 deletions

View File

@ -29,6 +29,7 @@ import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.text.MessageFormat; import java.text.MessageFormat;
import li.strolch.utils.RemoveCRFilterWriter;
import li.strolch.utils.exceptions.XmlException; import li.strolch.utils.exceptions.XmlException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -54,7 +55,7 @@ public class XmlHelper {
/** /**
* UNIX_LINE_SEP = "\n" : mostly we want this line separator, instead of the windows version * UNIX_LINE_SEP = "\n" : mostly we want this line separator, instead of the windows version
*/ */
private static final String UNIX_LINE_SEP = "\n"; //$NON-NLS-1$ public static final String UNIX_LINE_SEP = "\n"; //$NON-NLS-1$
/** /**
* DEFAULT_ENCODING = "utf-8" : defines the default UTF-8 encoding expected of XML files * DEFAULT_ENCODING = "utf-8" : defines the default UTF-8 encoding expected of XML files
@ -209,7 +210,12 @@ public class XmlHelper {
* if something went wrong while creating the XML configuration, or writing the element * if something went wrong while creating the XML configuration, or writing the element
*/ */
public static void writeDocument(Document document, File file, String encoding) throws RuntimeException { public static void writeDocument(Document document, File file, String encoding) throws RuntimeException {
writeDocument(document, new StreamResult(file), encoding); try (FileOutputStream out = new FileOutputStream(file)) {
writeDocument(document, new StreamResult(new RemoveCRFilterWriter(new OutputStreamWriter(out, encoding))),
encoding);
} catch (IOException e) {
throw new XmlException("Failed to create Document: " + e.getLocalizedMessage(), e); //$NON-NLS-1$
}
} }
/** /**
@ -217,14 +223,15 @@ public class XmlHelper {
* *
* @param document * @param document
* the {@link Document} to write to the file system * the {@link Document} to write to the file system
* @param outputStream * @param out
* stream to write document to * stream to write document to
* *
* @throws RuntimeException * @throws RuntimeException
* if something went wrong while creating the XML configuration, or writing the element * if something went wrong while creating the XML configuration, or writing the element
*/ */
public static void writeDocument(Document document, OutputStream outputStream) throws RuntimeException { public static void writeDocument(Document document, OutputStream out) throws RuntimeException {
writeDocument(document, new StreamResult(outputStream), DEFAULT_ENCODING); writeDocument(document, new StreamResult(new RemoveCRFilterWriter(new OutputStreamWriter(out))),
DEFAULT_ENCODING);
} }
/** /**
@ -239,7 +246,8 @@ public class XmlHelper {
public static String writeToString(Document document) throws RuntimeException { public static String writeToString(Document document) throws RuntimeException {
try { try {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
writeDocument(document, new StreamResult(out), DEFAULT_ENCODING); writeDocument(document, new StreamResult(new RemoveCRFilterWriter(new OutputStreamWriter(out))),
DEFAULT_ENCODING);
return out.toString(DEFAULT_ENCODING); return out.toString(DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new XmlException("Failed to create Document: " + e.getLocalizedMessage(), e); //$NON-NLS-1$ throw new XmlException("Failed to create Document: " + e.getLocalizedMessage(), e); //$NON-NLS-1$