001: /*
002: * $Id: FaultImpl.java,v 1.55 2006/01/27 12:49:35 vj135062 Exp $
003: * $Revision: 1.55 $
004: * $Date: 2006/01/27 12:49:35 $
005: */
006:
007: /*
008: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
009: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
010: *
011: * This code is free software; you can redistribute it and/or modify it
012: * under the terms of the GNU General Public License version 2 only, as
013: * published by the Free Software Foundation. Sun designates this
014: * particular file as subject to the "Classpath" exception as provided
015: * by Sun in the LICENSE file that accompanied this code.
016: *
017: * This code is distributed in the hope that it will be useful, but WITHOUT
018: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
019: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
020: * version 2 for more details (a copy is included in the LICENSE file that
021: * accompanied this code).
022: *
023: * You should have received a copy of the GNU General Public License version
024: * 2 along with this work; if not, write to the Free Software Foundation,
025: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
026: *
027: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
028: * CA 95054 USA or visit www.sun.com if you need additional information or
029: * have any questions.
030: */
031: package com.sun.xml.internal.messaging.saaj.soap.impl;
032:
033: import java.util.Locale;
034: import java.util.logging.Level;
035:
036: import javax.xml.namespace.QName;
037: import javax.xml.soap.*;
038:
039: import org.w3c.dom.Element;
040:
041: import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
042: import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
043: import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
044:
045: public abstract class FaultImpl extends ElementImpl implements
046: SOAPFault {
047:
048: /* This can also represent a fault reason element */
049: protected SOAPFaultElement faultStringElement;
050:
051: /* This can also represent a fault role element */
052: protected SOAPFaultElement faultActorElement;
053:
054: protected SOAPFaultElement faultCodeElement;
055:
056: protected Detail detail;
057:
058: protected FaultImpl(SOAPDocumentImpl ownerDoc, NameImpl name) {
059: super (ownerDoc, name);
060: }
061:
062: protected abstract NameImpl getDetailName();
063:
064: protected abstract NameImpl getFaultCodeName();
065:
066: protected abstract NameImpl getFaultStringName();
067:
068: protected abstract NameImpl getFaultActorName();
069:
070: protected abstract DetailImpl createDetail();
071:
072: protected abstract FaultElementImpl createSOAPFaultElement(
073: String localName);
074:
075: protected abstract void checkIfStandardFaultCode(String faultCode,
076: String uri) throws SOAPException;
077:
078: protected abstract void finallySetFaultCode(String faultcode)
079: throws SOAPException;
080:
081: protected abstract boolean isStandardFaultElement(String localName);
082:
083: protected abstract QName getDefaultFaultCode();
084:
085: protected void findFaultCodeElement() {
086: this .faultCodeElement = (SOAPFaultElement) findChild(getFaultCodeName());
087: }
088:
089: protected void findFaultActorElement() {
090: this .faultActorElement = (SOAPFaultElement) findChild(getFaultActorName());
091: }
092:
093: protected void findFaultStringElement() {
094: this .faultStringElement = (SOAPFaultElement) findChild(getFaultStringName());
095: }
096:
097: public void setFaultCode(String faultCode) throws SOAPException {
098: setFaultCode(NameImpl.getLocalNameFromTagName(faultCode),
099: NameImpl.getPrefixFromTagName(faultCode), null);
100: }
101:
102: public void setFaultCode(String faultCode, String prefix, String uri)
103: throws SOAPException {
104:
105: if (prefix == null || prefix.equals("")) {
106: if (uri == null || uri.equals("")) {
107: log.severe("SAAJ0140.impl.no.ns.URI");
108: throw new SOAPExceptionImpl(
109: "No NamespaceURI, SOAP requires faultcode content to be a QName");
110: }
111: prefix = getNamespacePrefix(uri);
112: if (prefix == null || prefix.equals("")) {
113: prefix = "ns0";
114: }
115: }
116:
117: if (this .faultCodeElement == null)
118: findFaultCodeElement();
119:
120: if (this .faultCodeElement == null)
121: this .faultCodeElement = addFaultCodeElement();
122: else
123: this .faultCodeElement.removeContents();
124:
125: if (uri == null || uri.equals("")) {
126: uri = this .faultCodeElement.getNamespaceURI(prefix);
127: }
128: if (uri == null || uri.equals("")) {
129: log.severe("SAAJ0140.impl.no.ns.URI");
130: throw new SOAPExceptionImpl(
131: "No NamespaceURI, SOAP requires faultcode content to be a QName");
132: } else {
133: checkIfStandardFaultCode(faultCode, uri);
134: ((FaultElementImpl) this .faultCodeElement)
135: .ensureNamespaceIsDeclared(prefix, uri);
136: }
137: finallySetFaultCode(prefix + ":" + faultCode);
138: }
139:
140: public void setFaultCode(Name faultCodeQName) throws SOAPException {
141: setFaultCode(faultCodeQName.getLocalName(), faultCodeQName
142: .getPrefix(), faultCodeQName.getURI());
143: }
144:
145: public void setFaultCode(QName faultCodeQName) throws SOAPException {
146: setFaultCode(faultCodeQName.getLocalPart(), faultCodeQName
147: .getPrefix(), faultCodeQName.getNamespaceURI());
148: }
149:
150: protected static QName convertCodeToQName(String code,
151: SOAPElement codeContainingElement) {
152:
153: int prefixIndex = code.indexOf(':');
154: if (prefixIndex == -1) {
155: return new QName(code);
156: }
157:
158: String prefix = code.substring(0, prefixIndex);
159: String nsName = ((ElementImpl) codeContainingElement)
160: .getNamespaceURI(prefix);
161: return new QName(nsName, getLocalPart(code), prefix);
162: }
163:
164: protected void initializeDetail() {
165: NameImpl detailName = getDetailName();
166: detail = (Detail) findChild(detailName);
167: }
168:
169: public Detail getDetail() {
170: if (detail == null)
171: initializeDetail();
172: if ((detail != null) && (detail.getParentNode() == null)) {
173: // a detach node was called on it
174: detail = null;
175: }
176: return detail;
177: }
178:
179: public Detail addDetail() throws SOAPException {
180: if (detail == null)
181: initializeDetail();
182: if (detail == null) {
183: detail = createDetail();
184: addNode(detail);
185: return detail;
186: } else {
187: // Log
188: throw new SOAPExceptionImpl("Error: Detail already exists");
189: }
190: }
191:
192: public boolean hasDetail() {
193: return (getDetail() != null);
194: }
195:
196: public void setFaultActor(String faultActor) throws SOAPException {
197: if (this .faultActorElement == null)
198: findFaultActorElement();
199: if (this .faultActorElement != null)
200: this .faultActorElement.detachNode();
201: if (faultActor == null)
202: return;
203: this .faultActorElement = addSOAPFaultElement(getFaultActorName()
204: .getLocalName());
205: this .faultActorElement.addTextNode(faultActor);
206: }
207:
208: public String getFaultActor() {
209: if (this .faultActorElement == null)
210: findFaultActorElement();
211: if (this .faultActorElement != null) {
212: return this .faultActorElement.getValue();
213: }
214: return null;
215: }
216:
217: public SOAPElement setElementQName(QName newName)
218: throws SOAPException {
219:
220: log.log(Level.SEVERE,
221: "SAAJ0146.impl.invalid.name.change.requested",
222: new Object[] { elementQName.getLocalPart(),
223: newName.getLocalPart() });
224: throw new SOAPException("Cannot change name for "
225: + elementQName.getLocalPart() + " to "
226: + newName.getLocalPart());
227: }
228:
229: protected SOAPElement convertToSoapElement(Element element) {
230: if (element instanceof SOAPFaultElement) {
231: return (SOAPElement) element;
232: } else if (element instanceof SOAPElement) {
233: SOAPElement soapElement = (SOAPElement) element;
234: if (getDetailName().equals(soapElement.getElementName())) {
235: return replaceElementWithSOAPElement(element,
236: createDetail());
237: } else {
238: String localName = soapElement.getElementName()
239: .getLocalName();
240: if (isStandardFaultElement(localName))
241: return replaceElementWithSOAPElement(element,
242: createSOAPFaultElement(localName));
243: return soapElement;
244: }
245: } else {
246: Name elementName = NameImpl.copyElementName(element);
247: ElementImpl newElement;
248: if (getDetailName().equals(elementName)) {
249: newElement = (ElementImpl) createDetail();
250: } else {
251: String localName = elementName.getLocalName();
252: if (isStandardFaultElement(localName))
253: newElement = (ElementImpl) createSOAPFaultElement(localName);
254: else
255: newElement = (ElementImpl) createElement(elementName);
256: }
257: return replaceElementWithSOAPElement(element, newElement);
258: }
259: }
260:
261: private SOAPFaultElement addFaultCodeElement() throws SOAPException {
262: if (this .faultCodeElement == null)
263: findFaultCodeElement();
264: if (this .faultCodeElement == null) {
265: this .faultCodeElement = addSOAPFaultElement(getFaultCodeName()
266: .getLocalName());
267: return this .faultCodeElement;
268: } else {
269: throw new SOAPExceptionImpl(
270: "Error: Faultcode already exists");
271: }
272: }
273:
274: private SOAPFaultElement addFaultStringElement()
275: throws SOAPException {
276: if (this .faultStringElement == null)
277: findFaultStringElement();
278: if (this .faultStringElement == null) {
279: this .faultStringElement = addSOAPFaultElement(getFaultStringName()
280: .getLocalName());
281: return this .faultStringElement;
282: } else {
283: // Log
284: throw new SOAPExceptionImpl(
285: "Error: Faultstring already exists");
286: }
287: }
288:
289: private SOAPFaultElement addFaultActorElement()
290: throws SOAPException {
291: if (this .faultActorElement == null)
292: findFaultActorElement();
293: if (this .faultActorElement == null) {
294: this .faultActorElement = addSOAPFaultElement(getFaultActorName()
295: .getLocalName());
296: return this .faultActorElement;
297: } else {
298: // Log
299: throw new SOAPExceptionImpl(
300: "Error: Faultactor already exists");
301: }
302: }
303:
304: protected SOAPElement addElement(Name name) throws SOAPException {
305: if (getDetailName().equals(name)) {
306: return addDetail();
307: } else if (getFaultCodeName().equals(name)) {
308: return addFaultCodeElement();
309: } else if (getFaultStringName().equals(name)) {
310: return addFaultStringElement();
311: } else if (getFaultActorName().equals(name)) {
312: return addFaultActorElement();
313: }
314: return super .addElement(name);
315: }
316:
317: protected SOAPElement addElement(QName name) throws SOAPException {
318: return addElement(NameImpl.convertToName(name));
319: }
320:
321: protected FaultElementImpl addSOAPFaultElement(String localName)
322: throws SOAPException {
323:
324: FaultElementImpl faultElem = createSOAPFaultElement(localName);
325: addNode(faultElem);
326: return faultElem;
327: }
328:
329: /**
330: * Convert an xml:lang attribute value into a Locale object
331: */
332: protected static Locale xmlLangToLocale(String xmlLang) {
333: if (xmlLang == null) {
334: return null;
335: }
336:
337: // Spec uses hyphen as separator
338: int index = xmlLang.indexOf("-");
339:
340: // Accept underscore as separator as well
341: if (index == -1) {
342: index = xmlLang.indexOf("_");
343: }
344:
345: if (index == -1) {
346: // No separator so assume only a language component
347: return new Locale(xmlLang, "");
348: }
349:
350: String language = xmlLang.substring(0, index);
351: String country = xmlLang.substring(index + 1);
352: return new Locale(language, country);
353: }
354:
355: protected static String localeToXmlLang(Locale locale) {
356: String xmlLang = locale.getLanguage();
357: String country = locale.getCountry();
358: if (!"".equals(country)) {
359: xmlLang += "-" + country;
360: }
361: return xmlLang;
362: }
363: }
|