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