1 26 package org.snipsnap.snip.storage; 27 28 import org.dom4j.Element; 29 import org.radeox.util.logging.Logger; 30 import org.snipsnap.snip.Links; 31 import org.snipsnap.snip.Snip; 32 import org.snipsnap.snip.attachment.Attachments; 33 import org.snipsnap.snip.label.Labels; 34 import org.snipsnap.user.Permissions; 35 36 import java.sql.Timestamp ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.Map ; 40 41 47 public class SnipSerializer extends SnipDataSerializer { 48 private static SnipSerializer serializer = null; 49 50 54 public synchronized static SnipSerializer getInstance() { 55 if (null == serializer) { 56 serializer = new SnipSerializer(); 57 } 58 return serializer; 59 } 60 61 protected SnipSerializer() { 62 63 } 64 65 70 public Element serialize(Snip snip) { 71 return serialize(createSnipMap(snip)); 72 } 73 74 79 public Snip deserialize(Element snipElement, Snip snip) { 80 Map snipMap = getElementMap(snipElement); 81 return deserialize(snipMap, snip); 82 } 83 84 public Snip deserialize(Map snipMap, Snip snip) { 85 Iterator elementIt = snipMap.keySet().iterator(); 87 while (elementIt.hasNext()) { 88 String element = (String ) elementIt.next(); 89 String value = ""; 90 Object elementValue = snipMap.get(element); 91 if (elementValue instanceof String ) { 92 value = notNull((String ) snipMap.get(element)); 93 } 94 95 if (SNIP_NAME.equals(element)) { 96 snip.setName(value); 98 } else if (SNIP_CTIME.equals(element) && !"".equals(value)) { 99 snip.setCTime(new Timestamp (Long.parseLong(value))); 100 } else if (SNIP_MTIME.equals(element) && !"".equals(value)) { 101 snip.setMTime(new Timestamp (Long.parseLong(value))); 102 } else if (SNIP_CUSER.equals(element)) { 103 if (null == snip.getOUser() || "".equals(snip.getOUser())) { 104 snip.setOUser(value); 105 } 106 snip.setCUser(value); 107 } else if (SNIP_MUSER.equals(element)) { 108 snip.setMUser(value); 109 } else if (SNIP_OUSER.equals(element)) { 110 if (!"".equals(value)) { 111 snip.setOUser(value); 112 } 113 } else if (SNIP_PARENT.equals(element)) { 114 if (value.trim().length() != 0) { 115 snip.setParentName(value); 116 } 117 } else if (SNIP_COMMENTED.equals(element)) { 118 if (value.trim().length() != 0) { 119 snip.setCommentedName(value); 120 } 121 } else if (SNIP_PERMISSIONS.equals(element)) { 122 snip.setPermissions(new Permissions(value)); 123 } else if (SNIP_BACKLINKS.equals(element)) { 124 snip.setBackLinks(new Links(value)); 125 } else if (SNIP_SNIPLINKS.equals(element)) { 126 snip.setSnipLinks(new Links(value)); 127 } else if (SNIP_LABELS.equals(element)) { 128 snip.setLabels(new Labels(snip, value)); 129 } else if (SNIP_ATTACHMENTS.equals(element)) { 130 if (elementValue instanceof Element) { 131 snip.setAttachments(new Attachments((Element) elementValue)); 132 } else { 133 snip.setAttachments(new Attachments(value)); 134 } 135 } else if (SNIP_VIEWCOUNT.equals(element) && !"".equals(value)) { 136 snip.setViewCount(Integer.parseInt(value)); 137 } else if (SNIP_CONTENT.equals(element)) { 138 snip.setContent(value); 139 } else if (SNIP_VERSION.equals(element) && !"".equals(value)) { 140 snip.setVersion(Integer.parseInt(value)); 141 } else if (SNIP_APPLICATION.equals(element)) { 142 snip.setApplication(value); 143 } else { 144 Logger.warn("unknown entry in serialized snip: " + element + "='" + value + "'"); 145 } 146 } 147 return snip; 148 } 149 150 public Map createSnipMap(Snip snip) { 151 Map snipMap = new HashMap (); 152 snipMap.put(SNIP_NAME, notNull(snip.getName())); 153 snipMap.put(SNIP_OUSER, notNull(snip.getOUser())); 154 snipMap.put(SNIP_CUSER, notNull(snip.getCUser())); 155 snipMap.put(SNIP_MUSER, notNull(snip.getMUser())); 156 snipMap.put(SNIP_CTIME, getStringTimestamp(snip.getCTime())); 157 snipMap.put(SNIP_MTIME, getStringTimestamp(snip.getMTime())); 158 snipMap.put(SNIP_PERMISSIONS, notNull(snip.getPermissions())); 159 snipMap.put(SNIP_BACKLINKS, notNull(snip.getBackLinks())); 160 snipMap.put(SNIP_SNIPLINKS, notNull(snip.getSnipLinks())); 161 snipMap.put(SNIP_LABELS, notNull(snip.getLabels())); 162 snipMap.put(SNIP_ATTACHMENTS, notNull(snip.getAttachments())); 163 snipMap.put(SNIP_VIEWCOUNT, "" + snip.getViewCount()); 164 snipMap.put(SNIP_CONTENT, notNull(snip.getContent())); 165 snipMap.put(SNIP_VERSION, "" + snip.getVersion()); 166 snipMap.put(SNIP_APPLICATION, notNull(snip.getApplication())); 167 168 Snip parent = snip.getParent(); 170 snipMap.put(SNIP_PARENT, null == parent ? "" : parent.getName()); 171 Snip comment = snip.getCommentedSnip(); 172 snipMap.put(SNIP_COMMENTED, null == comment ? "" : comment.getName()); 173 return snipMap; 174 } 175 } 176 | Popular Tags |