|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.internal.ws.wsdl.parser; |
|
27 |
|
28 import com.sun.tools.internal.ws.wscompile.WsimportOptions; |
|
29 import com.sun.tools.internal.ws.wsdl.document.WSDLConstants; |
|
30 import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants; |
|
31 import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants; |
|
32 import com.sun.tools.internal.xjc.util.DOMUtils; |
|
33 import org.w3c.dom.Element; |
|
34 import org.w3c.dom.NodeList; |
|
35 import org.xml.sax.Attributes; |
|
36 import org.xml.sax.helpers.XMLFilterImpl; |
|
37 |
|
38 /** |
|
39 * @author Vivek Pandey |
|
40 */ |
|
41 public class WSDLInternalizationLogic implements InternalizationLogic{ |
|
42 |
|
43 /** |
|
44 * This filter looks for <xs:import> and <xs:include> |
|
45 * and parses those documents referenced by them. |
|
46 */ |
|
47 private static final class ReferenceFinder extends AbstractReferenceFinderImpl { |
|
48 ReferenceFinder( DOMForest parent) { |
|
49 super(parent); |
|
50 } |
|
51 |
|
52 protected String findExternalResource( String nsURI, String localName, Attributes atts) { |
|
53 if(WSDLConstants.NS_WSDL.equals(nsURI) && "import".equals(localName)){ |
|
54 if(parent.isExtensionMode()){ |
|
55 //TODO: add support for importing schema using wsdl:import |
|
56 } |
|
57 return atts.getValue("location"); |
|
58 } |
|
59 |
|
60 // We don't need to do this anymore, JAXB handles the schema imports, includes etc., but this is useful for the clientJar option in |
|
61 // fetching the imported schemas to package in the jar.. |
|
62 if (parent.options.clientjar != null) { |
|
63 if (SchemaConstants.NS_XSD.equals(nsURI) && "import".equals(localName)) { |
|
64 return atts.getValue("schemaLocation"); |
|
65 } |
|
66 } |
|
67 return null; |
|
68 } |
|
69 } |
|
70 public XMLFilterImpl createExternalReferenceFinder(DOMForest parent) { |
|
71 return new ReferenceFinder(parent); |
|
72 } |
|
73 |
|
74 public boolean checkIfValidTargetNode(DOMForest parent, Element bindings, Element target) { |
|
75 return false; |
|
76 } |
|
77 |
|
78 public Element refineSchemaTarget(Element target) { |
|
79 // look for existing xs:annotation |
|
80 Element annotation = DOMUtils.getFirstChildElement(target, Constants.NS_XSD, "annotation"); |
|
81 if(annotation==null) |
|
82 // none exists. need to make one |
|
83 annotation = insertXMLSchemaElement( target, "annotation" ); |
|
84 |
|
85 // then look for appinfo |
|
86 Element appinfo = DOMUtils.getFirstChildElement(annotation, Constants.NS_XSD, "appinfo" ); |
|
87 if(appinfo==null) |
|
88 // none exists. need to make one |
|
89 appinfo = insertXMLSchemaElement( annotation, "appinfo" ); |
|
90 |
|
91 return appinfo; |
|
92 |
|
93 } |
|
94 |
|
95 public Element refineWSDLTarget(Element target){ |
|
96 // look for existing xs:annotation |
|
97 Element JAXWSBindings = DOMUtils.getFirstChildElement(target, JAXWSBindingsConstants.NS_JAXWS_BINDINGS, "bindings"); |
|
98 if(JAXWSBindings==null) |
|
99 // none exists. need to make one |
|
100 JAXWSBindings = insertJAXWSBindingsElement(target, "bindings" ); |
|
101 return JAXWSBindings; |
|
102 } |
|
103 |
|
104 private Element insertJAXWSBindingsElement( Element parent, String localName ) { |
|
105 String qname = "JAXWS:"+localName; |
|
106 |
|
107 Element child = parent.getOwnerDocument().createElementNS(JAXWSBindingsConstants.NS_JAXWS_BINDINGS, qname ); |
|
108 |
|
109 NodeList children = parent.getChildNodes(); |
|
110 |
|
111 if( children.getLength()==0 ) |
|
112 parent.appendChild(child); |
|
113 else |
|
114 parent.insertBefore( child, children.item(0) ); |
|
115 |
|
116 return child; |
|
117 } |
|
118 |
|
119 |
|
120 /** |
|
121 * Creates a new XML Schema element of the given local name |
|
122 * and insert it as the first child of the given parent node. |
|
123 * |
|
124 * @return |
|
125 * Newly create element. |
|
126 */ |
|
127 private Element insertXMLSchemaElement( Element parent, String localName ) { |
|
128 // use the same prefix as the parent node to avoid modifying |
|
129 // the namespace binding. |
|
130 String qname = parent.getTagName(); |
|
131 int idx = qname.indexOf(':'); |
|
132 if(idx==-1) qname = localName; |
|
133 else qname = qname.substring(0,idx+1)+localName; |
|
134 |
|
135 Element child = parent.getOwnerDocument().createElementNS( Constants.NS_XSD, qname ); |
|
136 |
|
137 NodeList children = parent.getChildNodes(); |
|
138 |
|
139 if( children.getLength()==0 ) |
|
140 parent.appendChild(child); |
|
141 else |
|
142 parent.insertBefore( child, children.item(0) ); |
|
143 |
|
144 return child; |
|
145 } |
|
146 |
|
147 } |