[Minor] Don't log writing to document in XmlHelper

This commit is contained in:
Robert von Burg 2018-09-28 13:28:29 +02:00
parent 30536cb11c
commit d46bf18fa2
1 changed files with 50 additions and 55 deletions

View File

@ -1,12 +1,12 @@
/* /*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch> * Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -38,7 +38,7 @@ import org.xml.sax.helpers.DefaultHandler;
/** /**
* Helper class for performing XML based tasks * Helper class for performing XML based tasks
* *
* @author Robert von Burg &lt;eitch@eitchnet.ch&gt; * @author Robert von Burg &lt;eitch@eitchnet.ch&gt;
*/ */
public class XmlHelper { public class XmlHelper {
@ -51,7 +51,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
*/ */
public static final String UNIX_LINE_SEP = "\n"; //$NON-NLS-1$ private 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
@ -62,9 +62,9 @@ public class XmlHelper {
/** /**
* Parses an XML file on the file system and returns the resulting {@link Document} object * Parses an XML file on the file system and returns the resulting {@link Document} object
* *
* @param xmlFile * @param xmlFile
* the {@link File} which has the path to the XML file to read * the {@link File} which has the path to the XML file to read
*/ */
public static void parseDocument(File xmlFile, DefaultHandler xmlHandler) { public static void parseDocument(File xmlFile, DefaultHandler xmlHandler) {
@ -83,9 +83,9 @@ public class XmlHelper {
/** /**
* Parses an XML file on the file system and returns the resulting {@link Document} object * Parses an XML file on the file system and returns the resulting {@link Document} object
* *
* @param xmlFileInputStream * @param xmlFileInputStream
* the XML {@link InputStream} which is to be parsed * the XML {@link InputStream} which is to be parsed
*/ */
public static void parseDocument(InputStream xmlFileInputStream, DefaultHandler xmlHandler) { public static void parseDocument(InputStream xmlFileInputStream, DefaultHandler xmlHandler) {
@ -104,12 +104,12 @@ public class XmlHelper {
throw new XmlException("The XML stream not be read: " + e.getMessage(), e); //$NON-NLS-1$ throw new XmlException("The XML stream not be read: " + e.getMessage(), e); //$NON-NLS-1$
} }
} }
/** /**
* Parses an XML file on the file system and returns the resulting {@link Document} object * Parses an XML file on the file system and returns the resulting {@link Document} object
* *
* @param xmlInputSource * @param xmlInputSource
* the XML {@link InputSource} which is to be parsed * the XML {@link InputSource} which is to be parsed
*/ */
public static void parseDocument(InputSource xmlInputSource, DefaultHandler xmlHandler) { public static void parseDocument(InputSource xmlInputSource, DefaultHandler xmlHandler) {
@ -131,14 +131,14 @@ public class XmlHelper {
/** /**
* Writes an {@link Element} to an XML file on the file system * Writes an {@link Element} to an XML file on the file system
* *
* @param rootElement * @param rootElement
* the {@link Element} to write to the file system * the {@link Element} to write to the file system
* @param file * @param file
* the {@link File} describing the path on the file system where the XML file should be written to * the {@link File} describing the path on the file system where the XML file should be written 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 writeElement(Element rootElement, File file) throws RuntimeException { public static void writeElement(Element rootElement, File file) throws RuntimeException {
Document document = createDocument(); Document document = createDocument();
@ -148,14 +148,14 @@ public class XmlHelper {
/** /**
* Writes a {@link Document} to an XML file on the file system * Writes a {@link Document} to an XML file on the file system
* *
* @param document * @param document
* the {@link Document} to write to the file system * the {@link Document} to write to the file system
* @param file * @param file
* the {@link File} describing the path on the file system where the XML file should be written to * the {@link File} describing the path on the file system where the XML file should be written 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, File file) throws RuntimeException { public static void writeDocument(Document document, File file) throws RuntimeException {
writeDocument(document, file, DEFAULT_ENCODING); writeDocument(document, file, DEFAULT_ENCODING);
@ -163,34 +163,31 @@ public class XmlHelper {
/** /**
* Writes a {@link Document} to an XML file on the file system * Writes a {@link Document} to an XML file on the file system
* *
* @param document * @param document
* the {@link Document} to write to the file system * the {@link Document} to write to the file system
* @param file * @param file
* the {@link File} describing the path on the file system where the XML file should be written to * the {@link File} describing the path on the file system where the XML file should be written to
* @param encoding * @param encoding
* encoding to use to write the file * encoding to use to write the file
* *
* @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, File file, String encoding) throws RuntimeException { public static void writeDocument(Document document, File file, String encoding) throws RuntimeException {
String msg = "Exporting document element {0} to {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, document.getNodeName(), file.getAbsolutePath());
XmlHelper.logger.info(msg);
writeDocument(document, new StreamResult(file), encoding); writeDocument(document, new StreamResult(file), encoding);
} }
/** /**
* Writes a {@link Document} to an XML file on the file system * Writes a {@link Document} to an XML file on the file system
* *
* @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 outputStream
* 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 outputStream) throws RuntimeException {
writeDocument(document, new StreamResult(outputStream), DEFAULT_ENCODING); writeDocument(document, new StreamResult(outputStream), DEFAULT_ENCODING);
@ -198,12 +195,12 @@ public class XmlHelper {
/** /**
* Writes a {@link Document} to an XML file on the file system * Writes a {@link Document} to an XML file on the file system
* *
* @param document * @param document
* the {@link Document} to write to the file system * the {@link Document} to write to the file system
* *
* @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 String writeToString(Document document) throws RuntimeException { public static String writeToString(Document document) throws RuntimeException {
try { try {
@ -217,16 +214,16 @@ public class XmlHelper {
/** /**
* Writes a {@link Document} to an XML file on the file system * Writes a {@link Document} to an XML file on the file system
* *
* @param document * @param document
* the {@link Document} to write to the file system * the {@link Document} to write to the file system
* @param streamResult * @param streamResult
* the destination * the destination
* @param encoding * @param encoding
* encoding to use to write the file * encoding to use to write the file
* *
* @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, StreamResult streamResult, String encoding) public static void writeDocument(Document document, StreamResult streamResult, String encoding)
throws RuntimeException { throws RuntimeException {
@ -250,7 +247,8 @@ public class XmlHelper {
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.ENCODING, docEncoding); transformer.setOutputProperty(OutputKeys.ENCODING, docEncoding);
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$ transformer
.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$
// transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\t"); // transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\t");
// Transform to file // Transform to file
@ -269,24 +267,21 @@ public class XmlHelper {
/** /**
* Returns a new document instance * Returns a new document instance
* *
* @return a new document instance * @return a new document instance
* *
* @throws RuntimeException * @throws RuntimeException
* if something went wrong while creating the XML configuration * if something went wrong while creating the XML configuration
*/ */
public static Document createDocument() throws RuntimeException { public static Document createDocument() throws RuntimeException {
try { try {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document document = docBuilder.newDocument();
return document; return docBuilder.newDocument();
} catch (DOMException e) { } catch (DOMException | ParserConfigurationException e) {
throw new XmlException("Failed to create Document: " + e.getLocalizedMessage(), e); //$NON-NLS-1$
} catch (ParserConfigurationException 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$
} }
} }