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,11 +104,22 @@ 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)
{ {
target.close(); try
{
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,58 +155,65 @@ 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");
//
{ {
// //
long id = Long.parseLong(tag.attributes().getByLabel("id").getValue()); XMLTag tag = in.readStartTag("element");
String key = StringEscapeUtils.unescapeXml(in.readContentTag("key").getContent());
String subkey = StringEscapeUtils.unescapeXml(in.readNullableContentTag("subkey").getContent());
String value = StringEscapeUtils.unescapeXml(in.readContentTag("value").getContent());
int size = Integer.parseInt(tag.attributes().getByLabel("size").getValue());
String digest = in.readContentTag("digest").getContent();
DateTime creationDate = DateTime.parse(in.readContentTag("creation_date").getContent());
DateTime editionDate = DateTime.parse(in.readContentTag("edition_date").getContent());
DateTime archiveDate; //
String archiveDateValue = in.readNullableContentTag("archive_date").getContent();
if (archiveDateValue == null)
{ {
archiveDate = null; //
} long id = Long.parseLong(tag.attributes().getByLabel("id").getValue());
else String key = StringEscapeUtils.unescapeXml(in.readContentTag("key").getContent());
{ String subkey = StringEscapeUtils.unescapeXml(in.readNullableContentTag("subkey").getContent());
archiveDate = DateTime.parse(archiveDateValue); String value = StringEscapeUtils.unescapeXml(in.readContentTag("value").getContent());
int size = Integer.parseInt(tag.attributes().getByLabel("size").getValue());
String digest = in.readContentTag("digest").getContent();
DateTime creationDate = DateTime.parse(in.readContentTag("creation_date").getContent());
DateTime editionDate = DateTime.parse(in.readContentTag("edition_date").getContent());
DateTime archiveDate;
String archiveDateValue = in.readNullableContentTag("archive_date").getContent();
if (archiveDateValue == null)
{
archiveDate = null;
}
else
{
archiveDate = DateTime.parse(archiveDateValue);
}
//
result = new Element();
result.setId(id);
result.setKey(key);
result.setSubkey(subkey);
result.setValue(value);
result.setSize(size);
result.setDigest(digest);
result.setCreationDate(creationDate);
result.setEditionDate(editionDate);
result.setArchiveDate(archiveDate);
} }
// //
result = new Element(); in.readEndTag("element");
}
result.setId(id); catch (XMLStreamException exception)
result.setKey(key); {
result.setSubkey(subkey); throw new SikevaDBException("Problem reading element.", exception);
result.setValue(value); }
result.setSize(size); catch (XMLBadFormatException exception)
result.setDigest(digest); {
result.setCreationDate(creationDate); throw new SikevaDBException("Problem reading element.", exception);
result.setEditionDate(editionDate);
result.setArchiveDate(archiveDate);
} }
//
in.readEndTag("element");
// //
return result; return result;
@ -209,30 +226,40 @@ 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");
//
if (list.getType() != TagType.EMPTY)
{ {
while (in.hasNextStartTag("element")) XMLTag list = in.readListTag("elements");
{
//
Element element = readElement(in);
//
target.put(element);
}
// //
in.readEndTag("elements"); if (list.getType() != TagType.EMPTY)
{
//
while (in.hasNextStartTag("element"))
{
Element element = readElement(in);
target.put(element);
}
//
in.readEndTag("elements");
}
}
catch (XMLStreamException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
}
catch (XMLBadFormatException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
} }
} }
@ -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)
{ {