1. package com.sun.org.apache.xerces.internal.parsers;
  2. import javax.xml.validation.Schema;
  3. import javax.xml.validation.ValidatorHandler;
  4. import com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent;
  5. import com.sun.org.apache.xerces.internal.jaxp.validation.InsulatedValidatorComponent;
  6. import com.sun.org.apache.xerces.internal.jaxp.validation.XercesSchema;
  7. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
  8. import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter;
  9. /**
  10. * {@link com.sun.org.apache.xerces.internal.xni.parser.XMLParseException} that
  11. * includes a JAXP {@link ValidatorHandler} in the middle.
  12. *
  13. * @author
  14. * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  15. * Venu Gopal (k.venugopal@sun.com)
  16. */
  17. public class JAXPConfiguration extends XIncludeParserConfiguration {
  18. /** can be null. */
  19. private final Schema fSchema;
  20. /**
  21. *
  22. * @param grammar
  23. * when non-null, the parser will include validation /
  24. * infoset augmentation by this {@link Schema}.
  25. */
  26. public JAXPConfiguration(Schema schema){
  27. this.fSchema = schema;
  28. }
  29. protected void configurePipeline() {
  30. super.configurePipeline();
  31. if (fSchema != null) {
  32. if( isXNICapabaleSchema(fSchema) ) {
  33. // if the validator is also from this Xerces,
  34. // we will use the XNI-based validator for
  35. // better performance
  36. InsulatedValidatorComponent v = ((XercesSchema)fSchema).newXNIValidator();
  37. addComponent(v);
  38. fLastComponent.setDocumentHandler(v.getValidator());
  39. v.getValidator().setDocumentSource(fLastComponent);
  40. fLastComponent = v.getValidator();
  41. v.getValidator().setDocumentHandler(fDocumentHandler);
  42. } else {
  43. // otherwise wrap that into JAXPValidatorComponent.
  44. XMLDocumentFilter validator = null;
  45. ValidatorHandler validatorHandler = fSchema.newValidatorHandler();
  46. validator = new JAXPValidatorComponent(validatorHandler);
  47. addComponent((XMLComponent)validator);
  48. fLastComponent.setDocumentHandler(validator);
  49. validator.setDocumentSource(fLastComponent);
  50. fLastComponent = validator;
  51. validator.setDocumentHandler(fDocumentHandler);
  52. }
  53. }
  54. }
  55. /**
  56. * Checks if the given {@link Schema} speaks XNI.
  57. */
  58. private static boolean isXNICapabaleSchema( Schema s ) {
  59. if(!(s instanceof XercesSchema )) return false;
  60. try {
  61. String v = System.getProperty(JAXPConfiguration.class.getName()+".noSchemaOptimization");
  62. if(v==null)
  63. // there might be a bug in the optimization we do.
  64. // this property provides an escape hatch for such a situation
  65. // by forcing non-optimized way.
  66. return false;
  67. } catch( Throwable t ) {
  68. ;
  69. }
  70. // otherwise if schema derives from XercesSchema,
  71. // we set up better optimized pipeline.
  72. return true;
  73. }
  74. public boolean getFeatureDefaultValue(String featureId){
  75. // reset every component
  76. int count = fComponents.size();
  77. for (int i = 0; i < count; i++) {
  78. XMLComponent c = (XMLComponent) fComponents.get(i);
  79. Boolean bo = c.getFeatureDefault(featureId);
  80. if(bo != null){
  81. return bo.booleanValue();
  82. }
  83. //null if component doesn't recognize this feature.
  84. //continue it might be present in some other components.
  85. //it might make sense to store default values of feature for
  86. //the current configuration that would make the lookup faster.
  87. }
  88. //if it wasn't found in all the components return false;
  89. return false;
  90. }
  91. }