Fri, 04 Oct 2013 16:21:34 +0100
8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.tools.internal.ws.wsdl.parser; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.istack.internal.NotNull; |
ohair@286 | 29 | import com.sun.istack.internal.Nullable; |
ohair@286 | 30 | import com.sun.istack.internal.SAXParseException2; |
ohair@286 | 31 | import com.sun.tools.internal.ws.resources.WsdlMessages; |
ohair@286 | 32 | import com.sun.tools.internal.ws.wscompile.ErrorReceiver; |
ohair@286 | 33 | import com.sun.tools.internal.ws.wscompile.WsimportOptions; |
ohair@286 | 34 | import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants; |
ohair@286 | 35 | import com.sun.tools.internal.xjc.util.DOMUtils; |
ohair@286 | 36 | import com.sun.xml.internal.bind.v2.util.EditDistance; |
ohair@286 | 37 | import com.sun.xml.internal.ws.util.DOMUtil; |
ohair@286 | 38 | import com.sun.xml.internal.ws.util.JAXWSUtils; |
alanb@368 | 39 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
alanb@368 | 40 | import org.w3c.dom.*; |
ohair@286 | 41 | import org.xml.sax.SAXParseException; |
ohair@286 | 42 | |
ohair@286 | 43 | import javax.xml.namespace.NamespaceContext; |
ohair@286 | 44 | import javax.xml.xpath.XPath; |
ohair@286 | 45 | import javax.xml.xpath.XPathConstants; |
ohair@286 | 46 | import javax.xml.xpath.XPathExpressionException; |
ohair@286 | 47 | import javax.xml.xpath.XPathFactory; |
ohair@286 | 48 | import java.net.MalformedURLException; |
ohair@286 | 49 | import java.net.URL; |
ohair@286 | 50 | import java.util.ArrayList; |
ohair@286 | 51 | import java.util.HashSet; |
ohair@286 | 52 | import java.util.Iterator; |
ohair@286 | 53 | import java.util.Set; |
ohair@286 | 54 | |
ohair@286 | 55 | |
ohair@286 | 56 | /** |
ohair@286 | 57 | * Internalizes external binding declarations. |
ohair@286 | 58 | * |
ohair@286 | 59 | * @author Vivek Pandey |
ohair@286 | 60 | */ |
ohair@286 | 61 | public class Internalizer { |
alanb@368 | 62 | |
alanb@368 | 63 | private static final XPathFactory xpf = XmlUtil.newXPathFactory(true); |
ohair@286 | 64 | private final XPath xpath = xpf.newXPath(); |
ohair@286 | 65 | private final DOMForest forest; |
ohair@286 | 66 | private final ErrorReceiver errorReceiver; |
ohair@286 | 67 | |
ohair@286 | 68 | |
ohair@286 | 69 | public Internalizer(DOMForest forest, WsimportOptions options, ErrorReceiver errorReceiver) { |
ohair@286 | 70 | this.forest = forest; |
ohair@286 | 71 | this.errorReceiver = errorReceiver; |
ohair@286 | 72 | } |
ohair@286 | 73 | |
ohair@286 | 74 | public void transform() { |
ohair@286 | 75 | for (Element jaxwsBinding : forest.outerMostBindings) { |
ohair@286 | 76 | internalize(jaxwsBinding, jaxwsBinding); |
ohair@286 | 77 | } |
ohair@286 | 78 | } |
ohair@286 | 79 | |
ohair@286 | 80 | /** |
ohair@286 | 81 | * Validates attributes of a <JAXWS:bindings> element. |
ohair@286 | 82 | */ |
ohair@286 | 83 | private void validate(Element bindings) { |
ohair@286 | 84 | NamedNodeMap atts = bindings.getAttributes(); |
ohair@286 | 85 | for (int i = 0; i < atts.getLength(); i++) { |
ohair@286 | 86 | Attr a = (Attr) atts.item(i); |
alanb@368 | 87 | if (a.getNamespaceURI() != null) { |
ohair@286 | 88 | continue; // all foreign namespace OK. |
alanb@368 | 89 | } |
alanb@368 | 90 | if (a.getLocalName().equals("node")) { |
ohair@286 | 91 | continue; |
alanb@368 | 92 | } |
alanb@368 | 93 | if (a.getLocalName().equals("wsdlLocation")) { |
ohair@286 | 94 | continue; |
alanb@368 | 95 | } |
ohair@286 | 96 | |
ohair@286 | 97 | // TODO: flag error for this undefined attribute |
ohair@286 | 98 | } |
ohair@286 | 99 | } |
ohair@286 | 100 | |
ohair@286 | 101 | private void internalize(Element bindings, Node inheritedTarget) { |
ohair@286 | 102 | // start by the inherited target |
ohair@286 | 103 | Node target = inheritedTarget; |
ohair@286 | 104 | |
ohair@286 | 105 | validate(bindings); // validate this node |
ohair@286 | 106 | |
ohair@286 | 107 | // look for @wsdlLocation |
ohair@286 | 108 | if (isTopLevelBinding(bindings)) { |
ohair@286 | 109 | String wsdlLocation; |
ohair@286 | 110 | if (bindings.getAttributeNode("wsdlLocation") != null) { |
ohair@286 | 111 | wsdlLocation = bindings.getAttribute("wsdlLocation"); |
ohair@286 | 112 | |
ohair@286 | 113 | try { |
ohair@286 | 114 | // absolutize this URI. |
ohair@286 | 115 | // TODO: use the URI class |
ohair@286 | 116 | // TODO: honor xml:base |
ohair@286 | 117 | wsdlLocation = new URL(new URL(forest.getSystemId(bindings.getOwnerDocument())), |
ohair@286 | 118 | wsdlLocation).toExternalForm(); |
ohair@286 | 119 | } catch (MalformedURLException e) { |
ohair@286 | 120 | wsdlLocation = JAXWSUtils.absolutize(JAXWSUtils.getFileOrURLName(wsdlLocation)); |
ohair@286 | 121 | } |
ohair@286 | 122 | } else { |
ohair@286 | 123 | //the node does not have |
ohair@286 | 124 | wsdlLocation = forest.getFirstRootDocument(); |
ohair@286 | 125 | } |
ohair@286 | 126 | target = forest.get(wsdlLocation); |
ohair@286 | 127 | |
ohair@286 | 128 | if (target == null) { |
ohair@286 | 129 | reportError(bindings, WsdlMessages.INTERNALIZER_INCORRECT_SCHEMA_REFERENCE(wsdlLocation, EditDistance.findNearest(wsdlLocation, forest.listSystemIDs()))); |
ohair@286 | 130 | return; // abort processing this <JAXWS:bindings> |
ohair@286 | 131 | } |
ohair@286 | 132 | } |
ohair@286 | 133 | |
ohair@286 | 134 | //if the target node is xs:schema, declare the jaxb version on it as latter on it will be |
ohair@286 | 135 | //required by the inlined schema bindings |
ohair@286 | 136 | |
ohair@286 | 137 | Element element = DOMUtil.getFirstElementChild(target); |
ohair@286 | 138 | if (element != null && element.getNamespaceURI().equals(Constants.NS_WSDL) && element.getLocalName().equals("definitions")) { |
ohair@286 | 139 | //get all schema elements |
ohair@286 | 140 | Element type = DOMUtils.getFirstChildElement(element, Constants.NS_WSDL, "types"); |
ohair@286 | 141 | if (type != null) { |
ohair@286 | 142 | for (Element schemaElement : DOMUtils.getChildElements(type, Constants.NS_XSD, "schema")) { |
ohair@286 | 143 | if (!schemaElement.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) { |
ohair@286 | 144 | schemaElement.setAttributeNS(Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS); |
ohair@286 | 145 | } |
ohair@286 | 146 | |
ohair@286 | 147 | //add jaxb:bindings version info. Lets put it to 1.0, may need to change latter |
ohair@286 | 148 | if (!schemaElement.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) { |
ohair@286 | 149 | schemaElement.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:version", JAXWSBindingsConstants.JAXB_BINDING_VERSION); |
ohair@286 | 150 | } |
ohair@286 | 151 | } |
ohair@286 | 152 | } |
ohair@286 | 153 | } |
ohair@286 | 154 | |
ohair@286 | 155 | |
ohair@286 | 156 | NodeList targetNodes = null; |
ohair@286 | 157 | boolean hasNode = true; |
ohair@286 | 158 | boolean isToplevelBinding = isTopLevelBinding(bindings); |
ohair@286 | 159 | if ((isJAXWSBindings(bindings) || isJAXBBindings(bindings)) && bindings.getAttributeNode("node") != null) { |
ohair@286 | 160 | targetNodes = evaluateXPathMultiNode(bindings, target, bindings.getAttribute("node"), new NamespaceContextImpl(bindings)); |
ohair@286 | 161 | } else |
ohair@286 | 162 | if (isJAXWSBindings(bindings) && (bindings.getAttributeNode("node") == null) && !isToplevelBinding) { |
ohair@286 | 163 | hasNode = false; |
ohair@286 | 164 | } else |
ohair@286 | 165 | if (isGlobalBinding(bindings) && !isWSDLDefinition(target) && isTopLevelBinding(bindings.getParentNode())) { |
ohair@286 | 166 | targetNodes = getWSDLDefintionNode(bindings, target); |
ohair@286 | 167 | } |
ohair@286 | 168 | |
ohair@286 | 169 | //if target is null or empty it means the xpath evaluation has some problem, |
ohair@286 | 170 | // just return |
alanb@368 | 171 | if (targetNodes == null && hasNode && !isToplevelBinding) { |
ohair@286 | 172 | return; |
alanb@368 | 173 | } |
ohair@286 | 174 | |
ohair@286 | 175 | if (hasNode) { |
ohair@286 | 176 | if (targetNodes != null) { |
ohair@286 | 177 | for (int i = 0; i < targetNodes.getLength(); i++) { |
ohair@286 | 178 | insertBinding(bindings, targetNodes.item(i)); |
ohair@286 | 179 | // look for child <JAXWS:bindings> and process them recursively |
ohair@286 | 180 | Element[] children = getChildElements(bindings); |
alanb@368 | 181 | for (Element child : children) { |
alanb@368 | 182 | if ("bindings".equals(child.getLocalName())) { |
ohair@286 | 183 | internalize(child, targetNodes.item(i)); |
ohair@286 | 184 | } |
alanb@368 | 185 | } |
ohair@286 | 186 | } |
ohair@286 | 187 | } |
ohair@286 | 188 | } |
ohair@286 | 189 | if (targetNodes == null) { |
ohair@286 | 190 | // look for child <JAXWS:bindings> and process them recursively |
ohair@286 | 191 | Element[] children = getChildElements(bindings); |
ohair@286 | 192 | |
alanb@368 | 193 | for (Element child : children) { |
ohair@286 | 194 | internalize(child, target); |
alanb@368 | 195 | } |
ohair@286 | 196 | } |
ohair@286 | 197 | } |
ohair@286 | 198 | |
ohair@286 | 199 | /** |
ohair@286 | 200 | * Moves JAXWS customizations under their respective target nodes. |
ohair@286 | 201 | */ |
ohair@286 | 202 | private void insertBinding(@NotNull Element bindings, @NotNull Node target) { |
ohair@286 | 203 | if ("bindings".equals(bindings.getLocalName())) { |
ohair@286 | 204 | Element[] children = DOMUtils.getChildElements(bindings); |
ohair@286 | 205 | for (Element item : children) { |
ohair@286 | 206 | if ("bindings".equals(item.getLocalName())) { |
ohair@286 | 207 | //done |
ohair@286 | 208 | } else { |
ohair@286 | 209 | moveUnder(item, (Element) target); |
ohair@286 | 210 | |
ohair@286 | 211 | } |
ohair@286 | 212 | } |
ohair@286 | 213 | } else { |
ohair@286 | 214 | moveUnder(bindings, (Element) target); |
ohair@286 | 215 | } |
ohair@286 | 216 | } |
ohair@286 | 217 | |
ohair@286 | 218 | private NodeList getWSDLDefintionNode(Node bindings, Node target) { |
ohair@286 | 219 | return evaluateXPathMultiNode(bindings, target, "wsdl:definitions", |
ohair@286 | 220 | new NamespaceContext() { |
alanb@368 | 221 | @Override |
ohair@286 | 222 | public String getNamespaceURI(String prefix) { |
ohair@286 | 223 | return "http://schemas.xmlsoap.org/wsdl/"; |
ohair@286 | 224 | } |
ohair@286 | 225 | |
alanb@368 | 226 | @Override |
ohair@286 | 227 | public String getPrefix(String nsURI) { |
ohair@286 | 228 | throw new UnsupportedOperationException(); |
ohair@286 | 229 | } |
ohair@286 | 230 | |
alanb@368 | 231 | @Override |
ohair@286 | 232 | public Iterator getPrefixes(String namespaceURI) { |
ohair@286 | 233 | throw new UnsupportedOperationException(); |
ohair@286 | 234 | } |
ohair@286 | 235 | }); |
ohair@286 | 236 | } |
ohair@286 | 237 | |
ohair@286 | 238 | private boolean isWSDLDefinition(Node target) { |
alanb@368 | 239 | if (target == null) { |
ohair@286 | 240 | return false; |
alanb@368 | 241 | } |
ohair@286 | 242 | String localName = target.getLocalName(); |
ohair@286 | 243 | String nsURI = target.getNamespaceURI(); |
ohair@286 | 244 | return fixNull(localName).equals("definitions") && fixNull(nsURI).equals("http://schemas.xmlsoap.org/wsdl/"); |
ohair@286 | 245 | } |
ohair@286 | 246 | |
ohair@286 | 247 | private boolean isTopLevelBinding(Node node) { |
ohair@286 | 248 | return node.getOwnerDocument().getDocumentElement() == node; |
ohair@286 | 249 | } |
ohair@286 | 250 | |
ohair@286 | 251 | private boolean isJAXWSBindings(Node bindings) { |
ohair@286 | 252 | return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) && bindings.getLocalName().equals("bindings")); |
ohair@286 | 253 | } |
ohair@286 | 254 | |
ohair@286 | 255 | private boolean isJAXBBindings(Node bindings) { |
ohair@286 | 256 | return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXB_BINDINGS) && bindings.getLocalName().equals("bindings")); |
ohair@286 | 257 | } |
ohair@286 | 258 | |
ohair@286 | 259 | private boolean isGlobalBinding(Node bindings) { |
ohair@286 | 260 | if (bindings.getNamespaceURI() == null) { |
ohair@286 | 261 | errorReceiver.warning(forest.locatorTable.getStartLocation((Element) bindings), WsdlMessages.INVALID_CUSTOMIZATION_NAMESPACE(bindings.getLocalName())); |
ohair@286 | 262 | return false; |
ohair@286 | 263 | } |
ohair@286 | 264 | return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) && |
ohair@286 | 265 | (bindings.getLocalName().equals("package") || |
ohair@286 | 266 | bindings.getLocalName().equals("enableAsyncMapping") || |
ohair@286 | 267 | bindings.getLocalName().equals("enableAdditionalSOAPHeaderMapping") || |
ohair@286 | 268 | bindings.getLocalName().equals("enableWrapperStyle") || |
ohair@286 | 269 | bindings.getLocalName().equals("enableMIMEContent"))); |
ohair@286 | 270 | } |
ohair@286 | 271 | |
ohair@286 | 272 | private static Element[] getChildElements(Element parent) { |
ohair@286 | 273 | ArrayList<Element> a = new ArrayList<Element>(); |
ohair@286 | 274 | NodeList children = parent.getChildNodes(); |
ohair@286 | 275 | for (int i = 0; i < children.getLength(); i++) { |
ohair@286 | 276 | Node item = children.item(i); |
alanb@368 | 277 | if (!(item instanceof Element)) { |
alanb@368 | 278 | continue; |
alanb@368 | 279 | } |
ohair@286 | 280 | if (JAXWSBindingsConstants.NS_JAXWS_BINDINGS.equals(item.getNamespaceURI()) || |
alanb@368 | 281 | JAXWSBindingsConstants.NS_JAXB_BINDINGS.equals(item.getNamespaceURI())) { |
ohair@286 | 282 | a.add((Element) item); |
alanb@368 | 283 | } |
ohair@286 | 284 | } |
ohair@286 | 285 | return a.toArray(new Element[a.size()]); |
ohair@286 | 286 | } |
ohair@286 | 287 | |
ohair@286 | 288 | private NodeList evaluateXPathMultiNode(Node bindings, Node target, String expression, NamespaceContext namespaceContext) { |
ohair@286 | 289 | NodeList nlst; |
ohair@286 | 290 | try { |
ohair@286 | 291 | xpath.setNamespaceContext(namespaceContext); |
ohair@286 | 292 | nlst = (NodeList) xpath.evaluate(expression, target, XPathConstants.NODESET); |
ohair@286 | 293 | } catch (XPathExpressionException e) { |
ohair@286 | 294 | reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATION_ERROR(e.getMessage()), e); |
ohair@286 | 295 | return null; // abort processing this <jaxb:bindings> |
ohair@286 | 296 | } |
ohair@286 | 297 | |
ohair@286 | 298 | if (nlst.getLength() == 0) { |
ohair@286 | 299 | reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATES_TO_NO_TARGET(expression)); |
ohair@286 | 300 | return null; // abort |
ohair@286 | 301 | } |
ohair@286 | 302 | |
ohair@286 | 303 | return nlst; |
ohair@286 | 304 | } |
ohair@286 | 305 | |
ohair@286 | 306 | private boolean isJAXBBindingElement(Element e) { |
ohair@286 | 307 | return fixNull(e.getNamespaceURI()).equals(JAXWSBindingsConstants.NS_JAXB_BINDINGS); |
ohair@286 | 308 | } |
ohair@286 | 309 | |
ohair@286 | 310 | private boolean isJAXWSBindingElement(Element e) { |
ohair@286 | 311 | return fixNull(e.getNamespaceURI()).equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS); |
ohair@286 | 312 | } |
ohair@286 | 313 | |
ohair@286 | 314 | /** |
ohair@286 | 315 | * Moves the "decl" node under the "target" node. |
ohair@286 | 316 | * |
ohair@286 | 317 | * @param decl A JAXWS customization element (e.g., <JAXWS:class>) |
ohair@286 | 318 | * @param target XML wsdl element under which the declaration should move. |
ohair@286 | 319 | * For example, <xs:element> |
ohair@286 | 320 | */ |
ohair@286 | 321 | private void moveUnder(Element decl, Element target) { |
ohair@286 | 322 | |
ohair@286 | 323 | //if there is @node on decl and has a child element jaxb:bindings, move it under the target |
ohair@286 | 324 | //Element jaxb = getJAXBBindingElement(decl); |
ohair@286 | 325 | if (isJAXBBindingElement(decl)) { |
ohair@286 | 326 | //add jaxb namespace declaration |
ohair@286 | 327 | if (!target.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) { |
ohair@286 | 328 | target.setAttributeNS(Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS); |
ohair@286 | 329 | } |
ohair@286 | 330 | |
ohair@286 | 331 | //add jaxb:bindings version info. Lets put it to 1.0, may need to change latter |
ohair@286 | 332 | if (!target.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) { |
ohair@286 | 333 | target.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:version", JAXWSBindingsConstants.JAXB_BINDING_VERSION); |
ohair@286 | 334 | } |
ohair@286 | 335 | |
ohair@286 | 336 | // HACK: allow XJC extension all the time. This allows people to specify |
ohair@286 | 337 | // the <xjc:someExtension> in the external bindings. Otherwise users lack the ability |
ohair@286 | 338 | // to specify jaxb:extensionBindingPrefixes, so it won't work. |
ohair@286 | 339 | // |
ohair@286 | 340 | // the current workaround is still problematic in the sense that |
ohair@286 | 341 | // it can't support user-defined extensions. This needs more careful thought. |
ohair@286 | 342 | |
ohair@286 | 343 | //JAXB doesn't allow writing jaxb:extensionbindingPrefix anywhere other than root element so lets write only on <xs:schema> |
ohair@286 | 344 | if (target.getLocalName().equals("schema") && target.getNamespaceURI().equals(Constants.NS_XSD) && !target.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "extensionBindingPrefixes")) { |
ohair@286 | 345 | target.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:extensionBindingPrefixes", "xjc"); |
ohair@286 | 346 | target.setAttributeNS(Constants.NS_XMLNS, "xmlns:xjc", JAXWSBindingsConstants.NS_XJC_BINDINGS); |
ohair@286 | 347 | } |
ohair@286 | 348 | |
ohair@286 | 349 | //insert xs:annotation/xs:appinfo where in jaxb:binding will be put |
ohair@286 | 350 | target = refineSchemaTarget(target); |
ohair@286 | 351 | copyInscopeNSAttributes(decl); |
ohair@286 | 352 | } else if (isJAXWSBindingElement(decl)) { |
ohair@286 | 353 | //add jaxb namespace declaration |
ohair@286 | 354 | if (!target.hasAttributeNS(Constants.NS_XMLNS, "JAXWS")) { |
ohair@286 | 355 | target.setAttributeNS(Constants.NS_XMLNS, "xmlns:JAXWS", JAXWSBindingsConstants.NS_JAXWS_BINDINGS); |
ohair@286 | 356 | } |
ohair@286 | 357 | |
ohair@286 | 358 | //insert xs:annotation/xs:appinfo where in jaxb:binding will be put |
ohair@286 | 359 | target = refineWSDLTarget(target); |
ohair@286 | 360 | copyInscopeNSAttributes(decl); |
ohair@286 | 361 | } else { |
ohair@286 | 362 | return; |
ohair@286 | 363 | } |
ohair@286 | 364 | |
ohair@286 | 365 | // finally move the declaration to the target node. |
ohair@286 | 366 | if (target.getOwnerDocument() != decl.getOwnerDocument()) { |
ohair@286 | 367 | // if they belong to different DOM documents, we need to clone them |
ohair@286 | 368 | decl = (Element) target.getOwnerDocument().importNode(decl, true); |
ohair@286 | 369 | |
ohair@286 | 370 | } |
ohair@286 | 371 | |
ohair@286 | 372 | target.appendChild(decl); |
ohair@286 | 373 | } |
ohair@286 | 374 | |
ohair@286 | 375 | /** |
ohair@286 | 376 | * Copy in-scope namespace declarations of the decl node |
ohair@286 | 377 | * to the decl node itself so that this move won't change |
ohair@286 | 378 | * the in-scope namespace bindings. |
ohair@286 | 379 | */ |
ohair@286 | 380 | private void copyInscopeNSAttributes(Element e) { |
ohair@286 | 381 | Element p = e; |
ohair@286 | 382 | Set<String> inscopes = new HashSet<String>(); |
ohair@286 | 383 | while (true) { |
ohair@286 | 384 | NamedNodeMap atts = p.getAttributes(); |
ohair@286 | 385 | for (int i = 0; i < atts.getLength(); i++) { |
ohair@286 | 386 | Attr a = (Attr) atts.item(i); |
ohair@286 | 387 | if (Constants.NS_XMLNS.equals(a.getNamespaceURI())) { |
ohair@286 | 388 | String prefix; |
alanb@368 | 389 | if (a.getName().indexOf(':') == -1) { |
alanb@368 | 390 | prefix = ""; |
alanb@368 | 391 | } else { |
alanb@368 | 392 | prefix = a.getLocalName(); |
alanb@368 | 393 | } |
ohair@286 | 394 | |
ohair@286 | 395 | if (inscopes.add(prefix) && p != e) { |
ohair@286 | 396 | // if this is the first time we see this namespace bindings, |
ohair@286 | 397 | // copy the declaration. |
ohair@286 | 398 | // if p==decl, there's no need to. Note that |
ohair@286 | 399 | // we want to add prefix to inscopes even if p==Decl |
ohair@286 | 400 | |
ohair@286 | 401 | e.setAttributeNodeNS((Attr) a.cloneNode(true)); |
ohair@286 | 402 | } |
ohair@286 | 403 | } |
ohair@286 | 404 | } |
ohair@286 | 405 | |
alanb@368 | 406 | if (p.getParentNode() instanceof Document) { |
ohair@286 | 407 | break; |
alanb@368 | 408 | } |
ohair@286 | 409 | |
ohair@286 | 410 | p = (Element) p.getParentNode(); |
ohair@286 | 411 | } |
ohair@286 | 412 | |
ohair@286 | 413 | if (!inscopes.contains("")) { |
ohair@286 | 414 | // if the default namespace was undeclared in the context of decl, |
ohair@286 | 415 | // it must be explicitly set to "" since the new environment might |
ohair@286 | 416 | // have a different default namespace URI. |
ohair@286 | 417 | e.setAttributeNS(Constants.NS_XMLNS, "xmlns", ""); |
ohair@286 | 418 | } |
ohair@286 | 419 | } |
ohair@286 | 420 | |
ohair@286 | 421 | public Element refineSchemaTarget(Element target) { |
ohair@286 | 422 | // look for existing xs:annotation |
ohair@286 | 423 | Element annotation = DOMUtils.getFirstChildElement(target, Constants.NS_XSD, "annotation"); |
alanb@368 | 424 | if (annotation == null) { |
ohair@286 | 425 | // none exists. need to make one |
ohair@286 | 426 | annotation = insertXMLSchemaElement(target, "annotation"); |
alanb@368 | 427 | } |
ohair@286 | 428 | |
ohair@286 | 429 | // then look for appinfo |
ohair@286 | 430 | Element appinfo = DOMUtils.getFirstChildElement(annotation, Constants.NS_XSD, "appinfo"); |
alanb@368 | 431 | if (appinfo == null) { |
ohair@286 | 432 | // none exists. need to make one |
ohair@286 | 433 | appinfo = insertXMLSchemaElement(annotation, "appinfo"); |
alanb@368 | 434 | } |
ohair@286 | 435 | |
ohair@286 | 436 | return appinfo; |
ohair@286 | 437 | } |
ohair@286 | 438 | |
ohair@286 | 439 | public Element refineWSDLTarget(Element target) { |
ohair@286 | 440 | // look for existing xs:annotation |
ohair@286 | 441 | Element JAXWSBindings = DOMUtils.getFirstChildElement(target, JAXWSBindingsConstants.NS_JAXWS_BINDINGS, "bindings"); |
alanb@368 | 442 | if (JAXWSBindings == null) { |
ohair@286 | 443 | // none exists. need to make one |
ohair@286 | 444 | JAXWSBindings = insertJAXWSBindingsElement(target, "bindings"); |
alanb@368 | 445 | } |
ohair@286 | 446 | return JAXWSBindings; |
ohair@286 | 447 | } |
ohair@286 | 448 | |
ohair@286 | 449 | /** |
ohair@286 | 450 | * Creates a new XML Schema element of the given local name |
ohair@286 | 451 | * and insert it as the first child of the given parent node. |
ohair@286 | 452 | * |
ohair@286 | 453 | * @return Newly create element. |
ohair@286 | 454 | */ |
ohair@286 | 455 | private Element insertXMLSchemaElement(Element parent, String localName) { |
ohair@286 | 456 | // use the same prefix as the parent node to avoid modifying |
ohair@286 | 457 | // the namespace binding. |
ohair@286 | 458 | String qname = parent.getTagName(); |
ohair@286 | 459 | int idx = qname.indexOf(':'); |
alanb@368 | 460 | if (idx == -1) { |
alanb@368 | 461 | qname = localName; |
alanb@368 | 462 | } else { |
alanb@368 | 463 | qname = qname.substring(0, idx + 1) + localName; |
alanb@368 | 464 | } |
ohair@286 | 465 | |
ohair@286 | 466 | Element child = parent.getOwnerDocument().createElementNS(Constants.NS_XSD, qname); |
ohair@286 | 467 | |
ohair@286 | 468 | NodeList children = parent.getChildNodes(); |
ohair@286 | 469 | |
alanb@368 | 470 | if (children.getLength() == 0) { |
ohair@286 | 471 | parent.appendChild(child); |
alanb@368 | 472 | } else { |
ohair@286 | 473 | parent.insertBefore(child, children.item(0)); |
alanb@368 | 474 | } |
ohair@286 | 475 | |
ohair@286 | 476 | return child; |
ohair@286 | 477 | } |
ohair@286 | 478 | |
ohair@286 | 479 | private Element insertJAXWSBindingsElement(Element parent, String localName) { |
ohair@286 | 480 | String qname = "JAXWS:" + localName; |
ohair@286 | 481 | |
ohair@286 | 482 | Element child = parent.getOwnerDocument().createElementNS(JAXWSBindingsConstants.NS_JAXWS_BINDINGS, qname); |
ohair@286 | 483 | |
ohair@286 | 484 | NodeList children = parent.getChildNodes(); |
ohair@286 | 485 | |
alanb@368 | 486 | if (children.getLength() == 0) { |
ohair@286 | 487 | parent.appendChild(child); |
alanb@368 | 488 | } else { |
ohair@286 | 489 | parent.insertBefore(child, children.item(0)); |
alanb@368 | 490 | } |
ohair@286 | 491 | |
ohair@286 | 492 | return child; |
ohair@286 | 493 | } |
ohair@286 | 494 | |
ohair@286 | 495 | @NotNull |
alanb@368 | 496 | static String fixNull(@Nullable String s) { |
alanb@368 | 497 | if (s == null) { |
alanb@368 | 498 | return ""; |
alanb@368 | 499 | } else { |
alanb@368 | 500 | return s; |
alanb@368 | 501 | } |
ohair@286 | 502 | } |
ohair@286 | 503 | |
ohair@286 | 504 | private void reportError(Element errorSource, String formattedMsg) { |
ohair@286 | 505 | reportError(errorSource, formattedMsg, null); |
ohair@286 | 506 | } |
ohair@286 | 507 | |
ohair@286 | 508 | private void reportError(Element errorSource, |
ohair@286 | 509 | String formattedMsg, Exception nestedException) { |
ohair@286 | 510 | |
ohair@286 | 511 | SAXParseException e = new SAXParseException2(formattedMsg, |
ohair@286 | 512 | forest.locatorTable.getStartLocation(errorSource), |
ohair@286 | 513 | nestedException); |
ohair@286 | 514 | errorReceiver.error(e); |
ohair@286 | 515 | } |
ohair@286 | 516 | |
ohair@286 | 517 | |
ohair@286 | 518 | } |