KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > XMLHashMapParser


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.common;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * It supplies the methods for the marshall and unmarshall of a HashMap in xml.
28  * <p>The xml begins with &#60;RECORD&#62; and ends with &#60;/RECORD&#62;
29  * <p>Every object of the table comes represented with formed following
30  * <p>&#60;FIELD&#62;<br>
31  * &nbsp;&nbsp;&#60;NAME&#62;name of the field&#60;/NAME&#62;<br>
32  * &nbsp;&nbsp;&#60;VALUE&#62;value of the field&#60;/VALUE&#62;<br>
33  * &#60;/FIELD&#62;<br>
34  *
35  * <p>Example of HashMap represented in xml:
36  * <p>&#60;RECORD&#62;<br>
37  * &nbsp;&nbsp;&#60;FIELD&#62;<br>
38  * &nbsp;&nbsp;&nbsp;&nbsp;&#60;NAME&#62;name&#60;/NAME&#62;<br>
39  * &nbsp;&nbsp;&nbsp;&nbsp;&#60;VALUE&#62;john&#60;/VALUE&#62;<br>
40  * &nbsp;&nbsp;&#60;/FIELD&#62;<br>
41  * &nbsp;&nbsp;&#60;FIELD&#62;<br>
42  * &nbsp;&nbsp;&nbsp;&nbsp;&#60;NAME&#62;birthDate&#60;/NAME&#62;<br>
43  * &nbsp;&nbsp;&nbsp;&nbsp;&#60;VALUE&#62;100128313321&#60;/VALUE&#62;<br>
44  * &nbsp;&nbsp;&#60;/FIELD&#62;<br>
45  * &#60;/RECORD&#62;
46  *
47  * <p>The null values are represent by <code>NULL_VALUE</code>
48  *
49  * @version $Id: XMLHashMapParser.java,v 1.2 2005/01/19 11:18:36 fabius Exp $
50  * @author Stefano Nichele
51  */

52
53 public class XMLHashMapParser {
54
55     // ----------------------------------------------------------------Constants
56
/** Representation of a null object */
57     private static final String JavaDoc NULL_VALUE = "NULL_VALUE";
58
59
60     // -----------------------------------------------------------Public methods
61
/**
62      * Converts a HashMap in xml
63      * @param values the HasMap to convert
64      * @return the representation of the given HasMap in xml
65      */

66     public static String JavaDoc toXML(Map JavaDoc values) {
67         StringBuffer JavaDoc sbXml = new StringBuffer JavaDoc("<RECORD>");
68
69         Iterator JavaDoc keys = values.keySet().iterator();
70         String JavaDoc name = null;
71         String JavaDoc value = null;
72         Object JavaDoc objectValue = null;
73         while (keys.hasNext()) {
74             name = (String JavaDoc)keys.next();
75             objectValue = values.get(name);
76             if (objectValue == null) {
77                 value = NULL_VALUE;
78             } else {
79                 value = objectValue.toString();
80             }
81
82             sbXml.append("\n<FIELD>\n<NAME>");
83             sbXml.append(name);
84             sbXml.append("</NAME>\n<VALUE>");
85             sbXml.append(value);
86             sbXml.append("</VALUE>\n</FIELD>");
87
88         }
89
90         sbXml.append("\n</RECORD>");
91
92         return sbXml.toString();
93     }
94
95     /**
96      * Converts a xml in HashMap
97      * @param xml the xml to convert
98      * @return the HashMap correspondent to the given xml
99      * @throws IllegalStateException if an error occurs during the conversion
100      */

101     public static Map JavaDoc toMap(String JavaDoc xml) throws IllegalStateException JavaDoc {
102         Map JavaDoc values = new HashMap JavaDoc();
103
104         String JavaDoc record = getTagContent(xml, "RECORD");
105
106         if (record == null) {
107             throw new IllegalStateException JavaDoc("Bad xml rappresentation");
108         }
109
110         Vector JavaDoc fields = getListTagContent(record, "FIELD");
111         String JavaDoc field = null;
112         String JavaDoc name = null;
113         String JavaDoc value = null;
114
115         int numFields = fields.size();
116         for (int i=0; i<numFields; i++) {
117             field = (String JavaDoc)fields.elementAt(i);
118             name = getTagContent(field, "NAME");
119             value = getTagContent(field, "VALUE");
120
121             values.put(name, value);
122         }
123
124         return values;
125     }
126
127     // ----------------------------------------------------------Private methods
128
/**
129      * Given a string (xml), return a Vector with the contents
130      * of all the occurrences of a given tag
131      * @param xml string in which searching the tag
132      * @param tag searched tag
133      * @return a vector that contains all the occurrences of a given tag
134      */

135     private static Vector JavaDoc getListTagContent(String JavaDoc xml, String JavaDoc tag) {
136         Vector JavaDoc tagsContent = new Vector JavaDoc();
137
138         String JavaDoc startTag = "<" + tag + ">";
139         String JavaDoc endTag = "</" + tag + ">";
140
141         int indexStartTag = 0;
142         int indexEndTag = 0;
143
144         String JavaDoc tagContent = null;
145
146         while ( (indexStartTag = xml.indexOf(startTag, indexEndTag)) != -1 ) {
147             indexEndTag = xml.indexOf(endTag, indexStartTag);
148
149             if (indexEndTag == -1) {
150                 // trovato l'inizio del tag ma non la fine
151
break;
152             }
153
154             if (indexStartTag == -1 || indexEndTag == -1) {
155                 return tagsContent;
156             }
157
158             tagContent = xml.substring(indexStartTag, indexEndTag);
159             tagsContent.addElement(tagContent);
160
161         }
162         return tagsContent;
163     }
164
165     /**
166      * Given a string (xml), return the first occurrence of the tag
167      * @param xml string in which searching the tag
168      * @param tag searched tag
169      * @return the first occurrence of the given tag
170      */

171     private static String JavaDoc getTagContent(String JavaDoc xml, String JavaDoc tag) {
172         String JavaDoc startTag = "<" + tag + ">";
173         String JavaDoc endTag = "</" + tag + ">";
174         int indexStartTag = xml.indexOf(startTag) + startTag.length();
175         int indexEndTag = xml.indexOf(endTag);
176
177         if (indexStartTag == -1 || indexEndTag == -1) {
178             return null;
179         }
180
181         return xml.substring(indexStartTag, indexEndTag);
182
183     }
184
185
186 }
Popular Tags