001/**
002 *
003 * Copyright © 2014-2015 Florian Schmaus
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.smack.provider;
018
019import java.io.IOException;
020import java.lang.reflect.InvocationTargetException;
021
022import org.jivesoftware.smack.SmackException;
023import org.jivesoftware.smack.packet.IQ;
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.util.ParserUtils;
026import org.xmlpull.v1.XmlPullParser;
027import org.xmlpull.v1.XmlPullParserException;
028
029public class IntrospectionProvider{
030
031    // Unfortunately, we have to create two introspection providers, with the exactly the same code here
032
033    public static abstract class IQIntrospectionProvider<I extends IQ> extends IQProvider<I> {
034        private final Class<I> elementClass;
035
036        protected IQIntrospectionProvider(Class<I> elementClass) {
037            this.elementClass = elementClass;
038        }
039
040        @SuppressWarnings("unchecked")
041        @Override
042        public I parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
043                        SmackException {
044            try {
045                return (I) parseWithIntrospection(elementClass, parser, initialDepth);
046            }
047            catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
048                            | IllegalArgumentException | InvocationTargetException | ClassNotFoundException e) {
049                throw new SmackException(e);
050            }
051        }
052    }
053
054    public static abstract class PacketExtensionIntrospectionProvider<PE extends ExtensionElement> extends ExtensionElementProvider<PE> {
055        private final Class<PE> elementClass;
056
057        protected PacketExtensionIntrospectionProvider(Class<PE> elementClass) {
058            this.elementClass = elementClass;
059        }
060
061        @SuppressWarnings("unchecked")
062        @Override
063        public PE parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
064                        SmackException {
065            try {
066                return (PE) parseWithIntrospection(elementClass, parser, initialDepth);
067            }
068            catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
069                            | IllegalArgumentException | InvocationTargetException | ClassNotFoundException e) {
070                throw new SmackException(e);
071            }
072        }
073    }
074
075    public static Object parseWithIntrospection(Class<?> objectClass,
076                    XmlPullParser parser, final int initialDepth) throws NoSuchMethodException, SecurityException,
077                    InstantiationException, IllegalAccessException, XmlPullParserException,
078                    IOException, IllegalArgumentException, InvocationTargetException,
079                    ClassNotFoundException {
080        ParserUtils.assertAtStartTag(parser);
081        Object object = objectClass.newInstance();
082        outerloop: while (true) {
083            int eventType = parser.next();
084            switch (eventType) {
085            case XmlPullParser.START_TAG:
086                String name = parser.getName();
087                String stringValue = parser.nextText();
088                Class<?> propertyType = object.getClass().getMethod(
089                                "get" + Character.toUpperCase(name.charAt(0)) + name.substring(1)).getReturnType();
090                // Get the value of the property by converting it from a
091                // String to the correct object type.
092                Object value = decode(propertyType, stringValue);
093                // Set the value of the bean.
094                object.getClass().getMethod(
095                                "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1),
096                                propertyType).invoke(object, value);
097                break;
098
099            case  XmlPullParser.END_TAG:
100                if (parser.getDepth() == initialDepth) {
101                    break outerloop;
102                }
103                break;
104            }
105        }
106        ParserUtils.assertAtEndTag(parser);
107        return object;
108    }
109
110    /**
111     * Decodes a String into an object of the specified type. If the object
112     * type is not supported, null will be returned.
113     *
114     * @param type the type of the property.
115     * @param value the encode String value to decode.
116     * @return the String value decoded into the specified type.
117     * @throws ClassNotFoundException
118     */
119    private static Object decode(Class<?> type, String value) throws ClassNotFoundException {
120        String name = type.getName();
121        switch (name) {
122        case "java.lang.String":
123            return value;
124        case "boolean":
125            return Boolean.valueOf(value);
126        case "int":
127            return Integer.valueOf(value);
128        case "long":
129            return Long.valueOf(value);
130        case "float":
131            return Float.valueOf(value);
132        case "double":
133            return Double.valueOf(value);
134        case "short":
135            return Short.valueOf(value);
136        case "byte":
137            return Byte.valueOf(value);
138        case "java.lang.Class":
139            return Class.forName(value);
140        }
141        return null;
142    }
143}