001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.saaj;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axiom.om.OMNamespace;
023: import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
024: import org.apache.axiom.om.impl.dom.ElementImpl;
025: import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
026: import org.w3c.dom.Element;
027:
028: import javax.xml.namespace.QName;
029: import javax.xml.soap.Detail;
030: import javax.xml.soap.Name;
031: import javax.xml.soap.SOAPConstants;
032: import javax.xml.soap.SOAPElement;
033: import javax.xml.soap.SOAPException;
034: import javax.xml.soap.SOAPFactory;
035: import javax.xml.soap.SOAPFault;
036: import java.util.Locale;
037:
038: /**
039: *
040: */
041: public class SOAPFactoryImpl extends SOAPFactory {
042:
043: protected String soapVersion = SOAPConstants.SOAP_1_1_PROTOCOL;
044:
045: /**
046: * Create a <code>SOAPElement</code> object initialized with the given <code>Name</code>
047: * object.
048: *
049: * @param name a <code>Name</code> object with the XML name for the new element
050: * @return the new <code>SOAPElement</code> object that was created
051: * @throws javax.xml.soap.SOAPException if there is an error in creating the <code>SOAPElement</code>
052: * object
053: */
054: public SOAPElement createElement(Name name) throws SOAPException {
055: String localName = name.getLocalName();
056: String prefix = name.getPrefix();
057: String uri = name.getURI();
058: OMElement omElement = null;
059: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
060: omElement = DOOMAbstractFactory.getSOAP12Factory()
061: .createOMElement(localName, uri, prefix);
062: } else {
063: omElement = DOOMAbstractFactory.getSOAP11Factory()
064: .createOMElement(localName, uri, prefix);
065: }
066: DOOMAbstractFactory.getOMFactory().createOMElement(localName,
067: uri, prefix);
068: return new SOAPElementImpl((ElementImpl) omElement);
069: }
070:
071: /**
072: * Create a <code>SOAPElement</code> object initialized with the given local name.
073: *
074: * @param localName a <code>String</code> giving the local name for the new element
075: * @return the new <code>SOAPElement</code> object that was created
076: * @throws javax.xml.soap.SOAPException if there is an error in creating the <code>SOAPElement</code>
077: * object
078: */
079: public SOAPElement createElement(String localName)
080: throws SOAPException {
081: OMDOMFactory omdomFactory = null;
082: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
083: omdomFactory = (OMDOMFactory) DOOMAbstractFactory
084: .getSOAP12Factory();
085: } else {
086: omdomFactory = (OMDOMFactory) DOOMAbstractFactory
087: .getSOAP11Factory();
088: }
089: OMElement omElement = omdomFactory.createOMElement(new QName(
090: localName));
091: return new SOAPElementImpl((ElementImpl) omElement);
092: }
093:
094: /**
095: * Create a new <code>SOAPElement</code> object with the given local name, prefix and uri.
096: *
097: * @param localName a <code>String</code> giving the local name for the new element
098: * @param prefix the prefix for this <code>SOAPElement</code>
099: * @param uri a <code>String</code> giving the URI of the namespace to which the new
100: * element belongs
101: * @return the new <code>SOAPElement</code> object that was created
102: * @throws javax.xml.soap.SOAPException if there is an error in creating the <code>SOAPElement</code>
103: * object
104: */
105: public SOAPElement createElement(String localName, String prefix,
106: String uri) throws SOAPException {
107: OMElement omElement = null;
108: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
109: omElement = DOOMAbstractFactory.getSOAP12Factory()
110: .createOMElement(localName, uri, prefix);
111: } else {
112: omElement = DOOMAbstractFactory.getSOAP11Factory()
113: .createOMElement(localName, uri, prefix);
114: }
115: return new SOAPElementImpl((ElementImpl) omElement);
116: }
117:
118: /**
119: * Creates a new <code>Detail</code> object which serves as a container for
120: * <code>DetailEntry</code> objects.
121: * <p/>
122: * This factory method creates <code>Detail</code> objects for use in situations where it is not
123: * practical to use the <code>SOAPFault</code> abstraction.
124: *
125: * @return a <code>Detail</code> object
126: * @throws javax.xml.soap.SOAPException if there is a SOAP error
127: */
128: public Detail createDetail() throws SOAPException {
129: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
130: return new DetailImpl(DOOMAbstractFactory
131: .getSOAP12Factory().createSOAPFaultDetail());
132: } else {
133: return new DetailImpl(DOOMAbstractFactory
134: .getSOAP11Factory().createSOAPFaultDetail());
135: }
136: }
137:
138: /**
139: * Creates a new <code>Name</code> object initialized with the given local name, namespace
140: * prefix, and namespace URI.
141: * <p/>
142: * This factory method creates <code>Name</code> objects for use in situations where it is not
143: * practical to use the <code>SOAPEnvelope</code> abstraction.
144: *
145: * @param localName a <code>String</code> giving the local name
146: * @param prefix a <code>String</code> giving the prefix of the namespace
147: * @param uri a <code>String</code> giving the URI of the namespace
148: * @return a <code>Name</code> object initialized with the given local name, namespace prefix,
149: * and namespace URI
150: * @throws javax.xml.soap.SOAPException if there is a SOAP error
151: */
152: public Name createName(String localName, String prefix, String uri)
153: throws SOAPException {
154: return new PrefixedQName(uri, localName, prefix);
155: }
156:
157: /**
158: * Creates a new <code>Name</code> object initialized with the given local name.
159: * <p/>
160: * This factory method creates <code>Name</code> objects for use in situations where it is not
161: * practical to use the <code>SOAPEnvelope</code> abstraction.
162: *
163: * @param localName a <code>String</code> giving the local name
164: * @return a <code>Name</code> object initialized with the given local name
165: * @throws javax.xml.soap.SOAPException if there is a SOAP error
166: */
167: public Name createName(String localName) throws SOAPException {
168: return new PrefixedQName(null, localName, null);
169: }
170:
171: /**
172: * Creates a new default SOAPFault object
173: *
174: * @return a SOAPFault object
175: * @throws SOAPException - if there is a SOAP error
176: */
177: public SOAPFault createFault() throws SOAPException {
178: org.apache.axiom.soap.SOAPFactory soapFactory;
179: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
180: soapFactory = DOOMAbstractFactory.getSOAP12Factory();
181: return new SOAPFaultImpl(soapFactory.createSOAPFault());
182: } else {
183: soapFactory = DOOMAbstractFactory.getSOAP11Factory();
184: return new SOAPFaultImpl(soapFactory.createSOAPFault());
185: }
186: }
187:
188: /**
189: * Creates a new SOAPFault object initialized with the given reasonText and faultCode
190: *
191: * @param reasonText - the ReasonText/FaultString for the fault faultCode - the FaultCode for
192: * the fault
193: * @return: a SOAPFault object
194: * @throws: SOAPException - if there is a SOAP error
195: */
196: public SOAPFault createFault(String reasonText, QName faultCode)
197: throws SOAPException {
198: SOAPFault soapFault;
199: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
200: soapFault = new SOAPFaultImpl(DOOMAbstractFactory
201: .getSOAP12Factory().createSOAPFault());
202: } else {
203: soapFault = new SOAPFaultImpl(DOOMAbstractFactory
204: .getSOAP11Factory().createSOAPFault());
205: }
206: soapFault.setFaultCode(faultCode);
207: try {
208: soapFault.addFaultReasonText(reasonText, Locale
209: .getDefault());
210: } catch (UnsupportedOperationException e) {
211: throw new SOAPException(e.getMessage());
212: }
213:
214: return soapFault;
215: }
216:
217: public void setSOAPVersion(String soapVersion) {
218: this .soapVersion = soapVersion;
219: }
220:
221: public SOAPElement createElement(QName qname) throws SOAPException {
222: String localName = qname.getLocalPart();
223: String prefix = qname.getPrefix();
224: String uri = qname.getNamespaceURI();
225: OMElement omElement = DOOMAbstractFactory.getOMFactory()
226: .createOMElement(localName, uri, prefix);
227: return new SOAPElementImpl((ElementImpl) omElement);
228: }
229:
230: public SOAPElement createElement(Element element)
231: throws SOAPException {
232: OMDOMFactory omdomFactory = null;
233: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
234: omdomFactory = (OMDOMFactory) DOOMAbstractFactory
235: .getSOAP12Factory();
236: } else {
237: omdomFactory = (OMDOMFactory) DOOMAbstractFactory
238: .getSOAP11Factory();
239: }
240: OMNamespace ns = omdomFactory.createOMNamespace(element
241: .getNamespaceURI(), element.getPrefix());
242: OMElement omElement = omdomFactory.createOMElement(element
243: .getLocalName(), ns);
244: return new SOAPElementImpl((ElementImpl) omElement);
245: }
246:
247: }
|