KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > parsers > JAXPConfiguration


1 package com.sun.org.apache.xerces.internal.parsers;
2
3 import javax.xml.validation.Schema JavaDoc;
4 import javax.xml.validation.ValidatorHandler JavaDoc;
5
6 import com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent;
7 import com.sun.org.apache.xerces.internal.jaxp.validation.InsulatedValidatorComponent;
8 import com.sun.org.apache.xerces.internal.jaxp.validation.XercesSchema;
9 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
10 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter;
11 /**
12  * {@link com.sun.org.apache.xerces.internal.xni.parser.XMLParseException} that
13  * includes a JAXP {@link ValidatorHandler} in the middle.
14  *
15  * @author
16  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
17  * Venu Gopal (k.venugopal@sun.com)
18  */

19 public class JAXPConfiguration extends XIncludeParserConfiguration {
20     
21     /** can be null. */
22     private final Schema JavaDoc fSchema;
23     
24     /**
25      *
26      * @param grammar
27      * when non-null, the parser will include validation /
28      * infoset augmentation by this {@link Schema}.
29      */

30     public JAXPConfiguration(Schema JavaDoc schema){
31         this.fSchema = schema;
32     }
33     
34     protected void configurePipeline() {
35         super.configurePipeline();
36         
37         if (fSchema != null) {
38             if( isXNICapabaleSchema(fSchema) ) {
39                 // if the validator is also from this Xerces,
40
// we will use the XNI-based validator for
41
// better performance
42
InsulatedValidatorComponent v = ((XercesSchema)fSchema).newXNIValidator();
43                 addComponent(v);
44                 
45                 fLastComponent.setDocumentHandler(v.getValidator());
46                 v.getValidator().setDocumentSource(fLastComponent);
47                 fLastComponent = v.getValidator();
48                 v.getValidator().setDocumentHandler(fDocumentHandler);
49             } else {
50                 // otherwise wrap that into JAXPValidatorComponent.
51
XMLDocumentFilter validator = null;
52                 ValidatorHandler JavaDoc validatorHandler = fSchema.newValidatorHandler();
53             
54                 validator = new JAXPValidatorComponent(validatorHandler);
55                 addComponent((XMLComponent)validator);
56                 
57                 fLastComponent.setDocumentHandler(validator);
58                 validator.setDocumentSource(fLastComponent);
59                 fLastComponent = validator;
60                 validator.setDocumentHandler(fDocumentHandler);
61             }
62         }
63     }
64     
65     /**
66      * Checks if the given {@link Schema} speaks XNI.
67      */

68     private static boolean isXNICapabaleSchema( Schema JavaDoc s ) {
69         if(!(s instanceof XercesSchema )) return false;
70         
71         try {
72             String JavaDoc v = System.getProperty(JAXPConfiguration.class.getName()+".noSchemaOptimization");
73             if(v==null)
74                 // there might be a bug in the optimization we do.
75
// this property provides an escape hatch for such a situation
76
// by forcing non-optimized way.
77
return false;
78         } catch( Throwable JavaDoc t ) {
79             ;
80         }
81         
82         // otherwise if schema derives from XercesSchema,
83
// we set up better optimized pipeline.
84
return true;
85     }
86     
87     public boolean getFeatureDefaultValue(String JavaDoc featureId){
88
89         // reset every component
90
int count = fComponents.size();
91         for (int i = 0; i < count; i++) {
92             XMLComponent c = (XMLComponent) fComponents.get(i);
93             Boolean JavaDoc bo = c.getFeatureDefault(featureId);
94             if(bo != null){
95                 return bo.booleanValue();
96             }
97             //null if component doesn't recognize this feature.
98
//continue it might be present in some other components.
99
//it might make sense to store default values of feature for
100
//the current configuration that would make the lookup faster.
101

102         }
103         //if it wasn't found in all the components return false;
104
return false;
105     }
106     
107 }
108
109
Popular Tags