1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
32 import javax.xml.soap.*;
33
34 import com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory;
35 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
36 import com.sun.xml.internal.messaging.saaj.util.*;
37
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.NodeList;
41 import org.w3c.dom.NamedNodeMap;
42 import org.w3c.dom.Attr;
43
44 public abstract class SOAPFactoryImpl extends SOAPFactory {
45
46 protected static final Logger
47 log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
48 "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
49
50 protected abstract SOAPDocumentImpl createDocument();
51
52 public SOAPElement createElement(String tagName) throws SOAPException {
53 if (tagName == null) {
54 log.log(
55 Level.SEVERE,"SAAJ0567.soap.null.input",
56 new Object[] {"tagName","SOAPFactory.createElement"});
57 throw new SOAPException("Null tagName argument passed to createElement");
58 }
59 return ElementFactory.createElement(createDocument(),
60 NameImpl.createFromTagName(tagName));
61 }
62
63 public SOAPElement createElement(Name name) throws SOAPException {
64 // @since SAAJ 1.3
65 // If the Name was null it would cause a NullPointerException in earlier release
66 if (name == null) {
67 log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
68 new Object[] {"name","SOAPFactory.createElement"});
69 throw new SOAPException("Null name argument passed to createElement");
70 }
71 return ElementFactory.createElement(createDocument(), name);
72 }
73
74 public SOAPElement createElement(QName qname) throws SOAPException {
75 if (qname == null) {
76 log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
77 new Object[] {"qname","SOAPFactory.createElement"});
78 throw new SOAPException("Null qname argument passed to createElement");
79 }
80 return ElementFactory.createElement(createDocument(),qname);
81 }
82
83 public SOAPElement createElement(
84 String localName,
85 String prefix,
86 String uri) throws SOAPException {
87
88 // @since SAAJ 1.3
89 // if prefix !=null but localName== null then in earlier releases it would create
90 // a Qualified Name <prefix>:null which is not meaningful
91 if (localName == null) {
92 log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
93 new Object[] {"localName","SOAPFactory.createElement"});
94 throw new SOAPException("Null localName argument passed to createElement");
95 }
96 return ElementFactory.createElement(createDocument(), localName, prefix, uri);
97 }
98
99 public Name createName(String localName, String prefix, String uri)
100 throws SOAPException {
101 // @since SAAJ 1.3
102 // if localName==null, earlier impl would create Name with localName=""
103 // which is absurd.
104 if (localName == null) {
105 log.log(
106 Level.SEVERE,"SAAJ0567.soap.null.input",
107 new Object[] {"localName","SOAPFactory.createName"});
108 throw new SOAPException("Null localName argument passed to createName");
109 }
110 return NameImpl.create(localName, prefix, uri);
111 }
112
113 public Name createName(String localName) throws SOAPException {
114 // @since SAAJ 1.3
115 // if localName==null, earlier impl would create Name with localName=null
116 // which is absurd.
117 if (localName == null) {
118 log.log(
119 Level.SEVERE,"SAAJ0567.soap.null.input",
120 new Object[] {"localName","SOAPFactory.createName"});
121 throw new SOAPException("Null localName argument passed to createName");
122 }
123 return NameImpl.createFromUnqualifiedName(localName);
124 }
125
126 // Note: the child elements might still be org.w3c.dom.Element's, but the
127 // getChildElements will do the conversion to SOAPElement when called.
128 public SOAPElement createElement(Element domElement) throws SOAPException {
129 if (domElement == null) {
130 return null;
131 }
132 return convertToSoapElement(domElement);
133 }
134
135 private SOAPElement convertToSoapElement(Element element) throws SOAPException {
136
137 if (element instanceof SOAPElement) {
138 return (SOAPElement) element;
139 }
140
141 SOAPElement copy = createElement(
142 element.getLocalName(),
143 element.getPrefix(),
144 element.getNamespaceURI());
145
146 Document ownerDoc = copy.getOwnerDocument();
147
148 NamedNodeMap attrMap = element.getAttributes();
149 for (int i=0; i < attrMap.getLength(); i++) {
150 Attr nextAttr = (Attr)attrMap.item(i);
151 Attr importedAttr = (Attr)ownerDoc.importNode(nextAttr, true);
152 copy.setAttributeNodeNS(importedAttr);
153 }
154
155
156 NodeList nl = element.getChildNodes();
157 for (int i=0; i < nl.getLength(); i++) {
158 org.w3c.dom.Node next = nl.item(i);
159 org.w3c.dom.Node imported = ownerDoc.importNode(next, true);
160 copy.appendChild(imported);
161 }
162
163 return copy;
164 }
165
166 public Detail createDetail() throws SOAPException {
167 throw new UnsupportedOperationException();
168 }
169
170 public SOAPFault createFault(String reasonText, QName faultCode) throws SOAPException {
171 throw new UnsupportedOperationException();
172 }
173
174 public SOAPFault createFault() throws SOAPException {
175 throw new UnsupportedOperationException();
176 }
177
178 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
32 import javax.xml.soap.*;
33
34 import com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory;
35 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
36 import com.sun.xml.internal.messaging.saaj.util.*;
37
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.NodeList;
41 import org.w3c.dom.NamedNodeMap;
42 import org.w3c.dom.Attr;
43
44 public abstract class SOAPFactoryImpl extends SOAPFactory {
45
46 protected static final Logger
47 log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
48 "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
49
50 protected abstract SOAPDocumentImpl createDocument();
51
52 @Override
53 public SOAPElement createElement(String tagName) throws SOAPException {
54 if (tagName == null) {
55 log.log(
56 Level.SEVERE,"SAAJ0567.soap.null.input",
57 new Object[] {"tagName","SOAPFactory.createElement"});
58 throw new SOAPException("Null tagName argument passed to createElement");
59 }
60 return ElementFactory.createElement(createDocument(),
61 NameImpl.createFromTagName(tagName));
62 }
63
64 @Override
65 public SOAPElement createElement(Name name) throws SOAPException {
66 // @since SAAJ 1.3
67 // If the Name was null it would cause a NullPointerException in earlier release
68 if (name == null) {
69 log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
70 new Object[] {"name","SOAPFactory.createElement"});
71 throw new SOAPException("Null name argument passed to createElement");
72 }
73 return ElementFactory.createElement(createDocument(), name);
74 }
75
76 @Override
77 public SOAPElement createElement(QName qname) throws SOAPException {
78 if (qname == null) {
79 log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
80 new Object[] {"qname","SOAPFactory.createElement"});
81 throw new SOAPException("Null qname argument passed to createElement");
82 }
83 return ElementFactory.createElement(createDocument(),qname);
84 }
85
86 @Override
87 public SOAPElement createElement(
88 String localName,
89 String prefix,
90 String uri) throws SOAPException {
91
92 // @since SAAJ 1.3
93 // if prefix !=null but localName== null then in earlier releases it would create
94 // a Qualified Name <prefix>:null which is not meaningful
95 if (localName == null) {
96 log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
97 new Object[] {"localName","SOAPFactory.createElement"});
98 throw new SOAPException("Null localName argument passed to createElement");
99 }
100 return ElementFactory.createElement(createDocument(), localName, prefix, uri);
101 }
102
103 @Override
104 public Name createName(String localName, String prefix, String uri)
105 throws SOAPException {
106 // @since SAAJ 1.3
107 // if localName==null, earlier impl would create Name with localName=""
108 // which is absurd.
109 if (localName == null) {
110 log.log(
111 Level.SEVERE,"SAAJ0567.soap.null.input",
112 new Object[] {"localName","SOAPFactory.createName"});
113 throw new SOAPException("Null localName argument passed to createName");
114 }
115 return NameImpl.create(localName, prefix, uri);
116 }
117
118 @Override
119 public Name createName(String localName) throws SOAPException {
120 // @since SAAJ 1.3
121 // if localName==null, earlier impl would create Name with localName=null
122 // which is absurd.
123 if (localName == null) {
124 log.log(
125 Level.SEVERE,"SAAJ0567.soap.null.input",
126 new Object[] {"localName","SOAPFactory.createName"});
127 throw new SOAPException("Null localName argument passed to createName");
128 }
129 return NameImpl.createFromUnqualifiedName(localName);
130 }
131
132 // Note: the child elements might still be org.w3c.dom.Element's, but the
133 // getChildElements will do the conversion to SOAPElement when called.
134 @Override
135 public SOAPElement createElement(Element domElement) throws SOAPException {
136 if (domElement == null) {
137 return null;
138 }
139 return convertToSoapElement(domElement);
140 }
141
142 private SOAPElement convertToSoapElement(Element element) throws SOAPException {
143
144 if (element instanceof SOAPElement) {
145 return (SOAPElement) element;
146 }
147
148 SOAPElement copy = createElement(
149 element.getLocalName(),
150 element.getPrefix(),
151 element.getNamespaceURI());
152
153 Document ownerDoc = copy.getOwnerDocument();
154
155 NamedNodeMap attrMap = element.getAttributes();
156 for (int i=0; i < attrMap.getLength(); i++) {
157 Attr nextAttr = (Attr)attrMap.item(i);
158 Attr importedAttr = (Attr)ownerDoc.importNode(nextAttr, true);
159 copy.setAttributeNodeNS(importedAttr);
160 }
161
162
163 NodeList nl = element.getChildNodes();
164 for (int i=0; i < nl.getLength(); i++) {
165 org.w3c.dom.Node next = nl.item(i);
166 org.w3c.dom.Node imported = ownerDoc.importNode(next, true);
167 copy.appendChild(imported);
168 }
169
170 return copy;
171 }
172
173 @Override
174 public Detail createDetail() throws SOAPException {
175 throw new UnsupportedOperationException();
176 }
177
178 @Override
179 public SOAPFault createFault(String reasonText, QName faultCode) throws SOAPException {
180 throw new UnsupportedOperationException();
181 }
182
183 @Override
184 public SOAPFault createFault() throws SOAPException {
185 throw new UnsupportedOperationException();
186 }
187
188 }
|