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
21package org.ksoap2.serialization;
22
23import java.io.*;
24import org.xmlpull.v1.*;
25import org.ksoap2.*;
26
27/**
28 * This class is not public, so save a few bytes by using a short class name (DM
29 * stands for DefaultMarshal)...
30 */
31class DM implements Marshal {
32
33    public Object readInstance(XmlPullParser parser, String namespace, String name,
34            PropertyInfo expected)
35            throws IOException, XmlPullParserException {
36        String text = parser.nextText();
37        switch (name.charAt(0)) {
38            case 's':
39                return text;
40            case 'i':
41                return new Integer(Integer.parseInt(text));
42            case 'l':
43                return new Long(Long.parseLong(text));
44            case 'b':
45                return new Boolean(SoapEnvelope.stringToBoolean(text));
46            default:
47                throw new RuntimeException();
48        }
49    }
50
51    /**
52     * Write the instance out. In case it is an AttributeContainer write those our first though.
53     * @param writer
54     *            the xml serializer.
55     * @param instance
56     * @throws IOException
57     */
58    public void writeInstance(XmlSerializer writer, Object instance) throws IOException {
59        if (instance instanceof AttributeContainer) {
60            AttributeContainer attributeContainer = (AttributeContainer) instance;
61            int cnt = attributeContainer.getAttributeCount();
62            for (int counter = 0; counter < cnt; counter++) {
63                AttributeInfo attributeInfo = new AttributeInfo();
64                attributeContainer.getAttributeInfo(counter, attributeInfo);
65                writer.attribute(attributeInfo.getNamespace(), attributeInfo.getName(),
66                        attributeInfo.getValue().toString());
67            }
68        }
69        writer.text(instance.toString());
70    }
71
72    public void register(SoapSerializationEnvelope cm) {
73        cm.addMapping(cm.xsd, "int", PropertyInfo.INTEGER_CLASS, this);
74        cm.addMapping(cm.xsd, "long", PropertyInfo.LONG_CLASS, this);
75        cm.addMapping(cm.xsd, "string", PropertyInfo.STRING_CLASS, this);
76        cm.addMapping(cm.xsd, "boolean", PropertyInfo.BOOLEAN_CLASS, this);
77    }
78}
79