src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/Internalizer.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 515
6cd506508147
     1.1 --- a/src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/Internalizer.java	Thu Apr 04 19:05:24 2013 -0700
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/Internalizer.java	Tue Apr 09 14:51:13 2013 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -36,12 +36,8 @@
    1.11  import com.sun.xml.internal.bind.v2.util.EditDistance;
    1.12  import com.sun.xml.internal.ws.util.DOMUtil;
    1.13  import com.sun.xml.internal.ws.util.JAXWSUtils;
    1.14 -import org.w3c.dom.Attr;
    1.15 -import org.w3c.dom.Document;
    1.16 -import org.w3c.dom.Element;
    1.17 -import org.w3c.dom.NamedNodeMap;
    1.18 -import org.w3c.dom.Node;
    1.19 -import org.w3c.dom.NodeList;
    1.20 +import com.sun.xml.internal.ws.util.xml.XmlUtil;
    1.21 +import org.w3c.dom.*;
    1.22  import org.xml.sax.SAXParseException;
    1.23  
    1.24  import javax.xml.namespace.NamespaceContext;
    1.25 @@ -52,10 +48,8 @@
    1.26  import java.net.MalformedURLException;
    1.27  import java.net.URL;
    1.28  import java.util.ArrayList;
    1.29 -import java.util.HashMap;
    1.30  import java.util.HashSet;
    1.31  import java.util.Iterator;
    1.32 -import java.util.Map;
    1.33  import java.util.Set;
    1.34  
    1.35  
    1.36 @@ -65,16 +59,15 @@
    1.37   * @author Vivek Pandey
    1.38   */
    1.39  public class Internalizer {
    1.40 -    private static final XPathFactory xpf = XPathFactory.newInstance();
    1.41 +
    1.42 +    private static final XPathFactory xpf = XmlUtil.newXPathFactory(true);
    1.43      private final XPath xpath = xpf.newXPath();
    1.44 -    private final WsimportOptions options;
    1.45      private final DOMForest forest;
    1.46      private final ErrorReceiver errorReceiver;
    1.47  
    1.48  
    1.49      public Internalizer(DOMForest forest, WsimportOptions options, ErrorReceiver errorReceiver) {
    1.50          this.forest = forest;
    1.51 -        this.options = options;
    1.52          this.errorReceiver = errorReceiver;
    1.53      }
    1.54  
    1.55 @@ -82,15 +75,6 @@
    1.56          for (Element jaxwsBinding : forest.outerMostBindings) {
    1.57              internalize(jaxwsBinding, jaxwsBinding);
    1.58          }
    1.59 -        /*
    1.60 -        Map<Element, Node> targetNodes = new HashMap<Element, Node>();
    1.61 -        for (Element jaxwsBinding : forest.outerMostBindings) {
    1.62 -            buildTargetNodeMap(jaxwsBinding, jaxwsBinding, targetNodes);
    1.63 -        }
    1.64 -        for (Element jaxwsBinding : forest.outerMostBindings) {
    1.65 -            move(jaxwsBinding, targetNodes);
    1.66 -        }
    1.67 -        */
    1.68      }
    1.69  
    1.70      /**
    1.71 @@ -100,12 +84,15 @@
    1.72          NamedNodeMap atts = bindings.getAttributes();
    1.73          for (int i = 0; i < atts.getLength(); i++) {
    1.74              Attr a = (Attr) atts.item(i);
    1.75 -            if (a.getNamespaceURI() != null)
    1.76 +            if (a.getNamespaceURI() != null) {
    1.77                  continue;   // all foreign namespace OK.
    1.78 -            if (a.getLocalName().equals("node"))
    1.79 +            }
    1.80 +            if (a.getLocalName().equals("node")) {
    1.81                  continue;
    1.82 -            if (a.getLocalName().equals("wsdlLocation"))
    1.83 +            }
    1.84 +            if (a.getLocalName().equals("wsdlLocation")) {
    1.85                  continue;
    1.86 +            }
    1.87  
    1.88              // TODO: flag error for this undefined attribute
    1.89          }
    1.90 @@ -181,9 +168,9 @@
    1.91  
    1.92          //if target is null or empty it means the xpath evaluation has some problem,
    1.93          // just return
    1.94 -        if (targetNodes == null && hasNode && !isToplevelBinding)
    1.95 +        if (targetNodes == null && hasNode && !isToplevelBinding) {
    1.96              return;
    1.97 -
    1.98 +        }
    1.99  
   1.100          if (hasNode) {
   1.101              if (targetNodes != null) {
   1.102 @@ -191,10 +178,11 @@
   1.103                      insertBinding(bindings, targetNodes.item(i));
   1.104                      // look for child <JAXWS:bindings> and process them recursively
   1.105                      Element[] children = getChildElements(bindings);
   1.106 -                    for (Element child : children)
   1.107 -                        if("bindings".equals(child.getLocalName())) {
   1.108 +                    for (Element child : children) {
   1.109 +                        if ("bindings".equals(child.getLocalName())) {
   1.110                              internalize(child, targetNodes.item(i));
   1.111                          }
   1.112 +                    }
   1.113                  }
   1.114              }
   1.115          }
   1.116 @@ -202,8 +190,9 @@
   1.117              // look for child <JAXWS:bindings> and process them recursively
   1.118              Element[] children = getChildElements(bindings);
   1.119  
   1.120 -            for (Element child : children)
   1.121 +            for (Element child : children) {
   1.122                  internalize(child, target);
   1.123 +            }
   1.124          }
   1.125      }
   1.126  
   1.127 @@ -226,105 +215,20 @@
   1.128          }
   1.129      }
   1.130  
   1.131 -
   1.132 -    /**
   1.133 -     * Determines the target node of the "bindings" element
   1.134 -     * by using the inherited target node, then put
   1.135 -     * the result into the "result" map.
   1.136 -     */
   1.137 -    /* TODO Remove this logic if there are no regressions with new internalization logic
   1.138 -    private void buildTargetNodeMap(Element bindings, Node inheritedTarget, Map<Element, Node> result) {
   1.139 -        // start by the inherited target
   1.140 -        Node target = inheritedTarget;
   1.141 -
   1.142 -        validate(bindings); // validate this node
   1.143 -
   1.144 -        // look for @wsdlLocation
   1.145 -        if (isTopLevelBinding(bindings)) {
   1.146 -            String wsdlLocation;
   1.147 -            if (bindings.getAttributeNode("wsdlLocation") != null) {
   1.148 -                wsdlLocation = bindings.getAttribute("wsdlLocation");
   1.149 -
   1.150 -                try {
   1.151 -                    // absolutize this URI.
   1.152 -                    // TODO: use the URI class
   1.153 -                    // TODO: honor xml:base
   1.154 -                    wsdlLocation = new URL(new URL(forest.getSystemId(bindings.getOwnerDocument())),
   1.155 -                            wsdlLocation).toExternalForm();
   1.156 -                } catch (MalformedURLException e) {
   1.157 -                    wsdlLocation = JAXWSUtils.absolutize(JAXWSUtils.getFileOrURLName(wsdlLocation));
   1.158 -                }
   1.159 -            } else {
   1.160 -                //the node does not have
   1.161 -                wsdlLocation = forest.getFirstRootDocument();
   1.162 -            }
   1.163 -            target = forest.get(wsdlLocation);
   1.164 -
   1.165 -            if (target == null) {
   1.166 -                reportError(bindings, WsdlMessages.INTERNALIZER_INCORRECT_SCHEMA_REFERENCE(wsdlLocation, EditDistance.findNearest(wsdlLocation, forest.listSystemIDs())));
   1.167 -                return; // abort processing this <JAXWS:bindings>
   1.168 -            }
   1.169 -        }
   1.170 -
   1.171 -        //if the target node is xs:schema, declare the jaxb version on it as latter on it will be
   1.172 -        //required by the inlined schema bindings
   1.173 -
   1.174 -        Element element = DOMUtil.getFirstElementChild(target);
   1.175 -        if (element != null && element.getNamespaceURI().equals(Constants.NS_WSDL) && element.getLocalName().equals("definitions")) {
   1.176 -            //get all schema elements
   1.177 -            Element type = DOMUtils.getFirstChildElement(element, Constants.NS_WSDL, "types");
   1.178 -            if (type != null) {
   1.179 -                for (Element schemaElement : DOMUtils.getChildElements(type, Constants.NS_XSD, "schema")) {
   1.180 -                    if (!schemaElement.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) {
   1.181 -                        schemaElement.setAttributeNS(Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS);
   1.182 -                    }
   1.183 -
   1.184 -                    //add jaxb:bindings version info. Lets put it to 1.0, may need to change latter
   1.185 -                    if (!schemaElement.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) {
   1.186 -                        schemaElement.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:version", JAXWSBindingsConstants.JAXB_BINDING_VERSION);
   1.187 -                    }
   1.188 -                }
   1.189 -            }
   1.190 -        }
   1.191 -
   1.192 -
   1.193 -        boolean hasNode = true;
   1.194 -        if ((isJAXWSBindings(bindings) || isJAXBBindings(bindings)) && bindings.getAttributeNode("node") != null) {
   1.195 -            target = evaluateXPathNode(bindings, target, bindings.getAttribute("node"), new NamespaceContextImpl(bindings));
   1.196 -        } else
   1.197 -        if (isJAXWSBindings(bindings) && (bindings.getAttributeNode("node") == null) && !isTopLevelBinding(bindings)) {
   1.198 -            hasNode = false;
   1.199 -        } else
   1.200 -        if (isGlobalBinding(bindings) && !isWSDLDefinition(target) && isTopLevelBinding(bindings.getParentNode())) {
   1.201 -            target = getWSDLDefintionNode(bindings, target);
   1.202 -        }
   1.203 -
   1.204 -        //if target is null it means the xpath evaluation has some problem,
   1.205 -        // just return
   1.206 -        if (target == null)
   1.207 -            return;
   1.208 -
   1.209 -        // update the result map
   1.210 -        if (hasNode)
   1.211 -            result.put(bindings, target);
   1.212 -
   1.213 -        // look for child <JAXWS:bindings> and process them recursively
   1.214 -        Element[] children = getChildElements(bindings);
   1.215 -        for (Element child : children)
   1.216 -            buildTargetNodeMap(child, target, result);
   1.217 -    }
   1.218 -    */
   1.219      private NodeList getWSDLDefintionNode(Node bindings, Node target) {
   1.220          return evaluateXPathMultiNode(bindings, target, "wsdl:definitions",
   1.221                  new NamespaceContext() {
   1.222 +                    @Override
   1.223                      public String getNamespaceURI(String prefix) {
   1.224                          return "http://schemas.xmlsoap.org/wsdl/";
   1.225                      }
   1.226  
   1.227 +                    @Override
   1.228                      public String getPrefix(String nsURI) {
   1.229                          throw new UnsupportedOperationException();
   1.230                      }
   1.231  
   1.232 +                    @Override
   1.233                      public Iterator getPrefixes(String namespaceURI) {
   1.234                          throw new UnsupportedOperationException();
   1.235                      }
   1.236 @@ -332,8 +236,9 @@
   1.237      }
   1.238  
   1.239      private boolean isWSDLDefinition(Node target) {
   1.240 -        if (target == null)
   1.241 +        if (target == null) {
   1.242              return false;
   1.243 +        }
   1.244          String localName = target.getLocalName();
   1.245          String nsURI = target.getNamespaceURI();
   1.246          return fixNull(localName).equals("definitions") && fixNull(nsURI).equals("http://schemas.xmlsoap.org/wsdl/");
   1.247 @@ -369,43 +274,17 @@
   1.248          NodeList children = parent.getChildNodes();
   1.249          for (int i = 0; i < children.getLength(); i++) {
   1.250              Node item = children.item(i);
   1.251 -            if (!(item instanceof Element)) continue;
   1.252 -
   1.253 +            if (!(item instanceof Element)) {
   1.254 +                continue;
   1.255 +            }
   1.256              if (JAXWSBindingsConstants.NS_JAXWS_BINDINGS.equals(item.getNamespaceURI()) ||
   1.257 -                    JAXWSBindingsConstants.NS_JAXB_BINDINGS.equals(item.getNamespaceURI()))
   1.258 +                    JAXWSBindingsConstants.NS_JAXB_BINDINGS.equals(item.getNamespaceURI())) {
   1.259                  a.add((Element) item);
   1.260 +            }
   1.261          }
   1.262          return a.toArray(new Element[a.size()]);
   1.263      }
   1.264  
   1.265 -    private Node evaluateXPathNode(Node bindings, Node target, String expression, NamespaceContext namespaceContext) {
   1.266 -        NodeList nlst;
   1.267 -        try {
   1.268 -            xpath.setNamespaceContext(namespaceContext);
   1.269 -            nlst = (NodeList) xpath.evaluate(expression, target, XPathConstants.NODESET);
   1.270 -        } catch (XPathExpressionException e) {
   1.271 -            reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATION_ERROR(e.getMessage()), e);
   1.272 -            return null; // abort processing this <jaxb:bindings>
   1.273 -        }
   1.274 -
   1.275 -        if (nlst.getLength() == 0) {
   1.276 -            reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATES_TO_NO_TARGET(expression));
   1.277 -            return null; // abort
   1.278 -        }
   1.279 -
   1.280 -        if (nlst.getLength() != 1) {
   1.281 -            reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVAULATES_TO_TOO_MANY_TARGETS(expression, nlst.getLength()));
   1.282 -            return null; // abort
   1.283 -        }
   1.284 -
   1.285 -        Node rnode = nlst.item(0);
   1.286 -        if (!(rnode instanceof Element)) {
   1.287 -            reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATES_TO_NON_ELEMENT(expression));
   1.288 -            return null; // abort
   1.289 -        }
   1.290 -        return rnode;
   1.291 -    }
   1.292 -
   1.293      private NodeList evaluateXPathMultiNode(Node bindings, Node target, String expression, NamespaceContext namespaceContext) {
   1.294          NodeList nlst;
   1.295          try {
   1.296 @@ -424,35 +303,6 @@
   1.297          return nlst;
   1.298      }
   1.299  
   1.300 -    /**
   1.301 -     * Moves JAXWS customizations under their respective target nodes.
   1.302 -     */
   1.303 -    private void move(Element bindings, Map<Element, Node> targetNodes) {
   1.304 -        Node target = targetNodes.get(bindings);
   1.305 -        if (target == null)
   1.306 -            // this must be the result of an error on the external binding.
   1.307 -            // recover from the error by ignoring this node
   1.308 -            return;
   1.309 -
   1.310 -        Element[] children = DOMUtils.getChildElements(bindings);
   1.311 -
   1.312 -        for (Element item : children) {
   1.313 -            if ("bindings".equals(item.getLocalName())) {
   1.314 -                // process child <jaxws:bindings> recursively
   1.315 -                move(item, targetNodes);
   1.316 -            } else if (isGlobalBinding(item)) {
   1.317 -                target = targetNodes.get(item);
   1.318 -                moveUnder(item, (Element) target);
   1.319 -            } else {
   1.320 -                if (!(target instanceof Element)) {
   1.321 -                    return; // abort
   1.322 -                }
   1.323 -                // move this node under the target
   1.324 -                moveUnder(item, (Element) target);
   1.325 -            }
   1.326 -        }
   1.327 -    }
   1.328 -
   1.329      private boolean isJAXBBindingElement(Element e) {
   1.330          return fixNull(e.getNamespaceURI()).equals(JAXWSBindingsConstants.NS_JAXB_BINDINGS);
   1.331      }
   1.332 @@ -536,8 +386,11 @@
   1.333                  Attr a = (Attr) atts.item(i);
   1.334                  if (Constants.NS_XMLNS.equals(a.getNamespaceURI())) {
   1.335                      String prefix;
   1.336 -                    if (a.getName().indexOf(':') == -1) prefix = "";
   1.337 -                    else prefix = a.getLocalName();
   1.338 +                    if (a.getName().indexOf(':') == -1) {
   1.339 +                        prefix = "";
   1.340 +                    } else {
   1.341 +                        prefix = a.getLocalName();
   1.342 +                    }
   1.343  
   1.344                      if (inscopes.add(prefix) && p != e) {
   1.345                          // if this is the first time we see this namespace bindings,
   1.346 @@ -550,8 +403,9 @@
   1.347                  }
   1.348              }
   1.349  
   1.350 -            if (p.getParentNode() instanceof Document)
   1.351 +            if (p.getParentNode() instanceof Document) {
   1.352                  break;
   1.353 +            }
   1.354  
   1.355              p = (Element) p.getParentNode();
   1.356          }
   1.357 @@ -567,15 +421,17 @@
   1.358      public Element refineSchemaTarget(Element target) {
   1.359          // look for existing xs:annotation
   1.360          Element annotation = DOMUtils.getFirstChildElement(target, Constants.NS_XSD, "annotation");
   1.361 -        if (annotation == null)
   1.362 +        if (annotation == null) {
   1.363              // none exists. need to make one
   1.364              annotation = insertXMLSchemaElement(target, "annotation");
   1.365 +        }
   1.366  
   1.367          // then look for appinfo
   1.368          Element appinfo = DOMUtils.getFirstChildElement(annotation, Constants.NS_XSD, "appinfo");
   1.369 -        if (appinfo == null)
   1.370 +        if (appinfo == null) {
   1.371              // none exists. need to make one
   1.372              appinfo = insertXMLSchemaElement(annotation, "appinfo");
   1.373 +        }
   1.374  
   1.375          return appinfo;
   1.376      }
   1.377 @@ -583,9 +439,10 @@
   1.378      public Element refineWSDLTarget(Element target) {
   1.379          // look for existing xs:annotation
   1.380          Element JAXWSBindings = DOMUtils.getFirstChildElement(target, JAXWSBindingsConstants.NS_JAXWS_BINDINGS, "bindings");
   1.381 -        if (JAXWSBindings == null)
   1.382 +        if (JAXWSBindings == null) {
   1.383              // none exists. need to make one
   1.384              JAXWSBindings = insertJAXWSBindingsElement(target, "bindings");
   1.385 +        }
   1.386          return JAXWSBindings;
   1.387      }
   1.388  
   1.389 @@ -600,17 +457,21 @@
   1.390          // the namespace binding.
   1.391          String qname = parent.getTagName();
   1.392          int idx = qname.indexOf(':');
   1.393 -        if (idx == -1) qname = localName;
   1.394 -        else qname = qname.substring(0, idx + 1) + localName;
   1.395 +        if (idx == -1) {
   1.396 +            qname = localName;
   1.397 +        } else {
   1.398 +            qname = qname.substring(0, idx + 1) + localName;
   1.399 +        }
   1.400  
   1.401          Element child = parent.getOwnerDocument().createElementNS(Constants.NS_XSD, qname);
   1.402  
   1.403          NodeList children = parent.getChildNodes();
   1.404  
   1.405 -        if (children.getLength() == 0)
   1.406 +        if (children.getLength() == 0) {
   1.407              parent.appendChild(child);
   1.408 -        else
   1.409 +        } else {
   1.410              parent.insertBefore(child, children.item(0));
   1.411 +        }
   1.412  
   1.413          return child;
   1.414      }
   1.415 @@ -622,22 +483,24 @@
   1.416  
   1.417          NodeList children = parent.getChildNodes();
   1.418  
   1.419 -        if (children.getLength() == 0)
   1.420 +        if (children.getLength() == 0) {
   1.421              parent.appendChild(child);
   1.422 -        else
   1.423 +        } else {
   1.424              parent.insertBefore(child, children.item(0));
   1.425 +        }
   1.426  
   1.427          return child;
   1.428      }
   1.429  
   1.430 -    private static
   1.431      @NotNull
   1.432 -    String fixNull(@Nullable String s) {
   1.433 -        if (s == null) return "";
   1.434 -        else return s;
   1.435 +    static String fixNull(@Nullable String s) {
   1.436 +        if (s == null) {
   1.437 +            return "";
   1.438 +        } else {
   1.439 +            return s;
   1.440 +        }
   1.441      }
   1.442  
   1.443 -
   1.444      private void reportError(Element errorSource, String formattedMsg) {
   1.445          reportError(errorSource, formattedMsg, null);
   1.446      }

mercurial