Refactored exception in XMLSikevaDB.

This commit is contained in:
Christian P. MOMON 2018-02-28 17:17:41 +01:00
parent e939664d89
commit 49d906a0b8

View file

@ -19,6 +19,7 @@
package fr.devinsy.sikevadb.core; package fr.devinsy.sikevadb.core;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
@ -45,17 +46,16 @@ public class XMLSikevaDB
private static final Logger logger = LoggerFactory.getLogger(XMLSikevaDB.class); private static final Logger logger = LoggerFactory.getLogger(XMLSikevaDB.class);
/** /**
* Saves a net in a file. * Export.
* *
* @param out * @param out
* the out * the out
* @param source * @param source
* Source. * the source
* * @throws SikevaDBException
* @throws Exception * the sikeva DB exception
* the exception
*/ */
public static void export(final OutputStream out, final SikevaDB source) throws Exception public static void export(final OutputStream out, final SikevaDB source) throws SikevaDBException
{ {
// //
String fileName = "sikevadb-" + DateTime.now().toString("yyyy-MM-dd-HH'h'mm'mn'ss's'") + ".xml.zip"; String fileName = "sikevadb-" + DateTime.now().toString("yyyy-MM-dd-HH'h'mm'mn'ss's'") + ".xml.zip";
@ -65,19 +65,18 @@ public class XMLSikevaDB
} }
/** /**
* Saves a net in a file. * Export.
* *
* @param out * @param out
* the out * the out
* @param source * @param source
* Source. * the source
* @param fileName * @param fileName
* the file name * the file name
* * @throws SikevaDBException
* @throws Exception * the sikeva DB exception
* the exception
*/ */
public static void export(final OutputStream out, final SikevaDB source, final String fileName) throws Exception public static void export(final OutputStream out, final SikevaDB source, final String fileName) throws SikevaDBException
{ {
if (out == null) if (out == null)
{ {
@ -105,12 +104,23 @@ public class XMLSikevaDB
// //
write(target, source); write(target, source);
} }
catch (IOException exception)
{
throw new SikevaDBException("Problem writing element.", exception);
}
finally finally
{ {
if (target != null) if (target != null)
{
try
{ {
target.close(); target.close();
} }
catch (IOException exception)
{
logger.warn("Problem closing stream.", exception);
}
}
} }
} }
} }
@ -126,7 +136,7 @@ public class XMLSikevaDB
* @throws Exception * @throws Exception
* the exception * the exception
*/ */
public static void importData(final SikevaDB database, final File file) throws Exception public static void importData(final SikevaDB database, final File file)
{ {
// XMLReader in = null; // XMLReader in = null;
// try { // try {
@ -145,16 +155,14 @@ public class XMLSikevaDB
* @param in * @param in
* the in * the in
* @return the element * @return the element
* * @throws SikevaDBException
* @throws XMLStreamException
* the XML stream exception
* @throws XMLBadFormatException
* the XML bad format exception
*/ */
public static Element readElement(final XMLReader in) throws XMLStreamException, XMLBadFormatException public static Element readElement(final XMLReader in) throws SikevaDBException
{ {
Element result; Element result;
try
{
// //
XMLTag tag = in.readStartTag("element"); XMLTag tag = in.readStartTag("element");
@ -197,6 +205,15 @@ public class XMLSikevaDB
// //
in.readEndTag("element"); in.readEndTag("element");
}
catch (XMLStreamException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
}
catch (XMLBadFormatException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
}
// //
return result; return result;
@ -209,25 +226,26 @@ public class XMLSikevaDB
* the target * the target
* @param in * @param in
* the in * the in
* @throws XMLBadFormatException
* @throws XMLStreamException
* @throws SikevaDBException
* *
* @throws Exception * @throws Exception
* the exception * the exception
*/ */
public static void readElements(final SikevaDB target, final XMLReader in) throws Exception public static void readElements(final SikevaDB target, final XMLReader in) throws SikevaDBException
{
try
{ {
//
XMLTag list = in.readListTag("elements"); XMLTag list = in.readListTag("elements");
// //
if (list.getType() != TagType.EMPTY) if (list.getType() != TagType.EMPTY)
{ {
//
while (in.hasNextStartTag("element")) while (in.hasNextStartTag("element"))
{ {
//
Element element = readElement(in); Element element = readElement(in);
//
target.put(element); target.put(element);
} }
@ -235,6 +253,15 @@ public class XMLSikevaDB
in.readEndTag("elements"); in.readEndTag("elements");
} }
} }
catch (XMLStreamException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
}
catch (XMLBadFormatException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
}
}
/** /**
* Write. * Write.
@ -300,11 +327,12 @@ public class XMLSikevaDB
* the out * the out
* @param source * @param source
* the source * the source
* @throws SikevaDBException
* *
* @throws Exception * @throws Exception
* the exception * the exception
*/ */
public static void write(final XMLWriter out, final SikevaDB source) throws Exception public static void write(final XMLWriter out, final SikevaDB source) throws SikevaDBException
{ {
if (out == null) if (out == null)
{ {