001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xml.saaj;
031:
032: import javax.xml.namespace.*;
033: import javax.xml.soap.*;
034: import java.util.*;
035:
036: public class SOAP12FaultImpl extends SOAP11FaultImpl {
037: private static final NameImpl SOAP_1_2_FAULT_NAME = new NameImpl(
038: SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "Fault",
039: SOAPConstants.SOAP_ENV_PREFIX);
040:
041: private String _faultNode;
042: private String _faultRole;
043: private final ArrayList<SOAPElement> _subcodes = new ArrayList<SOAPElement>();
044: private final Map<Locale, String> _faultReasons = new HashMap<Locale, String>();
045:
046: SOAP12FaultImpl(SOAPFactory factory) throws SOAPException {
047: this (factory, SOAP_1_2_FAULT_NAME);
048: }
049:
050: SOAP12FaultImpl(SOAPFactory factory, NameImpl name)
051: throws SOAPException {
052: super (factory, name);
053: }
054:
055: public Detail addDetail() throws SOAPException {
056: if (_detail != null)
057: throw new SOAPException(
058: "Fault already contains a valid Detail");
059:
060: _detail = new SOAP12DetailImpl(_factory);
061:
062: return _detail;
063: }
064:
065: // subcode
066:
067: public void appendFaultSubcode(QName subcode) throws SOAPException {
068: if (subcode.getPrefix() == null
069: || "".equals(subcode.getPrefix()))
070: throw new SOAPException(
071: "Fault subcodes must have qualified names");
072:
073: SOAPElement element = _factory.createElement(subcode);
074:
075: if (getNamespaceURI(subcode.getPrefix()) == null) {
076: element.addNamespaceDeclaration(subcode.getPrefix(),
077: subcode.getNamespaceURI());
078: }
079:
080: _subcodes.add(element);
081: _faultCode.addChildElement(element);
082: }
083:
084: public Iterator getFaultSubcodes() {
085: return new SubcodeIterator();
086: }
087:
088: public void removeAllFaultSubcodes() {
089: for (int i = 0; i < _subcodes.size(); i++)
090: _faultCode.removeChild(_subcodes.get(i));
091:
092: _subcodes.clear();
093: }
094:
095: // reason
096:
097: public void addFaultReasonText(String text, Locale locale)
098: throws SOAPException {
099: _faultReasons.put(locale, text);
100: }
101:
102: public Iterator getFaultReasonLocales() throws SOAPException {
103: return _faultReasons.keySet().iterator();
104: }
105:
106: public String getFaultReasonText(Locale locale)
107: throws SOAPException {
108: return _faultReasons.get(locale);
109: }
110:
111: public Iterator getFaultReasonTexts() throws SOAPException {
112: return _faultReasons.values().iterator();
113: }
114:
115: // faultstring
116:
117: public String getFaultString() {
118: String reason = null;
119:
120: try {
121: reason = (String) getFaultReasonTexts().next();
122: } catch (SOAPException e) {
123: }
124:
125: return reason;
126: }
127:
128: public Locale getFaultStringLocale() {
129: Locale locale = null;
130:
131: try {
132: locale = (Locale) getFaultReasonLocales().next();
133: } catch (SOAPException e) {
134: }
135:
136: return locale;
137: }
138:
139: public void setFaultString(String faultString) throws SOAPException {
140: addFaultReasonText(faultString, Locale.getDefault());
141: }
142:
143: public void setFaultString(String faultString, Locale locale)
144: throws SOAPException {
145: addFaultReasonText(faultString, locale);
146: }
147:
148: // faultnode
149:
150: public String getFaultNode() {
151: return _faultNode;
152: }
153:
154: public void setFaultNode(String uri) throws SOAPException {
155: _faultNode = uri;
156: }
157:
158: // faultrole
159:
160: public String getFaultRole() {
161: return _faultRole;
162: }
163:
164: public void setFaultRole(String uri) throws SOAPException {
165: _faultRole = uri;
166: }
167:
168: private class SubcodeIterator implements Iterator<QName> {
169: private Iterator<SOAPElement> _iterator;
170:
171: public SubcodeIterator() {
172: _iterator = _subcodes.iterator();
173: }
174:
175: public boolean hasNext() {
176: return _iterator.hasNext();
177: }
178:
179: public QName next() {
180: return _iterator.next().getElementQName();
181: }
182:
183: public void remove() {
184: throw new UnsupportedOperationException();
185: }
186: }
187: }
|