KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 /**
36  *
37  * Provide methods
38  * to convert XML to HashMap
39  * and HashMap to XML
40  *
41  */

42 public class SourceUtils {
43
44     //---------------------------------------------------------------- Constants
45

46     public static final String JavaDoc ROOT_NAME = "__root__name__" ;
47
48     public static final String JavaDoc TAG_XML_VERSION = "<?xml version=\"1.0\" encoding=\""
49                                                + System.getProperty("file.encoding")
50                                                + "\"?>" ;
51
52
53    //---------------------------------------------------------- Private data
54

55     //----------------------------------------------------------- Public methods
56

57     /**
58      * Make a HashMap of fieldName - fieldValue
59      *
60      * tagName -> key
61      * tagValue -> value
62      *
63      * @param content
64      * @return HashMap of fieldName - fieldValue
65      * @throws Exception
66      **/

67      public static HashMap JavaDoc xmlToHashMap(String JavaDoc content) throws Exception JavaDoc {
68          content = dropTagCData(content);
69          return xmlToHashMap(new ByteArrayInputStream JavaDoc(content.getBytes()));
70      }
71
72     /**
73      * Make a HashMap of fieldName - fieldValue
74      *
75      * tagName -> key
76      * tagValue -> value
77      *
78      * @param is
79      * @return HashMap of fieldName - fieldValue
80      * @throws Exception
81      **/

82     public static HashMap JavaDoc xmlToHashMap (InputStream JavaDoc is)
83     throws Exception JavaDoc {
84
85         DocumentBuilderFactory JavaDoc docBuilderFactory = null ;
86         DocumentBuilder JavaDoc docBuilder = null ;
87         Document JavaDoc docXml = null ;
88         NodeList JavaDoc lstChildren = null ;
89         Element JavaDoc el = null ;
90         Node JavaDoc node = null ;
91
92         HashMap JavaDoc fields = null ;
93
94         String JavaDoc value = null ;
95         String JavaDoc nodeValue = null ;
96         String JavaDoc rootName = null ;
97
98         try {
99
100             docBuilderFactory = DocumentBuilderFactory.newInstance ( ) ;
101             docBuilder = docBuilderFactory.newDocumentBuilder ( ) ;
102             docXml = docBuilder.parse ( is ) ;
103             el = docXml.getDocumentElement ( ) ;
104             rootName = el.getTagName ( ) ;
105             lstChildren = el.getChildNodes ( ) ;
106
107             fields = new HashMap JavaDoc();
108
109             for (int i=0, l = lstChildren.getLength(); i < l; i++) {
110
111                 node = lstChildren.item(i);
112
113                 if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
114
115                     if (node.getChildNodes().item(0) != null) {
116                         nodeValue = node.getChildNodes().item(0).getNodeValue();
117                     } else {
118                         nodeValue = "";
119                     }
120
121                     fields.put(node.getNodeName(), nodeValue);
122
123                 }
124
125             }
126
127             // put the rootName in the hashMap with key ROOT_NAME that is a conventional name
128
// for store the name of the root
129
if (rootName != null)
130                 fields.put(ROOT_NAME, rootName);
131
132         } catch (Exception JavaDoc e) {
133             throw new Exception JavaDoc(e.toString());
134         }
135
136         return fields;
137
138     }
139
140      /**
141      * Make xml from an HashMap.
142      * key -> tagName
143      * value -> tagValue
144      *
145      * @param fields
146      * @return String with the xml
147      * @throws Exception
148      **/

149
150     public static String JavaDoc hashMapToXml (HashMap JavaDoc fields)
151     throws Exception JavaDoc {
152
153         String JavaDoc fieldName = null ;
154         String JavaDoc fieldValue = null ;
155         String JavaDoc message = "";
156         String JavaDoc rootName = null ;
157
158         Iterator JavaDoc i = null ;
159         int j = 0;
160
161         i = fields.keySet().iterator();
162
163         while(i.hasNext()) {
164
165             fieldName = (String JavaDoc) i.next() ;
166             fieldValue = (String JavaDoc) fields.get((String JavaDoc)fieldName) ;
167
168             fieldValue = StringTools.escapeXml(fieldValue);
169
170             if (fieldName.equals (ROOT_NAME)) {
171                 rootName = fieldValue;
172             } else {
173                 message = message +
174                            "<" +
175                            fieldName +
176                            ">" +
177                            fieldValue +
178                            "</" +
179                            fieldName +
180                            ">" ;
181             }
182         }
183
184         message = "<" +
185                   rootName +
186                   ">" +
187                   message ;
188
189         message = message +
190                   "</" +
191                   rootName +
192                   ">" ;
193
194         return TAG_XML_VERSION + message;
195
196     }
197
198
199    //--------------------------------------------------------- Private methods
200

201     /**
202      * @param content
203      * @return input string by drop CData tag
204      **/

205     private static String JavaDoc dropTagCData(String JavaDoc content) {
206
207         String JavaDoc tmp = null ;
208
209         int startData = 0 ;
210         int endData = 0 ;
211
212         if (content.indexOf("<![CDATA[") != - 1) {
213
214             startData = content.indexOf("<![CDATA[") + "<![CDATA[".length();
215
216             //
217
// for server bug: server create end CDATA with ]]]]>
218
//
219
endData = content.lastIndexOf("]]]]>");
220
221             if (endData == -1) {
222                endData = content.lastIndexOf("]]>");
223             }
224
225             tmp = content.substring(startData, endData);
226
227             return content.substring(startData, endData);
228
229         } else {
230             return content;
231         }
232
233     }
234
235     /**
236      * This method is used to replace the String "=\r\n" with the
237      * String "\r\n ": This replace is necessary because the are devices that
238      * doesn't sent the correct line delimiting for long line.
239      *
240      * @param content the input content
241      * @return the input content with property handling long line
242      */

243     public static String JavaDoc handleLineDelimiting(String JavaDoc content) {
244         return content.replaceAll("=\r\n","\r\n ");
245     }
246 }
Popular Tags