KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > SnipSerializer


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

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 JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * A snip serializer that can store and load snips in XML format.
43  *
44  * @author Matthias L. Jugel
45  * @version $Id: SnipSerializer.java 1634 2004-06-08 11:20:05Z leo $
46  */

47 public class SnipSerializer extends SnipDataSerializer {
48   private static SnipSerializer serializer = null;
49
50   /**
51    * Get an instance of the snip serializer.
52    * @return the serializer
53    */

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   /**
66    * Store a snip in an XML node.
67    * @param snip the snip to store
68    * @return the serialized snip as XML
69    */

70   public Element serialize(Snip snip) {
71     return serialize(createSnipMap(snip));
72   }
73
74   /**
75    * Load snip from XML serialized file.
76    * @param snipElement the XML node containing the snip
77    * @return the modified snip
78    */

79   public Snip deserialize(Element snipElement, Snip snip) {
80     Map JavaDoc snipMap = getElementMap(snipElement);
81     return deserialize(snipMap, snip);
82   }
83
84   public Snip deserialize(Map JavaDoc snipMap, Snip snip) {
85 // System.out.println("deserializing: "+snipMap.get(SNIP_NAME));
86
Iterator JavaDoc elementIt = snipMap.keySet().iterator();
87     while (elementIt.hasNext()) {
88       String JavaDoc element = (String JavaDoc) elementIt.next();
89       String JavaDoc value = "";
90       Object JavaDoc elementValue = snipMap.get(element);
91       if (elementValue instanceof String JavaDoc) {
92         value = notNull((String JavaDoc) snipMap.get(element));
93       }
94
95       if (SNIP_NAME.equals(element)) {
96 // System.out.println("SnipSerializer.deserialize(" + value + ")");
97
snip.setName(value);
98       } else if (SNIP_CTIME.equals(element) && !"".equals(value)) {
99         snip.setCTime(new Timestamp JavaDoc(Long.parseLong(value)));
100       } else if (SNIP_MTIME.equals(element) && !"".equals(value)) {
101         snip.setMTime(new Timestamp JavaDoc(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 JavaDoc createSnipMap(Snip snip) {
151     Map JavaDoc snipMap = new HashMap JavaDoc();
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     // TODO deprecated
169
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