001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.jiveproperties.provider; 018 019import java.io.ByteArrayInputStream; 020import java.io.IOException; 021import java.io.ObjectInputStream; 022import java.util.HashMap; 023import java.util.Map; 024import java.util.logging.Level; 025import java.util.logging.Logger; 026 027import org.jivesoftware.smack.provider.ExtensionElementProvider; 028import org.jivesoftware.smack.util.stringencoder.Base64; 029import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager; 030import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension; 031import org.xmlpull.v1.XmlPullParser; 032import org.xmlpull.v1.XmlPullParserException; 033 034public class JivePropertiesExtensionProvider extends ExtensionElementProvider<JivePropertiesExtension> { 035 036 private static final Logger LOGGER = Logger.getLogger(JivePropertiesExtensionProvider.class.getName()); 037 038 /** 039 * Parse a properties sub-packet. If any errors occur while de-serializing Java object 040 * properties, an exception will be printed and not thrown since a thrown exception will shut 041 * down the entire connection. ClassCastExceptions will occur when both the sender and receiver 042 * of the stanza(/packet) don't have identical versions of the same class. 043 * <p> 044 * Note that you have to explicitly enabled Java object deserialization with @{link 045 * {@link JivePropertiesManager#setJavaObjectEnabled(boolean)} 046 * 047 * @param parser the XML parser, positioned at the start of a properties sub-packet. 048 * @return a map of the properties. 049 * @throws IOException 050 * @throws XmlPullParserException 051 */ 052 @Override 053 public JivePropertiesExtension parse(XmlPullParser parser, 054 int initialDepth) throws XmlPullParserException, 055 IOException { 056 Map<String, Object> properties = new HashMap<String, Object>(); 057 while (true) { 058 int eventType = parser.next(); 059 if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) { 060 // Parse a property 061 boolean done = false; 062 String name = null; 063 String type = null; 064 String valueText = null; 065 Object value = null; 066 while (!done) { 067 eventType = parser.next(); 068 if (eventType == XmlPullParser.START_TAG) { 069 String elementName = parser.getName(); 070 if (elementName.equals("name")) { 071 name = parser.nextText(); 072 } 073 else if (elementName.equals("value")) { 074 type = parser.getAttributeValue("", "type"); 075 valueText = parser.nextText(); 076 } 077 } 078 else if (eventType == XmlPullParser.END_TAG) { 079 if (parser.getName().equals("property")) { 080 if ("integer".equals(type)) { 081 value = Integer.valueOf(valueText); 082 } 083 else if ("long".equals(type)) { 084 value = Long.valueOf(valueText); 085 } 086 else if ("float".equals(type)) { 087 value = Float.valueOf(valueText); 088 } 089 else if ("double".equals(type)) { 090 value = Double.valueOf(valueText); 091 } 092 else if ("boolean".equals(type)) { 093 value = Boolean.valueOf(valueText); 094 } 095 else if ("string".equals(type)) { 096 value = valueText; 097 } 098 else if ("java-object".equals(type)) { 099 if (JivePropertiesManager.isJavaObjectEnabled()) { 100 try { 101 byte[] bytes = Base64.decode(valueText); 102 ObjectInputStream in = new ObjectInputStream( 103 new ByteArrayInputStream(bytes)); 104 value = in.readObject(); 105 } 106 catch (Exception e) { 107 LOGGER.log(Level.SEVERE, "Error parsing java object", e); 108 } 109 } 110 else { 111 LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)"); 112 } 113 } 114 if (name != null && value != null) { 115 properties.put(name, value); 116 } 117 done = true; 118 } 119 } 120 } 121 } 122 else if (eventType == XmlPullParser.END_TAG) { 123 if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) { 124 break; 125 } 126 } 127 } 128 return new JivePropertiesExtension(properties); 129 } 130 131}