1 18 package sync4j.framework.tools; 19 20 import java.io.*; 21 import java.util.Vector ; 22 23 import org.kxml.parser.ParseEvent; 24 import org.kxml.parser.XmlParser; 25 import org.kxml.parser.Tag; 26 import org.kxml.wap.SyncMLWriter; 27 import org.kxml.wap.SyncMLParser; 28 import org.kxml.wap.WapExtensionEvent; 29 import org.kxml.Xml; 30 import org.kxml.Attribute; 31 32 import sync4j.framework.core.SyncML; 33 import sync4j.framework.core.Util; 34 import sync4j.framework.core.Sync4jException; 35 36 import org.jibx.runtime.*; 37 import org.jibx.runtime.impl.*; 38 39 import org.apache.commons.lang.StringUtils; 40 41 47 public class WBXMLTools { 48 49 public static final String WELL_KNOWN_NS = ",DevInf,"; 51 52 private static String verDTD; 54 55 57 66 public static byte[] toWBXML(final String s) 67 throws Sync4jException { 68 SyncMLWriter writer = null; 69 try { 70 writer = new SyncMLWriter(verDTD); 71 XmlParser xml = new XmlParser(new StringReader(s)); 72 traverseXML(xml, writer); 73 } catch (IOException e) { 74 throw new Sync4jException(e.getMessage(), e); 75 } finally { 76 if (writer != null) try {writer.close();} catch (Exception e) {} 77 } 78 79 return writer.getBytes(); 80 } 81 82 96 public static byte[] toWBXML(SyncML msg) 97 throws Sync4jException { 98 99 verDTD = msg.getSyncHdr().getVerDTD().getValue(); 100 101 try { 102 103 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 104 105 IBindingFactory f = BindingDirectory.getFactory(SyncML.class); 106 IMarshallingContext c = f.createMarshallingContext(); 107 c.setIndent(0); 108 c.marshalDocument(msg, "UTF-8", null, bout); 109 110 String inputXml = new String (bout.toByteArray()); 111 112 return toWBXML(inputXml); 113 114 } catch(Exception e) { 115 e.printStackTrace(); 116 } 117 return null; 118 } 119 120 132 public static String wbxmlToXml(final byte[] msg) 133 throws Sync4jException { 134 return wbxmlToXml(msg, null); 135 } 136 137 149 public static String wbxmlToXml(final byte[] msg, String charset) 150 throws Sync4jException { 151 152 ByteArrayInputStream in = null; 153 try { 154 in = new ByteArrayInputStream(msg); 155 156 SyncMLParser parser = new SyncMLParser(in, charset); 157 158 return parseWBXML(parser); 159 } catch (Throwable t) { 160 throw new Sync4jException(t.getMessage(), t); 161 } 162 } 163 164 public static boolean isWellKnownNamespace(String ns) { 165 return (WELL_KNOWN_NS.indexOf(',' + ns + ',') >= 0); 166 } 167 168 private static void traverseXML(XmlParser parser, SyncMLWriter writer) throws IOException { 170 178 boolean leave = false; 179 180 do { 181 ParseEvent event = parser.read(); 182 183 switch (event.getType()) { 184 case Xml.START_TAG: 185 SyncMLWriter tagWriter = null; 186 187 String name = event.getName(); 188 189 if (isWellKnownNamespace(name)) { 190 tagWriter = new SyncMLWriter(((Tag)event).getNamespace(), verDTD); 191 } else { 192 tagWriter = writer; 193 } 194 195 tagWriter.startTag(name); 197 traverseXML(parser, tagWriter); 199 if (tagWriter != writer) { 200 tagWriter.close(); 201 writer.writeOpaque(new String (tagWriter.getBytes())); 202 tagWriter = null; 203 } 204 break; 205 206 case Xml.END_TAG: 207 writer.endTag(); 208 leave = true; 209 break; 210 211 case Xml.END_DOCUMENT: 212 leave = true; 213 break; 214 215 case Xml.TEXT: 216 writer.write(event.getText()); 217 break; 218 219 case Xml.WHITESPACE: 220 break; 221 222 default: 223 } 224 } while (!leave); 225 } 226 227 private static String parseWBXML(SyncMLParser parser) throws IOException { 228 boolean[] inTag = new boolean[6]; 229 return parseWBXML(parser, inTag); 230 } 231 232 private static String parseWBXML(SyncMLParser parser, boolean[] inTag) throws IOException{ 233 241 StringBuffer buf=new StringBuffer (); 242 boolean leave = false; 243 244 String tagName = null; 245 String text = null; 246 247 do { 248 ParseEvent event = parser.read(); 249 switch (event.getType()) { 250 case Xml.START_TAG: 251 tagName = event.getName(); 252 253 buf.append("<"); 254 buf.append(tagName); 255 Vector attrs=event.getAttributes(); 256 if(attrs!=null){ 257 for(int i=0;i<attrs.size();i++){ 258 Attribute attr=(Attribute)attrs.elementAt(i); 259 buf.append(" "); 260 buf.append(attr.getName()); 261 buf.append("='"); 262 buf.append(attr.getValue()); 263 buf.append("'"); 264 } 265 } 266 buf.append(">"); 267 268 if (!inTag[0]) { 273 inTag[0] = ("Put".equals(tagName) || "Results".equals(tagName)); 274 } 275 276 if (!inTag[0]) { 277 if (!inTag[1]) { 278 inTag[1] = "Item".equals(tagName); 279 } else if (inTag[1]) { 280 inTag[2] = "Data".equals(tagName); 281 } 282 } 283 284 if (!inTag[3]) { 289 inTag[3] = "Cred".equals(tagName); 290 } 291 292 text = parseWBXML(parser, inTag); 293 294 if (inTag[3]) { 295 if ("Meta".equals(tagName)) { 296 inTag[4] = (text.indexOf("b64") >= 0); 297 inTag[5] = (text.indexOf("auth-md5") >= 0); 298 buf.append(text); 299 text = parseWBXML(parser, inTag); 300 } 301 } 302 303 buf.append(text); 304 break; 305 306 case Xml.END_TAG: 307 if (tagName != null) { 308 if (tagName.equals("Put") || tagName.equals("Results")) { 309 if (inTag[0]) { 310 inTag[0] = false; 311 } 312 } else if (tagName.equals("Cred")) { 313 if (inTag[3]) { 314 inTag[3] = false; 315 } 316 } else if (tagName.equals("Item")) { 317 if (inTag[1]) { 318 inTag[1] = false; 319 } 320 } else if (tagName.equals("Data")) { 321 if (inTag[2]) { 322 inTag[2] = false; 323 } 324 } 325 } 326 buf.append("</"); 327 buf.append(event.getName()); 328 buf.append(">"); 329 leave = true; 330 break; 331 332 case Xml.END_DOCUMENT: 333 leave = true; 334 break; 335 336 case Xml.TEXT: 337 text = event.getText(); 338 339 if (!inTag[0] && inTag[1] && inTag[2]) { 340 text = replaceDataContent(text); 341 } 342 buf.append(text); 343 break; 344 345 case Xml.WAP_EXTENSION: 346 text = event.getText(); 347 348 if (!inTag[0] && inTag[1] && inTag[2]) { 349 text = replaceDataContent(text); 350 } 351 352 if (event instanceof WapExtensionEvent) { 353 WapExtensionEvent e = (WapExtensionEvent)event; 354 Object content = e.getContent(); 355 356 if (inTag[5] && !inTag[4] && content != null) { 357 if (content instanceof byte[]) { 358 text = new String (Base64.encode((byte[])content)); 359 } 360 } 361 } 362 363 buf.append(text); 364 break; 365 366 case Xml.WHITESPACE: 367 break; 368 369 default: 370 } 371 } while (!leave); 372 373 return buf.toString(); 374 } 375 376 383 private static String replaceDataContent(String text) { 384 text = StringUtils.replace(text, "&", "&"); 385 text = StringUtils.replace(text, "<", "<"); 386 text = StringUtils.replace(text, ">", ">"); 387 text = StringUtils.replace(text, "\"", """); 388 return text; 389 } 390 } 391 | Popular Tags |