📄 soapserializationenvelope.java
字号:
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);
} else if (obj instanceof SoapObject) {
// I think that this clause is totally useless since qNameToClass returns a class
// and not an instance so this case can never be reached.
obj = ((SoapObject) obj).newInstance();
} else if (obj == SoapObject.class) {
obj = new SoapObject(namespace, name);
} 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 SoapObject) {
readSerializable(parser, (SoapObject) obj);
} else 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]);
if (addAdornments) {
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 SoapObject) {
writeObjectBody(writer, (SoapObject) 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 SoapObject. This method write the attributes and then
* calls "writeObjectBody (writer, (KvmSerializable)obj);"
*/
public void writeObjectBody(XmlSerializer writer, SoapObject obj) throws IOException {
SoapObject soapObject = (SoapObject) obj;
for (int counter = 0; counter < soapObject.getAttributeCount(); counter++) {
AttributeInfo attributeInfo = new AttributeInfo();
soapObject.getAttributeInfo(counter, attributeInfo);
writer.attribute(attributeInfo.getNamespace(), attributeInfo.getName(), attributeInfo.getValue().toString());
}
writeObjectBody(writer, (KvmSerializable) obj);
}
/**
* 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 SoapObject) {
writeObjectBody(writer, (SoapObject) 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);
// I think that this needs an implicitTypes check, but don't have a failure case for that
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);
}
}
}
/**
* @return Returns the addAdornments.
*/
public boolean isAddAdornments() {
return addAdornments;
}
/**
* @param addAdornments The addAdornments to set.
*/
public void setAddAdornments(boolean addAdornments) {
this.addAdornments = addAdornments;
}
/**
* @return Returns the implicitTypes.
*/
public boolean isImplicitTypes() {
return implicitTypes;
}
/**
* @param implicitTypes The implicitTypes to set.
*/
public void setImplicitTypes(boolean implicitTypes) {
this.implicitTypes = implicitTypes;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -