KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > soap > impl > llom > SOAPFaultImpl


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.soap.impl.llom;
17
18 import org.apache.axis2.om.*;
19 import org.apache.axis2.om.impl.llom.OMElementImpl;
20 import org.apache.axis2.soap.*;
21 import org.apache.axis2.soap.impl.llom.soap12.SOAP12Constants;
22
23 import javax.xml.namespace.QName JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * Class SOAPFaultImpl
30  */

31 public abstract class SOAPFaultImpl extends SOAPElement
32         implements SOAPFault, OMConstants {
33
34     protected Exception JavaDoc e;
35
36     /**
37      * Constructor SOAPFaultImpl
38      *
39      * @param parent
40      * @param e
41      */

42     public SOAPFaultImpl(SOAPBody parent, Exception JavaDoc e) throws SOAPProcessingException {
43         super(parent, SOAPConstants.SOAPFAULT_LOCAL_NAME, true);
44         setException(e);
45     }
46
47     public void setException(Exception JavaDoc e) {
48         this.e = e;
49         putExceptionToSOAPFault(e);
50     }
51
52     public SOAPFaultImpl(SOAPBody parent) throws SOAPProcessingException {
53         super(parent, SOAPConstants.SOAPFAULT_LOCAL_NAME, true);
54     }
55
56     /**
57      * Constructor SOAPFaultImpl
58      *
59      * @param ns
60      * @param parent
61      * @param builder
62      */

63     public SOAPFaultImpl(SOAPBody parent, OMXMLParserWrapper builder) {
64         super(parent, SOAPConstants.SOAPFAULT_LOCAL_NAME, builder);
65     }
66
67
68     protected abstract SOAPFaultDetail getNewSOAPFaultDetail(SOAPFault fault) throws SOAPProcessingException;
69
70     // --------------- Getters and Settors --------------------------- //
71

72     public void setCode(SOAPFaultCode soapFaultCode) throws SOAPProcessingException {
73         setNewElement(getCode(), soapFaultCode);
74     }
75
76     public SOAPFaultCode getCode() {
77         return (SOAPFaultCode) this.getChildWithName(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME);
78     }
79
80     public void setReason(SOAPFaultReason reason) throws SOAPProcessingException {
81         setNewElement(getReason(), reason);
82     }
83
84     public SOAPFaultReason getReason() {
85         return (SOAPFaultReason) this.getChildWithName(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME);
86     }
87
88     public void setNode(SOAPFaultNode node) throws SOAPProcessingException {
89         setNewElement(getNode(), node);
90     }
91
92     public SOAPFaultNode getNode() {
93         return (SOAPFaultNode) this.getChildWithName(SOAP12Constants.SOAP_FAULT_NODE_LOCAL_NAME);
94     }
95
96     public void setRole(SOAPFaultRole role) throws SOAPProcessingException {
97         setNewElement(getRole(), role);
98     }
99
100     public SOAPFaultRole getRole() {
101         return (SOAPFaultRoleImpl) this.getChildWithName(SOAP12Constants.SOAP_FAULT_ROLE_LOCAL_NAME);
102     }
103
104     public void setDetail(SOAPFaultDetail detail) throws SOAPProcessingException {
105         setNewElement(getDetail(), detail);
106     }
107
108     public SOAPFaultDetail getDetail() {
109         return (SOAPFaultDetail) this.getChildWithName(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME);
110     }
111
112
113     // ---------------------------------------------------------------------------------------------//
114

115     public Exception JavaDoc getException() throws OMException {
116         getDetail();
117         if (getDetail() == null) {
118             return new Exception JavaDoc("No Exception element found in the SOAP Detail element");
119         }
120
121         OMElement exceptionElement = getDetail().getFirstChildWithName(new QName JavaDoc(SOAPConstants.SOAP_FAULT_DETAIL_EXCEPTION_ENTRY));
122         if (exceptionElement != null) {
123             return new Exception JavaDoc(exceptionElement.getText());
124         }
125         return new Exception JavaDoc("No Exception element found in the SOAP Detail element");
126     }
127
128     protected void putExceptionToSOAPFault(Exception JavaDoc e) throws SOAPProcessingException {
129         StringWriter JavaDoc sw = new StringWriter JavaDoc();
130         e.printStackTrace(new PrintWriter JavaDoc(sw));
131
132         getDetail();
133         if (getDetail() == null) {
134             setDetail(getNewSOAPFaultDetail(this));
135
136         }
137         OMElement faultDetailEnty = new OMElementImpl(SOAPConstants.SOAP_FAULT_DETAIL_EXCEPTION_ENTRY, this.getNamespace());
138         faultDetailEnty.setText(sw.getBuffer().toString());
139         getDetail().addChild(faultDetailEnty);
140     }
141
142     protected void setNewElement(OMElement myElement, OMElement newElement) {
143         if (myElement != null) {
144             myElement.discard();
145         }
146         if(newElement != null && newElement.getParent() != null){
147             newElement.discard();
148         }
149         this.addChild(newElement);
150         myElement = newElement;
151     }
152
153     protected OMElement getChildWithName(String JavaDoc childName) {
154         Iterator JavaDoc childrenIter = getChildren();
155         while (childrenIter.hasNext()) {
156             OMNode node = (OMNode) childrenIter.next();
157             if (node.getType() == OMNode.ELEMENT_NODE && childName.equals(((OMElement) node).getLocalName())) {
158                 return (OMElement) node;
159             }
160         }
161         return null;
162     }
163
164 }
165
Popular Tags