1 19 package org.netbeans.modules.xml.schema.model.validation; 20 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import javax.xml.XMLConstants ; 26 import javax.xml.transform.Source ; 27 import javax.xml.validation.Schema ; 28 import javax.xml.validation.SchemaFactory ; 29 import org.netbeans.modules.xml.schema.model.SchemaModel; 30 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 31 import org.netbeans.modules.xml.xam.Model; 32 import org.netbeans.modules.xml.xam.ModelSource; 33 import org.netbeans.modules.xml.xam.dom.DocumentModel; 34 import org.netbeans.modules.xml.xam.locator.CatalogModel; 35 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 36 import org.netbeans.modules.xml.xam.locator.CatalogModelFactory; 37 import org.netbeans.modules.xml.xam.spi.Validator; 38 import org.netbeans.modules.xml.xam.spi.XsdBasedValidator; 39 import org.openide.util.NbBundle; 40 import org.w3c.dom.ls.LSInput ; 41 import org.w3c.dom.ls.LSResourceResolver ; 42 import org.xml.sax.SAXException ; 43 44 48 public class SchemaXsdBasedValidator extends XsdBasedValidator { 49 50 private static Schema schema; 51 52 protected Schema getSchema(Model model) { 53 if (! (model instanceof SchemaModel)) { 54 return null; 55 } 56 57 if(schema == null) { 60 try { 61 schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(); 62 } catch(SAXException ex) { 63 assert false: "Error while creating compiled schema for"; } 65 } 66 return schema; 67 } 68 69 public String getName() { 70 return NbBundle.getMessage(SchemaXsdBasedValidator.class, "LBL_Schema_Validator"); 71 } 72 73 @Override 74 protected void validate(Model model, Schema schema, XsdBasedValidator.Handler handler) { 75 try { 76 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 77 CatalogModel cm = (CatalogModel) model.getModelSource().getLookup() 78 .lookup(CatalogModel.class); 79 if (cm != null) { 80 sf.setResourceResolver(cm); 81 } 82 sf.setErrorHandler(handler); 83 Source saxSource = getSource(model, handler); 84 if (saxSource == null) { 85 return; 86 } 87 sf.newSchema(saxSource); 88 } catch(SAXException sax) { 89 } catch(Exception ex) { 91 handler.logValidationErrors(Validator.ResultType.ERROR, ex.getMessage()); 92 } 93 } 94 95 public DocumentModel resolveResource(String systemId, Model currentModel) { 96 97 try { 98 CatalogModel cm = (CatalogModel) currentModel.getModelSource().getLookup() 99 .lookup(CatalogModel.class); 100 ModelSource ms = cm.getModelSource(new URI (systemId)); 101 if (ms != null) { 102 return SchemaModelFactory.getDefault().getModel(ms); 103 } 104 } catch(URISyntaxException ex) { 105 Logger.getLogger(getClass().getName()).log(Level.FINE, "resolveResource", ex); } catch(CatalogModelException ex) { 107 Logger.getLogger(getClass().getName()).log(Level.FINE, "resolveResource", ex); } 109 return null; 110 } 111 112 } 113 | Popular Tags |