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 java.util.*;
033:
034: import javax.xml.XMLConstants;
035: import javax.xml.namespace.*;
036: import javax.xml.soap.*;
037:
038: import org.w3c.dom.*;
039:
040: import com.caucho.xml.QDocument;
041: import com.caucho.xml.QNode;
042:
043: public class SOAP11BodyImpl extends SOAPElementImpl implements SOAPBody {
044: private SOAPFault _fault;
045:
046: SOAP11BodyImpl(SOAPFactory factory, NameImpl name)
047: throws SOAPException {
048: super (factory, name);
049: }
050:
051: public SOAPElement addChildElement(Name name) throws SOAPException {
052: return addBodyElement(name);
053: }
054:
055: public SOAPElement addChildElement(SOAPElement element)
056: throws SOAPException {
057: SOAPElement child = element;
058:
059: if (!(element instanceof SOAPBodyElement))
060: child = new SOAPBodyElementImpl(_factory, element);
061:
062: if (element instanceof SOAPFault)
063: _fault = (SOAPFault) element;
064:
065: return super .addChildElement(child);
066: }
067:
068: public SOAPBodyElement addBodyElement(Name name)
069: throws SOAPException {
070: SOAPBodyElement child = new SOAPBodyElementImpl(_factory,
071: NameImpl.fromName(name));
072:
073: appendChild(child);
074:
075: return child;
076: }
077:
078: public SOAPBodyElement addBodyElement(QName qname)
079: throws SOAPException {
080: return addBodyElement((Name) NameImpl.fromQName(qname));
081: }
082:
083: public SOAPBodyElement addDocument(Document document)
084: throws SOAPException {
085: SOAPBodyElement child = new SOAPBodyElementImpl(_factory,
086: document.getDocumentElement());
087:
088: super .addChildElement(child);
089:
090: return child;
091: }
092:
093: public SOAPFault addFault() throws SOAPException {
094: // XXX replace or throw exception?
095: _fault = _factory.createFault();
096:
097: addChildElement(_fault);
098:
099: return _fault;
100: }
101:
102: public SOAPFault addFault(Name faultCode, String faultString)
103: throws SOAPException {
104: _fault = _factory.createFault(faultString, NameImpl
105: .toQName(faultCode));
106:
107: addChildElement(_fault);
108:
109: return _fault;
110: }
111:
112: public SOAPFault addFault(Name faultCode, String faultString,
113: Locale locale) throws SOAPException {
114: _fault = _factory.createFault();
115:
116: _fault.setFaultString(faultString, locale);
117: _fault.setFaultCode(faultCode);
118:
119: addChildElement(_fault);
120:
121: return _fault;
122: }
123:
124: public SOAPFault addFault(QName faultCode, String faultString)
125: throws SOAPException {
126: _fault = _factory.createFault(faultString, faultCode);
127:
128: addChildElement(_fault);
129:
130: return _fault;
131: }
132:
133: public SOAPFault addFault(QName faultCode, String faultString,
134: Locale locale) throws SOAPException {
135: _fault = _factory.createFault();
136:
137: _fault.setFaultString(faultString, locale);
138: _fault.setFaultCode(faultCode);
139:
140: addChildElement(_fault);
141:
142: return _fault;
143: }
144:
145: public Document extractContentAsDocument() throws SOAPException {
146: if (getFirstChild() == null
147: || getFirstChild() != getLastChild())
148: throw new SOAPException(
149: "Body does not have exactly one child");
150:
151: SOAPElementImpl child = (SOAPElementImpl) getFirstChild();
152: removeContents();
153:
154: return new SimpleDocument(child);
155: }
156:
157: public SOAPFault getFault() {
158: return _fault;
159: }
160:
161: public boolean hasFault() {
162: return _fault != null;
163: }
164:
165: public SOAPElement setElementQName(QName newName)
166: throws SOAPException {
167: throw new SOAPException("Cannot set name of SOAP Body");
168: }
169:
170: public void detachNode() {
171: if (getParentNode() instanceof SOAPEnvelopeImpl) {
172: SOAPEnvelopeImpl parent = (SOAPEnvelopeImpl) getParentNode();
173: parent._body = null;
174: }
175:
176: super.detachNode();
177: }
178: }
|