KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * A snip data serializer that can store snip data in XML format.
39  *
40  * @author Matthias L. Jugel
41  * @version $Id: SnipDataSerializer.java 1618 2004-06-01 17:26:24Z leo $
42  */

43 public class SnipDataSerializer extends SerializerSupport implements Serializer {
44   public final static String JavaDoc SNIP = "snip";
45
46   public final static String JavaDoc SNIP_NAME = "name";
47   public final static String JavaDoc SNIP_CONTENT = "content";
48   public final static String JavaDoc SNIP_OUSER = "oUser";
49   public final static String JavaDoc SNIP_CUSER = "cUser";
50   public final static String JavaDoc SNIP_MUSER = "mUser";
51   public final static String JavaDoc SNIP_CTIME = "cTime";
52   public final static String JavaDoc SNIP_MTIME = "mTime";
53   public final static String JavaDoc SNIP_PERMISSIONS = "permissions";
54   public final static String JavaDoc SNIP_BACKLINKS = "backLinks";
55   public final static String JavaDoc SNIP_SNIPLINKS = "snipLinks";
56   public final static String JavaDoc SNIP_LABELS = "labels";
57   public final static String JavaDoc SNIP_ATTACHMENTS = "attachments";
58   public final static String JavaDoc SNIP_VIEWCOUNT = "viewCount";
59   public final static String JavaDoc SNIP_VERSION = "version";
60   public final static String JavaDoc SNIP_APPLICATION = "application";
61
62   // TODO deprecated
63
public final static String JavaDoc SNIP_COMMENTED = "commentSnip";
64   public final static String JavaDoc SNIP_PARENT = "parentSnip";
65
66   /**
67    * Special serialize method that does not have dependencies on regular Snip
68    * or other implementations used throughout snipsnap. This is necessary to
69    * be able to dump a database with a small utility.
70    *
71    * @param snipMap a map containing the snips data
72    * @return an element that can be serializes as XML
73    */

74   public Element serialize(Map JavaDoc snipMap) {
75     Element snipElement = DocumentHelper.createElement(SNIP);
76
77     snipElement.addElement(SNIP_NAME).addText((String JavaDoc) 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 JavaDoc) snipMap.get(SNIP_BACKLINKS)));
85     snipElement.add(addCDATAContent(SNIP_SNIPLINKS, (String JavaDoc) snipMap.get(SNIP_SNIPLINKS)));
86     snipElement.add(addCDATAContent(SNIP_LABELS, (String JavaDoc)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 JavaDoc) 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     // TODO deprecated
94
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   /**
101    * Add an element whose content is put into a cdata section
102    * @param elementName the element to be created
103    * @param content the content as a string
104    * @return the newly created element
105    */

106   private Element addCDATAContent(String JavaDoc elementName, String JavaDoc 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   /**
116    * Create an element whose content is also xml to make sure it is not
117    * escaped in the output
118    * @param elementName name of the new element
119    * @param content the actual xml as a string
120    * @return the serialized element
121    */

122   private Element addXMLContent(String JavaDoc elementName, String JavaDoc content) {
123     if (null != content && !"".equals(content)) {
124       try {
125         StringReader JavaDoc stringReader = new StringReader JavaDoc(content);
126         SAXReader saxReader = new SAXReader();
127         Document doc = saxReader.read(stringReader);
128         return doc.getRootElement();
129       } catch (Exception JavaDoc 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