⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soapserializationenvelope.java

📁 里面包含ksoap2J2ME类库和ksoap2的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                } else if (name == null && namespace == null) {                    if (parser.getAttributeValue(enc, ARRAY_TYPE_LABEL) != null) {                        namespace = enc;                        name = ARRAY_MAPPING_NAME;                    } else {                        Object[] names = getInfo(expected.type, null);                        namespace = (String) names[0];                        name = (String) names[1];                    }                }                obj = readInstance(parser, namespace, name, expected);                if (obj == null)                    obj = readUnknown(parser, namespace, name);            }            // finally, care about the id....            if (id != null) {                Object hlp = idMap.get(id);                if (hlp instanceof FwdRef) {                    FwdRef f = (FwdRef) hlp;                    do {                        if (f.obj instanceof KvmSerializable)                            ((KvmSerializable) f.obj).setProperty(f.index, obj);                        else                            ((Vector) f.obj).setElementAt(obj, f.index);                        f = f.next;                    } while (f != null);                } else if (hlp != null)                    throw new RuntimeException("double ID");                idMap.put(id, obj);            }        }        parser.require(XmlPullParser.END_TAG, null, elementName);        return obj;    }    /**     * Returns a new object read from the given parser. If no mapping is found,     * null is returned. This method is used by the SoapParser in order to     * convert the XML code to Java objects.     */    public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException {        Object obj = qNameToClass.get(new SoapPrimitive(namespace, name, null));        if (obj == null)            return null;        if (obj instanceof Marshal)            return ((Marshal) obj).readInstance(parser, namespace, name, expected);        if (obj instanceof SoapObject)            obj = ((SoapObject) obj).newInstance();        else            try {                obj = ((Class) obj).newInstance();            } catch (Exception e) {                throw new RuntimeException(e.toString());            }        // ok, obj is now the instance, fill it....        if (obj instanceof KvmSerializable)            readSerializable(parser, (KvmSerializable) obj);        else if (obj instanceof Vector)            readVector(parser, (Vector) obj, expected.elementType);        else            throw new RuntimeException("no deserializer for " + obj.getClass());        return obj;    }    /**     * Returns a string array containing the namespace, name, id and Marshal     * object for the given java object. This method is used by the SoapWriter     * in order to map Java objects to the corresponding SOAP section five XML     * code.     */    public Object[] getInfo(Object type, Object instance) {        if (type == null) {            if (instance instanceof SoapObject || instance instanceof SoapPrimitive)                type = instance;            else                type = instance.getClass();        }        if (type instanceof SoapObject) {            SoapObject so = (SoapObject) type;            return new Object[] { so.getNamespace(), so.getName(), null, null };        }        if (type instanceof SoapPrimitive) {            SoapPrimitive sp = (SoapPrimitive) type;            return new Object[] { sp.getNamespace(), sp.getName(), null, DEFAULT_MARSHAL };        }        if ((type instanceof Class) && type != PropertyInfo.OBJECT_CLASS) {            Object[] tmp = (Object[]) classToQName.get(((Class) type).getName());            if (tmp != null)                return tmp;        }        return new Object[] { xsd, ANY_TYPE_LABEL, null, null };    }    /**     * Defines a direct mapping from a namespace and name to a java class (and     * vice versa), using the given marshal mechanism     */    public void addMapping(String namespace, String name, Class clazz, Marshal marshal) {        qNameToClass.put(new SoapPrimitive(namespace, name, null), marshal == null ? (Object) clazz : marshal);        classToQName.put(clazz.getName(), new Object[] { namespace, name, null, marshal });    }    /**     * Defines a direct mapping from a namespace and name to a java class (and     * vice versa)     */    public void addMapping(String namespace, String name, Class clazz) {        addMapping(namespace, name, clazz, null);    }    /**     * Adds a SoapObject to the class map. During parsing, objects of the given     * type (namespace/name) will be mapped to corresponding copies of the given     * SoapObject, maintaining the structure of the template.     */    public void addTemplate(SoapObject so) {        qNameToClass.put(new SoapPrimitive(so.namespace, so.name, null), so);    }    /**     * Response from the soap call. Pulls the object from the wrapper object and     * returns it.     *      * @since 2.0.3     * @return response from the soap call.     * @throws SoapFault     */    public Object getResponse() throws SoapFault {        if (bodyIn instanceof SoapFault) {            throw (SoapFault) bodyIn;        }        KvmSerializable ks = (KvmSerializable) bodyIn;        return ks.getPropertyCount() == 0 ? null : ks.getProperty(0);    }    /**     * @deprecated Please use the getResponse going forward     * @see #getResponse()     */    public Object getResult() {        KvmSerializable ks = (KvmSerializable) bodyIn;        return ks.getPropertyCount() == 0 ? null : ks.getProperty(0);    }    /**     * Serializes the request object to the given XmlSerliazer object     *      * @param writer     *            XmlSerializer object to write the body into.     */    public void writeBody(XmlSerializer writer) throws IOException {        multiRef = new Vector();        multiRef.addElement(bodyOut);        types.addElement(PropertyInfo.OBJECT_TYPE);        Object[] qName = getInfo(null, bodyOut);        writer.startTag((String) qName[0], (String) qName[1]);        writer.attribute(null, ID_LABEL, qName[2] == null ? ("o" + 0) : (String) qName[2]);        writer.attribute(enc, ROOT_LABEL, "1");        if (qName[3] != null)            ((Marshal) qName[3]).writeInstance(writer, bodyOut);        else if (bodyOut instanceof KvmSerializable)            writeObjectBody(writer, (KvmSerializable) bodyOut);        else if (bodyOut instanceof Vector)            writeVectorBody(writer, (Vector) bodyOut, ((PropertyInfo) types.elementAt(0)).elementType);        else            throw new RuntimeException("Cannot serialize: " + bodyOut);        writer.endTag((String) qName[0], (String) qName[1]);    }    /**     * Writes the body of an KvmSerializable object. This method is public for     * access from Marshal subclasses.     */    public void writeObjectBody(XmlSerializer writer, KvmSerializable obj) throws IOException {        PropertyInfo info = new PropertyInfo();        int cnt = obj.getPropertyCount();//        String namespace = dotNet ? writer.getNamespace() : ""; // unused...curious        for (int i = 0; i < cnt; i++) {            obj.getPropertyInfo(i, properties, info);            if ((info.flags & PropertyInfo.TRANSIENT) == 0) {                // TODO: looks broken here                String nsp = info.namespace;                if (nsp == null)                    nsp = info.namespace;                writer.startTag(nsp, info.name);                writeProperty(writer, obj.getProperty(i), info);                writer.endTag(nsp, info.name);            }        }    }    protected void writeProperty(XmlSerializer writer, Object obj, PropertyInfo type) throws IOException {        if (obj == null) {            writer.attribute(xsi, version >= VER12 ? NIL_LABEL : NULL_LABEL, "true");            return;        }        Object[] qName = getInfo(null, obj);        if (type.multiRef || qName[2] != null) {            int i = multiRef.indexOf(obj);            if (i == -1) {                i = multiRef.size();                multiRef.addElement(obj);                types.addElement(type);            }            writer.attribute(null, HREF_LABEL, qName[2] == null ? ("#o" + i) : "#" + qName[2]);        } else {            if (!implicitTypes || obj.getClass() != type.type) {                String prefix = writer.getPrefix((String) qName[0], true);                writer.attribute(xsi, TYPE_LABEL, prefix + ":" + qName[1]);            }            if (qName[3] != null)                ((Marshal) qName[3]).writeInstance(writer, obj);            else if (obj instanceof KvmSerializable)                writeObjectBody(writer, (KvmSerializable) obj);            else if (obj instanceof Vector)                writeVectorBody(writer, (Vector) obj, type.elementType);            else                throw new RuntimeException("Cannot serialize: " + obj);        }    }    protected void writeVectorBody(XmlSerializer writer, Vector vector, PropertyInfo elementType) throws IOException {        if (elementType == null)            elementType = PropertyInfo.OBJECT_TYPE;        int cnt = vector.size();        Object[] arrType = getInfo(elementType.type, null);        writer.attribute(enc, ARRAY_TYPE_LABEL, writer.getPrefix((String) arrType[0], false) + ":" + arrType[1] + "[" + cnt + "]");        boolean skipped = false;        for (int i = 0; i < cnt; i++) {            if (vector.elementAt(i) == null)                skipped = true;            else {                writer.startTag(null, ITEM_LABEL);                if (skipped) {                    writer.attribute(enc, "position", "[" + i + "]");                    skipped = false;                }                writeProperty(writer, vector.elementAt(i), elementType);                writer.endTag(null, ITEM_LABEL);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -