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 com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  59. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
  60. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  61. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  62. import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter;
  63. /**
  64. * Wraps a validator {@link XMLComponent} and isolates
  65. * it from the rest of the components.
  66. *
  67. * <p>
  68. * For the performance reason, when a validator from Xerces is used
  69. * for a parser from Xerces, we will do "chating" by building an
  70. * XNI pipeline. This saves the overhead of conversion between
  71. * XNI events and SAX events.
  72. *
  73. * <p>
  74. * However, if we just insert the validator component into the
  75. * parser pipeline, the {@link XMLComponentManager} that the parser
  76. * uses could change the way the validator works. On the other hand,
  77. * certain configuration (such as error handlers) need to be given
  78. * through a parser configuration.
  79. *
  80. * <p>
  81. * To avoid this harmful interaction, this class wraps the validator
  82. * and behaves as an insulation. The class itself will implement
  83. * {@link XMLComponent}, and it selectively deliver properties to
  84. * the wrapped validator.
  85. *
  86. * <p>
  87. * Since the exact set of properties/features that require insulation
  88. * depends on the actual validator implementation, this class is
  89. * expected to be derived to add such validator-specific insulation
  90. * code.
  91. *
  92. * @author
  93. * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  94. */
  95. public abstract class InsulatedValidatorComponent implements
  96. XMLComponent, // this object behaves as a component to the parent component manager
  97. XMLComponentManager // this object behaves as a manager to the wrapped validator.
  98. {
  99. /**
  100. * The object being wrapped.
  101. * We require the validator to be both {@link XMLDocumentFilter}
  102. * and {@link XMLComponent}.
  103. */
  104. private final XMLDocumentFilter fValidator;
  105. /**
  106. * The same object as {@link #fValidator}.
  107. */
  108. private final XMLComponent fValidatorComponent;
  109. /**
  110. * We will not use external {@link ValidationManager} to
  111. * avoid interaction.
  112. * <p>
  113. * The existance of JAXP validator should not change the
  114. * semantics of the parser processing. IOW it should not
  115. * interact with the other validators in the parser pipeline.
  116. */
  117. private final ValidationManager fValidationManager = new ValidationManager();
  118. /**
  119. * The current component manager.
  120. */
  121. private XMLComponentManager fManager;
  122. public InsulatedValidatorComponent( XMLDocumentFilter validator ) {
  123. fValidator = validator;
  124. fValidatorComponent = (XMLComponent)validator;
  125. }
  126. /**
  127. * Obtains a reference to the validator as a filter.
  128. *
  129. * @return
  130. * non-null valid object.
  131. */
  132. public final XMLDocumentFilter getValidator() {
  133. return fValidator;
  134. }
  135. //
  136. //
  137. // XMLComponent implementation
  138. //
  139. //
  140. public final void reset(XMLComponentManager componentManager) throws XMLConfigurationException {
  141. fManager = componentManager;
  142. fValidatorComponent.reset(this);
  143. fValidationManager.reset();
  144. }
  145. public final String[] getRecognizedFeatures() {
  146. return fValidatorComponent.getRecognizedFeatures();
  147. }
  148. public final void setFeature(String featureId, boolean state) throws XMLConfigurationException {
  149. // don't allow features to be set.
  150. }
  151. public final String[] getRecognizedProperties() {
  152. return fValidatorComponent.getRecognizedProperties();
  153. }
  154. public final void setProperty(String propertyId, Object value) throws XMLConfigurationException {
  155. // don't allow properties to be set.
  156. }
  157. public final Boolean getFeatureDefault(String featureId) {
  158. return fValidatorComponent.getFeatureDefault(featureId);
  159. }
  160. public final Object getPropertyDefault(String propertyId) {
  161. return fValidatorComponent.getPropertyDefault(propertyId);
  162. }
  163. //
  164. //
  165. // XMLComponentManager implementation
  166. //
  167. //
  168. /**
  169. * Derived class may override this method to block additional features.
  170. */
  171. public boolean getFeature(String featureId) throws XMLConfigurationException {
  172. return fManager.getFeature(featureId);
  173. }
  174. /**
  175. * Derived class may override this method to block additional properties.
  176. */
  177. public Object getProperty(String propertyId) throws XMLConfigurationException {
  178. if( propertyId.equals(XercesConstants.VALIDATION_MANAGER) )
  179. return fValidationManager;
  180. return fManager.getProperty(propertyId);
  181. }
  182. }