1 19 20 package org.netbeans.modules.xml.schema.cookies; 21 22 import java.io.*; 23 import java.net.*; 24 25 import org.xml.sax.*; 26 27 import org.netbeans.spi.xml.cookies.*; 28 import org.netbeans.api.xml.parsers.SAXEntityParser; 29 import org.netbeans.api.xml.services.UserCatalog; 30 import org.openide.xml.XMLUtil; 31 import org.xml.sax.helpers.DefaultHandler ; 32 33 38 public final class ValidateSchemaSupport extends ValidateXMLSupport { 39 40 41 public ValidateSchemaSupport(InputSource inputSource) { 42 super( inputSource); 43 } 44 45 48 protected XMLReader createParser(boolean validate) { 49 final String XERCES_FEATURE_PREFIX = "http://apache.org/xml/features/"; 51 XMLReader parser = super.createParser(validate); 52 if (parser == null) return null; 53 54 if (validate) { 56 try { 58 if (parser.getFeature(XERCES_FEATURE_PREFIX + "validation/schema") == false) { return null; 61 } else { 62 parser.setFeature(XERCES_FEATURE_PREFIX + "validation/schema-full-checking", true); return new SchemaChecker(parser); 65 } 66 } catch (SAXException ex) { 67 return null; 68 } 69 70 } else { 71 return parser; 72 } 73 } 74 75 78 private static class SchemaChecker extends SAXEntityParser { 79 80 private static String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"; 81 82 private String ns = null; 84 85 public SchemaChecker(XMLReader parser) { 86 super( parser); 87 } 88 89 96 protected InputSource wrapInputSource(InputSource inputSource) { 97 String targetNamespace = getTargetNamespace(); 98 String url = inputSource.getSystemId(); 99 StringBuffer buffer = new StringBuffer (256); 100 String namespace = "http://www.w3.org/2001/XMLSchema-instance"; buffer.append("<schemaWrapper xmlns:xsi='" + namespace + "' "); 103 104 if (targetNamespace != null) { 105 buffer.append("xmlns='").append(targetNamespace).append("' "); buffer.append("xsi:schemaLocation='").append(targetNamespace).append(' ').append(url).append("'/>"); } else { 108 buffer.append("xsi:noNamespaceSchemaLocation='").append(url).append("'/>"); } 110 StringReader reader = new StringReader(buffer.toString()); 111 InputSource input = new InputSource(); 112 input.setCharacterStream(reader); 113 return input; 114 } 115 116 119 protected boolean propagateException(SAXParseException ex) { 120 if (super.propagateException(ex)) { 121 String message = ex.getMessage(); 123 if (message == null) return true; if (getTargetNamespace() == null || message.indexOf(getTargetNamespace()) < 0) { 125 return message.indexOf("schemaWrapper") < 0; } 127 } 128 return false; 129 } 130 131 public void parse(InputSource input) throws SAXException, IOException { 132 ShareableInputSource shared = ShareableInputSource.create(input); 133 try { 134 ns = parseForTargetNamespace(shared); 135 shared.reset(); 136 super.parse(shared); 137 } finally { 138 shared.closeAll(); 139 } 140 } 141 142 145 private String getTargetNamespace() { 146 return ns; 147 } 148 149 private String parseForTargetNamespace(InputSource schema) throws SAXException, IOException { 150 try { 151 XMLReader reader = XMLUtil.createXMLReader(false, true); 152 EntityResolver resolver = UserCatalog.getDefault().getEntityResolver(); 153 if (resolver != null) { 154 reader.setEntityResolver(resolver); 155 } 156 157 TargetNSScanner sniffer = new TargetNSScanner(); 158 reader.setContentHandler(sniffer); 159 reader.setErrorHandler(sniffer); 160 reader.parse(schema); 161 } catch (Stop ex) { 162 return ex.getNamespace(); 163 } 164 return null; 165 } 166 167 private class TargetNSScanner extends DefaultHandler { 169 public void startElement(String uri, String local, String qname, Attributes attrs) throws SAXException { 170 if ("schema".equals(local) && SCHEMA_NS.equals(uri)) { String targetNS = attrs.getValue("targetNamespace"); throw new Stop(targetNS); 173 } 174 throw new SAXException(Util.THIS.getString("MSG_missing_schema")); 176 } 177 } 178 179 private static class Stop extends SAXException { 180 Stop(String ns) { 181 super(ns); 182 } 183 184 String getNamespace() { 185 return getMessage(); 186 } 187 } 188 } 189 } 190 | Popular Tags |