ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.tools.internal.ws.wsdl.parser; ohair@286: ohair@286: import com.sun.istack.internal.NotNull; ohair@286: import com.sun.istack.internal.Nullable; ohair@286: import com.sun.istack.internal.SAXParseException2; ohair@286: import com.sun.tools.internal.ws.resources.WsdlMessages; ohair@286: import com.sun.tools.internal.ws.wscompile.ErrorReceiver; ohair@286: import com.sun.tools.internal.ws.wscompile.WsimportOptions; ohair@286: import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants; ohair@286: import com.sun.tools.internal.xjc.util.DOMUtils; ohair@286: import com.sun.xml.internal.bind.v2.util.EditDistance; ohair@286: import com.sun.xml.internal.ws.util.DOMUtil; ohair@286: import com.sun.xml.internal.ws.util.JAXWSUtils; alanb@368: import com.sun.xml.internal.ws.util.xml.XmlUtil; alanb@368: import org.w3c.dom.*; ohair@286: import org.xml.sax.SAXParseException; ohair@286: ohair@286: import javax.xml.namespace.NamespaceContext; ohair@286: import javax.xml.xpath.XPath; ohair@286: import javax.xml.xpath.XPathConstants; ohair@286: import javax.xml.xpath.XPathExpressionException; ohair@286: import javax.xml.xpath.XPathFactory; ohair@286: import java.net.MalformedURLException; ohair@286: import java.net.URL; ohair@286: import java.util.ArrayList; ohair@286: import java.util.HashSet; ohair@286: import java.util.Iterator; ohair@286: import java.util.Set; ohair@286: ohair@286: ohair@286: /** ohair@286: * Internalizes external binding declarations. ohair@286: * ohair@286: * @author Vivek Pandey ohair@286: */ ohair@286: public class Internalizer { alanb@368: alanb@368: private static final XPathFactory xpf = XmlUtil.newXPathFactory(true); ohair@286: private final XPath xpath = xpf.newXPath(); ohair@286: private final DOMForest forest; ohair@286: private final ErrorReceiver errorReceiver; ohair@286: ohair@286: ohair@286: public Internalizer(DOMForest forest, WsimportOptions options, ErrorReceiver errorReceiver) { ohair@286: this.forest = forest; ohair@286: this.errorReceiver = errorReceiver; ohair@286: } ohair@286: ohair@286: public void transform() { ohair@286: for (Element jaxwsBinding : forest.outerMostBindings) { ohair@286: internalize(jaxwsBinding, jaxwsBinding); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Validates attributes of a <JAXWS:bindings> element. ohair@286: */ ohair@286: private void validate(Element bindings) { ohair@286: NamedNodeMap atts = bindings.getAttributes(); ohair@286: for (int i = 0; i < atts.getLength(); i++) { ohair@286: Attr a = (Attr) atts.item(i); alanb@368: if (a.getNamespaceURI() != null) { ohair@286: continue; // all foreign namespace OK. alanb@368: } alanb@368: if (a.getLocalName().equals("node")) { ohair@286: continue; alanb@368: } alanb@368: if (a.getLocalName().equals("wsdlLocation")) { ohair@286: continue; alanb@368: } ohair@286: ohair@286: // TODO: flag error for this undefined attribute ohair@286: } ohair@286: } ohair@286: ohair@286: private void internalize(Element bindings, Node inheritedTarget) { ohair@286: // start by the inherited target ohair@286: Node target = inheritedTarget; ohair@286: ohair@286: validate(bindings); // validate this node ohair@286: ohair@286: // look for @wsdlLocation ohair@286: if (isTopLevelBinding(bindings)) { ohair@286: String wsdlLocation; ohair@286: if (bindings.getAttributeNode("wsdlLocation") != null) { ohair@286: wsdlLocation = bindings.getAttribute("wsdlLocation"); ohair@286: ohair@286: try { ohair@286: // absolutize this URI. ohair@286: // TODO: use the URI class ohair@286: // TODO: honor xml:base ohair@286: wsdlLocation = new URL(new URL(forest.getSystemId(bindings.getOwnerDocument())), ohair@286: wsdlLocation).toExternalForm(); ohair@286: } catch (MalformedURLException e) { ohair@286: wsdlLocation = JAXWSUtils.absolutize(JAXWSUtils.getFileOrURLName(wsdlLocation)); ohair@286: } ohair@286: } else { ohair@286: //the node does not have ohair@286: wsdlLocation = forest.getFirstRootDocument(); ohair@286: } ohair@286: target = forest.get(wsdlLocation); ohair@286: ohair@286: if (target == null) { ohair@286: reportError(bindings, WsdlMessages.INTERNALIZER_INCORRECT_SCHEMA_REFERENCE(wsdlLocation, EditDistance.findNearest(wsdlLocation, forest.listSystemIDs()))); ohair@286: return; // abort processing this ohair@286: } ohair@286: } ohair@286: ohair@286: //if the target node is xs:schema, declare the jaxb version on it as latter on it will be ohair@286: //required by the inlined schema bindings ohair@286: ohair@286: Element element = DOMUtil.getFirstElementChild(target); ohair@286: if (element != null && element.getNamespaceURI().equals(Constants.NS_WSDL) && element.getLocalName().equals("definitions")) { ohair@286: //get all schema elements ohair@286: Element type = DOMUtils.getFirstChildElement(element, Constants.NS_WSDL, "types"); ohair@286: if (type != null) { ohair@286: for (Element schemaElement : DOMUtils.getChildElements(type, Constants.NS_XSD, "schema")) { ohair@286: if (!schemaElement.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) { ohair@286: schemaElement.setAttributeNS(Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS); ohair@286: } ohair@286: ohair@286: //add jaxb:bindings version info. Lets put it to 1.0, may need to change latter ohair@286: if (!schemaElement.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) { ohair@286: schemaElement.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:version", JAXWSBindingsConstants.JAXB_BINDING_VERSION); ohair@286: } ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: NodeList targetNodes = null; ohair@286: boolean hasNode = true; ohair@286: boolean isToplevelBinding = isTopLevelBinding(bindings); ohair@286: if ((isJAXWSBindings(bindings) || isJAXBBindings(bindings)) && bindings.getAttributeNode("node") != null) { ohair@286: targetNodes = evaluateXPathMultiNode(bindings, target, bindings.getAttribute("node"), new NamespaceContextImpl(bindings)); ohair@286: } else ohair@286: if (isJAXWSBindings(bindings) && (bindings.getAttributeNode("node") == null) && !isToplevelBinding) { ohair@286: hasNode = false; ohair@286: } else ohair@286: if (isGlobalBinding(bindings) && !isWSDLDefinition(target) && isTopLevelBinding(bindings.getParentNode())) { ohair@286: targetNodes = getWSDLDefintionNode(bindings, target); ohair@286: } ohair@286: ohair@286: //if target is null or empty it means the xpath evaluation has some problem, ohair@286: // just return alanb@368: if (targetNodes == null && hasNode && !isToplevelBinding) { ohair@286: return; alanb@368: } ohair@286: ohair@286: if (hasNode) { ohair@286: if (targetNodes != null) { ohair@286: for (int i = 0; i < targetNodes.getLength(); i++) { ohair@286: insertBinding(bindings, targetNodes.item(i)); ohair@286: // look for child and process them recursively ohair@286: Element[] children = getChildElements(bindings); alanb@368: for (Element child : children) { alanb@368: if ("bindings".equals(child.getLocalName())) { ohair@286: internalize(child, targetNodes.item(i)); ohair@286: } alanb@368: } ohair@286: } ohair@286: } ohair@286: } ohair@286: if (targetNodes == null) { ohair@286: // look for child and process them recursively ohair@286: Element[] children = getChildElements(bindings); ohair@286: alanb@368: for (Element child : children) { ohair@286: internalize(child, target); alanb@368: } ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Moves JAXWS customizations under their respective target nodes. ohair@286: */ ohair@286: private void insertBinding(@NotNull Element bindings, @NotNull Node target) { ohair@286: if ("bindings".equals(bindings.getLocalName())) { ohair@286: Element[] children = DOMUtils.getChildElements(bindings); ohair@286: for (Element item : children) { ohair@286: if ("bindings".equals(item.getLocalName())) { ohair@286: //done ohair@286: } else { ohair@286: moveUnder(item, (Element) target); ohair@286: ohair@286: } ohair@286: } ohair@286: } else { ohair@286: moveUnder(bindings, (Element) target); ohair@286: } ohair@286: } ohair@286: ohair@286: private NodeList getWSDLDefintionNode(Node bindings, Node target) { ohair@286: return evaluateXPathMultiNode(bindings, target, "wsdl:definitions", ohair@286: new NamespaceContext() { alanb@368: @Override ohair@286: public String getNamespaceURI(String prefix) { ohair@286: return "http://schemas.xmlsoap.org/wsdl/"; ohair@286: } ohair@286: alanb@368: @Override ohair@286: public String getPrefix(String nsURI) { ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: alanb@368: @Override ohair@286: public Iterator getPrefixes(String namespaceURI) { ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: }); ohair@286: } ohair@286: ohair@286: private boolean isWSDLDefinition(Node target) { alanb@368: if (target == null) { ohair@286: return false; alanb@368: } ohair@286: String localName = target.getLocalName(); ohair@286: String nsURI = target.getNamespaceURI(); ohair@286: return fixNull(localName).equals("definitions") && fixNull(nsURI).equals("http://schemas.xmlsoap.org/wsdl/"); ohair@286: } ohair@286: ohair@286: private boolean isTopLevelBinding(Node node) { ohair@286: return node.getOwnerDocument().getDocumentElement() == node; ohair@286: } ohair@286: ohair@286: private boolean isJAXWSBindings(Node bindings) { ohair@286: return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) && bindings.getLocalName().equals("bindings")); ohair@286: } ohair@286: ohair@286: private boolean isJAXBBindings(Node bindings) { ohair@286: return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXB_BINDINGS) && bindings.getLocalName().equals("bindings")); ohair@286: } ohair@286: ohair@286: private boolean isGlobalBinding(Node bindings) { ohair@286: if (bindings.getNamespaceURI() == null) { ohair@286: errorReceiver.warning(forest.locatorTable.getStartLocation((Element) bindings), WsdlMessages.INVALID_CUSTOMIZATION_NAMESPACE(bindings.getLocalName())); ohair@286: return false; ohair@286: } ohair@286: return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) && ohair@286: (bindings.getLocalName().equals("package") || ohair@286: bindings.getLocalName().equals("enableAsyncMapping") || ohair@286: bindings.getLocalName().equals("enableAdditionalSOAPHeaderMapping") || ohair@286: bindings.getLocalName().equals("enableWrapperStyle") || ohair@286: bindings.getLocalName().equals("enableMIMEContent"))); ohair@286: } ohair@286: ohair@286: private static Element[] getChildElements(Element parent) { ohair@286: ArrayList a = new ArrayList(); ohair@286: NodeList children = parent.getChildNodes(); ohair@286: for (int i = 0; i < children.getLength(); i++) { ohair@286: Node item = children.item(i); alanb@368: if (!(item instanceof Element)) { alanb@368: continue; alanb@368: } ohair@286: if (JAXWSBindingsConstants.NS_JAXWS_BINDINGS.equals(item.getNamespaceURI()) || alanb@368: JAXWSBindingsConstants.NS_JAXB_BINDINGS.equals(item.getNamespaceURI())) { ohair@286: a.add((Element) item); alanb@368: } ohair@286: } ohair@286: return a.toArray(new Element[a.size()]); ohair@286: } ohair@286: ohair@286: private NodeList evaluateXPathMultiNode(Node bindings, Node target, String expression, NamespaceContext namespaceContext) { ohair@286: NodeList nlst; ohair@286: try { ohair@286: xpath.setNamespaceContext(namespaceContext); ohair@286: nlst = (NodeList) xpath.evaluate(expression, target, XPathConstants.NODESET); ohair@286: } catch (XPathExpressionException e) { ohair@286: reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATION_ERROR(e.getMessage()), e); ohair@286: return null; // abort processing this ohair@286: } ohair@286: ohair@286: if (nlst.getLength() == 0) { ohair@286: reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATES_TO_NO_TARGET(expression)); ohair@286: return null; // abort ohair@286: } ohair@286: ohair@286: return nlst; ohair@286: } ohair@286: ohair@286: private boolean isJAXBBindingElement(Element e) { ohair@286: return fixNull(e.getNamespaceURI()).equals(JAXWSBindingsConstants.NS_JAXB_BINDINGS); ohair@286: } ohair@286: ohair@286: private boolean isJAXWSBindingElement(Element e) { ohair@286: return fixNull(e.getNamespaceURI()).equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Moves the "decl" node under the "target" node. ohair@286: * ohair@286: * @param decl A JAXWS customization element (e.g., <JAXWS:class>) ohair@286: * @param target XML wsdl element under which the declaration should move. ohair@286: * For example, <xs:element> ohair@286: */ ohair@286: private void moveUnder(Element decl, Element target) { ohair@286: ohair@286: //if there is @node on decl and has a child element jaxb:bindings, move it under the target ohair@286: //Element jaxb = getJAXBBindingElement(decl); ohair@286: if (isJAXBBindingElement(decl)) { ohair@286: //add jaxb namespace declaration ohair@286: if (!target.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) { ohair@286: target.setAttributeNS(Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS); ohair@286: } ohair@286: ohair@286: //add jaxb:bindings version info. Lets put it to 1.0, may need to change latter ohair@286: if (!target.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) { ohair@286: target.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:version", JAXWSBindingsConstants.JAXB_BINDING_VERSION); ohair@286: } ohair@286: ohair@286: // HACK: allow XJC extension all the time. This allows people to specify ohair@286: // the in the external bindings. Otherwise users lack the ability ohair@286: // to specify jaxb:extensionBindingPrefixes, so it won't work. ohair@286: // ohair@286: // the current workaround is still problematic in the sense that ohair@286: // it can't support user-defined extensions. This needs more careful thought. ohair@286: ohair@286: //JAXB doesn't allow writing jaxb:extensionbindingPrefix anywhere other than root element so lets write only on ohair@286: if (target.getLocalName().equals("schema") && target.getNamespaceURI().equals(Constants.NS_XSD) && !target.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "extensionBindingPrefixes")) { ohair@286: target.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:extensionBindingPrefixes", "xjc"); ohair@286: target.setAttributeNS(Constants.NS_XMLNS, "xmlns:xjc", JAXWSBindingsConstants.NS_XJC_BINDINGS); ohair@286: } ohair@286: ohair@286: //insert xs:annotation/xs:appinfo where in jaxb:binding will be put ohair@286: target = refineSchemaTarget(target); ohair@286: copyInscopeNSAttributes(decl); ohair@286: } else if (isJAXWSBindingElement(decl)) { ohair@286: //add jaxb namespace declaration ohair@286: if (!target.hasAttributeNS(Constants.NS_XMLNS, "JAXWS")) { ohair@286: target.setAttributeNS(Constants.NS_XMLNS, "xmlns:JAXWS", JAXWSBindingsConstants.NS_JAXWS_BINDINGS); ohair@286: } ohair@286: ohair@286: //insert xs:annotation/xs:appinfo where in jaxb:binding will be put ohair@286: target = refineWSDLTarget(target); ohair@286: copyInscopeNSAttributes(decl); ohair@286: } else { ohair@286: return; ohair@286: } ohair@286: ohair@286: // finally move the declaration to the target node. ohair@286: if (target.getOwnerDocument() != decl.getOwnerDocument()) { ohair@286: // if they belong to different DOM documents, we need to clone them ohair@286: decl = (Element) target.getOwnerDocument().importNode(decl, true); ohair@286: ohair@286: } ohair@286: ohair@286: target.appendChild(decl); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Copy in-scope namespace declarations of the decl node ohair@286: * to the decl node itself so that this move won't change ohair@286: * the in-scope namespace bindings. ohair@286: */ ohair@286: private void copyInscopeNSAttributes(Element e) { ohair@286: Element p = e; ohair@286: Set inscopes = new HashSet(); ohair@286: while (true) { ohair@286: NamedNodeMap atts = p.getAttributes(); ohair@286: for (int i = 0; i < atts.getLength(); i++) { ohair@286: Attr a = (Attr) atts.item(i); ohair@286: if (Constants.NS_XMLNS.equals(a.getNamespaceURI())) { ohair@286: String prefix; alanb@368: if (a.getName().indexOf(':') == -1) { alanb@368: prefix = ""; alanb@368: } else { alanb@368: prefix = a.getLocalName(); alanb@368: } ohair@286: ohair@286: if (inscopes.add(prefix) && p != e) { ohair@286: // if this is the first time we see this namespace bindings, ohair@286: // copy the declaration. ohair@286: // if p==decl, there's no need to. Note that ohair@286: // we want to add prefix to inscopes even if p==Decl ohair@286: ohair@286: e.setAttributeNodeNS((Attr) a.cloneNode(true)); ohair@286: } ohair@286: } ohair@286: } ohair@286: alanb@368: if (p.getParentNode() instanceof Document) { ohair@286: break; alanb@368: } ohair@286: ohair@286: p = (Element) p.getParentNode(); ohair@286: } ohair@286: ohair@286: if (!inscopes.contains("")) { ohair@286: // if the default namespace was undeclared in the context of decl, ohair@286: // it must be explicitly set to "" since the new environment might ohair@286: // have a different default namespace URI. ohair@286: e.setAttributeNS(Constants.NS_XMLNS, "xmlns", ""); ohair@286: } ohair@286: } ohair@286: ohair@286: public Element refineSchemaTarget(Element target) { ohair@286: // look for existing xs:annotation ohair@286: Element annotation = DOMUtils.getFirstChildElement(target, Constants.NS_XSD, "annotation"); alanb@368: if (annotation == null) { ohair@286: // none exists. need to make one ohair@286: annotation = insertXMLSchemaElement(target, "annotation"); alanb@368: } ohair@286: ohair@286: // then look for appinfo ohair@286: Element appinfo = DOMUtils.getFirstChildElement(annotation, Constants.NS_XSD, "appinfo"); alanb@368: if (appinfo == null) { ohair@286: // none exists. need to make one ohair@286: appinfo = insertXMLSchemaElement(annotation, "appinfo"); alanb@368: } ohair@286: ohair@286: return appinfo; ohair@286: } ohair@286: ohair@286: public Element refineWSDLTarget(Element target) { ohair@286: // look for existing xs:annotation ohair@286: Element JAXWSBindings = DOMUtils.getFirstChildElement(target, JAXWSBindingsConstants.NS_JAXWS_BINDINGS, "bindings"); alanb@368: if (JAXWSBindings == null) { ohair@286: // none exists. need to make one ohair@286: JAXWSBindings = insertJAXWSBindingsElement(target, "bindings"); alanb@368: } ohair@286: return JAXWSBindings; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a new XML Schema element of the given local name ohair@286: * and insert it as the first child of the given parent node. ohair@286: * ohair@286: * @return Newly create element. ohair@286: */ ohair@286: private Element insertXMLSchemaElement(Element parent, String localName) { ohair@286: // use the same prefix as the parent node to avoid modifying ohair@286: // the namespace binding. ohair@286: String qname = parent.getTagName(); ohair@286: int idx = qname.indexOf(':'); alanb@368: if (idx == -1) { alanb@368: qname = localName; alanb@368: } else { alanb@368: qname = qname.substring(0, idx + 1) + localName; alanb@368: } ohair@286: ohair@286: Element child = parent.getOwnerDocument().createElementNS(Constants.NS_XSD, qname); ohair@286: ohair@286: NodeList children = parent.getChildNodes(); ohair@286: alanb@368: if (children.getLength() == 0) { ohair@286: parent.appendChild(child); alanb@368: } else { ohair@286: parent.insertBefore(child, children.item(0)); alanb@368: } ohair@286: ohair@286: return child; ohair@286: } ohair@286: ohair@286: private Element insertJAXWSBindingsElement(Element parent, String localName) { ohair@286: String qname = "JAXWS:" + localName; ohair@286: ohair@286: Element child = parent.getOwnerDocument().createElementNS(JAXWSBindingsConstants.NS_JAXWS_BINDINGS, qname); ohair@286: ohair@286: NodeList children = parent.getChildNodes(); ohair@286: alanb@368: if (children.getLength() == 0) { ohair@286: parent.appendChild(child); alanb@368: } else { ohair@286: parent.insertBefore(child, children.item(0)); alanb@368: } ohair@286: ohair@286: return child; ohair@286: } ohair@286: ohair@286: @NotNull alanb@368: static String fixNull(@Nullable String s) { alanb@368: if (s == null) { alanb@368: return ""; alanb@368: } else { alanb@368: return s; alanb@368: } ohair@286: } ohair@286: ohair@286: private void reportError(Element errorSource, String formattedMsg) { ohair@286: reportError(errorSource, formattedMsg, null); ohair@286: } ohair@286: ohair@286: private void reportError(Element errorSource, ohair@286: String formattedMsg, Exception nestedException) { ohair@286: ohair@286: SAXParseException e = new SAXParseException2(formattedMsg, ohair@286: forest.locatorTable.getStartLocation(errorSource), ohair@286: nestedException); ohair@286: errorReceiver.error(e); ohair@286: } ohair@286: ohair@286: ohair@286: }