1 26 package org.snipsnap.snip.storage; 27 28 import org.dom4j.Document; 29 import org.dom4j.DocumentHelper; 30 import org.dom4j.Element; 31 import org.dom4j.io.SAXReader; 32 import org.radeox.util.logging.Logger; 33 34 import java.io.StringReader ; 35 import java.util.Map ; 36 37 43 public class SnipDataSerializer extends SerializerSupport implements Serializer { 44 public final static String SNIP = "snip"; 45 46 public final static String SNIP_NAME = "name"; 47 public final static String SNIP_CONTENT = "content"; 48 public final static String SNIP_OUSER = "oUser"; 49 public final static String SNIP_CUSER = "cUser"; 50 public final static String SNIP_MUSER = "mUser"; 51 public final static String SNIP_CTIME = "cTime"; 52 public final static String SNIP_MTIME = "mTime"; 53 public final static String SNIP_PERMISSIONS = "permissions"; 54 public final static String SNIP_BACKLINKS = "backLinks"; 55 public final static String SNIP_SNIPLINKS = "snipLinks"; 56 public final static String SNIP_LABELS = "labels"; 57 public final static String SNIP_ATTACHMENTS = "attachments"; 58 public final static String SNIP_VIEWCOUNT = "viewCount"; 59 public final static String SNIP_VERSION = "version"; 60 public final static String SNIP_APPLICATION = "application"; 61 62 public final static String SNIP_COMMENTED = "commentSnip"; 64 public final static String SNIP_PARENT = "parentSnip"; 65 66 74 public Element serialize(Map snipMap) { 75 Element snipElement = DocumentHelper.createElement(SNIP); 76 77 snipElement.addElement(SNIP_NAME).addText((String ) snipMap.get(SNIP_NAME)); 78 snipElement.addElement(SNIP_OUSER).addText(notNull(snipMap.get(SNIP_OUSER))); 79 snipElement.addElement(SNIP_CUSER).addText(notNull(snipMap.get(SNIP_CUSER))); 80 snipElement.addElement(SNIP_MUSER).addText(notNull(snipMap.get(SNIP_MUSER))); 81 snipElement.addElement(SNIP_CTIME).addText(notNull(snipMap.get(SNIP_CTIME))); 82 snipElement.addElement(SNIP_MTIME).addText(notNull(snipMap.get(SNIP_MTIME))); 83 snipElement.addElement(SNIP_PERMISSIONS).addText(notNull(snipMap.get(SNIP_PERMISSIONS))); 84 snipElement.add(addCDATAContent(SNIP_BACKLINKS, (String ) snipMap.get(SNIP_BACKLINKS))); 85 snipElement.add(addCDATAContent(SNIP_SNIPLINKS, (String ) snipMap.get(SNIP_SNIPLINKS))); 86 snipElement.add(addCDATAContent(SNIP_LABELS, (String )snipMap.get(SNIP_LABELS))); 87 snipElement.add(addXMLContent(SNIP_ATTACHMENTS, notNull(snipMap.get(SNIP_ATTACHMENTS)))); 88 snipElement.addElement(SNIP_VIEWCOUNT).addText(notNull(snipMap.get(SNIP_VIEWCOUNT))); 89 snipElement.add(addCDATAContent(SNIP_CONTENT, (String ) snipMap.get(SNIP_CONTENT))); 90 snipElement.addElement(SNIP_VERSION).addText(notNull(snipMap.get(SNIP_VERSION))); 91 snipElement.addElement(SNIP_APPLICATION).addText(notNull(snipMap.get(SNIP_APPLICATION))); 92 93 snipElement.addElement(SNIP_PARENT).addText(notNull(snipMap.get(SNIP_PARENT))); 95 snipElement.addElement(SNIP_COMMENTED).addText(notNull(snipMap.get(SNIP_COMMENTED))); 96 97 return snipElement; 98 } 99 100 106 private Element addCDATAContent(String elementName, String content) { 107 Element element = DocumentHelper.createElement(elementName); 108 if (null == content || "".equals(content)) { 109 return element; 110 } 111 element.addCDATA(content); 112 return element; 113 } 114 115 122 private Element addXMLContent(String elementName, String content) { 123 if (null != content && !"".equals(content)) { 124 try { 125 StringReader stringReader = new StringReader (content); 126 SAXReader saxReader = new SAXReader(); 127 Document doc = saxReader.read(stringReader); 128 return doc.getRootElement(); 129 } catch (Exception e) { 130 Logger.warn("SnipSerializer: unable to add xml content: " + e); 131 e.printStackTrace(); 132 } 133 } 134 return DocumentHelper.createElement(elementName); 135 } 136 } 137 | Popular Tags |