1 19 package org.netbeans.modules.xml.wsdl.validator; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.stream.StreamSource ; 31 import javax.xml.validation.Schema ; 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 ; 38 import org.w3c.dom.ls.LSResourceResolver ; 39 import org.xml.sax.ErrorHandler ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.SAXParseException ; 42 43 public class WSDLSchemaValidator extends XsdBasedValidator { 44 45 49 static final String wsdlXSDUrl = "/org/netbeans/modules/xml/wsdl/validator/resources/wsdl-2004-08-24.xsd"; 50 51 public String getName() { 52 return "WSDLSchemaValidator"; } 54 55 @Override 56 protected Schema getSchema(Model model) { 57 if (! (model instanceof WSDLModel)) { 58 return null; 59 } 60 61 InputStream wsdlSchemaInputStream = WSDLSchemaValidator.class.getResourceAsStream(wsdlXSDUrl); 62 Source wsdlSource = new StreamSource (wsdlSchemaInputStream); 63 wsdlSource.setSystemId(WSDLSchemaValidator.class.getResource(wsdlXSDUrl).toString()); 64 65 Collection <ValidatorSchemaFactory> extSchemaFactories = ValidatorSchemaFactoryRegistry.getDefault().getAllValidatorSchemaFactories(); 67 68 ArrayList <Source > isList = new ArrayList <Source >(); 69 isList.add(wsdlSource); 70 for (ValidatorSchemaFactory factory : extSchemaFactories) { 71 Source is = factory.getSchemaSource(); 72 if(is != null) { 73 isList.add(is); 74 } else { 75 Logger.getLogger(getClass().getName()).severe("getSchema: " + factory.getClass() +" returned null input stream for its schema"); 77 } 78 } 79 80 Schema schema = getCompiledSchema(isList.toArray(new Source [isList.size()]), new CentralLSResourceResolver(extSchemaFactories), new SchemaErrorHandler()); 81 82 return schema; 83 } 84 85 class SchemaErrorHandler implements ErrorHandler { 86 public void error(SAXParseException exception) throws SAXException { 87 Logger.getLogger(getClass().getName()).log(Level.SEVERE, "SchemaErrorHandler: " + exception.getMessage(), exception); 88 } 89 90 public void fatalError(SAXParseException exception) throws SAXException { 91 Logger.getLogger(getClass().getName()).log(Level.SEVERE, "SchemaErrorHandler: " + exception.getMessage(), exception); 92 } 93 94 public void warning(SAXParseException exception) throws SAXException { 95 96 } 97 } 98 99 class CentralLSResourceResolver implements LSResourceResolver { 100 101 private Collection <ValidatorSchemaFactory> mExtSchemaFactories; 102 103 CentralLSResourceResolver(Collection <ValidatorSchemaFactory> extSchemaFactories) { 104 mExtSchemaFactories = extSchemaFactories; 105 } 106 107 public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { 108 LSInput input = null; 109 110 Iterator <ValidatorSchemaFactory> it = mExtSchemaFactories.iterator(); 111 while(it.hasNext()) { 112 ValidatorSchemaFactory fac = it.next(); 113 LSResourceResolver 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 |