/** * Helper function for SoapObject.Equals * Checks if a given property and index are the same as in this * * @param otherProp, index * @return */ public bool isPropertyEqual(Object otherProp, int index) { if (index >= getPropertyCount()) { return(false); } Object thisProp = this.properties[index]; if (otherProp is PropertyInfo && thisProp is PropertyInfo) { // Get both PropertInfos and compare values PropertyInfo otherPropInfo = (PropertyInfo)otherProp; PropertyInfo thisPropInfo = (PropertyInfo)thisProp; return(otherPropInfo.getName().Equals(thisPropInfo.getName()) && otherPropInfo.getValue().Equals(thisPropInfo.getValue())); } else if (otherProp is SoapObject && thisProp is SoapObject) { SoapObject otherPropSoap = (SoapObject)otherProp; SoapObject thisPropSoap = (SoapObject)thisProp; return(otherPropSoap.Equals(thisPropSoap)); } return(false); }
/** * 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 is SoapObject || instance is SoapPrimitive) { type = instance; } else { type = instance.GetType(); } } if (type is SoapObject) { SoapObject so = (SoapObject)type; return(new Object[] { so.getNamespace(), so.getName(), null, null }); } if (type is SoapPrimitive) { SoapPrimitive sp = (SoapPrimitive)type; return(new Object[] { sp.getNamespace(), sp.getName(), null, DEFAULT_MARSHAL }); } if ((type is Type) && type != PropertyInfo.OBJECT_CLASS) { Object[] tmp = (Object[])classToQName[((Type)type).Name]; if (tmp != null) { return(tmp); } } return(new Object[] { xsd, ANY_TYPE_LABEL, null, null }); }
public override bool Equals(object obj) { if (!(obj is SoapObject)) { return(false); } SoapObject otherSoapObject = (SoapObject)obj; if (!name.Equals(otherSoapObject.name) || !namespace_.Equals(otherSoapObject.namespace_)) { return(false); } int numProperties = properties.Count; if (numProperties != otherSoapObject.properties.Count) { return(false); } // SoapObjects are only considered the same if properties Equals and in the same order for (int propIndex = 0; propIndex < numProperties; propIndex++) { Object thisProp = this.properties[propIndex]; if (!otherSoapObject.isPropertyEqual(thisProp, propIndex)) { return(false); } } return(attributesAreEqual(otherSoapObject)); }
/** * Creates a new SoapObject based on this, allows usage of SoapObjects as * templates. One application is to set the expected return type of a soap * call if the server does not send explicit type information. * * @return a copy of this. */ public SoapObject newInstance() { SoapObject o = new SoapObject(namespace_, name); for (int propIndex = 0; propIndex < properties.Count; propIndex++) { Object prop = properties[propIndex]; if (prop is PropertyInfo) { PropertyInfo propertyInfo = (PropertyInfo)properties[propIndex]; PropertyInfo propertyInfoClonned = (PropertyInfo)propertyInfo.clone(); o.addProperty(propertyInfoClonned); } else if (prop is SoapObject) { o.addSoapObject(((SoapObject)prop).newInstance()); } } for (int attribIndex = 0; attribIndex < getAttributeCount(); attribIndex++) { AttributeInfo newAI = new AttributeInfo(); getAttributeInfo(attribIndex, newAI); AttributeInfo attributeInfo = newAI; // (AttributeInfo) // attributes.elementAt(attribIndex); o.addAttribute(attributeInfo); } return(o); }
/** * Read a SoapObject. This extracts any attributes and then reads the object as a FvmSerializable. */ protected void readSerializable(XmlPullParser parser, SoapObject obj) { for (int counter = 0; counter < parser.getAttributeCount(); counter++) { String attributeName = parser.getAttributeName(counter); String value = parser.getAttributeValue(counter); ((SoapObject)obj).addAttribute(attributeName, value); } readSerializable(parser, (FvmSerializable)obj); }
public void writeInstance(XmlSerializer writer, Object instance) { Dictionary <object, object> h = instance as Dictionary <object, object>; SoapObject item = new SoapObject(null, null); item.addProperty("key", null); item.addProperty("value", null); foreach (KeyValuePair <object, object> keyValuePair in h) { writer.startTag("", "item"); item.setProperty(0, keyValuePair.Key); item.setProperty(1, keyValuePair.Value); envelope.writeObjectBodyWithAttributes(writer, item); writer.endTag("", "item"); } }
protected void writeArrayListBody(XmlSerializer writer, List <object> list) { FvmSerializable obj = (FvmSerializable)list; int cnt = list.Count; PropertyInfo propertyInfo = new PropertyInfo(); String namespace_; String name; String type; for (int i = 0; i < cnt; i++) { // get the property Object prop = obj.getProperty(i); // and importantly also get the property info which holds the name potentially! obj.getPropertyInfo(i, properties, propertyInfo); if (!(prop is SoapObject)) { // prop is a PropertyInfo if ((propertyInfo.flags & PropertyInfo.TRANSIENT) == 0) { Object objValue = obj.getProperty(i); if ((prop != null || !skipNullProperties) && (objValue != SoapPrimitive.NullSkip)) { writer.startTag(propertyInfo.namespace_, propertyInfo.name); writeProperty(writer, objValue, propertyInfo); writer.endTag(propertyInfo.namespace_, propertyInfo.name); } } } else { // prop is a SoapObject SoapObject nestedSoap = (SoapObject)prop; // lets get the info from the soap object itself Object[] qName = getInfo(null, nestedSoap); namespace_ = (String)qName[QNAME_NAMESPACE]; type = (String)qName[QNAME_TYPE]; // prefer the name from the property info if (propertyInfo.name != null && propertyInfo.name.Length > 0) { name = propertyInfo.name; } else { name = (String)qName[QNAME_TYPE]; } // prefer the namespace_ from the property info if (propertyInfo.namespace_ != null && propertyInfo.namespace_.Length > 0) { namespace_ = propertyInfo.namespace_; } else { namespace_ = (String)qName[QNAME_NAMESPACE]; } writer.startTag(namespace_, name); if (!implicitTypes) { String prefix = writer.getPrefix(namespace_, true); writer.attribute(xsi, TYPE_LABEL, prefix + ":" + type); } writeObjectBodyWithAttributes(writer, nestedSoap); writer.endTag(namespace_, name); } } if (obj is HasInnerText) { if (((HasInnerText)obj).getInnerText() != null) { writer.cdsect(((HasInnerText)obj).getInnerText()); } } }
/** * 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[new SoapPrimitive(so.namespace_, so.name, null)] = so; }
/** * 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) { SoapPrimitive key = new SoapPrimitive(namespace_, name, null); if (!qNameToClass.ContainsKey(key)) { return(null); } Object obj = qNameToClass[key]; if (obj is Marshal) { return(((Marshal)obj).readInstance(parser, namespace_, name, expected)); } else if (obj is SoapObject) { obj = ((SoapObject)obj).newInstance(); } else if (obj == typeof(SoapObject)) { obj = new SoapObject(namespace_, name); } else { try { obj = Activator.CreateInstance((Type)obj); } catch (Exception e) { throw new Exception(e.ToString()); } } if (obj is HasAttributes) { HasAttributes soapObject = (HasAttributes)obj; int cnt = parser.getAttributeCount(); for (int counter = 0; counter < cnt; counter++) { AttributeInfo attributeInfo = new AttributeInfo(); attributeInfo.setName(parser.getAttributeName(counter)); attributeInfo.setValue(parser.getAttributeValue(counter)); attributeInfo.setNamespace(parser.getAttributeNamespace(counter)); attributeInfo.setType(parser.getAttributeType(counter)); soapObject.setAttribute(attributeInfo); } } // ok, obj is now the instance, fill it.... if (obj is SoapObject) { readSerializable(parser, (SoapObject)obj); } else if (obj is FvmSerializable) { if (obj is HasInnerText) { ((HasInnerText)obj).setInnerText((parser.getText() != null) ? parser.getText() : ""); } readSerializable(parser, (FvmSerializable)obj); } else if (obj is List <object> ) { readVector(parser, (List <object>)obj, expected.elementType); } else { throw new Exception("no deserializer for " + obj.GetType()); } return(obj); }
/** * 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 unknownObject wrapped as a SoapPrimitive or SoapObject * @throws IOException * @throws XmlPullParserException */ protected Object readUnknown(XmlPullParser parser, String typeNamespace, String typeName) { String name = parser.getName(); String namespace_ = parser.getNamespace(); // cache the attribute info list from the current element before we move on List <object> attributeInfoVector = new List <object>(); for (int attributeCount = 0; attributeCount < parser.getAttributeCount(); attributeCount++) { AttributeInfo attributeInfo = new AttributeInfo(); attributeInfo.setName(parser.getAttributeName(attributeCount)); attributeInfo.setValue(parser.getAttributeValue(attributeCount)); attributeInfo.setNamespace(parser.getAttributeNamespace(attributeCount)); attributeInfo.setType(parser.getAttributeType(attributeCount)); attributeInfoVector.Add(attributeInfo); } 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(); SoapPrimitive sp = new SoapPrimitive(typeNamespace, typeName, text); result = sp; // apply all the cached attribute info list before we add the property and descend further for parsing for (int i = 0; i < attributeInfoVector.Count; i++) { sp.addAttribute((AttributeInfo)attributeInfoVector[i]); } parser.next(); } else if (parser.getEventType() == XmlPullParser.END_TAG) { SoapObject so = new SoapObject(typeNamespace, typeName); // apply all the cached attribute info list before we add the property and descend further for parsing for (int i = 0; i < attributeInfoVector.Count; i++) { so.addAttribute((AttributeInfo)attributeInfoVector[i]); } result = so; } if (parser.getEventType() == XmlPullParser.START_TAG) { if (text != null && text.Trim().Length != 0) { throw new Exception("Malformed input: Mixed content"); } SoapObject so = new SoapObject(typeNamespace, typeName); // apply all the cached attribute info list before we add the property and descend further for parsing for (int i = 0; i < attributeInfoVector.Count; i++) { so.addAttribute((AttributeInfo)attributeInfoVector[i]); } while (parser.getEventType() != XmlPullParser.END_TAG) { so.addProperty(parser.getNamespace(), 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); }
/** * Adds a SoapObject the properties array. This is a sub element to * allow nested SoapObjects * * @param soapObject * to be added as a property of the current object */ public SoapObject addSoapObject(SoapObject soapObject) { properties.Add(soapObject); return(this); }