Update library. Add XML export.

This commit is contained in:
Christian P. MOMON 2013-09-11 14:37:30 +02:00
parent 9097faf0e2
commit a2e7f31068
5 changed files with 229 additions and 1 deletions

View file

@ -20,7 +20,7 @@
<attribute name="owner.project.facets" value="java"/> <attribute name="owner.project.facets" value="java"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.4.jar"/>
<classpathentry kind="lib" path="lib/mysql-jdbc-5.0.8.jar"/> <classpathentry kind="lib" path="lib/mysql-jdbc-5.0.8.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.6.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View file

@ -0,0 +1,228 @@
package fr.devinsy.sikevadb;
import java.io.File;
import java.io.OutputStream;
import java.util.Date;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.util.xml.XMLBadFormatException;
import fr.devinsy.util.xml.XMLReader;
import fr.devinsy.util.xml.XMLTag;
import fr.devinsy.util.xml.XMLTag.TagType;
import fr.devinsy.util.xml.XMLWriter;
/**
* This class represents a AccountManager File reader and writer.
*
* @author TIP
*/
public class XMLSikevaDB {
static private final Logger logger = LoggerFactory.getLogger(XMLSikevaDB.class);
/**
* Saves a net in a file.
*
* @param file
* Target.
* @param source
* Source.
*
*/
public static void export(final OutputStream out, final SikevaDB source, final String fileName) throws Exception {
if (out == null) {
throw new NullPointerException("out is null.");
} else if (source == null) {
throw new NullPointerException("source is null.");
} else {
//
XMLWriter target = new XMLWriter(out);// new XMLZipWriter(out,
// fileName,
// "Generated by SikevaDB.");
//
target.writeXMLHeader();
//
write(target, source);
}
}
/**
*
* @param file
* @return
*/
static public void importData(final SikevaDB database, final File file) throws Exception {
// XMLReader in = null;
// try {
// in = new XMLZipReader(file);
// result = readAccountManager(in);
// } finally {
// if (in != null) {
// in.close();
// }
// }
}
/**
*
* @param target
* @param in
* @throws XMLBadFormatException
* @throws XMLStreamException
* @throws Exception
*/
static public Element readElement(final XMLReader in) throws XMLStreamException, XMLBadFormatException {
Element result;
//
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();
Date creationDate = new Date(Long.parseLong(in.readContentTag("creation_date").getContent()));
Date editionDate = new Date(Long.parseLong(in.readContentTag("edition_date").getContent()));
Date archiveDate;
String archiveDateValue = in.readNullableContentTag("archive_date").getContent();
if (archiveDateValue == null) {
archiveDate = null;
} else {
archiveDate = new Date(Long.parseLong(archiveDateValue));
}
//
result = new Element();
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");
//
return result;
}
/**
*
* @param target
* @param in
* @throws Exception
*/
static public void readElements(final SikevaDB target, final XMLReader in) throws Exception {
//
XMLTag list = in.readListTag("elements");
//
if (list.getType() != TagType.EMPTY) {
while (in.hasNextStartTag("element")) {
//
Element element = readElement(in);
//
target.put(element);
}
//
in.readEndTag("elements");
}
}
/**
*
* @param source
* @return
*/
static public void write(final XMLWriter out, final Element source) {
if (out == null) {
throw new NullPointerException("out is null.");
} else if (source == null) {
throw new NullPointerException("element is null.");
} else {
//
out.writeStartTag("element");
//
{
out.writeTag("key", StringEscapeUtils.escapeXml(source.getKey()));
if (source.getSubkey() == null) {
out.writeTag("subkey", null);
} else {
out.writeTag("subkey", StringEscapeUtils.escapeXml(source.getSubkey()));
}
out.writeTag("value", StringEscapeUtils.escapeXml(source.getValue()));
out.writeTag("size", String.valueOf(source.getSize()));
out.writeTag("digest", source.getDigest());
out.writeTag("creation_date", String.valueOf(source.getCreationDate().getTime()));
out.writeTag("edition_date", String.valueOf(source.getEditionDate().getTime()));
if (source.getArchiveDate() == null) {
out.writeTag("archive_date", null);
} else {
out.writeTag("archive_date", String.valueOf(source.getArchiveDate().getTime()));
}
}
//
out.writeEndTag("element");
}
}
/**
*
* @param out
* @param source
* @throws Exception
*/
static public void write(final XMLWriter out, final SikevaDB source) throws Exception {
if (out == null) {
throw new NullPointerException("out is null.");
} else if (source == null) {
out.writeEmptyTag("elements");
} else {
//
out.writeStartTag("elements");
for (String key : source.getAllKeys()) {
//
Element element = source.getElement(key);
if (element != null) {
write(out, element);
}
//
for (String subkey : source.getSubkeys(key)) {
Element subElement = source.getElement(key, subkey);
write(out, subElement);
}
}
out.writeEndTag("elements");
}
}
}