1 /* |
1 /* |
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
32 import com.sun.xml.internal.xsom.impl.SchemaImpl; |
32 import com.sun.xml.internal.xsom.impl.SchemaImpl; |
33 import com.sun.xml.internal.xsom.impl.UName; |
33 import com.sun.xml.internal.xsom.impl.UName; |
34 import com.sun.xml.internal.xsom.impl.Const; |
34 import com.sun.xml.internal.xsom.impl.Const; |
35 import com.sun.xml.internal.xsom.impl.parser.state.NGCCRuntime; |
35 import com.sun.xml.internal.xsom.impl.parser.state.NGCCRuntime; |
36 import com.sun.xml.internal.xsom.impl.parser.state.Schema; |
36 import com.sun.xml.internal.xsom.impl.parser.state.Schema; |
37 import com.sun.xml.internal.xsom.impl.util.Uri; |
|
38 import com.sun.xml.internal.xsom.parser.AnnotationParser; |
37 import com.sun.xml.internal.xsom.parser.AnnotationParser; |
39 import com.sun.xml.internal.org.relaxng.datatype.ValidationContext; |
38 import com.sun.xml.internal.org.relaxng.datatype.ValidationContext; |
40 import org.xml.sax.Attributes; |
39 import org.xml.sax.Attributes; |
41 import org.xml.sax.EntityResolver; |
40 import org.xml.sax.EntityResolver; |
42 import org.xml.sax.ErrorHandler; |
41 import org.xml.sax.ErrorHandler; |
46 import org.xml.sax.SAXParseException; |
45 import org.xml.sax.SAXParseException; |
47 import org.xml.sax.helpers.LocatorImpl; |
46 import org.xml.sax.helpers.LocatorImpl; |
48 |
47 |
49 import java.io.IOException; |
48 import java.io.IOException; |
50 import java.net.URI; |
49 import java.net.URI; |
|
50 import java.net.URL; |
51 import java.text.MessageFormat; |
51 import java.text.MessageFormat; |
52 import java.util.Stack; |
52 import java.util.Stack; |
|
53 import java.util.regex.Pattern; |
53 |
54 |
54 /** |
55 /** |
55 * NGCCRuntime extended with various utility methods for |
56 * NGCCRuntime extended with various utility methods for |
56 * parsing XML Schema. |
57 * parsing XML Schema. |
57 * |
58 * |
148 } |
149 } |
149 |
150 |
150 |
151 |
151 |
152 |
152 /* registers a patcher that will run after all the parsing has finished. */ |
153 /* registers a patcher that will run after all the parsing has finished. */ |
|
154 @Override |
153 public void addPatcher( Patch patcher ) { |
155 public void addPatcher( Patch patcher ) { |
154 parser.patcherManager.addPatcher(patcher); |
156 parser.patcherManager.addPatcher(patcher); |
155 } |
157 } |
|
158 @Override |
156 public void addErrorChecker( Patch patcher ) { |
159 public void addErrorChecker( Patch patcher ) { |
157 parser.patcherManager.addErrorChecker(patcher); |
160 parser.patcherManager.addErrorChecker(patcher); |
158 } |
161 } |
|
162 @Override |
159 public void reportError( String msg, Locator loc ) throws SAXException { |
163 public void reportError( String msg, Locator loc ) throws SAXException { |
160 parser.patcherManager.reportError(msg,loc); |
164 parser.patcherManager.reportError(msg,loc); |
161 } |
165 } |
162 public void reportError( String msg ) throws SAXException { |
166 public void reportError( String msg ) throws SAXException { |
163 reportError(msg,getLocator()); |
167 reportError(msg,getLocator()); |
186 baseUri=documentSystemId; |
190 baseUri=documentSystemId; |
187 |
191 |
188 EntityResolver er = parser.getEntityResolver(); |
192 EntityResolver er = parser.getEntityResolver(); |
189 String systemId = null; |
193 String systemId = null; |
190 |
194 |
191 if (relativeUri!=null) |
195 if (relativeUri!=null) { |
192 systemId = Uri.resolve(baseUri,relativeUri); |
196 if (isAbsolute(relativeUri)) { |
|
197 systemId = relativeUri; |
|
198 } |
|
199 if (baseUri == null || !isAbsolute(baseUri)) { |
|
200 throw new IOException("Unable to resolve relative URI " + relativeUri + " because base URI is not absolute: " + baseUri); |
|
201 } |
|
202 systemId = new URL(new URL(baseUri), relativeUri).toString(); |
|
203 } |
193 |
204 |
194 if (er!=null) { |
205 if (er!=null) { |
195 InputSource is = er.resolveEntity(namespaceURI,systemId); |
206 InputSource is = er.resolveEntity(namespaceURI,systemId); |
196 if (is == null) { |
207 if (is == null) { |
197 try { |
208 try { |
215 parser.errorHandler.error(se); |
226 parser.errorHandler.error(se); |
216 return null; |
227 return null; |
217 } |
228 } |
218 } |
229 } |
219 |
230 |
220 /** Includes the specified schema. */ |
231 private static final Pattern P = Pattern.compile(".*[/#?].*"); |
|
232 |
|
233 private static boolean isAbsolute(String uri) { |
|
234 int i = uri.indexOf(':'); |
|
235 if (i < 0) { |
|
236 return false; |
|
237 } |
|
238 return !P.matcher(uri.substring(0, i)).matches(); |
|
239 } |
|
240 |
|
241 /** |
|
242 * Includes the specified schema. |
|
243 * |
|
244 * @param schemaLocation |
|
245 * @throws org.xml.sax.SAXException */ |
221 public void includeSchema( String schemaLocation ) throws SAXException { |
246 public void includeSchema( String schemaLocation ) throws SAXException { |
222 NGCCRuntimeEx runtime = new NGCCRuntimeEx(parser,chameleonMode,this); |
247 NGCCRuntimeEx runtime = new NGCCRuntimeEx(parser,chameleonMode,this); |
223 runtime.currentSchema = this.currentSchema; |
248 runtime.currentSchema = this.currentSchema; |
224 runtime.blockDefault = this.blockDefault; |
249 runtime.blockDefault = this.blockDefault; |
225 runtime.finalDefault = this.finalDefault; |
250 runtime.finalDefault = this.finalDefault; |
233 |
258 |
234 runtime.parseEntity( resolveRelativeURL(null,schemaLocation), |
259 runtime.parseEntity( resolveRelativeURL(null,schemaLocation), |
235 true, currentSchema.getTargetNamespace(), getLocator() ); |
260 true, currentSchema.getTargetNamespace(), getLocator() ); |
236 } |
261 } |
237 |
262 |
238 /** Imports the specified schema. */ |
263 /** |
|
264 * Imports the specified schema. |
|
265 * |
|
266 * @param ns |
|
267 * @param schemaLocation |
|
268 * @throws org.xml.sax.SAXException */ |
239 public void importSchema( String ns, String schemaLocation ) throws SAXException { |
269 public void importSchema( String ns, String schemaLocation ) throws SAXException { |
240 NGCCRuntimeEx newRuntime = new NGCCRuntimeEx(parser,false,this); |
270 NGCCRuntimeEx newRuntime = new NGCCRuntimeEx(parser,false,this); |
241 InputSource source = resolveRelativeURL(ns,schemaLocation); |
271 InputSource source = resolveRelativeURL(ns,schemaLocation); |
242 if(source!=null) |
272 if(source!=null) |
243 newRuntime.parseEntity( source, false, ns, getLocator() ); |
273 newRuntime.parseEntity( source, false, ns, getLocator() ); |
315 } |
345 } |
316 |
346 |
317 /** |
347 /** |
318 * Parses the specified entity. |
348 * Parses the specified entity. |
319 * |
349 * |
|
350 * @param source |
320 * @param importLocation |
351 * @param importLocation |
321 * The source location of the import/include statement. |
352 * The source location of the import/include statement. |
322 * Used for reporting errors. |
353 * Used for reporting errors. |
|
354 * @param includeMode |
|
355 * @param expectedNamespace |
|
356 * @throws org.xml.sax.SAXException |
323 */ |
357 */ |
324 public void parseEntity( InputSource source, boolean includeMode, String expectedNamespace, Locator importLocation ) |
358 public void parseEntity( InputSource source, boolean includeMode, String expectedNamespace, Locator importLocation ) |
325 throws SAXException { |
359 throws SAXException { |
326 |
360 |
327 documentSystemId = source.getSystemId(); |
361 documentSystemId = source.getSystemId(); |
340 } |
374 } |
341 } |
375 } |
342 |
376 |
343 /** |
377 /** |
344 * Creates a new instance of annotation parser. |
378 * Creates a new instance of annotation parser. |
|
379 * |
|
380 * @return Annotation parser |
345 */ |
381 */ |
346 public AnnotationParser createAnnotationParser() { |
382 public AnnotationParser createAnnotationParser() { |
347 if(parser.getAnnotationParserFactory()==null) |
383 if(parser.getAnnotationParserFactory()==null) |
348 return DefaultAnnotationParser.theInstance; |
384 return DefaultAnnotationParser.theInstance; |
349 else |
385 else |
350 return parser.getAnnotationParserFactory().create(); |
386 return parser.getAnnotationParserFactory().create(); |
351 } |
387 } |
352 |
388 |
353 /** |
389 /** |
354 * Gets the element name that contains the annotation element. |
390 * Gets the element name that contains the annotation element.This method works correctly only when called by the annotation handler. |
355 * This method works correctly only when called by the annotation handler. |
391 * |
|
392 * @return Element name |
356 */ |
393 */ |
357 public String getAnnotationContextElementName() { |
394 public String getAnnotationContextElementName() { |
358 return elementNames.get( elementNames.size()-2 ); |
395 return elementNames.get( elementNames.size()-2 ); |
359 } |
396 } |
360 |
397 |
361 /** Creates a copy of the current locator object. */ |
398 /** |
|
399 * Creates a copy of the current locator object. |
|
400 * |
|
401 * @return Locator copy |
|
402 */ |
362 public Locator copyLocator() { |
403 public Locator copyLocator() { |
363 return new LocatorImpl(getLocator()); |
404 return new LocatorImpl(getLocator()); |
364 } |
405 } |
365 |
406 |
366 public ErrorHandler getErrorHandler() { |
407 public ErrorHandler getErrorHandler() { |
395 this.previous = _context; |
436 this.previous = _context; |
396 this.prefix = _prefix; |
437 this.prefix = _prefix; |
397 this.uri = _uri; |
438 this.uri = _uri; |
398 } |
439 } |
399 |
440 |
|
441 @Override |
400 public String resolveNamespacePrefix(String p) { |
442 public String resolveNamespacePrefix(String p) { |
401 if(p.equals(prefix)) return uri; |
443 if(p.equals(prefix)) return uri; |
402 if(previous==null) return null; |
444 if(previous==null) return null; |
403 else return previous.resolveNamespacePrefix(p); |
445 else return previous.resolveNamespacePrefix(p); |
404 } |
446 } |
406 private final String prefix; |
448 private final String prefix; |
407 private final String uri; |
449 private final String uri; |
408 private final Context previous; |
450 private final Context previous; |
409 |
451 |
410 // XSDLib don't use those methods, so we cut a corner here. |
452 // XSDLib don't use those methods, so we cut a corner here. |
|
453 @Override |
411 public String getBaseUri() { return null; } |
454 public String getBaseUri() { return null; } |
|
455 @Override |
412 public boolean isNotation(String arg0) { return false; } |
456 public boolean isNotation(String arg0) { return false; } |
|
457 @Override |
413 public boolean isUnparsedEntity(String arg0) { return false; } |
458 public boolean isUnparsedEntity(String arg0) { return false; } |
414 } |
459 } |
415 |
460 |
416 private Context currentContext=null; |
461 private Context currentContext=null; |
417 |
462 |
418 /** Returns an immutable snapshot of the current context. */ |
463 /** Returns an immutable snapshot of the current context. |
|
464 * |
|
465 * @return Snapshot of current context |
|
466 */ |
419 public ValidationContext createValidationContext() { |
467 public ValidationContext createValidationContext() { |
420 return currentContext; |
468 return currentContext; |
421 } |
469 } |
422 |
470 |
423 public XmlString createXmlString(String value) { |
471 public XmlString createXmlString(String value) { |
444 |
492 |
445 /** |
493 /** |
446 * Parses UName under the given context. |
494 * Parses UName under the given context. |
447 * @param qname Attribute name. |
495 * @param qname Attribute name. |
448 * @return New {@link UName} instance based on attribute name. |
496 * @return New {@link UName} instance based on attribute name. |
|
497 * @throws org.xml.sax.SAXException |
449 */ |
498 */ |
450 public UName parseUName(final String qname ) throws SAXException { |
499 public UName parseUName(final String qname ) throws SAXException { |
451 int idx = qname.indexOf(':'); |
500 int idx = qname.indexOf(':'); |
452 if(idx<0) { |
501 if(idx<0) { |
453 String uri = resolveNamespacePrefix(""); |
502 String uri = resolveNamespacePrefix(""); |