Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added serialization for Collection

  • Loading branch information...
commit bb91ac91714170453f47b54ad6b2ff395a91dd91 1 parent 6c5b97e
Tom tomhaggie authored
97 src/com/indicee/api/xml/CollectionXml.java
View
@@ -0,0 +1,97 @@
+package com.indicee.api.xml;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.indicee.api.objects.collection.Collection;
+import com.indicee.api.util.XmlHelper;
+
+public class CollectionXml {
+ public static final String TAG = "collection";
+
+ private static enum Tag {
+ id, name, qname, description, owner_id, datasets, dataset, deleted
+ }
+
+ private static final Map<String,Tag> tagLookup = ToXml.createTagLookup(Tag.values());
+
+ public static final ToXml<Collection> toXml = new ToXml<Collection>() {
+ @Override
+ public Element call(Document document, Collection collection) {
+ Element element = createElement(document, TAG);
+ append(element, Tag.id, collection.getId());
+ append(element, Tag.name, collection.getName());
+ append(element, Tag.qname, collection.getQname());
+ append(element, Tag.description, collection.getDescription());
+ append(element, Tag.owner_id, collection.getOwnerId());
+
+ List<String> datasets = collection.getDataSetQnames();
+ if (datasets != null) {
+ Element datasetsElement = createElement(document, Tag.datasets);
+ element.appendChild(datasetsElement);
+ for (String dataset : datasets) {
+ append(datasetsElement, Tag.dataset, dataset);
+ }
+ }
+
+ append(element, Tag.deleted, collection.getDeleted());
+
+ return element;
+ }
+ };
+
+ public static final FromXml<Collection> fromXml = new FromXml<Collection>() {
+ @Override
+ public Collection call(Element element) {
+ Collection collection = new Collection();
+
+ for (Element child : XmlHelper.childElements(element)) {
+ String tagName = child.getTagName();
+ Tag t = tagLookup.get(tagName);
+ if (t != null) {
+ switch (t) {
+ case id:
+ collection.setId(getLong(child));
+ break;
+ case name:
+ collection.setName(getText(child));
+ break;
+ case qname:
+ collection.setQname(getText(child));
+ break;
+ case description:
+ collection.setDescription(getText(child));
+ break;
+ case owner_id:
+ collection.setOwnerId(getLong(child));
+ break;
+ case datasets:
+ collection.setDataSetQnames(getDatasets(child));
+ break;
+ case deleted:
+ collection.setDeleted(getBoolean(child));
+ break;
+ }
+ }
+ }
+
+ return collection;
+ }
+ };
+
+ private static List<String> getDatasets(Element datasetsElement) {
+ List<Element> datasetElements = XmlHelper.childElements(datasetsElement, Tag.dataset.toString());
+ int nDatasets = datasetElements.size();
+ List<String> datasets = new ArrayList<String>(nDatasets);
+ for (int i=0; i<nDatasets; ++i) {
+ Element datasetElement = datasetElements.get(i);
+ String dataset = datasetElement.getTextContent();
+ datasets.add(dataset);
+ }
+ return datasets;
+ }
+}
12 src/com/indicee/api/xml/ToXml.java
View
@@ -42,6 +42,18 @@ protected static final Element append(Element element, Object tag, String value)
}
return XmlHelper.appendTextElement(element, String.valueOf(tag), value);
}
+
+ protected static final Element append(Element element, Object tag, boolean value) {
+ return append(element, tag, Boolean.toString(value));
+ }
+
+ protected static final Element append(Element element, Object tag, Boolean value) {
+ if (value == null) {
+ return null;
+ }
+ return XmlHelper.appendTextElement(element, String.valueOf(tag), String.valueOf(value));
+ }
+
protected static final Element append(Element element, Object tag, long value) {
return append(element, tag, Long.toString(value));
}
4 test/com/indicee/api/xml/SerializationCheck.java
View
@@ -51,8 +51,8 @@
// Axis.class,
// AxisColumn.class,
// AxisSort.class,
- Group.class,
- Collection.class,
+// Group.class,
+// Collection.class,
Column.class,
Consumer.class,
Contribution.class,
Please sign in to comment.
Something went wrong with that request. Please try again.