001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wsdl.parser;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.tools.ws.resources.WscompileMessages;
042: import com.sun.tools.ws.resources.WsdlMessages;
043: import com.sun.tools.ws.wscompile.ErrorReceiver;
044: import com.sun.tools.ws.wscompile.WsimportOptions;
045: import com.sun.tools.ws.wsdl.document.WSDLConstants;
046: import com.sun.tools.ws.wsdl.document.schema.SchemaConstants;
047: import com.sun.tools.ws.wsdl.framework.ParseException;
048: import com.sun.xml.ws.api.wsdl.parser.MetaDataResolver;
049: import com.sun.xml.ws.api.wsdl.parser.MetadataResolverFactory;
050: import com.sun.xml.ws.api.wsdl.parser.ServiceDescriptor;
051: import com.sun.xml.ws.util.DOMUtil;
052: import com.sun.xml.ws.util.ServiceFinder;
053: import org.w3c.dom.Document;
054: import org.w3c.dom.Element;
055: import org.w3c.dom.Node;
056: import org.w3c.dom.NodeList;
057: import org.xml.sax.InputSource;
058: import org.xml.sax.SAXException;
059: import org.xml.sax.SAXParseException;
060:
061: import javax.xml.transform.Source;
062: import javax.xml.transform.dom.DOMSource;
063: import java.io.FileNotFoundException;
064: import java.io.IOException;
065: import java.net.URI;
066: import java.net.URISyntaxException;
067: import java.util.HashSet;
068: import java.util.List;
069: import java.util.Set;
070:
071: /**
072: * @author Vivek Pandey
073: */
074: public final class MetadataFinder extends DOMForest {
075:
076: public boolean isMexMetadata;
077: private String rootWSDL;
078: private Set<String> rootWsdls = new HashSet<String>();
079:
080: public MetadataFinder(InternalizationLogic logic,
081: WsimportOptions options, ErrorReceiver errReceiver) {
082: super (logic, options, errReceiver);
083:
084: }
085:
086: public void parseWSDL() {
087: // parse source grammars
088: for (InputSource value : options.getWSDLs()) {
089: String systemID = value.getSystemId();
090: errorReceiver.pollAbort();
091:
092: Document dom;
093: Element doc = null;
094:
095: try {
096: //if there is entity resolver use it
097: if (options.entityResolver != null)
098: value = options.entityResolver.resolveEntity(null,
099: systemID);
100: if (value == null)
101: value = new InputSource(systemID);
102: dom = parse(value, true);
103:
104: doc = dom.getDocumentElement();
105: if (doc == null) {
106: continue;
107: }
108: //if its not a WSDL document, retry with MEX
109: if (doc.getNamespaceURI() == null
110: || !doc.getNamespaceURI().equals(
111: WSDLConstants.NS_WSDL)
112: || !doc.getLocalName().equals("definitions")) {
113: throw new SAXParseException(
114: WsdlMessages
115: .INVALID_WSDL(
116: systemID,
117: com.sun.xml.ws.wsdl.parser.WSDLConstants.QNAME_DEFINITIONS,
118: doc.getNodeName(),
119: locatorTable
120: .getStartLocation(
121: doc)
122: .getLineNumber()),
123: locatorTable.getStartLocation(doc));
124: }
125: } catch (FileNotFoundException e) {
126: errorReceiver.error(WsdlMessages
127: .FILE_NOT_FOUND(systemID), e);
128: return;
129: } catch (IOException e) {
130: doc = getFromMetadataResolver(systemID, e);
131: } catch (SAXParseException e) {
132: doc = getFromMetadataResolver(systemID, e);
133: } catch (SAXException e) {
134: doc = getFromMetadataResolver(systemID, e);
135: }
136:
137: if (doc == null) {
138: continue;
139: }
140:
141: NodeList schemas = doc.getElementsByTagNameNS(
142: SchemaConstants.NS_XSD, "schema");
143: for (int i = 0; i < schemas.getLength(); i++) {
144: if (!inlinedSchemaElements.contains(schemas.item(i)))
145: inlinedSchemaElements
146: .add((Element) schemas.item(i));
147: }
148: }
149: identifyRootWslds();
150: }
151:
152: /**
153: * Gives the root wsdl document systemId. A root wsdl document is the one which has wsdl:service.
154: * @return null if there is no root wsdl
155: */
156: public @Nullable
157: String getRootWSDL() {
158: return rootWSDL;
159: }
160:
161: /**
162: * Gives all the WSDL documents.
163: */
164: public @NotNull
165: Set<String> getRootWSDLs() {
166: return rootWsdls;
167: }
168:
169: /**
170: * Identifies WSDL documents from the {@link DOMForest}. Also identifies the root wsdl document.
171: */
172: private void identifyRootWslds() {
173: for (String location : rootDocuments) {
174: Document doc = get(location);
175: if (doc != null) {
176: Element definition = doc.getDocumentElement();
177: if (definition == null
178: || definition.getLocalName() == null
179: || definition.getNamespaceURI() == null)
180: continue;
181: if (definition.getNamespaceURI().equals(
182: WSDLConstants.NS_WSDL)
183: && definition.getLocalName().equals(
184: "definitions")) {
185: rootWsdls.add(location);
186: //set the root wsdl at this point. Root wsdl is one which has wsdl:service in it
187: NodeList nl = definition.getElementsByTagNameNS(
188: WSDLConstants.NS_WSDL, "service");
189:
190: //TODO:what if there are more than one wsdl with wsdl:service element. Probably such cases
191: //are rare and we will take any one of them, this logic should still work
192: if (nl.getLength() > 0)
193: rootWSDL = location;
194: }
195: }
196: }
197: //no wsdl with wsdl:service found, throw error
198: if (rootWSDL == null) {
199: StringBuffer strbuf = new StringBuffer();
200: for (String str : rootWsdls) {
201: strbuf.append(str);
202: strbuf.append('\n');
203: }
204: errorReceiver.error(null, WsdlMessages
205: .FAILED_NOSERVICE(strbuf.toString()));
206: }
207: }
208:
209: /*
210: * If source and target namespace are also passed in,
211: * then if the mex resolver is found and it cannot get
212: * the data, wsimport attempts to add ?wsdl to the
213: * address and retrieve the data with a normal http get.
214: * This behavior should only happen when trying a
215: * mex request first.
216: */
217: private @Nullable
218: Element getFromMetadataResolver(String systemId, Exception ex) {
219: //try MEX
220: MetaDataResolver resolver;
221: ServiceDescriptor serviceDescriptor = null;
222: for (MetadataResolverFactory resolverFactory : ServiceFinder
223: .find(MetadataResolverFactory.class)) {
224: resolver = resolverFactory
225: .metadataResolver(options.entityResolver);
226: try {
227: serviceDescriptor = resolver.resolve(new URI(systemId));
228: //we got the ServiceDescriptor, now break
229: if (serviceDescriptor != null)
230: break;
231: } catch (URISyntaxException e) {
232: throw new ParseException(e);
233: }
234: }
235:
236: if (serviceDescriptor != null) {
237: errorReceiver.warning(new SAXParseException(WsdlMessages
238: .TRY_WITH_MEX(ex.getMessage()), null, ex));
239: return parseMetadata(systemId, serviceDescriptor);
240: } else {
241: errorReceiver.error(null, WsdlMessages
242: .PARSING_UNABLE_TO_GET_METADATA(ex.getMessage(),
243: WscompileMessages
244: .WSIMPORT_NO_WSDL(systemId)), ex);
245: }
246: return null;
247: }
248:
249: private Element parseMetadata(@NotNull
250: String systemId, @NotNull
251: ServiceDescriptor serviceDescriptor) {
252: List<? extends Source> mexWsdls = serviceDescriptor.getWSDLs();
253: List<? extends Source> mexSchemas = serviceDescriptor
254: .getSchemas();
255: Document root = null;
256: for (Source src : mexWsdls) {
257: if (src instanceof DOMSource) {
258: Node n = ((DOMSource) src).getNode();
259: Document doc;
260: if (n.getNodeType() == Node.ELEMENT_NODE
261: && n.getOwnerDocument() == null) {
262: doc = DOMUtil.createDom();
263: doc.importNode(n, true);
264: } else {
265: doc = n.getOwnerDocument();
266: }
267:
268: // Element e = (n.getNodeType() == Node.ELEMENT_NODE)?(Element)n: DOMUtil.getFirstElementChild(n);
269: if (root == null) {
270: //check if its main wsdl, then set it to root
271: NodeList nl = doc.getDocumentElement()
272: .getElementsByTagNameNS(
273: WSDLConstants.NS_WSDL, "service");
274: if (nl.getLength() > 0) {
275: root = doc;
276: rootWSDL = src.getSystemId();
277: }
278: }
279: NodeList nl = doc.getDocumentElement()
280: .getElementsByTagNameNS(WSDLConstants.NS_WSDL,
281: "import");
282: for (int i = 0; i < nl.getLength(); i++) {
283: Element imp = (Element) nl.item(i);
284: String loc = imp.getAttribute("location");
285: if (loc != null) {
286: if (!externalReferences.contains(loc))
287: externalReferences.add(loc);
288: }
289: }
290: if (core.keySet().contains(systemId))
291: core.remove(systemId);
292: core.put(src.getSystemId(), doc);
293: isMexMetadata = true;
294: }
295:
296: //TODO:handle SAXSource
297: //TODO:handler StreamSource
298: }
299:
300: for (Source src : mexSchemas) {
301: if (src instanceof DOMSource) {
302: Node n = ((DOMSource) src).getNode();
303: Element e = (n.getNodeType() == Node.ELEMENT_NODE) ? (Element) n
304: : DOMUtil.getFirstElementChild(n);
305: inlinedSchemaElements.add(e);
306: }
307: //TODO:handle SAXSource
308: //TODO:handler StreamSource
309: }
310: return root.getDocumentElement();
311: }
312: }
|