Improve robustness of AvatarMetadata parser.
authorDa Risk <darisk972@gmail.com>
Sat, 05 Mar 2011 17:44:41 +0100
changeset 879 2236fe5b2db1
parent 878 d051db285d7e
child 880 d23d8ad3b9ba
Improve robustness of AvatarMetadata parser.
src/com/beem/project/beem/smack/avatar/AvatarMetadataProvider.java
--- a/src/com/beem/project/beem/smack/avatar/AvatarMetadataProvider.java	Sat Feb 26 16:47:59 2011 +0100
+++ b/src/com/beem/project/beem/smack/avatar/AvatarMetadataProvider.java	Sat Mar 05 17:44:41 2011 +0100
@@ -70,16 +70,34 @@
 	    int eventType = parser.next();
 	    if (eventType == XmlPullParser.START_TAG) {
 		if ("info".equals(parser.getName())) {
-		    int bytes = Integer.parseInt(parser.getAttributeValue(null, "bytes"));
-		    int height = Integer.parseInt(parser.getAttributeValue(null, "height"));
-		    int width = Integer.parseInt(parser.getAttributeValue(null, "width"));
 		    String id = parser.getAttributeValue(null, "id");
 		    String type = parser.getAttributeValue(null, "type");
+		    String sbytes = parser.getAttributeValue(null, "bytes");
+		    String sheight = parser.getAttributeValue(null, "height");
+		    String swidth = parser.getAttributeValue(null, "width");
+		    int bytes = 0;
+		    AvatarMetadataExtension.Info info = null;
+		    try {
+			if (sbytes != null)
+			    bytes = Integer.parseInt(sbytes);
+		    } catch (NumberFormatException e) { }
+		    if (bytes != 0 && id != null && type != null)
+			info = new AvatarMetadataExtension.Info(id, type, bytes);
+		    else // invalid info
+			continue;
+
 		    String url = parser.getAttributeValue(null, "url");
-		    AvatarMetadataExtension.Info info = new AvatarMetadataExtension.Info(id, type, bytes);
-		    info.setHeight(height);
-		    info.setWidth(width);
 		    info.setUrl(url);
+		    try {
+			int height = 0;
+			int width = 0;
+			if (sheight != null)
+			    height = Integer.parseInt(parser.getAttributeValue(null, "height"));
+			if (swidth != null)
+			    width = Integer.parseInt(parser.getAttributeValue(null, "width"));
+			info.setHeight(height);
+			info.setWidth(width);
+		    } catch (NumberFormatException e) { }
 		    metadata.addInfo(info);
 		}
 	    } else if (eventType == XmlPullParser.END_TAG) {