com.unbxd.client.feed.FeedFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbxd-java-sdk Show documentation
Show all versions of unbxd-java-sdk Show documentation
The project is a Unbxd Java Library
The newest version!
package com.unbxd.client.feed;
import org.apache.commons.lang.StringEscapeUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: sourabh
* Date: 07/07/14
* Time: 6:01 PM
* To change this template use File | Settings | File Templates.
*/
public class FeedFile {
private Document _doc;
public FeedFile(Collection fields,
Collection addedDocs,
Collection updatedDocs,
Collection deletedDocs,
Collection taxonomyNodes,
Map> taxonomyMappings){
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
_doc = docBuilder.newDocument();
Element root = _doc.createElement("feed");
_doc.appendChild(root);
if(fields.size() > 0 || addedDocs.size() > 0 || updatedDocs.size() > 0 || deletedDocs.size() > 0){
Element catalog = _doc.createElement("catalog");
root.appendChild(catalog);
if(fields.size() > 0)
writeSchema(fields, catalog);
if(addedDocs.size() > 0){
Element addNode = _doc.createElement("add");
catalog.appendChild(addNode);
writeAdd(addedDocs, addNode);
}
if(updatedDocs.size() > 0){
Element updateNode = _doc.createElement("update");
catalog.appendChild(updateNode);
writeUpdate(updatedDocs, updateNode);
}
if(deletedDocs.size() > 0){
Element deleteNode = _doc.createElement("delete");
catalog.appendChild(deleteNode);
writeDelete(deletedDocs, deleteNode);
}
}
if(taxonomyNodes.size() > 0 || taxonomyMappings.size() > 0){
Element taxonomy = _doc.createElement("taxonomy");
root.appendChild(taxonomy);
if(taxonomyNodes.size() > 0){
writeTree(taxonomyNodes, taxonomy);
}
if(taxonomyMappings.size() > 0){
writeMapping(taxonomyMappings, taxonomy);
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
private void writeSchema(Collection fields, Element parent){
for(FeedField field : fields){
Element schema = _doc.createElement("schema");
Element fieldName = _doc.createElement("fieldName");
fieldName.appendChild(_doc.createTextNode(field.getName()));
schema.appendChild(fieldName);
Element dataType = _doc.createElement("dataType");
dataType.appendChild(_doc.createTextNode(field.getDataType().getValue()));
schema.appendChild(dataType);
Element multiValued = _doc.createElement("multiValued");
multiValued.appendChild(_doc.createTextNode(new Boolean(field.isMultiValued()).toString()));
schema.appendChild(multiValued);
Element autoSuggest = _doc.createElement("autoSuggest");
autoSuggest.appendChild(_doc.createTextNode(new Boolean(field.isAutoSuggest()).toString()));
schema.appendChild(autoSuggest);
parent.appendChild(schema);
}
}
private void writeAttribute(Element items, String field, Object o, boolean associated){
Collection