1 25 26 package org.snipsnap.snip.attachment; 27 28 import org.dom4j.Document; 29 import org.dom4j.DocumentHelper; 30 import org.dom4j.Element; 31 import org.dom4j.io.OutputFormat; 32 import org.dom4j.io.SAXReader; 33 import org.dom4j.io.XMLWriter; 34 import org.radeox.util.logging.Logger; 35 import org.snipsnap.app.Application; 36 37 import java.io.ByteArrayOutputStream ; 38 import java.io.IOException ; 39 import java.io.StringReader ; 40 import java.io.UnsupportedEncodingException ; 41 import java.util.ArrayList ; 42 import java.util.Collections ; 43 import java.util.Comparator ; 44 import java.util.Date ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Map ; 49 50 56 57 public class Attachments { 58 59 private String cache = null; 60 private final static Comparator attSorter = new Comparator () { 61 public int compare(Object o1, Object o2) { 62 return ((Attachment) o1).getName().compareTo(((Attachment) o2).getName()); 63 } 64 }; 65 66 69 public Attachments(String serialized) { 70 if (null != serialized) { 71 if (serialized.startsWith("<" + ATTACHMENTS + ">")) { 72 cache = serialized; 73 } else { 74 cache = "<" + ATTACHMENTS + ">" + serialized + "</" + ATTACHMENTS + ">"; 75 } 76 } 77 } 78 79 public Attachments(Element serialized) { 80 cache = toString(serialized); 81 } 82 83 public Attachments() { 84 cache = ""; 85 } 86 87 private Map attachments = null; 88 89 public Attachment addAttachment(Attachment attachment) { 90 deserialize(); 91 attachments.put(attachment.getName(), attachment); 92 return attachment; 93 } 94 95 public Attachment addAttachment(String name, String contentType, long size, String location) { 96 Attachment attachment = new Attachment(name, contentType, size, new Date (), location); 97 addAttachment(attachment); 98 return attachment; 99 } 100 101 public Attachment getAttachment(String name) { 102 deserialize(); 103 return (Attachment) attachments.get(name); 104 } 105 106 public void removeAttachment(String name) { 107 deserialize(); 108 Attachment attachment = (Attachment) attachments.get(name); 109 if (attachment != null) { 110 removeAttachment(attachment); 111 } 112 } 113 114 public void removeAttachment(Attachment attachment) { 115 deserialize(); 116 attachments.remove(attachment.getName()); 117 } 118 119 public Iterator iterator() { 120 return getAll().iterator(); 121 } 122 123 public List getAll() { 124 deserialize(); 125 List list = new ArrayList (attachments.values()); 126 Collections.sort(list, attSorter); 127 return list; 128 } 129 130 public boolean isEmpty() { 131 deserialize(); 132 return null == attachments || attachments.size() == 0; 133 } 134 135 public final static String ATTACHMENTS = "attachments"; 136 public final static String ATTACHMENT = "attachment"; 137 138 public final static String NAME = "name"; 139 public final static String CONTENTTYPE = "content-type"; 140 public final static String SIZE = "size"; 141 public final static String DATE = "date"; 142 public final static String LOCATION = "location"; 143 144 148 private synchronized void deserialize() { 149 if (null == attachments) { 150 attachments = new HashMap (); 151 if (null != cache && !"".equals(cache)) { 152 Document attXml; 153 try { 154 SAXReader saxReader = new SAXReader(); 155 attXml = saxReader.read(new StringReader (cache)); 156 Element root = attXml.getRootElement(); 157 Iterator it = root.elementIterator(ATTACHMENT); 158 while (it.hasNext()) { 159 Element attElement = (Element) it.next(); 160 try { 161 String name = attElement.element(NAME).getText(); 162 String contentType = attElement.element(CONTENTTYPE).getTextTrim(); 163 int size = Integer.parseInt(attElement.element(SIZE).getTextTrim()); 164 Date date = new Date (Long.parseLong(attElement.element(DATE).getTextTrim())); 165 String location = attElement.element(LOCATION).getTextTrim(); 166 attachments.put(name, new Attachment(name, contentType, size, date, location)); 167 } catch (Exception e) { 168 Logger.warn("Attachments: ignoring attachment: " + attElement); 169 } 170 } 171 } catch (Exception e) { 172 Logger.warn("Attachments: unable to deserialize: '" + cache + "'"); 173 } 174 } 175 } 176 } 177 178 private String serialize() { 179 if (null == attachments) { 180 return cache; 181 } 182 183 Element attElement = DocumentHelper.createElement(ATTACHMENTS); 184 Iterator it = attachments.values().iterator(); 185 while (it.hasNext()) { 186 Attachment attachment = (Attachment) it.next(); 187 Element attachmentNode = attElement.addElement(ATTACHMENT); 188 attachmentNode.addElement(NAME).addText(attachment.getName()); 189 attachmentNode.addElement(CONTENTTYPE).addText(attachment.getContentType()); 190 attachmentNode.addElement(SIZE).addText("" + attachment.getSize()); 191 attachmentNode.addElement(DATE).addText("" + attachment.getDate().getTime()); 192 attachmentNode.addElement(LOCATION).addText(attachment.getLocation()); 193 } 194 195 return cache = toString(attElement); 196 } 197 198 private String toString(Element attElement) { 199 OutputFormat outputFormat = OutputFormat.createCompactFormat(); 200 ByteArrayOutputStream out = new ByteArrayOutputStream (); 201 try { 202 XMLWriter xmlWriter = new XMLWriter(out, outputFormat); 203 xmlWriter.write(attElement); 204 xmlWriter.flush(); 205 } catch (IOException e) { 206 Logger.warn("Attachments: unable to serialize", e); 207 } 208 209 try { 210 String enc = Application.get().getConfiguration().getEncoding(); 211 return out.toString(enc == null ? "UTF-8" : enc); 212 } catch (UnsupportedEncodingException e) { 213 return out.toString(); 214 } 215 } 216 217 public String toString() { 218 return serialize(); 219 } 220 } 221 | Popular Tags |