📄 soapserializationenvelope.java
字号:
/* Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */package org.ksoap2.serialization;import java.util.*;import java.io.*;import org.ksoap2.*;import org.xmlpull.v1.*;/** * @author Stefan Haustein * * This class extends the SoapEnvelope with * Soap Serialization functionality. */public class SoapSerializationEnvelope extends SoapEnvelope { static final Marshal DEFAULT_MARSHAL = new DM(); public Hashtable properties = new Hashtable(); Hashtable idMap = new Hashtable(); Vector multiRef; // = new Vector(); Vector types = new Vector(); public boolean implicitTypes; /** * Set this variable to true for compatibility with what seems to be * the default encoding for .Net-Services. This feature is an extremely * ugly hack. A much better * option is to change the configuration of the .Net-Server to * standard Soap Serialization! */ public boolean dotNet; /** * Map from XML qualified names to Java classes */ protected Hashtable qNameToClass = new Hashtable(); /** * Map from Java class names to XML name and namespace pairs */ protected Hashtable classToQName = new Hashtable(); public SoapSerializationEnvelope(int version) { super(version); addMapping(enc, "Array", PropertyInfo.VECTOR_CLASS); DEFAULT_MARSHAL.register(this); } public void parseBody(XmlPullParser parser) throws IOException, XmlPullParserException { bodyIn = null; //System.out.println ("start parsing...."); parser.nextTag(); if (parser.getEventType() == XmlPullParser.START_TAG && parser.getNamespace().equals(env) && parser.getName().equals("Fault")) { SoapFault fault = new SoapFault(); fault.parse(parser); bodyIn = fault; } else { while (parser.getEventType() == XmlPullParser.START_TAG) { // int type = parser.getEventType(); // if (type == XmlPullParser.END_TAG || type == XmlPullParser.END_DOCUMENT) // break; // String name = namespaceMap.getPackage (start.getNamespace ()) // + "." + start.getName (); String rootAttr = parser.getAttributeValue(enc, "root"); Object o = read( parser, null, -1, parser.getNamespace(), parser.getName(), PropertyInfo.OBJECT_TYPE); if ("1".equals(rootAttr) || bodyIn == null) bodyIn = o; parser.nextTag(); } } //System.out.println ("leaving root read"); } protected void readSerializable(XmlPullParser parser, KvmSerializable obj) throws IOException, XmlPullParserException { int testIndex = -1; // inc at beg. of loop for perf. reasons int sourceIndex = 0; int cnt = obj.getPropertyCount(); PropertyInfo info = new PropertyInfo(); while (true) { if (parser.nextTag() == XmlPullParser.END_TAG) break; String name = parser.getName(); // System.out.println ("tagname:"+name); int countdown = cnt; while (true) { if (countdown-- == 0) throw new RuntimeException("Unknwon Property: " + name); if (++testIndex >= cnt) testIndex = 0; obj.getPropertyInfo(testIndex, properties, info); if (info.name == null ? testIndex == sourceIndex : (info.name.equals(name) && info == null || info.namespace.equals(parser.getNamespace()))) break; } obj.setProperty( testIndex, read(parser, obj, testIndex, null, null, info)); sourceIndex = 0; } parser.require(XmlPullParser.END_TAG, null, null); } /** * If the type of the object cannot be determined, and thus * no Marshal class can handle the object, this method is * called. It will build either a SoapPrimitive or a SoapObject * * @param parser * @param typeNamespace * @param typeName * @return * @throws IOException * @throws XmlPullParserException */ protected Object readUnknown( XmlPullParser parser, String typeNamespace, String typeName) throws IOException, XmlPullParserException { String name = parser.getName(); String namespace = parser.getNamespace(); parser.next(); // move to text, inner start tag or end tag Object result = null; String text = null; if (parser.getEventType() == XmlPullParser.TEXT) { text = parser.getText(); result = new SoapPrimitive(typeNamespace, typeName, text); parser.next(); } else if (parser.getEventType() == XmlPullParser.END_TAG) { result = new SoapObject(typeNamespace, typeName); } if (parser.getEventType() == XmlPullParser.START_TAG) { if (text != null && text.trim().length() != 0) { throw new RuntimeException("Malformed input: Mixed content"); } SoapObject so = new SoapObject(typeNamespace, typeName); while (parser.getEventType() != XmlPullParser.END_TAG) { so.addProperty( parser.getName(), read( parser, so, so.getPropertyCount(), null, null, PropertyInfo.OBJECT_TYPE)); parser.nextTag(); } result = so; } parser.require(XmlPullParser.END_TAG, namespace, name); return result; } private int getIndex(String value, int start, int dflt) { if (value == null) return dflt; return value.length() - start < 3 ? dflt : Integer.parseInt(value.substring(start + 1, value.length() - 1)); } /** Reads a vector. Precondition: on "outer" start tag! */ protected void readVector( XmlPullParser parser, Vector v, PropertyInfo elementType) throws IOException, XmlPullParserException { String namespace = null; String name = null; int size = v.size(); boolean dynamic = true; String type = parser.getAttributeValue(enc, "arrayType"); if (type != null) { int cut0 = type.indexOf(':'); int cut1 = type.indexOf("[", cut0); name = type.substring(cut0 + 1, cut1); String prefix = cut0 == -1 ? "" : type.substring(0, cut0); namespace = parser.getNamespace(prefix); size = getIndex(type, cut1, -1); if (size != -1) { v.setSize(size); dynamic = false; } } if (elementType == null) elementType = PropertyInfo.OBJECT_TYPE; parser.nextTag(); int position = getIndex(parser.getAttributeValue(enc, "offset"), 0, 0); while (parser.getEventType() != XmlPullParser.END_TAG) { // handle position position = getIndex( parser.getAttributeValue(enc, "position"), 0, position); if (dynamic && position >= size) { size = position + 1; v.setSize(size); } // implicit handling of position exceeding specified size v.setElementAt( read(parser, v, position, namespace, name, elementType), position); position++; parser.nextTag(); } parser.require(XmlPullParser.END_TAG, null, null); } /** * Builds an object from the XML stream. This method * is public for usage in conjuction with Marshal subclasses. * Precondition: On the start tag of the object or property, * so href can be read. */ public Object read( XmlPullParser parser, Object owner, int index, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException { // System.out.println ("in read"); // determine wire element type String elementName = parser.getName(); String href = parser.getAttributeValue(null, "href"); Object obj; if (href != null) { if (owner == null) throw new RuntimeException("href at root level?!?"); href = href.substring(1); obj = idMap.get(href); if (obj == null || obj instanceof FwdRef) { FwdRef f = new FwdRef(); f.next = (FwdRef) obj; f.obj = owner; f.index = index; idMap.put(href, f); obj = null; } parser.nextTag(); // start tag parser.require(XmlPullParser.END_TAG, null, elementName); } else { String nullAttr = parser.getAttributeValue(xsi, "nil"); String id = parser.getAttributeValue(null, "id"); if (nullAttr == null) nullAttr = parser.getAttributeValue(xsi, "null"); if (nullAttr != null && SoapEnvelope.stringToBoolean(nullAttr)) { obj = null; parser.nextTag(); parser.require(XmlPullParser.END_TAG, null, elementName); } else { String type = parser.getAttributeValue(xsi, "type"); if (type != null) { int cut = type.indexOf(':'); name = type.substring(cut + 1); String prefix = cut == -1 ? "" : type.substring(0, cut); namespace = parser.getNamespace(prefix); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -