001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.validator.internal;
019:
020: import java.io.File;
021: import java.io.FileFilter;
022: import java.io.IOException;
023: import java.net.JarURLConnection;
024: import java.net.URI;
025: import java.net.URISyntaxException;
026: import java.net.URL;
027: import java.util.ArrayList;
028: import java.util.Collections;
029: import java.util.Comparator;
030: import java.util.Enumeration;
031: import java.util.List;
032: import java.util.jar.JarEntry;
033: import java.util.jar.JarFile;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036: import javax.wsdl.Definition;
037:
038: import org.w3c.dom.Document;
039: import org.xml.sax.InputSource;
040:
041: import org.apache.cxf.common.i18n.Message;
042: import org.apache.cxf.common.logging.LogUtils;
043: import org.apache.cxf.common.util.StringUtils;
044: import org.apache.cxf.helpers.XMLUtils;
045: import org.apache.cxf.resource.URIResolver;
046: import org.apache.cxf.tools.common.ToolConstants;
047: import org.apache.cxf.tools.common.ToolContext;
048: import org.apache.cxf.tools.common.ToolException;
049: import org.apache.cxf.tools.util.URIParserUtil;
050: import org.apache.cxf.tools.validator.AbstractValidator;
051:
052: public class WSDL11Validator extends AbstractDefinitionValidator {
053: protected static final Logger LOG = LogUtils
054: .getL7dLogger(SchemaValidator.class);
055: private final List<AbstractValidator> validators = new ArrayList<AbstractValidator>();
056:
057: public WSDL11Validator(Definition definition) {
058: super (definition);
059: }
060:
061: public WSDL11Validator(Definition definition, ToolContext pe) {
062: super (definition, pe);
063: }
064:
065: private Document getWSDLDoc(String wsdl) {
066: LOG.log(Level.INFO, new Message("VALIDATE_WSDL", LOG, wsdl)
067: .toString());
068: try {
069: return XMLUtils.parse(new InputSource(URIParserUtil
070: .getAbsoluteURI(wsdl)));
071: } catch (Exception e) {
072: throw new ToolException(e);
073: }
074: }
075:
076: public boolean isValid() throws ToolException {
077: //boolean isValid = true;
078: String schemaDir = getSchemaDir();
079: SchemaValidator schemaValidator = null;
080: String[] schemas = (String[]) env
081: .get(ToolConstants.CFG_SCHEMA_URL);
082: // Tool will use the following sequence to find the schema files
083: // 1.ToolConstants.CFG_SCHEMA_DIR from ToolContext
084: // 2.ToolConstants.CXF_SCHEMA_DIR from System property
085: // 3.If 1 and 2 is null , then load these schema files from jar file
086: String wsdl = (String) env.get(ToolConstants.CFG_WSDLURL);
087:
088: WSDLRefValidator wsdlRefValidator = new WSDLRefValidator(wsdl,
089: getWSDLDoc(wsdl));
090: wsdlRefValidator.setSuppressWarnings(env
091: .optionSet(ToolConstants.CFG_SUPPRESS_WARNINGS));
092:
093: this .def = wsdlRefValidator.getDefinition();
094:
095: validators.add(wsdlRefValidator);
096: validators.add(new UniqueBodyPartsValidator(this .def));
097: validators.add(new WSIBPValidator(this .def));
098: validators.add(new MIMEBindingValidator(this .def));
099:
100: for (AbstractValidator validator : validators) {
101: if (!validator.isValid()) {
102: addErrorMessage(validator.getErrorMessage());
103: throw new ToolException(this .getErrorMessage());
104: }
105: }
106:
107: if (!StringUtils.isEmpty(schemaDir)) {
108: schemaValidator = new SchemaValidator(schemaDir, wsdl,
109: schemas);
110: } else {
111: try {
112: schemaValidator = new SchemaValidator(
113: getDefaultSchemas(), wsdl, schemas);
114: } catch (IOException e) {
115: throw new ToolException(
116: "Schemas can not be loaded before validating wsdl",
117: e);
118: }
119:
120: }
121: if (!schemaValidator.isValid()) {
122: this .addErrorMessage(schemaValidator.getErrorMessage());
123: throw new ToolException(this .getErrorMessage());
124:
125: }
126:
127: return true;
128: }
129:
130: public String getSchemaDir() {
131: String dir = "";
132: if (env.get(ToolConstants.CFG_SCHEMA_DIR) == null) {
133: dir = System.getProperty(ToolConstants.CXF_SCHEMA_DIR);
134: } else {
135: dir = (String) env.get(ToolConstants.CFG_SCHEMA_DIR);
136: }
137: return dir;
138: }
139:
140: protected List<InputSource> getDefaultSchemas() throws IOException {
141: List<InputSource> xsdList = new ArrayList<InputSource>();
142: ClassLoader clzLoader = Thread.currentThread()
143: .getContextClassLoader();
144: Enumeration<URL> urls = clzLoader
145: .getResources(ToolConstants.CXF_SCHEMAS_DIR_INJAR);
146: while (urls.hasMoreElements()) {
147: URL url = urls.nextElement();
148: //from jar files
149: if (url.toString().startsWith("jar")) {
150:
151: JarURLConnection jarConnection = (JarURLConnection) url
152: .openConnection();
153:
154: JarFile jarFile = jarConnection.getJarFile();
155:
156: Enumeration<JarEntry> entry = jarFile.entries();
157:
158: while (entry.hasMoreElements()) {
159: JarEntry ele = (JarEntry) entry.nextElement();
160: if (ele.getName().endsWith(".xsd")
161: && ele
162: .getName()
163: .indexOf(
164: ToolConstants.CXF_SCHEMAS_DIR_INJAR) > -1) {
165:
166: URIResolver resolver = new URIResolver(ele
167: .getName());
168: if (resolver.isResolved()) {
169: InputSource is = new InputSource(resolver
170: .getInputStream());
171: // Use the resolved URI of the schema if available.
172: // The ibm jdk won't resolve the schema if we set
173: // the id to the relative path.
174: if (resolver.getURI() != null) {
175: is.setSystemId(resolver.getURI()
176: .toString());
177: } else {
178: is.setSystemId(ele.getName());
179: }
180: xsdList.add(is);
181: }
182: }
183: }
184: //from class path direcotry
185: } else if (url.toString().startsWith("file")) {
186: URI loc = null;
187: try {
188: loc = url.toURI();
189: } catch (URISyntaxException e) {
190: //
191: }
192: java.io.File file = new java.io.File(loc);
193: if (file.exists()) {
194: File[] files = file.listFiles(new FileFilter() {
195: public boolean accept(File pathname) {
196: if (pathname.getAbsolutePath().endsWith(
197: ".xsd")) {
198: return true;
199: }
200: return false;
201: }
202: });
203: for (int i = 0; i < files.length; i++) {
204: InputSource is = new InputSource(files[i]
205: .toURI().toURL().openStream());
206: is.setSystemId(files[i].toURI().toURL()
207: .toString());
208: xsdList.add(is);
209: }
210: }
211:
212: }
213: }
214:
215: sort(xsdList);
216: return xsdList;
217: }
218:
219: private void sort(List<InputSource> list) {
220: Collections.sort(list, new Comparator<InputSource>() {
221: public int compare(InputSource i1, InputSource i2) {
222: if (i1 == null && i2 == null) {
223: return -1;
224: }
225: return i1.getSystemId().compareTo(i2.getSystemId());
226: }
227: });
228: }
229: }
|