1/*
2 * Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany
3 *
4 * Copyright (c) 2011, Petter Uvesten, Everichon AB, Sweden
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
10 * following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or substantial
13 * portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
17 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22package org.ksoap2;
23
24import java.io.IOException;
25
26import org.ksoap2.kdom.Node;
27import org.xmlpull.v1.XmlPullParser;
28import org.xmlpull.v1.XmlPullParserException;
29import org.xmlpull.v1.XmlSerializer;
30
31/**
32 * Exception class encapsulating SOAP 1.2 Faults
33 *
34 * see http://www.w3.org/TR/soap12-part1/#soapfault for explanation of fields
35 *
36 * @author Petter Uvesten
37 */
38
39public class SoapFault12 extends SoapFault {
40    private static final long serialVersionUID = 1012001L;
41
42    /** Top-level nodes */
43    public Node Code;
44    public Node Reason;
45    public Node Node;
46    public Node Role;
47    public Node Detail;
48
49    public SoapFault12() {
50        super();
51        this.version = SoapEnvelope.VER12;
52    }
53
54    public SoapFault12(int version) {
55        super();
56        this.version = version;
57    }
58
59    /** Fills the fault details from the given XML stream */
60    public void parse(XmlPullParser parser) throws IOException, XmlPullParserException
61    {
62        parseSelf(parser);
63        // done parsing, populate some of the legacy public members
64        this.faultcode = Code.getElement(SoapEnvelope.ENV2003, "Value").getText(0);
65        this.faultstring = Reason.getElement(SoapEnvelope.ENV2003, "Text").getText(0);
66        this.detail = this.Detail;
67        this.faultactor = null;
68    }
69
70    private void parseSelf(XmlPullParser parser) throws IOException, XmlPullParserException {
71        parser.require(XmlPullParser.START_TAG, SoapEnvelope.ENV2003, "Fault");
72
73        while (parser.nextTag() == XmlPullParser.START_TAG) {
74            String name = parser.getName();
75            parser.nextTag();
76            if (name.equals("Code")) {
77                this.Code = new Node();
78                this.Code.parse(parser);
79            } else if (name.equals("Reason")) {
80                this.Reason = new Node();
81                this.Reason.parse(parser);
82            } else if (name.equals("Node")) {
83                this.Node = new Node();
84                this.Node.parse(parser);
85            } else if (name.equals("Role")) {
86                this.Role = new Node();
87                this.Role.parse(parser);
88            } else if (name.equals("Detail")) {
89                this.Detail = new Node();
90                this.Detail.parse(parser);
91            } else {
92                throw new RuntimeException("unexpected tag:" + name);
93            }
94
95            parser.require(XmlPullParser.END_TAG, SoapEnvelope.ENV2003, name);
96        }
97        parser.require(XmlPullParser.END_TAG, SoapEnvelope.ENV2003, "Fault");
98        parser.nextTag();
99
100    }
101
102    /** Writes the fault to the given XML stream */
103    public void write(XmlSerializer xw) throws IOException
104    {
105        xw.startTag(SoapEnvelope.ENV2003, "Fault");
106        //this.Code.write(xw);
107
108        xw.startTag(SoapEnvelope.ENV2003, "Code");
109        this.Code.write(xw);
110        xw.endTag(SoapEnvelope.ENV2003, "Code");
111        xw.startTag(SoapEnvelope.ENV2003, "Reason");
112        this.Reason.write(xw);
113        xw.endTag(SoapEnvelope.ENV2003, "Reason");
114
115        if (this.Node != null) {
116            xw.startTag(SoapEnvelope.ENV2003, "Node");
117            this.Node.write(xw);
118            xw.endTag(SoapEnvelope.ENV2003, "Node");
119        }
120        if (this.Role != null) {
121            xw.startTag(SoapEnvelope.ENV2003, "Role");
122            this.Role.write(xw);
123            xw.endTag(SoapEnvelope.ENV2003, "Role");
124        }
125
126        if (this.Detail != null) {
127            xw.startTag(SoapEnvelope.ENV2003, "Detail");
128            this.Detail.write(xw);
129            xw.endTag(SoapEnvelope.ENV2003, "Detail");
130        }
131        xw.endTag(SoapEnvelope.ENV2003, "Fault");
132    }
133
134    /**
135     * @see java.lang.Throwable#getMessage()
136     */
137    public String getMessage() {
138        return Reason.getElement(SoapEnvelope.ENV2003, "Text").getText(0);
139    }
140
141    /** Returns a string representation of the fault */
142    public String toString() {
143        String reason = Reason.getElement(SoapEnvelope.ENV2003, "Text").getText(0);
144        String code = Code.getElement(SoapEnvelope.ENV2003, "Value").getText(0);
145        return "Code: " + code + ", Reason: " + reason;
146    }
147}
148