soapserializationenvelope.java
来自「it is a tools for developing J2ME applic」· Java 代码 · 共 488 行 · 第 1/2 页
JAVA
488 行
int cut = type.indexOf(':');
name = type.substring(cut + 1);
String prefix = cut == -1 ? "" : type.substring(0, cut);
namespace = parser.getNamespace(prefix);
} 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);
Object[] qName = getInfo(null, bodyOut);
writer.startTag((dotNet) ? "" : (String) qName[QNAME_NAMESPACE], (String) qName[QNAME_TYPE]);
if (dotNet) {
writer.attribute(null, "xmlns", (String) qName[QNAME_NAMESPACE]);
}
writer.attribute(null, ID_LABEL, qName[2] == null ? ("o" + 0) : (String) qName[2]);
writer.attribute(enc, ROOT_LABEL, "1");
writeElement(writer, bodyOut, null, qName[QNAME_MARSHAL]);
writer.endTag((dotNet) ? "" : (String) qName[QNAME_NAMESPACE], (String) qName[QNAME_TYPE]);
}
/**
* 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();
for (int i = 0; i < cnt; i++) {
obj.getPropertyInfo(i, properties, info);
if ((info.flags & PropertyInfo.TRANSIENT) == 0) {
writer.startTag(info.namespace, info.name);
writeProperty(writer, obj.getProperty(i), info);
writer.endTag(info.namespace, 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);
}
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[QNAME_NAMESPACE], true);
writer.attribute(xsi, TYPE_LABEL, prefix + ":" + qName[QNAME_TYPE]);
}
writeElement(writer, obj, type, qName[QNAME_MARSHAL]);
}
}
private void writeElement(XmlSerializer writer, Object element, PropertyInfo type, Object marshal) throws IOException {
if (marshal != null)
((Marshal) marshal).writeInstance(writer, element);
else if (element instanceof KvmSerializable)
writeObjectBody(writer, (KvmSerializable) element);
else if (element instanceof Vector)
writeVectorBody(writer, (Vector) element, type.elementType);
else
throw new RuntimeException("Cannot serialize: " + element);
}
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 + =
减小字号Ctrl + -
显示快捷键?