KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > validator > WSDLSchemaValidator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.wsdl.validator;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
31 import javax.xml.validation.Schema JavaDoc;
32
33 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
34 import org.netbeans.modules.xml.wsdl.validator.spi.ValidatorSchemaFactory;
35 import org.netbeans.modules.xml.xam.Model;
36 import org.netbeans.modules.xml.xam.spi.XsdBasedValidator;
37 import org.w3c.dom.ls.LSInput JavaDoc;
38 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
39 import org.xml.sax.ErrorHandler JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.SAXParseException JavaDoc;
42
43 public class WSDLSchemaValidator extends XsdBasedValidator {
44     
45     /*
46      * Uses the WSDL Basic Profile 1.1 schema from
47      * http://www.ws-i.org/Profiles/BasicProfile-1.1.html#WSDLDOCSTRUCT
48      */

49     static final String JavaDoc wsdlXSDUrl = "/org/netbeans/modules/xml/wsdl/validator/resources/wsdl-2004-08-24.xsd";
50     
51     public String JavaDoc getName() {
52         return "WSDLSchemaValidator"; //NO I18N
53
}
54     
55     @Override JavaDoc
56     protected Schema JavaDoc getSchema(Model model) {
57         if (! (model instanceof WSDLModel)) {
58             return null;
59         }
60         
61         InputStream JavaDoc wsdlSchemaInputStream = WSDLSchemaValidator.class.getResourceAsStream(wsdlXSDUrl);
62         Source JavaDoc wsdlSource = new StreamSource JavaDoc(wsdlSchemaInputStream);
63         wsdlSource.setSystemId(WSDLSchemaValidator.class.getResource(wsdlXSDUrl).toString());
64         
65         //combine all possible schemas through ElementFactoryProvider mechanism
66
Collection JavaDoc<ValidatorSchemaFactory> extSchemaFactories = ValidatorSchemaFactoryRegistry.getDefault().getAllValidatorSchemaFactories();
67         
68         ArrayList JavaDoc<Source JavaDoc> isList = new ArrayList JavaDoc<Source JavaDoc>();
69         isList.add(wsdlSource);
70         for (ValidatorSchemaFactory factory : extSchemaFactories) {
71             Source JavaDoc is = factory.getSchemaSource();
72             if(is != null) {
73                 isList.add(is);
74             } else {
75                 //any validator should not return a null input stream
76
Logger.getLogger(getClass().getName()).severe("getSchema: " + factory.getClass() +" returned null input stream for its schema");
77             }
78         }
79
80         Schema JavaDoc schema = getCompiledSchema(isList.toArray(new Source JavaDoc[isList.size()]), new CentralLSResourceResolver(extSchemaFactories), new SchemaErrorHandler());
81         
82         return schema;
83     }
84
85     class SchemaErrorHandler implements ErrorHandler JavaDoc {
86         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
87             Logger.getLogger(getClass().getName()).log(Level.SEVERE, "SchemaErrorHandler: " + exception.getMessage(), exception);
88         }
89         
90         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
91             Logger.getLogger(getClass().getName()).log(Level.SEVERE, "SchemaErrorHandler: " + exception.getMessage(), exception);
92         }
93         
94         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
95             
96         }
97     }
98     
99     class CentralLSResourceResolver implements LSResourceResolver JavaDoc {
100         
101         private Collection JavaDoc<ValidatorSchemaFactory> mExtSchemaFactories;
102                 
103         CentralLSResourceResolver(Collection JavaDoc<ValidatorSchemaFactory> extSchemaFactories) {
104             mExtSchemaFactories = extSchemaFactories;
105         }
106         
107         public LSInput JavaDoc resolveResource(String JavaDoc type, String JavaDoc namespaceURI, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc baseURI) {
108             LSInput JavaDoc input = null;
109             
110             Iterator JavaDoc<ValidatorSchemaFactory> it = mExtSchemaFactories.iterator();
111             while(it.hasNext()) {
112                 ValidatorSchemaFactory fac = it.next();
113                 LSResourceResolver JavaDoc resolver = fac.getLSResourceResolver();
114                 if(resolver != null) {
115                     input = resolver.resolveResource(type, namespaceURI, publicId, systemId, baseURI);
116                     if(input != null) {
117                        break;
118                     }
119                 }
120             }
121             
122             return input;
123         }
124         
125     }
126 }
127
Popular Tags