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

📄 xmlrequestprocessor2.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

                String name = field.getName();
                SimpleXMLParserDocumentNode child = node.getChild(name);

                // There's no value for this field given in the XML.
                if (child == null)
                    continue;

                Class type = field.getType();

                debug_in("Deserialising field (\"" + name + "\", type: " + RPUtils.getName(type) + ")");

                if (type.isArray()) {
                    Class sub_type = type.getComponentType();

                    SimpleXMLParserDocumentNode[] entries = child.getChildren();

                    Object array = Array.newInstance(sub_type, entries.length);

                    /**
                     * hack. for request objects we deserialise the
                     * parameters array by using the method signature
                     */
                    String[] bits = null;
                    if (request_method != null) {
                        bits = MethodSignature.parse(request_method).arg_classes;
                    }
                    else {
                        int arr_length = Array.getLength(array);
                        bits = (String[])Collections.nCopies(arr_length, "String").toArray(new String[arr_length]);
                    }

                    debug_in("Attempting to deserialise " + entries.length + " entries for this field (which is an array type).");

                    for (int j=0; j<entries.length; j++) {

                        SimpleXMLParserDocumentNode array_child = entries[j];
                        SimpleXMLParserDocumentAttribute index_attr = array_child.getAttribute("index");
                        String index_str = index_attr==null?null:index_attr.getValue().trim();
                        int array_index = index_str==null?j:Integer.parseInt(index_str);
                        String bit = bits[array_index];
                        String sub_value = array_child.getValue().trim();
                        Object value_to_store = deserialiseValue(array_child, sub_value, bit, true);
                        Array.set(array, array_index, value_to_store);

                    }

                    debug_in("Finished deserialising array, about to set value.");
                    field.set(obj, array);
                    debug_in("Field \"" + name + "\" deserialised and set.");
                }
                else {
                    String value = child.getValue().trim();

                    // We need to look at remote objects - because we need to
                    // get the object field to store on RPRequest.
                    Object obj_value = deserialiseValue(child, value, RPUtils.getName(type), true);
                    field.set(obj, obj_value);
                    debug_in("Field \"" + name + "\" deserialised and set.");
                }
            } // end for loop
            return obj;

        }
        catch (RPException e) {
            throw e;
        }
    }

    protected void serialiseObject(Object obj, int original_modifier_filter, XMLElement container) {
        int modifier_filter = original_modifier_filter & (~(Modifier.TRANSIENT | Modifier.STATIC));

        Class cla = null;
        if (obj != null) {
        	cla = obj.getClass();
        }
        
        String cla_name = (cla == null) ? "null" : RPUtils.getName(cla);
        
        debug_out("Serialising object of type \"" + cla_name + "\"");
        String value = XMLSerialisationUtils.serialise(obj, cla);
        if (value != null) {
            debug_out("Value was easily serialised into a string format.");
            container.addContent(value);
            return;
        }

        if (cla.isArray()) {
            int len = Array.getLength(obj);
            debug_out("Object is array type - processing " + len + " item(s).");
            XMLElement entry_xml = null;
            for (int i=0;i<len;i++) {
                entry_xml = new XMLElement("ENTRY", true);
                entry_xml.addAttribute("index", i);
                serialiseObject(Array.get(obj, i), original_modifier_filter, entry_xml);
                container.addContent(entry_xml);
            }
            debug_out("Finished serialising array.");
            return;
        }

        String obj_descr = RPUtils.describeObject(obj);

        debug_out("Need to use XMLSerialisationUtils to serialise " + obj_descr);
        Map[] attribute_data = XMLSerialisationUtils.getAttributeData(obj, modifier_filter);
        Map attribute_types = attribute_data[0];
        Map attribute_values = attribute_data[1];

        debug_out("Going to process " + attribute_values.size() + " attributes on " + obj_descr);

        Iterator attr_itr = attribute_values.entrySet().iterator();
        Map.Entry me = null;
        String key = null;
        while (attr_itr.hasNext()) {
            me = (Map.Entry)attr_itr.next();
            key = (String)me.getKey();
            if (me.getValue() != null) {
                debug_out("About to serialise attribute \"" + key + "\"");
                Class attr_class = (Class)attribute_types.get(key);
                if (attr_class == null) {
                    throw new NullPointerException("Trying to serialise attribute on " + obj + " (which belongs to " + container + ") - attrname: " + key + ", attrvalue: " + me.getValue() + ", but type not given!");
                }
                XMLElement attribute_content = container.makeContent(key, !attr_class.isArray());
                serialiseObject(me.getValue(), original_modifier_filter, attribute_content);
            }
            else {
                debug_out("Attribute \"" + key + "\" was null, skipping.");
            }
        }
        debug_out("Finished serialising " + obj_descr);
    }

    public Object deserialiseValue(SimpleXMLParserDocumentNode node, String string_value, String class_type, boolean rp_lookup) {

        Object parsed_value = null;
        Class result_class = XMLSerialisationUtils.getClass(class_type);
        if (result_class != null) {
            debug_in("Attempting to deserialise simple value of type \"" + RPUtils.getName(result_class) + "\"");

            try {
                parsed_value = XMLSerialisationUtils.deserialise(string_value, result_class);
            }
            catch (Exception e) {
                throw new RPDeserialiseParseException(e, string_value, result_class);
            }
        }

        if (parsed_value != null) {
            debug_in("Deserialisation of simple value successful.");
            return parsed_value;
        }

        if (rp_lookup) {
            debug_in("About to see if there is an object reference to process.");
            SimpleXMLParserDocumentNode obj_node_parent, obj_node = null;

            /**
             * We'll be a bit flexible with how we handle object references.
             *
             * In most cases, they should be embedded in a OBJECT/_object_id
             * tag. However, that's not always possible - an RPRequest instance
             * never has the ID in an OBJECT tag.
             *
             * Given that we can be called when node is either the parent of
             * an OBJECT tag or an _object_id tag, we'll cope with both
             * situations.
             */
            obj_node_parent = node.getChild("OBJECT");
            if (obj_node_parent == null) {
                obj_node_parent = node;
            }

            obj_node = obj_node_parent.getChild("_object_id");

            if (obj_node != null) {
                debug_in("Found object ID node, processing...");
                String oid_str = obj_node.getValue().trim();
                long oid = Long.parseLong(oid_str);
                RPObject local_rp_obj = RPObject._lookupLocal(oid);

                /**
                 * If we need an RPObject, then we'll just use this object.
                 * (RPRequest wants a RPObject).
                 */
                if (class_type.equals("RPObject")) {
                    debug_in("Found object (type required was RPObject, so we don't need to unwrap the value).");
                    return local_rp_obj;
                }

                /**
                 * Otherwise, we want to provide the underlying object
                 * (like if we are passing a parameter).
                 */

                // Type check.
                Object local_obj = local_rp_obj._getDelegate();
                if (!RPUtils.issubclassByName(local_obj.getClass(), class_type)) {
                    throw new RPDeserialiseClassMismatchException(class_type, local_rp_obj._getName());
                }

                debug_in("Found object with ID - returning " + RPUtils.describeObject(local_obj));
                return local_obj;
            }
            else {
                debug_in("No object ID node found.");
            }
        }
        throw new RPUnsupportedInputTypeException(class_type);
    }

    // Logging.
    public void debug_in(String s) {
        if (deserialise_debug) {
            this.logger.log(s);
        }
    }

    public void debug_in(String s, Throwable t) {
        if (deserialise_debug) {
            this.logger.log(s, t);
        }
    }

    public void debug_out(String s) {
        if (serialise_debug) {
            this.logger.log(s);
        }
    }

    public void debug_out(String s, Throwable t) {
        if (serialise_debug) {
            this.logger.log(s, t);
        }
    }

    public void log_general(String s) {
        this.logger.log(s);
    }

}

⌨️ 快捷键说明

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