1 /* Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining a copy 4 * of this software and associated documentation files (the "Software"), to deal 5 * in the Software without restriction, including without limitation the rights 6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 * sell copies of the Software, and to permit persons to whom the Software is 8 * furnished to do so, subject to the following conditions: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 * IN THE SOFTWARE. */ 20 21 package org.ksoap2.serialization; 22 23 import java.io.IOException; 24 25 import org.ksoap2.SoapEnvelope; 26 import org.xmlpull.v1.XmlPullParser; 27 import org.xmlpull.v1.XmlPullParserException; 28 import org.xmlpull.v1.XmlSerializer; 29 30 /** 31 * This class is not public, so save a few bytes by using a short class name (DM 32 * stands for DefaultMarshal)... 33 */ 34 class DM implements Marshal { 35 readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo excepted)36 public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo excepted) 37 throws IOException, XmlPullParserException { 38 String text = parser.nextText(); 39 switch (name.charAt(0)) { 40 case 's': 41 return text; 42 case 'i': 43 return new Integer(Integer.parseInt(text)); 44 case 'l': 45 return new Long(Long.parseLong(text)); 46 case 'b': 47 return new Boolean(SoapEnvelope.stringToBoolean(text)); 48 default: 49 throw new RuntimeException(); 50 } 51 } 52 53 /** 54 * Write the instance out. In case it is an AttributeContainer write those our first though. 55 * If it HasAttributes then write the attributes and values. 56 * 57 * @param writer the xml serializer. 58 */ writeInstance(XmlSerializer writer, Object instance)59 public void writeInstance(XmlSerializer writer, Object instance) throws IOException { 60 if (instance instanceof AttributeContainer) { 61 AttributeContainer attributeContainer = (AttributeContainer) instance; 62 int cnt = attributeContainer.getAttributeCount(); 63 for (int counter = 0; counter < cnt; counter++) { 64 AttributeInfo attributeInfo = new AttributeInfo(); 65 attributeContainer.getAttributeInfo(counter, attributeInfo); 66 try { 67 attributeContainer.getAttribute(counter, attributeInfo); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 71 if (attributeInfo.getValue() != null) { 72 writer.attribute(attributeInfo.getNamespace(), attributeInfo.getName(), 73 (attributeInfo.getValue() != null) ? attributeInfo.getValue().toString() 74 : ""); 75 } 76 } 77 } else if (instance instanceof HasAttributes) { 78 HasAttributes soapObject = (HasAttributes) instance; 79 int cnt = soapObject.getAttributeCount(); 80 for (int counter = 0; counter < cnt; counter++) { 81 AttributeInfo attributeInfo = new AttributeInfo(); 82 soapObject.getAttributeInfo(counter, attributeInfo); 83 try { 84 soapObject.getAttribute(counter, attributeInfo); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 if (attributeInfo.getValue() != null) { 89 writer.attribute(attributeInfo.getNamespace(), attributeInfo.getName(), 90 attributeInfo.getValue() != null ? attributeInfo.getValue().toString() 91 : ""); 92 } 93 } 94 } 95 96 if (instance instanceof ValueWriter) { 97 ((ValueWriter) instance).write(writer); 98 } else { 99 writer.text(instance.toString()); 100 } 101 102 } 103 register(SoapSerializationEnvelope cm)104 public void register(SoapSerializationEnvelope cm) { 105 cm.addMapping(cm.xsd, "int", PropertyInfo.INTEGER_CLASS, this); 106 cm.addMapping(cm.xsd, "long", PropertyInfo.LONG_CLASS, this); 107 cm.addMapping(cm.xsd, "string", PropertyInfo.STRING_CLASS, this); 108 cm.addMapping(cm.xsd, "boolean", PropertyInfo.BOOLEAN_CLASS, this); 109 } 110 } 111