[Minor] cleaned up compiler warnings
This commit is contained in:
parent
3357787f32
commit
d3e77afef2
|
@ -52,6 +52,7 @@ import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
|
||||||
import ch.eitchnet.utils.exceptions.XmlException;
|
import ch.eitchnet.utils.exceptions.XmlException;
|
||||||
|
import ch.eitchnet.utils.helper.StringHelper;
|
||||||
import ch.eitchnet.utils.helper.XmlHelper;
|
import ch.eitchnet.utils.helper.XmlHelper;
|
||||||
import ch.eitchnet.xmlpers.api.DomParser;
|
import ch.eitchnet.xmlpers.api.DomParser;
|
||||||
import ch.eitchnet.xmlpers.api.DomUtil;
|
import ch.eitchnet.xmlpers.api.DomUtil;
|
||||||
|
@ -62,6 +63,9 @@ import ch.eitchnet.xmlpers.impl.XmlPersistenceStreamWriter;
|
||||||
|
|
||||||
public class FileIo {
|
public class FileIo {
|
||||||
|
|
||||||
|
public static final String DEFAULT_XML_VERSION = "1.0"; //$NON-NLS-1$
|
||||||
|
public static final String DEFAULT_ENCODING = "utf-8"; //$NON-NLS-1$
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(FileIo.class);
|
private static final Logger logger = LoggerFactory.getLogger(FileIo.class);
|
||||||
|
|
||||||
private final File path;
|
private final File path;
|
||||||
|
@ -79,11 +83,11 @@ public class FileIo {
|
||||||
writeSax(context);
|
writeSax(context);
|
||||||
break;
|
break;
|
||||||
case DEFAULT:
|
case DEFAULT:
|
||||||
logger.info("Using default XML IO Handler SAX");
|
logger.info("Using default XML IO Handler SAX"); //$NON-NLS-1$
|
||||||
writeSax(context);
|
writeSax(context);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
String msg = "The Xml IO Mode {0} is not supported!";
|
String msg = "The Xml IO Mode {0} is not supported!"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, context.getIoMode());
|
msg = MessageFormat.format(msg, context.getIoMode());
|
||||||
throw new UnsupportedOperationException(msg);
|
throw new UnsupportedOperationException(msg);
|
||||||
}
|
}
|
||||||
|
@ -98,11 +102,11 @@ public class FileIo {
|
||||||
readSax(context);
|
readSax(context);
|
||||||
break;
|
break;
|
||||||
case DEFAULT:
|
case DEFAULT:
|
||||||
logger.info("Using default XML IO Handler SAX");
|
logger.info("Using default XML IO Handler SAX"); //$NON-NLS-1$
|
||||||
readSax(context);
|
readSax(context);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
String msg = "The Xml IO Mode {0} is not supported!";
|
String msg = "The Xml IO Mode {0} is not supported!"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, context.getIoMode());
|
msg = MessageFormat.format(msg, context.getIoMode());
|
||||||
throw new UnsupportedOperationException(msg);
|
throw new UnsupportedOperationException(msg);
|
||||||
}
|
}
|
||||||
|
@ -112,38 +116,36 @@ public class FileIo {
|
||||||
|
|
||||||
XMLStreamWriter writer = null;
|
XMLStreamWriter writer = null;
|
||||||
try {
|
try {
|
||||||
XMLOutputFactory factory = XMLOutputFactory.newInstance();
|
try (FileWriter fileWriter = new FileWriter(this.path);) {
|
||||||
writer = factory.createXMLStreamWriter(new FileWriter(this.path));
|
|
||||||
writer = new IndentingXMLStreamWriter(writer);
|
|
||||||
|
|
||||||
// start document
|
XMLOutputFactory factory = XMLOutputFactory.newInstance();
|
||||||
writer.writeStartDocument("utf-8", "1.0");
|
writer = factory.createXMLStreamWriter(fileWriter);
|
||||||
|
writer = new IndentingXMLStreamWriter(writer);
|
||||||
|
|
||||||
// then delegate object writing to caller
|
// start document
|
||||||
XmlPersistenceStreamWriter xmlWriter = new XmlPersistenceStreamWriter(writer);
|
writer.writeStartDocument(DEFAULT_ENCODING, DEFAULT_XML_VERSION);
|
||||||
SaxParser<T> saxParser = context.getParserFactor().getSaxParser();
|
|
||||||
saxParser.setObject(context.getObject());
|
|
||||||
saxParser.write(xmlWriter);
|
|
||||||
|
|
||||||
// and now end
|
// then delegate object writing to caller
|
||||||
writer.writeEndDocument();
|
XmlPersistenceStreamWriter xmlWriter = new XmlPersistenceStreamWriter(writer);
|
||||||
writer.flush();
|
SaxParser<T> saxParser = context.getParserFactor().getSaxParser();
|
||||||
|
saxParser.setObject(context.getObject());
|
||||||
|
saxParser.write(xmlWriter);
|
||||||
|
|
||||||
|
// and now end
|
||||||
|
writer.writeEndDocument();
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
|
||||||
} catch (FactoryConfigurationError | XMLStreamException | IOException e) {
|
} catch (FactoryConfigurationError | XMLStreamException | IOException e) {
|
||||||
if (this.path.exists())
|
if (this.path.exists())
|
||||||
this.path.delete();
|
this.path.delete();
|
||||||
throw new XmlException("Writing to file failed due to internal error: " + e.getMessage(), e);
|
String msg = "Writing to file failed due to internal error: {0}"; //$NON-NLS-1$
|
||||||
} finally {
|
msg = MessageFormat.format(msg, e.getMessage());
|
||||||
if (writer != null) {
|
throw new XmlException(msg, e);
|
||||||
try {
|
|
||||||
writer.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Failed to close stream: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Wrote SAX to " + this.path.getAbsolutePath());
|
String msg = "Wrote SAX to {0}"; //$NON-NLS-1$
|
||||||
|
logger.info(MessageFormat.format(msg, this.path.getAbsolutePath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void readSax(PersistenceContext<T> context) {
|
private <T> void readSax(PersistenceContext<T> context) {
|
||||||
|
@ -160,10 +162,12 @@ public class FileIo {
|
||||||
|
|
||||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||||
|
|
||||||
throw new XmlPersistenceException("Parsing failed due to internal error: " + e.getMessage(), e);
|
String msg = "Parsing failed due to internal error: {0}"; //$NON-NLS-1$
|
||||||
|
throw new XmlPersistenceException(MessageFormat.format(msg, e.getMessage()), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("SAX parsed file " + this.path.getAbsolutePath());
|
String msg = "SAX parsed file {0}"; //$NON-NLS-1$
|
||||||
|
logger.info(MessageFormat.format(msg, this.path.getAbsolutePath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void writeDom(PersistenceContext<T> context) {
|
private <T> void writeDom(PersistenceContext<T> context) {
|
||||||
|
@ -178,19 +182,19 @@ public class FileIo {
|
||||||
encoding = XmlHelper.DEFAULT_ENCODING;
|
encoding = XmlHelper.DEFAULT_ENCODING;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lineSep.equals("\n")) {
|
if (!lineSep.equals(StringHelper.NEW_LINE)) {
|
||||||
logger.info("Overriding line separator to \\n");
|
logger.info("Overriding line separator to \\n"); //$NON-NLS-1$
|
||||||
System.setProperty(XmlHelper.PROP_LINE_SEPARATOR, "\n");
|
System.setProperty(XmlHelper.PROP_LINE_SEPARATOR, StringHelper.NEW_LINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up a transformer
|
// Set up a transformer
|
||||||
TransformerFactory transfac = TransformerFactory.newInstance();
|
TransformerFactory transfac = TransformerFactory.newInstance();
|
||||||
Transformer transformer = transfac.newTransformer();
|
Transformer transformer = transfac.newTransformer();
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$
|
||||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
|
||||||
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
|
||||||
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
|
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
|
||||||
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "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
|
||||||
|
@ -198,12 +202,15 @@ public class FileIo {
|
||||||
Source xmlSource = new DOMSource(document);
|
Source xmlSource = new DOMSource(document);
|
||||||
transformer.transform(xmlSource, result);
|
transformer.transform(xmlSource, result);
|
||||||
|
|
||||||
logger.info("Wrote DOM to " + this.path.getAbsolutePath());
|
String msg = MessageFormat.format("Wrote DOM to {0}", this.path.getAbsolutePath()); //$NON-NLS-1$
|
||||||
|
logger.info(msg);
|
||||||
|
|
||||||
} catch (TransformerFactoryConfigurationError | TransformerException e) {
|
} catch (TransformerFactoryConfigurationError | TransformerException e) {
|
||||||
if (this.path.exists())
|
if (this.path.exists())
|
||||||
this.path.delete();
|
this.path.delete();
|
||||||
throw new XmlException("Writing to file failed due to internal error: " + e.getMessage(), e);
|
String msg = "Writing to file failed due to internal error: {0}"; //$NON-NLS-1$
|
||||||
|
msg = MessageFormat.format(msg, e.getMessage());
|
||||||
|
throw new XmlException(msg, e);
|
||||||
} finally {
|
} finally {
|
||||||
System.setProperty(XmlHelper.PROP_LINE_SEPARATOR, lineSep);
|
System.setProperty(XmlHelper.PROP_LINE_SEPARATOR, lineSep);
|
||||||
}
|
}
|
||||||
|
@ -217,9 +224,13 @@ public class FileIo {
|
||||||
domParser.fromDom(document);
|
domParser.fromDom(document);
|
||||||
context.setObject(domParser.getObject());
|
context.setObject(domParser.getObject());
|
||||||
} catch (SAXException | IOException e) {
|
} catch (SAXException | IOException e) {
|
||||||
throw new XmlPersistenceException("Parsing failed due to internal error: " + e.getMessage(), e);
|
String msg = "Parsing failed due to internal error: {0}"; //$NON-NLS-1$
|
||||||
|
msg = MessageFormat.format(msg, e.getMessage());
|
||||||
|
throw new XmlPersistenceException(msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("DOM parsed file " + this.path.getAbsolutePath());
|
String msg = "DOM parsed file {0}"; //$NON-NLS-1$
|
||||||
|
msg = MessageFormat.format(msg, this.path.getAbsolutePath());
|
||||||
|
logger.info(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue