1. // $Id: ValidatorHandler.java,v 1.23.16.1 2004/06/28 18:26:43 ndw Exp $
  2. /*
  3. * @(#)ValidatorHandler.java 1.7 04/07/26
  4. *
  5. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  6. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  7. */
  8. package javax.xml.validation;
  9. import org.w3c.dom.ls.LSResourceResolver;
  10. import org.xml.sax.ContentHandler;
  11. import org.xml.sax.ErrorHandler;
  12. import org.xml.sax.SAXNotRecognizedException;
  13. import org.xml.sax.SAXNotSupportedException;
  14. /**
  15. * Streaming validator that works on SAX stream.
  16. *
  17. * <p>
  18. * A {@link ValidatorHandler} object is a thread-unsafe, non-reentrant object.
  19. * In other words, it is the application's responsibility to make
  20. * sure that one {@link ValidatorHandler} object is not used from
  21. * more than one thread at any given time.
  22. *
  23. * <p>
  24. * {@link ValidatorHandler} checks if the SAX events follow
  25. * the set of constraints described in the associated {@link Schema},
  26. * and additionally it may modify the SAX events (for example
  27. * by adding default values, etc.)
  28. *
  29. * <p>
  30. * {@link ValidatorHandler} extends from {@link ContentHandler},
  31. * but it refines the underlying {@link ContentHandler} in
  32. * the following way:
  33. * <ol>
  34. * <li>startElement/endElement events must receive non-null String
  35. * for <code>uri</code>, <code>localName</code>, and <code>qname</code>,
  36. * even though SAX allows some of them to be null.
  37. * Similarly, the user-specified {@link ContentHandler} will receive non-null
  38. * Strings for all three parameters.
  39. *
  40. * <li>Applications must ensure that {@link ValidatorHandler}'s
  41. * {@link ContentHandler#startPrefixMapping(String,String)} and
  42. * {@link ContentHandler#endPrefixMapping(String)} are invoked
  43. * properly. Similarly, the user-specified {@link ContentHandler}
  44. * will receive startPrefixMapping/endPrefixMapping events.
  45. * If the {@link ValidatorHandler} introduces additional namespace
  46. * bindings, the user-specified {@link ContentHandler} will receive
  47. * additional startPrefixMapping/endPrefixMapping events.
  48. *
  49. * <li>{@link org.xml.sax.Attributes} for the
  50. * {@link ContentHandler#startElement(String,String,String,Attributes)} method
  51. * may or may not include xmlns* attributes.
  52. * </ol>
  53. *
  54. * <p>
  55. * A {@link ValidatorHandler} is automatically reset every time
  56. * the startDocument method is invoked.
  57. *
  58. * <h2>Recognized Properties and Features</h2>
  59. * <p>
  60. * This spec defines the following feature that must be recognized
  61. * by all {@link ValidatorHandler} implementations.
  62. *
  63. * <h3><code>http://xml.org/sax/features/namespace-prefixes</code></h3>
  64. * <p>
  65. * This feature controls how a {@link ValidatorHandler} introduces
  66. * namespace bindings that were not present in the original SAX event
  67. * stream.
  68. * When this feature is set to true, it must make
  69. * sure that the user's {@link ContentHandler} will see
  70. * the corresponding <code>xmlns*</code> attribute in
  71. * the {@link org.xml.sax.Attributes} object of the
  72. * {@link ContentHandler#startElement(String,String,String,Attributes)}
  73. * callback. Otherwise, <code>xmlns*</code> attributes must not be
  74. * added to {@link org.xml.sax.Attributes} that's passed to the
  75. * user-specified {@link ContentHandler}.
  76. * <p>
  77. * (Note that regardless of this switch, namespace bindings are
  78. * always notified to applications through
  79. * {@link ContentHandler#startPrefixMapping(String,String)} and
  80. * {@link ContentHandler#endPrefixMapping(String)} methods of the
  81. * {@link ContentHandler} specified by the user.)
  82. *
  83. * <p>
  84. * Note that this feature does <em>NOT</em> affect the way
  85. * a {@link ValidatorHandler} receives SAX events. It merely
  86. * changes the way it augments SAX events.
  87. *
  88. * <p>This feature is set to <code>false</code> by default.</p>
  89. *
  90. * @author <a href="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
  91. * @version $Revision: 1.23.16.1 $, $Date: 2004/06/28 18:26:43 $
  92. * @since 1.5
  93. */
  94. public abstract class ValidatorHandler implements ContentHandler {
  95. /**
  96. * Constructor for derived classes.
  97. *
  98. * <p>
  99. * The constructor does nothing.
  100. *
  101. * <p>
  102. * Derived classes must create {@link ValidatorHandler} objects that have
  103. * <tt>null</tt> {@link ErrorHandler} and
  104. * <tt>null</tt> {@link LSResourceResolver}.
  105. */
  106. protected ValidatorHandler() {
  107. }
  108. /**
  109. * Sets the {@link ContentHandler} which receives
  110. * the augmented validation result.
  111. *
  112. * <p>
  113. * When a {@link ContentHandler} is specified, a
  114. * {@link ValidatorHandler} will work as a filter
  115. * and basically copy the incoming events to the
  116. * specified {@link ContentHandler}.
  117. *
  118. * <p>
  119. * In doing so, a {@link ValidatorHandler} may modify
  120. * the events, for example by adding defaulted attributes.
  121. *
  122. * <p>
  123. * A {@link ValidatorHandler} may buffer events to certain
  124. * extent, but to allow {@link ValidatorHandler} to be used
  125. * by a parser, the following requirement has to be met.
  126. *
  127. * <ol>
  128. * <li>When
  129. * {@link ContentHandler#startElement(String, String, String, Attributes)},
  130. * {@link ContentHandler#endElement(String, String, String)},
  131. * {@link ContentHandler#startDocument()}, or
  132. * {@link ContentHandler#endDocument()}
  133. * are invoked on a {@link ValidatorHandler},
  134. * the same method on the user-specified {@link ContentHandler}
  135. * must be invoked for the same event before the callback
  136. * returns.
  137. * <li>{@link ValidatorHandler} may not introduce new elements that
  138. * were not present in the input.
  139. *
  140. * <li>{@link ValidatorHandler} may not remove attributes that were
  141. * present in the input.
  142. * </ol>
  143. *
  144. * <p>
  145. * When a callback method on the specified {@link ContentHandler}
  146. * throws an exception, the same exception object must be thrown
  147. * from the {@link ValidatorHandler}. The {@link ErrorHandler}
  148. * should not be notified of such an exception.
  149. *
  150. * <p>
  151. * This method can be called even during a middle of a validation.
  152. *
  153. * @param receiver
  154. * A {@link ContentHandler} or a null value.
  155. */
  156. public abstract void setContentHandler(ContentHandler receiver);
  157. /**
  158. * Gets the {@link ContentHandler} which receives the
  159. * augmented validation result.
  160. *
  161. * @return
  162. * This method returns the object that was last set through
  163. * the {@link #getContentHandler()} method, or null
  164. * if that method has never been called since this {@link ValidatorHandler}
  165. * has created.
  166. *
  167. * @see #setContentHandler(ContentHandler)
  168. */
  169. public abstract ContentHandler getContentHandler();
  170. /**
  171. * Sets the {@link ErrorHandler} to receive errors encountered
  172. * during the validation.
  173. *
  174. * <p>
  175. * Error handler can be used to customize the error handling process
  176. * during a validation. When an {@link ErrorHandler} is set,
  177. * errors found during the validation will be first sent
  178. * to the {@link ErrorHandler}.
  179. *
  180. * <p>
  181. * The error handler can abort further validation immediately
  182. * by throwing {@link org.xml.sax.SAXException} from the handler. Or for example
  183. * it can print an error to the screen and try to continue the
  184. * validation by returning normally from the {@link ErrorHandler}
  185. *
  186. * <p>
  187. * If any {@link Throwable} is thrown from an {@link ErrorHandler},
  188. * the same {@link Throwable} object will be thrown toward the
  189. * root of the call stack.
  190. *
  191. * <p>
  192. * {@link ValidatorHandler} is not allowed to
  193. * throw {@link org.xml.sax.SAXException} without first reporting it to
  194. * {@link ErrorHandler}.
  195. *
  196. * <p>
  197. * When the {@link ErrorHandler} is null, the implementation will
  198. * behave as if the following {@link ErrorHandler} is set:
  199. * <pre>
  200. * class DraconianErrorHandler implements {@link ErrorHandler} {
  201. * public void fatalError( {@link org.xml.sax.SAXParseException} e ) throws {@link org.xml.sax.SAXException} {
  202. * throw e;
  203. * }
  204. * public void error( {@link org.xml.sax.SAXParseException} e ) throws {@link org.xml.sax.SAXException} {
  205. * throw e;
  206. * }
  207. * public void warning( {@link org.xml.sax.SAXParseException} e ) throws {@link org.xml.sax.SAXException} {
  208. * // noop
  209. * }
  210. * }
  211. * </pre>
  212. *
  213. * <p>
  214. * When a new {@link ValidatorHandler} object is created, initially
  215. * this field is set to null.
  216. *
  217. * @param errorHandler
  218. * A new error handler to be set. This parameter can be null.
  219. */
  220. public abstract void setErrorHandler(ErrorHandler errorHandler);
  221. /**
  222. * Gets the current {@link ErrorHandler} set to this {@link ValidatorHandler}.
  223. *
  224. * @return
  225. * This method returns the object that was last set through
  226. * the {@link #setErrorHandler(ErrorHandler)} method, or null
  227. * if that method has never been called since this {@link ValidatorHandler}
  228. * has created.
  229. *
  230. * @see #setErrorHandler(ErrorHandler)
  231. */
  232. public abstract ErrorHandler getErrorHandler();
  233. /**
  234. * Sets the {@link LSResourceResolver} to customize
  235. * resource resolution while in a validation episode.
  236. *
  237. * <p>
  238. * {@link ValidatorHandler} uses a {@link LSResourceResolver}
  239. * when it needs to locate external resources while a validation,
  240. * although exactly what constitutes "locating external resources" is
  241. * up to each schema language.
  242. *
  243. * <p>
  244. * When the {@link LSResourceResolver} is null, the implementation will
  245. * behave as if the following {@link LSResourceResolver} is set:
  246. * <pre>
  247. * class DumbLSResourceResolver implements {@link LSResourceResolver} {
  248. * public {@link org.w3c.dom.ls.LSInput} resolveResource(
  249. * String publicId, String systemId, String baseURI) {
  250. *
  251. * return null; // always return null
  252. * }
  253. * }
  254. * </pre>
  255. *
  256. * <p>
  257. * If a {@link LSResourceResolver} throws a {@link RuntimeException}
  258. * (or instances of its derived classes),
  259. * then the {@link ValidatorHandler} will abort the parsing and
  260. * the caller of the <code>validate</code> method will receive
  261. * the same {@link RuntimeException}.
  262. *
  263. * <p>
  264. * When a new {@link ValidatorHandler} object is created, initially
  265. * this field is set to null.
  266. *
  267. * @param resourceResolver
  268. * A new resource resolver to be set. This parameter can be null.
  269. */
  270. public abstract void setResourceResolver(LSResourceResolver resourceResolver);
  271. /**
  272. * Gets the current {@link LSResourceResolver} set to this {@link ValidatorHandler}.
  273. *
  274. * @return
  275. * This method returns the object that was last set through
  276. * the {@link #setResourceResolver(LSResourceResolver)} method, or null
  277. * if that method has never been called since this {@link ValidatorHandler}
  278. * has created.
  279. *
  280. * @see #setErrorHandler(ErrorHandler)
  281. */
  282. public abstract LSResourceResolver getResourceResolver();
  283. /**
  284. * Obtains the {@link TypeInfoProvider} implementation of this
  285. * {@link ValidatorHandler}.
  286. *
  287. * <p>
  288. * The obtained {@link TypeInfoProvider} can be queried during a parse
  289. * to access the type information determined by the validator.
  290. *
  291. * <p>
  292. * Some schema languages do not define the notion of type,
  293. * for those languages, this method may not be supported.
  294. * However, to be compliant with this specification, implementations
  295. * for W3C XML Schema 1.0 must support this operation.
  296. *
  297. * @return
  298. * null if the validator / schema language does not support
  299. * the notion of {@link org.w3c.dom.TypeInfo}.
  300. * Otherwise a non-null valid {@link TypeInfoProvider}.
  301. */
  302. public abstract TypeInfoProvider getTypeInfoProvider();
  303. /**
  304. * Look up the value of a feature flag.
  305. *
  306. * <p>The feature name is any fully-qualified URI. It is
  307. * possible for a {@link ValidatorHandler} to recognize a feature name but
  308. * temporarily be unable to return its value.
  309. * Some feature values may be available only in specific
  310. * contexts, such as before, during, or after a validation.
  311. *
  312. * <p>Implementors are free (and encouraged) to invent their own features,
  313. * using names built on their own URIs.</p>
  314. *
  315. * @param name The feature name, which is a non-null fully-qualified URI.
  316. * @return The current value of the feature (true or false).
  317. * @exception org.xml.sax.SAXNotRecognizedException If the feature
  318. * value can't be assigned or retrieved.
  319. * @exception org.xml.sax.SAXNotSupportedException When the
  320. * {@link ValidatorHandler} recognizes the feature name but
  321. * cannot determine its value at this time.
  322. * @throws NullPointerException
  323. * When the name parameter is null.
  324. * @see #setFeature(String, boolean)
  325. */
  326. public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
  327. if(name==null)
  328. throw new NullPointerException();
  329. throw new SAXNotRecognizedException(name);
  330. }
  331. /**
  332. * Set the value of a feature flag.
  333. *
  334. * <p>
  335. * Feature can be used to control the way a {@link ValidatorHandler}
  336. * parses schemas, although {@link ValidatorHandler}s are not required
  337. * to recognize any specific property names.</p>
  338. *
  339. * <p>The feature name is any fully-qualified URI. It is
  340. * possible for a {@link ValidatorHandler} to expose a feature value but
  341. * to be unable to change the current value.
  342. * Some feature values may be immutable or mutable only
  343. * in specific contexts, such as before, during, or after
  344. * a validation.</p>
  345. *
  346. * @param name The feature name, which is a non-null fully-qualified URI.
  347. * @param value The requested value of the feature (true or false).
  348. *
  349. * @exception org.xml.sax.SAXNotRecognizedException If the feature
  350. * value can't be assigned or retrieved.
  351. * @exception org.xml.sax.SAXNotSupportedException When the
  352. * {@link ValidatorHandler} recognizes the feature name but
  353. * cannot set the requested value.
  354. * @throws NullPointerException
  355. * When the name parameter is null.
  356. *
  357. * @see #getFeature(String)
  358. */
  359. public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
  360. if(name==null)
  361. throw new NullPointerException();
  362. throw new SAXNotRecognizedException(name);
  363. }
  364. /**
  365. * Set the value of a property.
  366. *
  367. * <p>The property name is any fully-qualified URI. It is
  368. * possible for a {@link ValidatorHandler} to recognize a property name but
  369. * to be unable to change the current value.
  370. * Some property values may be immutable or mutable only
  371. * in specific contexts, such as before, during, or after
  372. * a validation.</p>
  373. *
  374. * <p>{@link ValidatorHandler}s are not required to recognize setting
  375. * any specific property names.</p>
  376. *
  377. * @param name The property name, which is a non-null fully-qualified URI.
  378. * @param object The requested value for the property.
  379. *
  380. * @exception org.xml.sax.SAXNotRecognizedException If the property
  381. * value can't be assigned or retrieved.
  382. * @exception org.xml.sax.SAXNotSupportedException When the
  383. * {@link ValidatorHandler} recognizes the property name but
  384. * cannot set the requested value.
  385. * @throws NullPointerException
  386. * When the name parameter is null.
  387. */
  388. public void setProperty(String name, Object object) throws SAXNotRecognizedException, SAXNotSupportedException {
  389. if(name==null)
  390. throw new NullPointerException();
  391. throw new SAXNotRecognizedException(name);
  392. }
  393. /**
  394. * Look up the value of a property.
  395. *
  396. * <p>The property name is any fully-qualified URI. It is
  397. * possible for a {@link ValidatorHandler} to recognize a property name but
  398. * temporarily be unable to return its value.
  399. * Some property values may be available only in specific
  400. * contexts, such as before, during, or after a validation.</p>
  401. *
  402. * <p>{@link ValidatorHandler}s are not required to recognize any specific
  403. * property names.</p>
  404. *
  405. * <p>Implementors are free (and encouraged) to invent their own properties,
  406. * using names built on their own URIs.</p>
  407. *
  408. * @param name The property name, which is a non-null fully-qualified URI.
  409. * @return The current value of the property.
  410. * @exception org.xml.sax.SAXNotRecognizedException If the property
  411. * value can't be assigned or retrieved.
  412. * @exception org.xml.sax.SAXNotSupportedException When the
  413. * XMLReader recognizes the property name but
  414. * cannot determine its value at this time.
  415. * @throws NullPointerException
  416. * When the name parameter is null.
  417. * @see #setProperty(String, Object)
  418. */
  419. public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
  420. if(name==null)
  421. throw new NullPointerException();
  422. throw new SAXNotRecognizedException(name);
  423. }
  424. }