soapserializationenvelope.java

来自「it is a tools for developing J2ME applic」· Java 代码 · 共 725 行 · 第 1/2 页

JAVA
725
字号
                else if (name == null && namespace == null) {
                    if (parser.getAttributeValue(enc, "arrayType") != null) {
                        namespace = enc;
                        name = "Array";
                    }
                    else {
                        Object[] names = getInfo(expected.type, null);

                        namespace = (String) names[0];
                        name = (String) names[1];

                        //  System.out.println ("getInfo for "+expected.type+": {"+namespace+"}"+name);
                    }
                }

                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);

        //  System.out.println ("leaving read");

        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 {

        /*  if (xsdNamespace.equals (namespace)) {
            if ("int".equals (name)) 
            return new Integer (Integer.parseInt (readText (parser)));
            else if ("long".equals (name))
            return new Long (Long.parseLong (readText (parser)));
            else if ("string".equals (name))
            return readText (parser);
            else if ("boolean".equals (name)) 
            return new Boolean (SoapEnvelope.stringToBoolean (readText (parser)));
            }*/

        Class clazz = null;

        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, "anyType", 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 });

        //    if (prefixMap.getPrefix(namespace) == null)
        //       prefixMap = new PrefixMap(prefixMap, "n" + (cnt++), namespace);
    }

    /** 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);

        //     if (prefixMap.getPrefix(so.namespace) == null)
        //        prefixMap = new PrefixMap(prefixMap, "n" + (cnt++), so.namespace);
    }

    public Object getResult() {
        KvmSerializable ks = (KvmSerializable) bodyIn;
        return ks.getPropertyCount() == 0 ? null : ks.getProperty(0);
    }

    /** Serializes the given object */

    public void writeBody(XmlSerializer writer) throws IOException {

        multiRef = new Vector();
        multiRef.addElement(bodyOut);
        types.addElement(PropertyInfo.OBJECT_TYPE);

        for (int i = 0; i < multiRef.size(); i++) {

            Object obj = multiRef.elementAt(i);

            Object[] qName = getInfo(null, obj);

            writer.startTag((String) qName[0], (String) qName[1]);
            writer.attribute(
                null,
                "id",
                qName[2] == null ? ("o" + i) : (String) qName[2]);

            if (i == 0)
                writer.attribute(enc, "root", "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,
                    ((PropertyInfo) types.elementAt(i)).elementType);
            else
                throw new RuntimeException("Cannot serialize: " + obj);

            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() : "";

        for (int i = 0; i < cnt; i++) {

            obj.getPropertyInfo(i, properties, info);

            //      Object value = obj.getProperty (i);

            if ((info.flags & PropertyInfo.TRANSIENT) == 0) {
                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" : "null", "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",
                qName[2] == null ? ("#o" + i) : "#" + qName[2]);
        }
        else {

            if (!implicitTypes || obj.getClass() != type.type) {

                String prefix = writer.getPrefix((String) qName[0], true);

                /*                if (prefix == null)
                                    throw new RuntimeException(
                                        "Prefix for namespace " + qName[0] + " undefined!");
                */
                writer.attribute(xsi, "type", 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,
            "arrayType",
            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");
                if (skipped) {
                    writer.attribute(enc, "position", "[" + i + "]");
                    skipped = false;
                }

                writeProperty(writer, vector.elementAt(i), elementType);
                writer.endTag(null, "item");
            }
        }
    }

}

⌨️ 快捷键说明

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