KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > notification > WBXMLTools


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 package sync4j.server.notification;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import sync4j.framework.core.Sync4jException;
27 import sync4j.framework.core.bootstrap.WapProvisioningDoc;
28 import sync4j.framework.tools.Base64;
29
30 import org.kxml.Attribute;
31 import org.kxml.Xml;
32 import org.kxml.parser.ParseEvent;
33 import org.kxml.parser.XmlParser;
34 import org.kxml.wap.WapExtensionEvent;
35
36 /**
37  * Utility class for WBXML
38  *
39  * @author Stefano Nichele @ Funambol
40  * @version $Id: WBXMLTools.java,v 1.1 2005/05/16 17:32:57 nichele Exp $
41  */

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

46     // ------------------------------------------------------------ Private data
47

48     // ---------------------------------------------------------- Public methods
49

50     /**
51      * Converts a string to a WBXML message.
52      *
53      * @param s the String to convert - NOT NULL
54      *
55      * @return the WBXML message
56      *
57      * @throws Sync4jException in case of parser errors
58      */

59     public static byte[] toWBXML(final String JavaDoc s) throws Sync4jException {
60         WapProvisioningDocWriter writer = null;
61         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
62
63         try {
64             writer = new WapProvisioningDocWriter(out);
65             XmlParser xml = new XmlParser(new StringReader JavaDoc(s));
66             traverseXML(xml, writer);
67         } catch (IOException JavaDoc e) {
68             throw new Sync4jException(e.getMessage(), e);
69         } finally {
70             if (writer != null)try {writer.close();
71             } catch (Exception JavaDoc e) {}
72         }
73
74         return out.toByteArray();
75     }
76
77     /**
78      * Encodes a <i>WapProvisioningDoc</i> to WBXML
79      *
80      * @param msg the message to encode
81      *
82      * @return the encoded stream of bytes (as a byte[] buffer).
83      *
84      * @throws Exception in case of errors
85      */

86     public static byte[] toWBXML(WapProvisioningDoc msg) throws Exception JavaDoc {
87         return toWBXML(msg.toXml());
88     }
89
90     // --------------------------------------------------------- Private methods
91
private static void traverseXML(XmlParser parser, WapProvisioningDocWriter writer) throws
92         IOException JavaDoc {
93
94         boolean leave = false;
95
96         do {
97             ParseEvent event = parser.read();
98
99             switch (event.getType()) {
100                 case Xml.START_TAG:
101                     WapProvisioningDocWriter tagWriter = null;
102
103                     String JavaDoc name = event.getName();
104
105                     tagWriter = writer;
106                     tagWriter.startTag(name);
107
108                     // see API doc of StartTag for more access methods
109

110                     Vector JavaDoc attributes = event.getAttributes();
111                     if (attributes != null) {
112                         int numAttr = attributes.size();
113                         Attribute attr = null;
114                         for (int i = 0; i < numAttr; i++) {
115                             attr = event.getAttribute(i);
116                             tagWriter.attribute(attr.getName(), attr.getValue());
117                         }
118                     }
119
120                     traverseXML(parser, tagWriter); // recursion
121

122                     break;
123
124                 case Xml.END_TAG:
125                     writer.endTag();
126                     leave = true;
127                     break;
128
129                 case Xml.END_DOCUMENT:
130                     leave = true;
131                     break;
132
133                 case Xml.TEXT:
134                     writer.write(event.getText());
135                     break;
136
137                 case Xml.WHITESPACE:
138                     break;
139
140                 default:
141
142             }
143         } while (!leave);
144     }
145
146     /**
147      * Converts a WBXML message into the corresponding XML message.
148      *
149      * @param msg the message to convert - NOT NULL
150      *
151      * @return the XML message or NULL if an error occurred
152      *
153      * @throws Sync4jException in case of parser errors
154      */

155     public static String JavaDoc wbxmlToXml(final byte[] msg) throws Sync4jException {
156
157         ByteArrayInputStream JavaDoc in = null;
158         try {
159             in = new ByteArrayInputStream JavaDoc(msg);
160
161             WapProvisioningDocParser parser = new WapProvisioningDocParser(in);
162
163             return parseWBXML(parser);
164
165         } catch (Throwable JavaDoc t) {
166             t.printStackTrace();
167             throw new Sync4jException(t.getMessage(), t);
168         }
169     }
170
171     private static String JavaDoc parseWBXML(WapProvisioningDocParser parser) throws IOException JavaDoc {
172         return parseWBXML(parser, false, false, false);
173     }
174
175     private static String JavaDoc parseWBXML(WapProvisioningDocParser parser, boolean inCred, boolean b64,
176                                      boolean auth_md5) throws IOException JavaDoc {
177
178         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
179         boolean leave = false;
180
181         String JavaDoc tagName = null;
182         String JavaDoc text = null;
183
184         do {
185             ParseEvent event = parser.read();
186             switch (event.getType()) {
187                 case Xml.START_TAG:
188
189                     tagName = event.getName();
190
191                     buf.append("<");
192                     buf.append(tagName);
193                     Vector JavaDoc attrs = event.getAttributes();
194                     if (attrs != null) {
195                         for (int i = 0; i < attrs.size(); i++) {
196                             Attribute attr = (Attribute)attrs.elementAt(i);
197                             buf.append(" ");
198                             buf.append(attr.getName());
199                             buf.append("='");
200                             buf.append(attr.getValue());
201                             buf.append("'");
202                         }
203                     }
204                     buf.append(">");
205
206                     if (inCred == false) {
207                         text = parseWBXML(parser, "Cred".equals(tagName), false, false);
208                     } else {
209                         text = parseWBXML(parser, true, b64, auth_md5);
210                     }
211
212                     if (inCred) {
213                         if ("Meta".equals(tagName)) {
214                             b64 = (text.indexOf("b64") >= 0);
215                             auth_md5 = (text.indexOf("auth-md5") >= 0);
216                             buf.append(text);
217                             text = parseWBXML(parser, true, b64, auth_md5);
218                         }
219                     }
220
221                     buf.append(text);
222                     break;
223
224                 case Xml.END_TAG:
225                     buf.append("</");
226                     buf.append(event.getName());
227                     buf.append(">");
228                     leave = true;
229                     break;
230
231                 case Xml.END_DOCUMENT:
232                     leave = true;
233                     break;
234
235                 case Xml.TEXT:
236                     buf.append(event.getText());
237                     break;
238
239                 case Xml.WAP_EXTENSION:
240                     text = event.getText();
241
242                     if (event instanceof WapExtensionEvent) {
243                         WapExtensionEvent e = (WapExtensionEvent)event;
244                         Object JavaDoc content = e.getContent();
245
246                         if (auth_md5 && !b64 && content != null) {
247
248                             if (content instanceof byte[]) {
249                                 text = new String JavaDoc(Base64.encode( (byte[])content));
250                             }
251                         }
252                     }
253
254                     buf.append(text);
255                     break;
256
257                 case Xml.WHITESPACE:
258                     break;
259
260                 default:
261             }
262         } while (!leave);
263
264         return buf.toString();
265     }
266 }
Popular Tags