package com.sun.tools.internal.ws.wsdl.parser;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import com.sun.istack.internal.SAXParseException2;
import com.sun.tools.internal.ws.resources.WsdlMessages;
import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
import com.sun.tools.internal.ws.wscompile.WsimportOptions;
import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants;
import com.sun.tools.internal.xjc.util.DOMUtils;
import com.sun.xml.internal.bind.v2.util.EditDistance;
import com.sun.xml.internal.ws.util.DOMUtil;
import com.sun.xml.internal.ws.util.JAXWSUtils;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import org.w3c.dom.*;
import org.xml.sax.SAXParseException;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Internalizer {
private final XPath xpath = xpf.get().newXPath();
private final DOMForest forest;
private final ErrorReceiver errorReceiver;
public Internalizer(DOMForest forest, WsimportOptions options, ErrorReceiver errorReceiver) {
this.forest = forest;
this.errorReceiver = errorReceiver;
}
public void transform() {
for (Element jaxwsBinding : forest.outerMostBindings) {
internalize(jaxwsBinding, jaxwsBinding);
}
}
private static final ContextClassloaderLocal<XPathFactory> xpf = new ContextClassloaderLocal<XPathFactory>() {
@Override
protected XPathFactory initialValue() throws Exception {
return XmlUtil.newXPathFactory(true);
}
};
private void validate(Element bindings) {
NamedNodeMap atts = bindings.getAttributes();
for (int i = 0; i < atts.getLength(); i++) {
Attr a = (Attr) atts.item(i);
if (a.getNamespaceURI() != null) {
continue;
}
if (a.getLocalName().equals("node")) {
continue;
}
if (a.getLocalName().equals("wsdlLocation")) {
continue;
}
}
}
private void internalize(Element bindings, Node inheritedTarget) {
Node target = inheritedTarget;
validate(bindings);
if (isTopLevelBinding(bindings)) {
String wsdlLocation;
if (bindings.getAttributeNode("wsdlLocation") != null) {
wsdlLocation = bindings.getAttribute("wsdlLocation");
try {
wsdlLocation = new URL(new URL(forest.getSystemId(bindings.getOwnerDocument())),
wsdlLocation).toExternalForm();
} catch (MalformedURLException e) {
wsdlLocation = JAXWSUtils.absolutize(JAXWSUtils.getFileOrURLName(wsdlLocation));
}
} else {
wsdlLocation = forest.getFirstRootDocument();
}
target = forest.get(wsdlLocation);
if (target == null) {
reportError(bindings, WsdlMessages.INTERNALIZER_INCORRECT_SCHEMA_REFERENCE(wsdlLocation, EditDistance.findNearest(wsdlLocation, forest.listSystemIDs())));
return;
}
}
Element element = DOMUtil.getFirstElementChild(target);
if (element != null && element.getNamespaceURI().equals(Constants.NS_WSDL) && element.getLocalName().equals("definitions")) {
Element type = DOMUtils.getFirstChildElement(element, Constants.NS_WSDL, "types");
if (type != null) {
for (Element schemaElement : DOMUtils.getChildElements(type, Constants.NS_XSD, "schema")) {
if (!schemaElement.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) {
schemaElement.setAttributeNS(Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS);
}
if (!schemaElement.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) {
schemaElement.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:version", JAXWSBindingsConstants.JAXB_BINDING_VERSION);
}
}
}
}
NodeList targetNodes = null;
boolean hasNode = true;
boolean isToplevelBinding = isTopLevelBinding(bindings);
if ((isJAXWSBindings(bindings) || isJAXBBindings(bindings)) && bindings.getAttributeNode("node") != null) {
targetNodes = evaluateXPathMultiNode(bindings, target, bindings.getAttribute("node"), new NamespaceContextImpl(bindings));
} else
if (isJAXWSBindings(bindings) && (bindings.getAttributeNode("node") == null) && !isToplevelBinding) {
hasNode = false;
} else
if (isGlobalBinding(bindings) && !isWSDLDefinition(target) && isTopLevelBinding(bindings.getParentNode())) {
targetNodes = getWSDLDefintionNode(bindings, target);
}
if (targetNodes == null && hasNode && !isToplevelBinding) {
return;
}
if (hasNode) {
if (targetNodes != null) {
for (int i = 0; i < targetNodes.getLength(); i++) {
insertBinding(bindings, targetNodes.item(i));
Element[] children = getChildElements(bindings);
for (Element child : children) {
if ("bindings".equals(child.getLocalName())) {
internalize(child, targetNodes.item(i));
}
}
}
}
}
if (targetNodes == null) {
Element[] children = getChildElements(bindings);
for (Element child : children) {
internalize(child, target);
}
}
}
private void insertBinding(@NotNull Element bindings, @NotNull Node target) {
if ("bindings".equals(bindings.getLocalName())) {
Element[] children = DOMUtils.getChildElements(bindings);
for (Element item : children) {
if ("bindings".equals(item.getLocalName())) {
} else {
moveUnder(item, (Element) target);
}
}
} else {
moveUnder(bindings, (Element) target);
}
}
private NodeList getWSDLDefintionNode(Node bindings, Node target) {
return evaluateXPathMultiNode(bindings, target, "wsdl:definitions",
new NamespaceContext() {
@Override
public String getNamespaceURI(String prefix) {
return "http://schemas.xmlsoap.org/wsdl/";
}
@Override
public String getPrefix(String nsURI) {
throw new UnsupportedOperationException();
}
@Override
public Iterator getPrefixes(String namespaceURI) {
throw new UnsupportedOperationException();
}
});
}
private boolean isWSDLDefinition(Node target) {
if (target == null) {
return false;
}
String localName = target.getLocalName();
String nsURI = target.getNamespaceURI();
return fixNull(localName).equals("definitions") && fixNull(nsURI).equals("http://schemas.xmlsoap.org/wsdl/");
}
private boolean isTopLevelBinding(Node node) {
return node.getOwnerDocument().getDocumentElement() == node;
}
private boolean isJAXWSBindings(Node bindings) {
return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) && bindings.getLocalName().equals("bindings"));
}
private boolean isJAXBBindings(Node bindings) {
return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXB_BINDINGS) && bindings.getLocalName().equals("bindings"));
}
private boolean isGlobalBinding(Node bindings) {
if (bindings.getNamespaceURI() == null) {
errorReceiver.warning(forest.locatorTable.getStartLocation((Element) bindings), WsdlMessages.INVALID_CUSTOMIZATION_NAMESPACE(bindings.getLocalName()));
return false;
}
return (bindings.getNamespaceURI().equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) &&
(bindings.getLocalName().equals("package") ||
bindings.getLocalName().equals("enableAsyncMapping") ||
bindings.getLocalName().equals("enableAdditionalSOAPHeaderMapping") ||
bindings.getLocalName().equals("enableWrapperStyle") ||
bindings.getLocalName().equals("enableMIMEContent")));
}
private static Element[] getChildElements(Element parent) {
ArrayList<Element> a = new ArrayList<Element>();
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node item = children.item(i);
if (!(item instanceof Element)) {
continue;
}
if (JAXWSBindingsConstants.NS_JAXWS_BINDINGS.equals(item.getNamespaceURI()) ||
JAXWSBindingsConstants.NS_JAXB_BINDINGS.equals(item.getNamespaceURI())) {
a.add((Element) item);
}
}
return a.toArray(new Element[a.size()]);
}
private NodeList evaluateXPathMultiNode(Node bindings, Node target, String expression, NamespaceContext namespaceContext) {
NodeList nlst;
try {
xpath.setNamespaceContext(namespaceContext);
nlst = (NodeList) xpath.evaluate(expression, target, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATION_ERROR(e.getMessage()), e);
return null;
}
if (nlst.getLength() == 0) {
reportError((Element) bindings, WsdlMessages.INTERNALIZER_X_PATH_EVALUATES_TO_NO_TARGET(expression));
return null;
}
return nlst;
}
private boolean isJAXBBindingElement(Element e) {
return fixNull(e.getNamespaceURI()).equals(JAXWSBindingsConstants.NS_JAXB_BINDINGS);
}
private boolean isJAXWSBindingElement(Element e) {
return fixNull(e.getNamespaceURI()).equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS);
}
private void moveUnder(Element decl, Element target) {
if (isJAXBBindingElement(decl)) {
if (!target.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) {
target.setAttributeNS(Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS);
}
if (!target.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) {
target.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:version", JAXWSBindingsConstants.JAXB_BINDING_VERSION);
}
if (target.getLocalName().equals("schema") && target.getNamespaceURI().equals(Constants.NS_XSD) && !target.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "extensionBindingPrefixes")) {
target.setAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "jaxb:extensionBindingPrefixes", "xjc");
target.setAttributeNS(Constants.NS_XMLNS, "xmlns:xjc", JAXWSBindingsConstants.NS_XJC_BINDINGS);
}
target = refineSchemaTarget(target);
copyInscopeNSAttributes(decl);
} else if (isJAXWSBindingElement(decl)) {
if (!target.hasAttributeNS(Constants.NS_XMLNS, "JAXWS")) {
target.setAttributeNS(Constants.NS_XMLNS, "xmlns:JAXWS", JAXWSBindingsConstants.NS_JAXWS_BINDINGS);
}
target = refineWSDLTarget(target);
copyInscopeNSAttributes(decl);
} else {
return;
}
if (target.getOwnerDocument() != decl.getOwnerDocument()) {
decl = (Element) target.getOwnerDocument().importNode(decl, true);
}
target.appendChild(decl);
}
private void copyInscopeNSAttributes(Element e) {
Element p = e;
Set<String> inscopes = new HashSet<String>();
while (true) {
NamedNodeMap atts = p.getAttributes();
for (int i = 0; i < atts.getLength(); i++) {
Attr a = (Attr) atts.item(i);
if (Constants.NS_XMLNS.equals(a.getNamespaceURI())) {
String prefix;
if (a.getName().indexOf(':') == -1) {
prefix = "";
} else {
prefix = a.getLocalName();
}
if (inscopes.add(prefix) && p != e) {
e.setAttributeNodeNS((Attr) a.cloneNode(true));
}
}
}
if (p.getParentNode() instanceof Document) {
break;
}
p = (Element) p.getParentNode();
}
if (!inscopes.contains("")) {
e.setAttributeNS(Constants.NS_XMLNS, "xmlns", "");
}
}
public Element refineSchemaTarget(Element target) {
Element annotation = DOMUtils.getFirstChildElement(target, Constants.NS_XSD, "annotation");
if (annotation == null) {
annotation = insertXMLSchemaElement(target, "annotation");
}
Element appinfo = DOMUtils.getFirstChildElement(annotation, Constants.NS_XSD, "appinfo");
if (appinfo == null) {
appinfo = insertXMLSchemaElement(annotation, "appinfo");
}
return appinfo;
}
public Element refineWSDLTarget(Element target) {
Element JAXWSBindings = DOMUtils.getFirstChildElement(target, JAXWSBindingsConstants.NS_JAXWS_BINDINGS, "bindings");
if (JAXWSBindings == null) {
JAXWSBindings = insertJAXWSBindingsElement(target, "bindings");
}
return JAXWSBindings;
}
private Element insertXMLSchemaElement(Element parent, String localName) {
String qname = parent.getTagName();
int idx = qname.indexOf(':');
if (idx == -1) {
qname = localName;
} else {
qname = qname.substring(0, idx + 1) + localName;
}
Element child = parent.getOwnerDocument().createElementNS(Constants.NS_XSD, qname);
NodeList children = parent.getChildNodes();
if (children.getLength() == 0) {
parent.appendChild(child);
} else {
parent.insertBefore(child, children.item(0));
}
return child;
}
private Element insertJAXWSBindingsElement(Element parent, String localName) {
String qname = "JAXWS:" + localName;
Element child = parent.getOwnerDocument().createElementNS(JAXWSBindingsConstants.NS_JAXWS_BINDINGS, qname);
NodeList children = parent.getChildNodes();
if (children.getLength() == 0) {
parent.appendChild(child);
} else {
parent.insertBefore(child, children.item(0));
}
return child;
}
@NotNull
static String fixNull(@Nullable String s) {
if (s == null) {
return "";
} else {
return s;
}
}
private void reportError(Element errorSource, String formattedMsg) {
reportError(errorSource, formattedMsg, null);
}
private void reportError(Element errorSource,
String formattedMsg, Exception nestedException) {
SAXParseException e = new SAXParseException2(formattedMsg,
forest.locatorTable.getStartLocation(errorSource),
nestedException);
errorReceiver.error(e);
}
}