diff --git a/.classpath b/.classpath index aef547d..1dbc32b 100644 --- a/.classpath +++ b/.classpath @@ -20,7 +20,7 @@ - + diff --git a/bin/fr/devinsy/sikevadb/Element.class b/bin/fr/devinsy/sikevadb/Element.class index d1c81db..c1f8954 100644 Binary files a/bin/fr/devinsy/sikevadb/Element.class and b/bin/fr/devinsy/sikevadb/Element.class differ diff --git a/lib/devinsy-utils-0.2.4-sources.zip b/lib/devinsy-utils-0.2.6-sources.zip similarity index 79% rename from lib/devinsy-utils-0.2.4-sources.zip rename to lib/devinsy-utils-0.2.6-sources.zip index ac88eb7..639aa1b 100644 Binary files a/lib/devinsy-utils-0.2.4-sources.zip and b/lib/devinsy-utils-0.2.6-sources.zip differ diff --git a/lib/devinsy-utils-0.2.4.jar b/lib/devinsy-utils-0.2.6.jar similarity index 73% rename from lib/devinsy-utils-0.2.4.jar rename to lib/devinsy-utils-0.2.6.jar index 802aefa..18a4d59 100644 Binary files a/lib/devinsy-utils-0.2.4.jar and b/lib/devinsy-utils-0.2.6.jar differ diff --git a/src/fr/devinsy/sikevadb/XMLSikevaDB.java b/src/fr/devinsy/sikevadb/XMLSikevaDB.java new file mode 100644 index 0000000..7c4d201 --- /dev/null +++ b/src/fr/devinsy/sikevadb/XMLSikevaDB.java @@ -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"); + } + } +}