KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.snipsnap.snip.storage;
26
27 import org.dom4j.Element;
28 import org.radeox.util.logging.Logger;
29
30 import java.sql.Timestamp JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 public class SerializerSupport {
36
37   /**
38    * Produce a map with all elements and its values found below the top element.
39    * @param el the root element
40    * @return the map with names and values
41    */

42   public Map JavaDoc getElementMap(Element el) {
43     Map JavaDoc elements = new HashMap JavaDoc();
44     Iterator JavaDoc childIterator = el.elementIterator();
45     while (childIterator.hasNext()) {
46       Element element = (Element) childIterator.next();
47       if (element.isTextOnly()) {
48         elements.put(element.getName(), notNull(element.getText()));
49       } else {
50         elements.put(element.getName(), element);
51       }
52     }
53     return elements;
54   }
55
56   /**
57    * Create a timestamp out of a string value.
58    * @param value the string value
59    * @return the timestamp
60    */

61   protected Timestamp JavaDoc getTimestamp(String JavaDoc value) {
62     if(null != value && !"".equals(value)) {
63       try {
64         return new Timestamp JavaDoc(Long.parseLong(value));
65       } catch (NumberFormatException JavaDoc e) {
66         Logger.warn("SerializerSupport: timestamp value invalid: "+value);
67       }
68     }
69     return null;
70   }
71
72   /**
73    * Create a string representation of a timestamp.
74    * @param ts the timestamp
75    * @return the string representation
76    */

77   protected String JavaDoc getStringTimestamp(Timestamp JavaDoc ts) {
78     if(null != ts) {
79       return "" + ts.getTime();
80     }
81     return "";
82   }
83
84   /**
85    * Make sure a string is not null but rather an empty string
86    * @param str a value
87    * @return the string
88    */

89   protected String JavaDoc notNull(String JavaDoc str) {
90     return str == null ? "" : str;
91   }
92
93   /**
94    * Make sure the object is not null but rather an empty string or
95    * convert the object to string.
96    * @param obj the object
97    * @return the string value or ""
98    */

99   protected String JavaDoc notNull(Object JavaDoc obj) {
100     return obj == null ? "" : obj.toString();
101   }
102
103 }
104
Popular Tags