1 20 21 package org.jivesoftware.smack.util; 22 23 import java.beans.PropertyDescriptor ; 24 import java.util.Map ; 25 import java.util.Iterator ; 26 import java.util.HashMap ; 27 import java.io.ObjectInputStream ; 28 import java.io.ByteArrayInputStream ; 29 30 import org.jivesoftware.smack.packet.*; 31 import org.jivesoftware.smack.provider.PacketExtensionProvider; 32 import org.jivesoftware.smack.provider.ProviderManager; 33 import org.xmlpull.v1.XmlPullParser; 34 import org.xmlpull.v1.XmlPullParserException; 35 36 42 public class PacketParserUtils { 43 44 47 private static final String PROPERTIES_NAMESPACE = 48 "http://www.jivesoftware.com/xmlns/xmpp/properties"; 49 50 57 public static Packet parseMessage(XmlPullParser parser) throws Exception { 58 Message message = new Message(); 59 String id = parser.getAttributeValue("", "id"); 60 message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id); 61 message.setTo(parser.getAttributeValue("", "to")); 62 message.setFrom(parser.getAttributeValue("", "from")); 63 message.setType(Message.Type.fromString(parser.getAttributeValue("", "type"))); 64 65 boolean done = false; 69 String subject = null; 70 String body = null; 71 String thread = null; 72 Map properties = null; 73 while (!done) { 74 int eventType = parser.next(); 75 if (eventType == XmlPullParser.START_TAG) { 76 String elementName = parser.getName(); 77 String namespace = parser.getNamespace(); 78 if (elementName.equals("subject")) { 79 if (subject == null) { 80 subject = parser.nextText(); 81 } 82 } 83 else if (elementName.equals("body")) { 84 if (body == null) { 85 body = parser.nextText(); 86 } 87 } 88 else if (elementName.equals("thread")) { 89 if (thread == null) { 90 thread = parser.nextText(); 91 } 92 } 93 else if (elementName.equals("error")) { 94 message.setError(parseError(parser)); 95 } 96 else if (elementName.equals("properties") && 97 namespace.equals(PROPERTIES_NAMESPACE)) 98 { 99 properties = parseProperties(parser); 100 } 101 else { 103 message.addExtension( 104 PacketParserUtils.parsePacketExtension(elementName, namespace, parser)); 105 } 106 } 107 else if (eventType == XmlPullParser.END_TAG) { 108 if (parser.getName().equals("message")) { 109 done = true; 110 } 111 } 112 } 113 message.setSubject(subject); 114 message.setBody(body); 115 message.setThread(thread); 116 if (properties != null) { 118 for (Iterator i=properties.keySet().iterator(); i.hasNext(); ) { 119 String name = (String )i.next(); 120 message.setProperty(name, properties.get(name)); 121 } 122 } 123 return message; 124 } 125 126 133 public static Presence parsePresence(XmlPullParser parser) throws Exception { 134 Presence.Type type = Presence.Type.fromString(parser.getAttributeValue("", "type")); 135 136 Presence presence = new Presence(type); 137 presence.setTo(parser.getAttributeValue("", "to")); 138 presence.setFrom(parser.getAttributeValue("", "from")); 139 String id = parser.getAttributeValue("", "id"); 140 presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id); 141 142 boolean done = false; 144 while (!done) { 145 int eventType = parser.next(); 146 if (eventType == XmlPullParser.START_TAG) { 147 String elementName = parser.getName(); 148 String namespace = parser.getNamespace(); 149 if (elementName.equals("status")) { 150 presence.setStatus(parser.nextText()); 151 } 152 else if (elementName.equals("priority")) { 153 try { 154 int priority = Integer.parseInt(parser.nextText()); 155 presence.setPriority(priority); 156 } 157 catch (NumberFormatException nfe) { } 158 catch (IllegalArgumentException iae) { 159 presence.setPriority(0); 161 } 162 } 163 else if (elementName.equals("show")) { 164 presence.setMode(Presence.Mode.fromString(parser.nextText())); 165 } 166 else if (elementName.equals("error")) { 167 presence.setError(parseError(parser)); 168 } 169 else if (elementName.equals("properties") && 170 namespace.equals(PROPERTIES_NAMESPACE)) 171 { 172 Map properties = parseProperties(parser); 173 for (Iterator i=properties.keySet().iterator(); i.hasNext(); ) { 175 String name = (String )i.next(); 176 presence.setProperty(name, properties.get(name)); 177 } 178 } 179 else { 181 presence.addExtension( 182 PacketParserUtils.parsePacketExtension(elementName, namespace, parser)); 183 } 184 } 185 else if (eventType == XmlPullParser.END_TAG) { 186 if (parser.getName().equals("presence")) { 187 done = true; 188 } 189 } 190 } 191 return presence; 192 } 193 194 205 public static Map parseProperties(XmlPullParser parser) throws Exception { 206 Map properties = new HashMap (); 207 while (true) { 208 int eventType = parser.next(); 209 if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) { 210 boolean done = false; 212 String name = null; 213 String type = null; 214 String valueText = null; 215 Object value = null; 216 while (!done) { 217 eventType = parser.next(); 218 if (eventType == XmlPullParser.START_TAG) { 219 String elementName = parser.getName(); 220 if (elementName.equals("name")) { 221 name = parser.nextText(); 222 } 223 else if (elementName.equals("value")) { 224 type = parser.getAttributeValue("", "type"); 225 valueText = parser.nextText(); 226 } 227 } 228 else if (eventType == XmlPullParser.END_TAG) { 229 if (parser.getName().equals("property")) { 230 if ("integer".equals(type)) { 231 value = new Integer (valueText); 232 } 233 else if ("long".equals(type)) { 234 value = new Long (valueText); 235 } 236 else if ("float".equals(type)) { 237 value = new Float (valueText); 238 } 239 else if ("double".equals(type)) { 240 value = new Double (valueText); 241 } 242 else if ("boolean".equals(type)) { 243 value = new Boolean (valueText); 244 } 245 else if ("string".equals(type)) { 246 value = valueText; 247 } 248 else if ("java-object".equals(type)) { 249 try { 250 byte [] bytes = StringUtils.decodeBase64(valueText); 251 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (bytes)); 252 value = in.readObject(); 253 } 254 catch (Exception e) { 255 e.printStackTrace(); 256 } 257 } 258 if (name != null && value != null) { 259 properties.put(name, value); 260 } 261 done = true; 262 } 263 } 264 } 265 } 266 else if (eventType == XmlPullParser.END_TAG) { 267 if (parser.getName().equals("properties")) { 268 break; 269 } 270 } 271 } 272 return properties; 273 } 274 275 282 public static XMPPError parseError(XmlPullParser parser) throws Exception { 283 String errorCode = "-1"; 284 String message = null; 285 for (int i=0; i<parser.getAttributeCount(); i++) { 286 if (parser.getAttributeName(i).equals("code")) { 287 errorCode = parser.getAttributeValue("", "code"); 288 } 289 } 290 try { 292 message = parser.nextText(); 293 } 294 catch (XmlPullParserException ex) {} 295 while (true) { 296 if (parser.getEventType() == XmlPullParser.END_TAG && parser.getName().equals("error")) { 297 break; 298 } 299 parser.next(); 300 } 301 return new XMPPError(Integer.parseInt(errorCode), message); 302 } 303 304 313 public static PacketExtension parsePacketExtension(String elementName, String namespace, XmlPullParser parser) 314 throws Exception 315 { 316 Object provider = ProviderManager.getExtensionProvider(elementName, namespace); 318 if (provider != null) { 319 if (provider instanceof PacketExtensionProvider) { 320 return ((PacketExtensionProvider)provider).parseExtension(parser); 321 } 322 else if (provider instanceof Class ) { 323 return (PacketExtension)parseWithIntrospection( 324 elementName, (Class )provider, parser); 325 } 326 } 327 DefaultPacketExtension extension = new DefaultPacketExtension(elementName, namespace); 329 boolean done = false; 330 while (!done) { 331 int eventType = parser.next(); 332 if (eventType == XmlPullParser.START_TAG) { 333 String name = parser.getName(); 334 if (parser.isEmptyElementTag()) { 336 extension.setValue(name,""); 337 } 338 else { 340 eventType = parser.next(); 341 if (eventType == XmlPullParser.TEXT) { 342 String value = parser.getText(); 343 extension.setValue(name, value); 344 } 345 } 346 } 347 else if (eventType == XmlPullParser.END_TAG) { 348 if (parser.getName().equals(elementName)) { 349 done = true; 350 } 351 } 352 } 353 return extension; 354 } 355 356 public static Object parseWithIntrospection(String elementName, 357 Class objectClass, XmlPullParser parser) throws Exception 358 { 359 boolean done = false; 360 Object object = objectClass.newInstance(); 361 while (!done) { 362 int eventType = parser.next(); 363 if (eventType == XmlPullParser.START_TAG) { 364 String name = parser.getName(); 365 String stringValue = parser.nextText(); 366 PropertyDescriptor descriptor = new PropertyDescriptor (name, objectClass); 367 Class propertyType = descriptor.getPropertyType(); 369 Object value = decode(propertyType, stringValue); 372 descriptor.getWriteMethod().invoke(object, new Object [] { value }); 374 } 375 else if (eventType == XmlPullParser.END_TAG) { 376 if (parser.getName().equals(elementName)) { 377 done = true; 378 } 379 } 380 } 381 return object; 382 } 383 384 392 private static Object decode(Class type, String value) throws Exception { 393 if (type.getName().equals("java.lang.String")) { 394 return value; 395 } 396 if (type.getName().equals("boolean")) { 397 return Boolean.valueOf(value); 398 } 399 if (type.getName().equals("int")) { 400 return Integer.valueOf(value); 401 } 402 if (type.getName().equals("long")) { 403 return Long.valueOf(value); 404 } 405 if (type.getName().equals("float")) { 406 return Float.valueOf(value); 407 } 408 if (type.getName().equals("double")) { 409 return Double.valueOf(value); 410 } 411 if (type.getName().equals("java.lang.Class")) { 412 return Class.forName(value); 413 } 414 return null; 415 } 416 417 } 418 | Popular Tags |