1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.jaxp.validation;
  58. import java.io.IOException;
  59. import javax.xml.parsers.FactoryConfigurationError;
  60. import javax.xml.parsers.SAXParserFactory;
  61. import javax.xml.transform.Result;
  62. import javax.xml.transform.Source;
  63. import javax.xml.transform.Transformer;
  64. import javax.xml.transform.TransformerConfigurationException;
  65. import javax.xml.transform.TransformerException;
  66. import javax.xml.transform.TransformerFactoryConfigurationError;
  67. import javax.xml.transform.dom.DOMResult;
  68. import javax.xml.transform.dom.DOMSource;
  69. import javax.xml.transform.sax.SAXResult;
  70. import javax.xml.transform.sax.SAXSource;
  71. import javax.xml.transform.sax.SAXTransformerFactory;
  72. import javax.xml.transform.sax.TransformerHandler;
  73. import javax.xml.transform.stream.StreamSource;
  74. import javax.xml.validation.Schema;
  75. import javax.xml.validation.Validator;
  76. import javax.xml.validation.ValidatorHandler;
  77. import org.w3c.dom.ls.LSInput;
  78. import org.w3c.dom.ls.LSResourceResolver;
  79. import org.xml.sax.EntityResolver;
  80. import org.xml.sax.ErrorHandler;
  81. import org.xml.sax.InputSource;
  82. import org.xml.sax.SAXException;
  83. import org.xml.sax.SAXParseException;
  84. import org.xml.sax.XMLReader;
  85. /**
  86. * <b>(For Implementors)</b> Default implementation of {@link Validator}.
  87. *
  88. * <p>
  89. * This class is intended to be used in conjunction with
  90. * {@link AbstractSchemaImpl} to promote consistent
  91. * behaviors among {@link Schema} implementations.
  92. *
  93. * <p>
  94. * This class wraps a {@link javax.xml.validation.ValidatorHandler}
  95. * object and implements the {@link Validator} semantics.
  96. *
  97. * @author <a href="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
  98. * @version $Revision: 1.5 $, $Date: 2004/07/12 20:38:39 $
  99. * @since 1.5
  100. */
  101. class ValidatorImpl extends Validator {
  102. /**
  103. * The actual validation will be done by this object.
  104. */
  105. private final ValidatorHandlerImpl handler;
  106. /**
  107. * Lazily created identity transformer.
  108. */
  109. private Transformer identityTransformer1 = null;
  110. private TransformerHandler identityTransformer2 = null;
  111. ValidatorImpl( ValidatorHandlerImpl _handler ) {
  112. this.handler = _handler;
  113. }
  114. public LSResourceResolver getResourceResolver() {
  115. return handler.getResourceResolver();
  116. }
  117. public ErrorHandler getErrorHandler() {
  118. return handler.getErrorHandler();
  119. }
  120. public void setResourceResolver(LSResourceResolver resolver) {
  121. handler.setResourceResolver(resolver);
  122. }
  123. public void setErrorHandler(ErrorHandler errorHandler) {
  124. handler.setErrorHandler(errorHandler);
  125. }
  126. public void validate(Source source, Result result) throws SAXException, IOException {
  127. if( source instanceof DOMSource ) {
  128. if( result!=null && !(result instanceof DOMResult) )
  129. throw new IllegalArgumentException(result.getClass().getName());
  130. process( (DOMSource)source, (DOMResult)result );
  131. return;
  132. }
  133. if( source instanceof SAXSource ) {
  134. if( result!=null && !(result instanceof SAXResult) )
  135. throw new IllegalArgumentException(result.getClass().getName());
  136. process( (SAXSource)source, (SAXResult)result );
  137. return;
  138. }
  139. if( source instanceof StreamSource ) {
  140. if( result!=null )
  141. throw new IllegalArgumentException(result.getClass().getName());
  142. StreamSource ss = (StreamSource)source;
  143. InputSource is = new InputSource();
  144. is.setByteStream(ss.getInputStream());
  145. is.setCharacterStream(ss.getReader());
  146. is.setPublicId(ss.getPublicId());
  147. is.setSystemId(ss.getSystemId());
  148. process( new SAXSource(is), null );
  149. return;
  150. }
  151. throw new IllegalArgumentException(source.getClass().getName());
  152. }
  153. /**
  154. * Parses a {@link SAXSource} potentially to a {@link SAXResult}.
  155. */
  156. private void process(SAXSource source, SAXResult result) throws IOException, SAXException {
  157. if( result!=null ) {
  158. handler.setContentHandler(result.getHandler());
  159. }
  160. try {
  161. XMLReader reader = source.getXMLReader();
  162. if( reader==null ) {
  163. // create one now
  164. SAXParserFactory spf = SAXParserFactory.newInstance();
  165. spf.setNamespaceAware(true);
  166. try {
  167. reader = spf.newSAXParser().getXMLReader();
  168. } catch( Exception e ) {
  169. // this is impossible, but better safe than sorry
  170. throw new FactoryConfigurationError(e);
  171. }
  172. }
  173. reader.setErrorHandler(errorForwarder);
  174. reader.setEntityResolver(resolutionForwarder);
  175. reader.setContentHandler(handler);
  176. InputSource is = source.getInputSource();
  177. reader.parse(is);
  178. } finally {
  179. // release the reference to user's handler ASAP
  180. handler.setContentHandler(null);
  181. }
  182. }
  183. /**
  184. * Parses a {@link DOMSource} potentially to a {@link DOMResult}.
  185. */
  186. private void process( DOMSource source, DOMResult result ) throws SAXException {
  187. if( identityTransformer1==null ) {
  188. try {
  189. SAXTransformerFactory tf = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  190. identityTransformer1 = tf.newTransformer();
  191. identityTransformer2 = tf.newTransformerHandler();
  192. } catch (TransformerConfigurationException e) {
  193. // this is impossible, but again better safe than sorry
  194. throw new TransformerFactoryConfigurationError(e);
  195. }
  196. }
  197. if( result!=null ) {
  198. handler.setContentHandler(identityTransformer2);
  199. identityTransformer2.setResult(result);
  200. }
  201. try {
  202. identityTransformer1.transform( source, new SAXResult(handler) );
  203. } catch (TransformerException e) {
  204. if( e.getException() instanceof SAXException )
  205. throw (SAXException)e.getException();
  206. throw new SAXException(e);
  207. } finally {
  208. handler.setContentHandler(null);
  209. }
  210. }
  211. /**
  212. * Forwards the error to the {@link ValidatorHandler}.
  213. * If the {@link ValidatorHandler} doesn't have its own
  214. * {@link ErrorHandler}, behave draconian.
  215. */
  216. private final ErrorHandler errorForwarder = new ErrorHandler() {
  217. public void warning(SAXParseException exception) throws SAXException {
  218. ErrorHandler realHandler = handler.getErrorHandler();
  219. if( realHandler!=null )
  220. realHandler.warning(exception);
  221. }
  222. public void error(SAXParseException exception) throws SAXException {
  223. ErrorHandler realHandler = handler.getErrorHandler();
  224. if( realHandler!=null )
  225. realHandler.error(exception);
  226. else
  227. throw exception;
  228. }
  229. public void fatalError(SAXParseException exception) throws SAXException {
  230. ErrorHandler realHandler = handler.getErrorHandler();
  231. if( realHandler!=null )
  232. realHandler.fatalError(exception);
  233. else
  234. throw exception;
  235. }
  236. };
  237. /**
  238. * Forwards the entity resolution to the {@link ValidatorHandler}.
  239. * If the {@link ValidatorHandler} doesn't have its own
  240. * {@link DOMResourceResolver}, let the parser do the resolution.
  241. */
  242. private final EntityResolver resolutionForwarder = new EntityResolver() {
  243. public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  244. LSResourceResolver resolver = handler.getResourceResolver();
  245. if( resolver==null ) return null;
  246. LSInput di = resolver.resolveResource(null,null,publicId,systemId,null);
  247. if(di==null) return null;
  248. InputSource r = new InputSource();
  249. r.setByteStream(di.getByteStream());
  250. r.setCharacterStream(di.getCharacterStream());
  251. r.setEncoding(di.getEncoding());
  252. r.setPublicId(di.getPublicId());
  253. r.setSystemId(di.getSystemId());
  254. return r;
  255. }
  256. };
  257. public void reset() {
  258. handler.reset();
  259. // I don't think this is necessary, but I don't think it hurts either.
  260. // so reset just for the kick.
  261. if(identityTransformer1!=null) {
  262. identityTransformer1.reset();
  263. }
  264. }
  265. }