lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | 5f556c5 | 2008-03-26 19:37:20 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2008 IBM Corporation and others. |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.wst.xml.core.internal.validation; |
| 13 | |
lmandel | adbf5dc | 2005-10-12 02:58:29 +0000 | [diff] [blame] | 14 | import java.io.InputStream; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 15 | import java.io.Reader; |
lmandel | adbf5dc | 2005-10-12 02:58:29 +0000 | [diff] [blame] | 16 | import java.net.URL; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 17 | import java.util.List; |
amywu | fecd7a9 | 2006-05-10 07:22:58 +0000 | [diff] [blame] | 18 | import com.ibm.icu.util.StringTokenizer; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 19 | import java.util.Vector; |
lmandel | adbf5dc | 2005-10-12 02:58:29 +0000 | [diff] [blame] | 20 | |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 21 | import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver; |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 22 | import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin; |
vbaciu | 185864d | 2007-04-11 20:20:32 +0000 | [diff] [blame] | 23 | import org.eclipse.wst.common.uriresolver.internal.util.URIHelper; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 24 | import org.xml.sax.Attributes; |
| 25 | import org.xml.sax.InputSource; |
| 26 | import org.xml.sax.SAXException; |
| 27 | import org.xml.sax.SAXParseException; |
| 28 | import org.xml.sax.XMLReader; |
| 29 | import org.xml.sax.ext.LexicalHandler; |
| 30 | |
| 31 | /** |
| 32 | * A helper class for the XML validator. |
| 33 | * |
| 34 | * @author Craig Salter, IBM |
| 35 | * @author Lawrence Mandel, IBM |
| 36 | */ |
| 37 | public class ValidatorHelper |
| 38 | { |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 39 | public List namespaceURIList = new Vector(); |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 40 | public boolean isGrammarEncountered = false; |
| 41 | public boolean isDTDEncountered = false; |
| 42 | public boolean isNamespaceEncountered = false; |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 43 | public String schemaLocationString = ""; //$NON-NLS-1$ |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 44 | public int numDTDElements = 0; |
| 45 | |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Constructor. |
| 48 | */ |
| 49 | public ValidatorHelper() |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create an XML Reader. |
| 55 | * |
| 56 | * @return An XML Reader if one can be created or null. |
| 57 | * @throws Exception |
| 58 | */ |
csalter | 59f3bd3 | 2005-11-12 06:55:35 +0000 | [diff] [blame] | 59 | protected XMLReader createXMLReader(String uri) throws Exception |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 60 | { |
| 61 | XMLReader reader = null; |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 62 | |
| 63 | reader = new org.apache.xerces.parsers.SAXParser(); |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 64 | reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false); //$NON-NLS-1$ |
| 65 | reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); //$NON-NLS-1$ |
| 66 | reader.setFeature("http://xml.org/sax/features/namespaces", false); //$NON-NLS-1$ |
| 67 | reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$ |
csalter | 59f3bd3 | 2005-11-12 06:55:35 +0000 | [diff] [blame] | 68 | reader.setContentHandler(new MyContentHandler(uri)); |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 69 | reader.setErrorHandler(new InternalErrorHandler()); |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 70 | |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 71 | LexicalHandler lexicalHandler = new LexicalHandler() |
| 72 | { |
| 73 | public void startDTD (String name, String publicId, String systemId) |
| 74 | { |
| 75 | isGrammarEncountered = true; |
| 76 | isDTDEncountered = true; |
| 77 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 78 | |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 79 | public void endDTD() throws SAXException |
| 80 | { |
| 81 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 82 | |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 83 | public void startEntity(String name) throws SAXException |
| 84 | { |
| 85 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 86 | |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 87 | public void endEntity(String name) throws SAXException |
| 88 | { |
| 89 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 90 | |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 91 | public void startCDATA() throws SAXException |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | public void endCDATA() throws SAXException |
| 96 | { |
| 97 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 98 | |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 99 | public void comment (char ch[], int start, int length) throws SAXException |
| 100 | { |
| 101 | } |
| 102 | }; |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 103 | reader.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler); //$NON-NLS-1$ |
lmandel | edffe17 | 2005-11-03 00:00:30 +0000 | [diff] [blame] | 104 | |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 105 | return reader; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * An error handler to suppress error and warning information. |
| 110 | */ |
| 111 | private class InternalErrorHandler implements org.xml.sax.ErrorHandler |
| 112 | { |
lmandel | 4ff07b9 | 2006-04-18 05:12:19 +0000 | [diff] [blame] | 113 | public InternalErrorHandler() |
| 114 | { |
| 115 | super(); |
| 116 | } |
| 117 | |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 118 | public void error(SAXParseException exception) throws SAXException |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | public void fatalError(SAXParseException exception) throws SAXException |
| 123 | { |
| 124 | } |
| 125 | |
| 126 | public void warning(SAXParseException exception) throws SAXException |
| 127 | { |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Figures out the information needed for validation. |
| 134 | * |
| 135 | * @param uri The uri of the file to validate. |
| 136 | * @param uriResolver A helper to resolve locations. |
| 137 | */ |
| 138 | public void computeValidationInformation(String uri, Reader characterStream, URIResolver uriResolver) |
| 139 | { |
| 140 | try |
| 141 | { |
csalter | 59f3bd3 | 2005-11-12 06:55:35 +0000 | [diff] [blame] | 142 | XMLReader reader = createXMLReader(uri); |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 143 | InputSource inputSource = new InputSource(uri); |
| 144 | inputSource.setCharacterStream(characterStream); |
| 145 | reader.parse(inputSource); |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 146 | } |
| 147 | catch (Exception e) |
| 148 | { |
| 149 | //System.out.println(e); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Handle the content while parsing the file. |
| 157 | */ |
| 158 | class MyContentHandler extends org.xml.sax.helpers.DefaultHandler |
| 159 | { |
| 160 | /* (non-Javadoc) |
| 161 | * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) |
| 162 | */ |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 163 | boolean isRootElement = true; |
csalter | 59f3bd3 | 2005-11-12 06:55:35 +0000 | [diff] [blame] | 164 | String baseURI; |
| 165 | |
| 166 | MyContentHandler(String uri) |
| 167 | { |
| 168 | this.baseURI = uri; |
| 169 | } |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 170 | |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 171 | public void error(SAXParseException e) throws SAXException |
| 172 | { |
| 173 | } |
| 174 | /* (non-Javadoc) |
| 175 | * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException) |
| 176 | */ |
| 177 | public void fatalError(SAXParseException e) throws SAXException |
| 178 | { |
| 179 | } |
| 180 | /* (non-Javadoc) |
| 181 | * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) |
| 182 | */ |
| 183 | public void warning(SAXParseException e) throws SAXException |
| 184 | { |
| 185 | } |
| 186 | public String getPrefix(String name) |
| 187 | { |
| 188 | String prefix = null; |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 189 | int index = name.indexOf(":"); //$NON-NLS-1$ |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 190 | if (index != -1) |
| 191 | { |
| 192 | prefix = name.substring(0, index); |
| 193 | } |
| 194 | return prefix; |
| 195 | } |
| 196 | |
| 197 | public String getUnprefixedName(String name) |
| 198 | { |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 199 | int index = name.indexOf(":"); //$NON-NLS-1$ |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 200 | if (index != -1) |
| 201 | { |
| 202 | name = name.substring(index + 1); |
| 203 | } |
| 204 | return name; |
| 205 | } |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 206 | |
| 207 | public String getPrefixedName(String prefix, String localName) |
| 208 | { |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 209 | return prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName; //$NON-NLS-1$ |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 210 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 211 | |
| 212 | public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) |
| 213 | { |
| 214 | //String explicitLocation = null; |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 215 | if (isRootElement) |
| 216 | { |
| 217 | |
| 218 | isRootElement = false; |
| 219 | int nAtts = atts.getLength(); |
| 220 | String schemaInstancePrefix = null; |
| 221 | for (int i =0; i < nAtts; i++) |
| 222 | { |
| 223 | String attributeName = atts.getQName(i); |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 224 | if (attributeName.equals("xmlns") || attributeName.startsWith("xmlns:")) //$NON-NLS-1$ //$NON-NLS-2$ |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 225 | { |
| 226 | isNamespaceEncountered = true; |
| 227 | String value = atts.getValue(i); |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 228 | if (value.startsWith("http://www.w3.org/") && value.endsWith("/XMLSchema-instance")) //$NON-NLS-1$ //$NON-NLS-2$ |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 229 | { |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 230 | schemaInstancePrefix = attributeName.equals("xmlns") ? "" : getUnprefixedName(attributeName); //$NON-NLS-1$ //$NON-NLS-2$ |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | String prefix = getPrefix(rawName); |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 236 | String rootElementNamespaceDeclarationName = (prefix != null && prefix.length() > 0) ? "xmlns:" + prefix : "xmlns"; //$NON-NLS-1$ //$NON-NLS-2$ |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 237 | String rootElementNamespace = rootElementNamespaceDeclarationName != null ? atts.getValue(rootElementNamespaceDeclarationName) : null; |
| 238 | |
| 239 | String location = null; |
| 240 | |
| 241 | // first we use any 'xsi:schemaLocation' or 'xsi:noNamespaceSchemaLocation' attribute |
| 242 | // to determine a location |
| 243 | if (schemaInstancePrefix != null) |
| 244 | { |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 245 | location = atts.getValue(getPrefixedName(schemaInstancePrefix, "noNamespaceSchemaLocation")); //$NON-NLS-1$ |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 246 | if (location == null) |
csalter | 59f3bd3 | 2005-11-12 06:55:35 +0000 | [diff] [blame] | 247 | { |
nitind | 95409d0 | 2005-11-10 05:32:16 +0000 | [diff] [blame] | 248 | String schemaLoc = atts.getValue(getPrefixedName(schemaInstancePrefix, "schemaLocation")); //$NON-NLS-1$ |
csalter | 59f3bd3 | 2005-11-12 06:55:35 +0000 | [diff] [blame] | 249 | location = getSchemaLocationForNamespace(schemaLoc, rootElementNamespace); |
| 250 | } |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 251 | } |
lmandel | 8587504 | 2006-05-02 22:54:39 +0000 | [diff] [blame] | 252 | if (rootElementNamespace == null) |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 253 | { |
lmandel | 8587504 | 2006-05-02 22:54:39 +0000 | [diff] [blame] | 254 | rootElementNamespace = ""; |
| 255 | } |
| 256 | |
| 257 | location = URIResolverPlugin.createResolver().resolve(baseURI, rootElementNamespace, location); |
| 258 | location = URIResolverPlugin.createResolver().resolvePhysicalLocation(baseURI, rootElementNamespace, location); |
vbaciu | 185864d | 2007-04-11 20:20:32 +0000 | [diff] [blame] | 259 | location = URIHelper.addImpliedFileProtocol(location); |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 260 | |
kchong | 5f556c5 | 2008-03-26 19:37:20 +0000 | [diff] [blame] | 261 | schemaLocationString = location; |
| 262 | |
csalter | 2b69684 | 2005-07-09 04:25:15 +0000 | [diff] [blame] | 263 | if (location != null) |
| 264 | { |
lmandel | adbf5dc | 2005-10-12 02:58:29 +0000 | [diff] [blame] | 265 | InputStream is = null; |
| 266 | try |
| 267 | { |
| 268 | URL url = new URL(location); |
| 269 | is = url.openStream(); |
| 270 | isGrammarEncountered = true; |
| 271 | } |
| 272 | catch(Exception e) |
| 273 | { |
| 274 | // Do nothing. |
| 275 | } |
| 276 | finally |
| 277 | { |
| 278 | if(is != null) |
| 279 | { |
| 280 | try |
| 281 | { |
| 282 | is.close(); |
| 283 | } |
| 284 | catch(Exception e) |
| 285 | { |
| 286 | // Do nothing. |
| 287 | } |
| 288 | } |
| 289 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | /* (non-Javadoc) |
| 294 | * @see org.xml.sax.ext.DeclHandler#elementDecl(java.lang.String, java.lang.String) |
| 295 | */ |
| 296 | public void elementDecl(String name, String model) |
| 297 | { |
| 298 | numDTDElements++; |
| 299 | } |
csalter | 59f3bd3 | 2005-11-12 06:55:35 +0000 | [diff] [blame] | 300 | |
| 301 | // The xsiSchemaLocationValue is a list of namespace/location pairs that are separated by whitespace |
| 302 | // this method walks the list of pairs looking for the specified namespace and returns the associated |
| 303 | // location. |
| 304 | // |
| 305 | protected String getSchemaLocationForNamespace(String xsiSchemaLocationValue, String namespace) |
| 306 | { |
| 307 | String result = null; |
| 308 | if (xsiSchemaLocationValue != null && namespace != null) |
| 309 | { |
| 310 | |
| 311 | StringTokenizer st = new StringTokenizer(xsiSchemaLocationValue); |
| 312 | while(st.hasMoreTokens()) |
| 313 | { |
| 314 | if(st.nextToken().equals(namespace)) |
| 315 | { |
| 316 | if(st.hasMoreTokens()) |
| 317 | { |
| 318 | result = st.nextToken(); |
| 319 | } |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | if(st.hasMoreTokens()) |
| 324 | { |
| 325 | st.nextToken(); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return result; |
| 331 | } |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 332 | } |
| 333 | |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | * Replace all instances in the string of the old pattern with the new pattern. |
| 337 | * |
| 338 | * @param string The string to replace the patterns in. |
| 339 | * @param oldPattern The old pattern to replace. |
| 340 | * @param newPattern The pattern used for replacement. |
| 341 | * @return The modified string with all occurrances of oldPattern replaced by new Pattern. |
| 342 | */ |
| 343 | protected static String replace(String string, String oldPattern, String newPattern) |
| 344 | { |
| 345 | int index = 0; |
| 346 | while (index != -1) |
| 347 | { |
| 348 | index = string.indexOf(oldPattern, index); |
| 349 | if (index != -1) |
| 350 | { |
| 351 | string = string.substring(0, index) + newPattern + string.substring(index + oldPattern.length()); |
| 352 | index = index + oldPattern.length(); |
| 353 | } |
| 354 | } |
| 355 | return string; |
| 356 | } |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 357 | } |