KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > attachment > Attachments


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.StringReader JavaDoc;
40 import java.io.UnsupportedEncodingException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.util.Comparator JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Map JavaDoc;
49
50 /**
51  * Class for grouping and managing attachments for a snip
52  *
53  * @author Stephan J. Schmidt
54  * @version $Id: Attachments.java 1606 2004-05-17 10:56:18Z leo $
55  */

56
57 public class Attachments {
58
59   private String JavaDoc cache = null;
60   private final static Comparator JavaDoc attSorter = new Comparator JavaDoc() {
61     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
62       return ((Attachment) o1).getName().compareTo(((Attachment) o2).getName());
63     }
64   };
65
66   /**
67    * Initialize Attachments object with a serialized string.
68    */

69   public Attachments(String JavaDoc 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 JavaDoc 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 JavaDoc name, String JavaDoc contentType, long size, String JavaDoc location) {
96     Attachment attachment = new Attachment(name, contentType, size, new Date JavaDoc(), location);
97     addAttachment(attachment);
98     return attachment;
99   }
100
101   public Attachment getAttachment(String JavaDoc name) {
102     deserialize();
103     return (Attachment) attachments.get(name);
104   }
105
106   public void removeAttachment(String JavaDoc 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 JavaDoc iterator() {
120     return getAll().iterator();
121   }
122
123   public List JavaDoc getAll() {
124     deserialize();
125     List JavaDoc list = new ArrayList JavaDoc(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 JavaDoc ATTACHMENTS = "attachments";
136   public final static String JavaDoc ATTACHMENT = "attachment";
137
138   public final static String JavaDoc NAME = "name";
139   public final static String JavaDoc CONTENTTYPE = "content-type";
140   public final static String JavaDoc SIZE = "size";
141   public final static String JavaDoc DATE = "date";
142   public final static String JavaDoc LOCATION = "location";
143
144   /**
145    * Deserialize the attachments from the database string.
146    * TODO: synchronized due to race conditions while starting up
147    */

148   private synchronized void deserialize() {
149     if (null == attachments) {
150       attachments = new HashMap JavaDoc();
151       if (null != cache && !"".equals(cache)) {
152         Document attXml;
153         try {
154           SAXReader saxReader = new SAXReader();
155           attXml = saxReader.read(new StringReader JavaDoc(cache));
156           Element root = attXml.getRootElement();
157           Iterator JavaDoc it = root.elementIterator(ATTACHMENT);
158           while (it.hasNext()) {
159             Element attElement = (Element) it.next();
160             try {
161               String JavaDoc name = attElement.element(NAME).getText();
162               String JavaDoc contentType = attElement.element(CONTENTTYPE).getTextTrim();
163               int size = Integer.parseInt(attElement.element(SIZE).getTextTrim());
164               Date JavaDoc date = new Date JavaDoc(Long.parseLong(attElement.element(DATE).getTextTrim()));
165               String JavaDoc location = attElement.element(LOCATION).getTextTrim();
166               attachments.put(name, new Attachment(name, contentType, size, date, location));
167             } catch (Exception JavaDoc e) {
168               Logger.warn("Attachments: ignoring attachment: " + attElement);
169             }
170           }
171         } catch (Exception JavaDoc e) {
172           Logger.warn("Attachments: unable to deserialize: '" + cache + "'");
173         }
174       }
175     }
176   }
177
178   private String JavaDoc serialize() {
179     if (null == attachments) {
180       return cache;
181     }
182
183     Element attElement = DocumentHelper.createElement(ATTACHMENTS);
184     Iterator JavaDoc 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 JavaDoc toString(Element attElement) {
199     OutputFormat outputFormat = OutputFormat.createCompactFormat();
200     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
201     try {
202       XMLWriter xmlWriter = new XMLWriter(out, outputFormat);
203       xmlWriter.write(attElement);
204       xmlWriter.flush();
205     } catch (IOException JavaDoc e) {
206       Logger.warn("Attachments: unable to serialize", e);
207     }
208     
209     try {
210       String JavaDoc enc = Application.get().getConfiguration().getEncoding();
211       return out.toString(enc == null ? "UTF-8" : enc);
212     } catch (UnsupportedEncodingException JavaDoc e) {
213       return out.toString();
214     }
215   }
216
217   public String JavaDoc toString() {
218     return serialize();
219   }
220 }
221
Popular Tags