1 18 package sync4j.server.notification; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.StringReader ; 24 import java.util.Vector ; 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 42 public class WBXMLTools { 43 44 46 48 50 59 public static byte[] toWBXML(final String s) throws Sync4jException { 60 WapProvisioningDocWriter writer = null; 61 ByteArrayOutputStream out = new ByteArrayOutputStream (); 62 63 try { 64 writer = new WapProvisioningDocWriter(out); 65 XmlParser xml = new XmlParser(new StringReader (s)); 66 traverseXML(xml, writer); 67 } catch (IOException e) { 68 throw new Sync4jException(e.getMessage(), e); 69 } finally { 70 if (writer != null)try {writer.close(); 71 } catch (Exception e) {} 72 } 73 74 return out.toByteArray(); 75 } 76 77 86 public static byte[] toWBXML(WapProvisioningDoc msg) throws Exception { 87 return toWBXML(msg.toXml()); 88 } 89 90 private static void traverseXML(XmlParser parser, WapProvisioningDocWriter writer) throws 92 IOException { 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 name = event.getName(); 104 105 tagWriter = writer; 106 tagWriter.startTag(name); 107 108 110 Vector 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); 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 155 public static String wbxmlToXml(final byte[] msg) throws Sync4jException { 156 157 ByteArrayInputStream in = null; 158 try { 159 in = new ByteArrayInputStream (msg); 160 161 WapProvisioningDocParser parser = new WapProvisioningDocParser(in); 162 163 return parseWBXML(parser); 164 165 } catch (Throwable t) { 166 t.printStackTrace(); 167 throw new Sync4jException(t.getMessage(), t); 168 } 169 } 170 171 private static String parseWBXML(WapProvisioningDocParser parser) throws IOException { 172 return parseWBXML(parser, false, false, false); 173 } 174 175 private static String parseWBXML(WapProvisioningDocParser parser, boolean inCred, boolean b64, 176 boolean auth_md5) throws IOException { 177 178 StringBuffer buf = new StringBuffer (); 179 boolean leave = false; 180 181 String tagName = null; 182 String 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 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 content = e.getContent(); 245 246 if (auth_md5 && !b64 && content != null) { 247 248 if (content instanceof byte[]) { 249 text = new String (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 |