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;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.stream.XMLStreamException;
@ -45,17 +46,16 @@ public class XMLSikevaDB
private static final Logger logger = LoggerFactory.getLogger(XMLSikevaDB.class);
/**
* Saves a net in a file.
* Export.
*
* @param out
* the out
* @param source
* Source.
*
* @throws Exception
* the exception
* the source
* @throws SikevaDBException
* the sikeva DB 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";
@ -65,19 +65,18 @@ public class XMLSikevaDB
}
/**
* Saves a net in a file.
* Export.
*
* @param out
* the out
* @param source
* Source.
* the source
* @param fileName
* the file name
*
* @throws Exception
* the exception
* @throws SikevaDBException
* the sikeva DB 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)
{
@ -105,11 +104,22 @@ public class XMLSikevaDB
//
write(target, source);
}
catch (IOException exception)
{
throw new SikevaDBException("Problem writing element.", exception);
}
finally
{
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
* 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;
// try {
@ -145,58 +155,65 @@ public class XMLSikevaDB
* @param in
* the in
* @return the element
*
* @throws XMLStreamException
* the XML stream exception
* @throws XMLBadFormatException
* the XML bad format exception
* @throws SikevaDBException
*/
public static Element readElement(final XMLReader in) throws XMLStreamException, XMLBadFormatException
public static Element readElement(final XMLReader in) throws SikevaDBException
{
Element result;
//
XMLTag tag = in.readStartTag("element");
//
try
{
//
long id = Long.parseLong(tag.attributes().getByLabel("id").getValue());
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());
XMLTag tag = in.readStartTag("element");
DateTime archiveDate;
String archiveDateValue = in.readNullableContentTag("archive_date").getContent();
if (archiveDateValue == null)
//
{
archiveDate = null;
}
else
{
archiveDate = DateTime.parse(archiveDateValue);
//
long id = Long.parseLong(tag.attributes().getByLabel("id").getValue());
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;
}
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();
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);
in.readEndTag("element");
}
catch (XMLStreamException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
}
catch (XMLBadFormatException exception)
{
throw new SikevaDBException("Problem reading element.", exception);
}
//
in.readEndTag("element");
//
return result;
@ -209,30 +226,40 @@ public class XMLSikevaDB
* the target
* @param in
* the in
* @throws XMLBadFormatException
* @throws XMLStreamException
* @throws SikevaDBException
*
* @throws 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
{
//
XMLTag list = in.readListTag("elements");
//
if (list.getType() != TagType.EMPTY)
try
{
while (in.hasNextStartTag("element"))
{
//
Element element = readElement(in);
//
target.put(element);
}
XMLTag list = in.readListTag("elements");
//
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
* @param source
* the source
* @throws SikevaDBException
*
* @throws 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)
{