templates.stub.ksoap.simpletypemarshal.vm Maven / Gradle / Ivy
#parse("${include}/generic.include.vm")
#parse("${include}/webclient.ksoap.include.vm")
#set ( $className ="SimpleTypeMarshal")
$codewriter.setCurrentJavaFilename("$pkg", "${className}.java")
package $pkg;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
/**
* 简单数据类型处理
* 覆盖 {@link org.ksoap2.serialization.DM},相对DM,增加对short,byte,integer,anyURI类型的处理
*/
class $className implements Marshal {
public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo propertyInfo)
throws IOException, XmlPullParserException {
String stringValue = parser.nextText();
Object result;
if(name.equals("string")){
if(propertyInfo.type==Character.class||propertyInfo.type==char.class)
result=stringValue.charAt(0);
else
result=stringValue;
}else if (name.equals("int")) {
result = Integer.valueOf(stringValue);
} else if (name.equals("short")) {
result = Short.valueOf(stringValue);
} else if (name.equals("long")) {
result = Long.valueOf(stringValue);
} else if (name.equals("boolean")) {
result = ${C_UTILITS}.convertToBoolean(stringValue);
} else if (name.equals("byte")) {
result =Byte.valueOf(stringValue);
}else if (name.equals("integer")) {
result= new BigInteger(stringValue);
} else if (name.equals("anyURI")) {
try {
result = new URI(stringValue);
} catch (URISyntaxException e) {
throw new XmlPullParserException(" Invalid URI " + stringValue);
}
} else {
throw new RuntimeException("string,int,long,boolean,short, byte, integer,or anyURI expected");
}
return result;
}
public void writeInstance(XmlSerializer writer, Object instance) throws IOException {
writer.text(instance.toString());
}
public void register(SoapSerializationEnvelope cm) {
cm.addMapping(cm.xsd, "int", Integer.class, this);
cm.addMapping(cm.xsd, "long", Long.class, this);
cm.addMapping(cm.xsd, "string", String.class, this);
cm.addMapping(cm.xsd, "boolean", Boolean.class, this);
cm.addMapping(cm.xsd, "short", Short.class, this);
cm.addMapping(cm.xsd, "byte", Byte.class, this);
cm.addMapping(cm.xsd, "integer", BigInteger.class, this);
cm.addMapping(cm.xsd, "anyURI", URI.class, this);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy