1 /*
2 * $Id: FaultImpl.java,v 1.55 2006/01/27 12:49:35 vj135062 Exp $
3 * $Revision: 1.55 $
4 * $Date: 2006/01/27 12:49:35 $
5 */
6
7 /*
8 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Sun designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Sun in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
28 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * have any questions.
30 */
31 package com.sun.xml.internal.messaging.saaj.soap.impl;
32
33 import java.util.Locale;
34 import java.util.logging.Level;
35
36 import javax.xml.namespace.QName;
37 import javax.xml.soap;
38
39 import org.w3c.dom.Element;
40
41 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
42 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
43 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
44
45 public abstract class FaultImpl extends ElementImpl implements SOAPFault {
46
47 /* This can also represent a fault reason element */
48 protected SOAPFaultElement faultStringElement;
49
50 /* This can also represent a fault role element */
51 protected SOAPFaultElement faultActorElement;
52
53 protected SOAPFaultElement faultCodeElement;
54
55 protected Detail detail;
56
57 protected FaultImpl(SOAPDocumentImpl ownerDoc, NameImpl name) {
58 super(ownerDoc, name);
59 }
60
61
62 protected abstract NameImpl getDetailName();
63 protected abstract NameImpl getFaultCodeName();
64 protected abstract NameImpl getFaultStringName();
65 protected abstract NameImpl getFaultActorName();
66 protected abstract DetailImpl createDetail();
67 protected abstract FaultElementImpl createSOAPFaultElement(String localName);
68 protected abstract void checkIfStandardFaultCode(String faultCode, String uri) throws SOAPException;
69 protected abstract void finallySetFaultCode(String faultcode) throws SOAPException;
70 protected abstract boolean isStandardFaultElement(String localName);
71 protected abstract QName getDefaultFaultCode();
72
73
74 protected void findFaultCodeElement() {
75 this.faultCodeElement =
76 (SOAPFaultElement) findChild(getFaultCodeName());
77 }
78
79 protected void findFaultActorElement() {
80 this.faultActorElement =
81 (SOAPFaultElement) findChild(getFaultActorName());
82 }
83
84 protected void findFaultStringElement() {
85 this.faultStringElement =
86 (SOAPFaultElement) findChild(getFaultStringName());
87 }
88
89 public void setFaultCode(String faultCode) throws SOAPException {
90 setFaultCode(
91 NameImpl.getLocalNameFromTagName(faultCode),
92 NameImpl.getPrefixFromTagName(faultCode),
93 null);
94 }
95
96 public void setFaultCode(String faultCode, String prefix, String uri)
97 throws SOAPException {
98
99 if (prefix == null || prefix.equals("")) {
100 if (uri == null || uri.equals("")) {
101 log.severe("SAAJ0140.impl.no.ns.URI");
102 throw new SOAPExceptionImpl("No NamespaceURI, SOAP requires faultcode content to be a QName");
103 }
104 prefix = getNamespacePrefix(uri);
105 if (prefix == null || prefix.equals("")) {
106 prefix = "ns0";
107 }
108 }
109
110 if (this.faultCodeElement == null)
111 findFaultCodeElement();
112
113 if (this.faultCodeElement == null)
114 this.faultCodeElement = addFaultCodeElement();
115 else
116 this.faultCodeElement.removeContents();
117
118 if (uri == null || uri.equals("")) {
119 uri = this.faultCodeElement.getNamespaceURI(prefix);
120 }
121 if (uri == null || uri.equals("")) {
122 log.severe("SAAJ0140.impl.no.ns.URI");
123 throw new SOAPExceptionImpl("No NamespaceURI, SOAP requires faultcode content to be a QName");
124 } else {
125 checkIfStandardFaultCode(faultCode, uri);
126 ((FaultElementImpl) this.faultCodeElement).ensureNamespaceIsDeclared(prefix, uri);
127 }
128 finallySetFaultCode(prefix + ":" + faultCode);
129 }
130
131 public void setFaultCode(Name faultCodeQName) throws SOAPException {
132 setFaultCode(
133 faultCodeQName.getLocalName(),
134 faultCodeQName.getPrefix(),
135 faultCodeQName.getURI());
136 }
137
138 public void setFaultCode(QName faultCodeQName) throws SOAPException {
139 setFaultCode(
140 faultCodeQName.getLocalPart(),
141 faultCodeQName.getPrefix(),
142 faultCodeQName.getNamespaceURI());
143 }
144
145 protected static QName convertCodeToQName(
146 String code,
147 SOAPElement codeContainingElement) {
148
149 int prefixIndex = code.indexOf(':');
150 if (prefixIndex == -1) {
151 return new QName(code);
152 }
153
154 String prefix = code.substring(0, prefixIndex);
155 String nsName =
156 ((ElementImpl) codeContainingElement).getNamespaceURI(prefix);
157 return new QName(nsName, getLocalPart(code), prefix);
158 }
159
160 protected void initializeDetail() {
161 NameImpl detailName = getDetailName();
162 detail = (Detail) findChild(detailName);
163 }
164
165 public Detail getDetail() {
166 if (detail == null)
167 initializeDetail();
168 if ((detail != null) && (detail.getParentNode() == null)) {
169 // a detach node was called on it
170 detail = null;
171 }
172 return detail;
173 }
174
175 public Detail addDetail() throws SOAPException {
176 if (detail == null)
177 initializeDetail();
178 if (detail == null) {
179 detail = createDetail();
180 addNode(detail);
181 return detail;
182 } else {
183 // Log
184 throw new SOAPExceptionImpl("Error: Detail already exists");
185 }
186 }
187
188 public boolean hasDetail() {
189 return (getDetail() != null);
190 }
191
192 public void setFaultActor(String faultActor) throws SOAPException {
193 if (this.faultActorElement == null)
194 findFaultActorElement();
195 if (this.faultActorElement != null)
196 this.faultActorElement.detachNode();
197 if (faultActor == null)
198 return;
199 this.faultActorElement =
200 addSOAPFaultElement(getFaultActorName().getLocalName());
201 this.faultActorElement.addTextNode(faultActor);
202 }
203
204 public String getFaultActor() {
205 if (this.faultActorElement == null)
206 findFaultActorElement();
207 if (this.faultActorElement != null) {
208 return this.faultActorElement.getValue();
209 }
210 return null;
211 }
212
213 public SOAPElement setElementQName(QName newName) throws SOAPException {
214
215 log.log(
216 Level.SEVERE,
217 "SAAJ0146.impl.invalid.name.change.requested",
218 new Object[] {elementQName.getLocalPart(), newName.getLocalPart()});
219 throw new SOAPException(
220 "Cannot change name for " + elementQName.getLocalPart() + " to " + newName.getLocalPart());
221 }
222
223 protected SOAPElement convertToSoapElement(Element element) {
224 if (element instanceof SOAPFaultElement) {
225 return (SOAPElement) element;
226 } else if (element instanceof SOAPElement) {
227 SOAPElement soapElement = (SOAPElement) element;
228 if (getDetailName().equals(soapElement.getElementName())) {
229 return replaceElementWithSOAPElement(element, createDetail());
230 } else {
231 String localName =
232 soapElement.getElementName().getLocalName();
233 if (isStandardFaultElement(localName))
234 return replaceElementWithSOAPElement(
235 element,
236 createSOAPFaultElement(localName));
237 return soapElement;
238 }
239 } else {
240 Name elementName = NameImpl.copyElementName(element);
241 ElementImpl newElement;
242 if (getDetailName().equals(elementName)) {
243 newElement = (ElementImpl) createDetail();
244 } else {
245 String localName = elementName.getLocalName();
246 if (isStandardFaultElement(localName))
247 newElement =
248 (ElementImpl) createSOAPFaultElement(localName);
249 else
250 newElement = (ElementImpl) createElement(elementName);
251 }
252 return replaceElementWithSOAPElement(element, newElement);
253 }
254 }
255
256 private SOAPFaultElement addFaultCodeElement() throws SOAPException {
257 if (this.faultCodeElement == null)
258 findFaultCodeElement();
259 if (this.faultCodeElement == null) {
260 this.faultCodeElement =
261 addSOAPFaultElement(getFaultCodeName().getLocalName());
262 return this.faultCodeElement;
263 } else {
264 throw new SOAPExceptionImpl("Error: Faultcode already exists");
265 }
266 }
267
268 private SOAPFaultElement addFaultStringElement() throws SOAPException {
269 if (this.faultStringElement == null)
270 findFaultStringElement();
271 if (this.faultStringElement == null) {
272 this.faultStringElement =
273 addSOAPFaultElement(getFaultStringName().getLocalName());
274 return this.faultStringElement;
275 } else {
276 // Log
277 throw new SOAPExceptionImpl("Error: Faultstring already exists");
278 }
279 }
280
281 private SOAPFaultElement addFaultActorElement() throws SOAPException {
282 if (this.faultActorElement == null)
283 findFaultActorElement();
284 if (this.faultActorElement == null) {
285 this.faultActorElement =
286 addSOAPFaultElement(getFaultActorName().getLocalName());
287 return this.faultActorElement;
288 } else {
289 // Log
290 throw new SOAPExceptionImpl("Error: Faultactor already exists");
291 }
292 }
293
294 protected SOAPElement addElement(Name name) throws SOAPException {
295 if (getDetailName().equals(name)) {
296 return addDetail();
297 } else if(getFaultCodeName().equals(name)) {
298 return addFaultCodeElement();
299 } else if(getFaultStringName().equals(name)) {
300 return addFaultStringElement();
301 } else if(getFaultActorName().equals(name)) {
302 return addFaultActorElement();
303 }
304 return super.addElement(name);
305 }
306
307 protected SOAPElement addElement(QName name) throws SOAPException {
308 return addElement(NameImpl.convertToName(name));
309 }
310
311 protected FaultElementImpl addSOAPFaultElement(String localName)
312 throws SOAPException {
313
314 FaultElementImpl faultElem = createSOAPFaultElement(localName);
315 addNode(faultElem);
316 return faultElem;
317 }
318
319 /**
320 * Convert an xml:lang attribute value into a Locale object
321 */
322 protected static Locale xmlLangToLocale(String xmlLang) {
323 if (xmlLang == null) {
324 return null;
325 }
326
327 // Spec uses hyphen as separator
328 int index = xmlLang.indexOf("-");
329
330 // Accept underscore as separator as well
331 if (index == -1) {
332 index = xmlLang.indexOf("_");
333 }
334
335 if (index == -1) {
336 // No separator so assume only a language component
337 return new Locale(xmlLang, "");
338 }
339
340 String language = xmlLang.substring(0, index);
341 String country = xmlLang.substring(index + 1);
342 return new Locale(language, country);
343 }
344
345 protected static String localeToXmlLang(Locale locale) {
346 String xmlLang = locale.getLanguage();
347 String country = locale.getCountry();
348 if (!"".equals(country)) {
349 xmlLang += "-" + country;
350 }
351 return xmlLang;
352 }
353 }