Author: knoaman
Date: Mon Jul  7 07:46:45 2008
New Revision: 674512

URL: http://svn.apache.org/viewvc?rev=674512&view=rev
Log:
Add support for conditional inclusion

Modified:
    
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/SchemaSymbols.java
    
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/opti/SchemaDOMParser.java
    
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/traversers/XSDHandler.java

Modified: 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/SchemaSymbols.java
URL: 
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/SchemaSymbols.java?rev=674512&r1=674511&r2=674512&view=diff
==============================================================================
--- 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/SchemaSymbols.java
 (original)
+++ 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/SchemaSymbols.java
 Mon Jul  7 07:46:45 2008
@@ -45,6 +45,9 @@
     // schema namespace
     public static final String URI_SCHEMAFORSCHEMA            = 
"http://www.w3.org/2001/XMLSchema".intern();
 
+    // schema version namespace
+    public static final String URI_SCHEMAVERSION = 
"http://www.w3.org/2007/XMLSchema-versioning".intern();
+
     // all possible schema element names
     public static final String ELT_ALL                  = "all".intern();
     public static final String ELT_ANNOTATION           = 
"annotation".intern();
@@ -125,6 +128,8 @@
     public static final String ATT_VERSION              = "version".intern();
     public static final String ATT_XML_LANG             = "xml:lang".intern();
     public static final String ATT_XPATH                = "xpath".intern();
+    public static final String ATT_MINVERSION           = 
"minVersion".intern();
+    public static final String ATT_MAXVERSION           = 
"maxVersion".intern();    
 
     // all possible schema attribute values
     public static final String ATTVAL_TWOPOUNDANY       = "##any";

Modified: 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/opti/SchemaDOMParser.java
URL: 
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/opti/SchemaDOMParser.java?rev=674512&r1=674511&r2=674512&view=diff
==============================================================================
--- 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/opti/SchemaDOMParser.java
 (original)
+++ 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/opti/SchemaDOMParser.java
 Mon Jul  7 07:46:45 2008
@@ -21,6 +21,9 @@
 
 import org.apache.xerces.impl.Constants;
 import org.apache.xerces.impl.XMLErrorReporter;
+import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
+import org.apache.xerces.impl.dv.xs.DecimalDV;
+import org.apache.xerces.impl.dv.xs.TypeValidator;
 import org.apache.xerces.impl.xs.SchemaSymbols;
 import org.apache.xerces.impl.xs.XSMessageFormatter;
 import org.apache.xerces.util.XMLAttributesImpl;
@@ -35,6 +38,7 @@
 import org.apache.xerces.xni.parser.XMLEntityResolver;
 import org.apache.xerces.xni.parser.XMLInputSource;
 import org.apache.xerces.xni.parser.XMLParserConfiguration;
+import org.apache.xerces.xs.datatypes.XSDecimal;
 import org.w3c.dom.Document;
 
 /**
@@ -100,6 +104,12 @@
     private BooleanStack fHasNonSchemaAttributes = new BooleanStack();
     private BooleanStack fSawAnnotation = new BooleanStack();
     private XMLAttributes fEmptyAttr = new XMLAttributesImpl();
+
+    // fields for conditional inclusion
+    private final TypeValidator fDecimalDV = new DecimalDV();
+    private XSDecimal fSupportedVersion;
+    private int fIgnoreDepth = -1;
+    private boolean fPerformConditionalInclusion = true; //REVISIT: use feature
     
     //
     // XMLDocumentHandler methods
@@ -117,6 +127,7 @@
         fAnnotationDepth = -1;
         fInnerAnnotationDepth = -1;
         fDepth = -1;
+        fIgnoreDepth = -1;
         fLocator = locator;
         fNamespaceContext = namespaceContext;
         schemaDOM.setDocumentURI(locator.getExpandedSystemId());
@@ -144,7 +155,7 @@
      *                   Thrown by application to signal an error.
      */
     public void comment(XMLString text, Augmentations augs) throws 
XNIException {
-        if(fAnnotationDepth > -1) {
+        if(fAnnotationDepth > -1 && fIgnoreDepth == -1) {
             schemaDOM.comment(text);
         }
     }
@@ -169,7 +180,7 @@
      */
     public void processingInstruction(String target, XMLString data, 
Augmentations augs)
     throws XNIException {
-        if (fAnnotationDepth > -1) {
+        if (fAnnotationDepth > -1 && fIgnoreDepth == -1) {
             schemaDOM.processingInstruction(target, data);
         }
     }
@@ -184,6 +195,9 @@
      *                   Thrown by handler to signal an error.
      */
     public void characters(XMLString text, Augmentations augs) throws 
XNIException {
+        if (fIgnoreDepth > -1) {
+            return;
+        }
         // when it's not within xs:appinfo or xs:documentation
         if (fInnerAnnotationDepth == -1 ) {
             for (int i=text.offset; i<text.offset+text.length; i++) {
@@ -208,7 +222,6 @@
         else {
             schemaDOM.characters(text);
         }
-        
     }
     
     
@@ -224,8 +237,26 @@
      */
     public void startElement(QName element, XMLAttributes attributes, 
Augmentations augs)
     throws XNIException {
-        
+
         fDepth++;
+
+        // conditional inclusion
+        // We ignore descendants if parent is being ignored (mismatch of 
version supported)
+        if (fPerformConditionalInclusion) {
+            if (fIgnoreDepth > -1) {
+                fIgnoreDepth++;
+                return;
+            }
+
+            // check for version mismatch if any (does not apply to <schema> 
element)
+            if (fDepth > 0) {
+                checkSupportedVersion(element, attributes);
+                if (fIgnoreDepth > -1) {
+                    return;
+                }
+            }
+        }
+
         // while it is true that non-whitespace character data
         // may only occur in appInfo or documentation
         // elements, it's certainly legal for comments and PI's to
@@ -282,7 +313,20 @@
      */
     public void emptyElement(QName element, XMLAttributes attributes, 
Augmentations augs)
     throws XNIException {
-        
+
+        if (fPerformConditionalInclusion) {
+            if (fIgnoreDepth > -1) {
+                return;
+            }
+
+            if (fDepth > -1) {
+                checkSupportedVersion(element, attributes);
+                if (fIgnoreDepth > -1) {
+                    return;
+                }
+            }
+        }
+
         if (fGenerateSyntheticAnnotation && fAnnotationDepth == -1 && 
                 element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && 
element.localpart != SchemaSymbols.ELT_ANNOTATION && 
hasNonSchemaAttributes(element, attributes)) { 
             
@@ -358,37 +402,41 @@
         
         // when we reach the endElement of xs:appinfo or xs:documentation,
         // change fInnerAnnotationDepth to -1
-        if(fAnnotationDepth > -1) {
-            if (fInnerAnnotationDepth == fDepth) {
-                fInnerAnnotationDepth = -1;
-                schemaDOM.endAnnotationElement(element);
-                schemaDOM.endElement();
-            } else if (fAnnotationDepth == fDepth) {
-                fAnnotationDepth = -1;
-                schemaDOM.endAnnotation(element, fCurrentAnnotationElement);
-                schemaDOM.endElement();
-            } else { // inside a child of annotation
-                schemaDOM.endAnnotationElement(element);
-            }
-        } else { // not in an annotation at all
-            if(element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && 
fGenerateSyntheticAnnotation) {
-                boolean value = fHasNonSchemaAttributes.pop();
-                boolean sawann = fSawAnnotation.pop();
-                if (value && !sawann) {
-                    String schemaPrefix = 
fNamespaceContext.getPrefix(SchemaSymbols.URI_SCHEMAFORSCHEMA);
-                    QName annQName = new QName(schemaPrefix, 
SchemaSymbols.ELT_ANNOTATION, schemaPrefix + (schemaPrefix.length() == 
0?"":":") + SchemaSymbols.ELT_ANNOTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
-                    schemaDOM.startAnnotation(annQName, fEmptyAttr, 
fNamespaceContext);
-                    QName elemQName = new QName(schemaPrefix, 
SchemaSymbols.ELT_DOCUMENTATION, schemaPrefix + (schemaPrefix.length() == 
0?"":":") + SchemaSymbols.ELT_DOCUMENTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
-                    schemaDOM.startAnnotationElement(elemQName, fEmptyAttr);
-                    schemaDOM.characters(new 
XMLString("SYNTHETIC_ANNOTATION".toCharArray(), 0, 20 ));     
-                    schemaDOM.endSyntheticAnnotationElement(elemQName, false);
-                    schemaDOM.endSyntheticAnnotationElement(annQName, true);
+        if (fIgnoreDepth == -1) {
+            if(fAnnotationDepth > -1) {
+                if (fInnerAnnotationDepth == fDepth) {
+                    fInnerAnnotationDepth = -1;
+                    schemaDOM.endAnnotationElement(element);
+                    schemaDOM.endElement();
+                } else if (fAnnotationDepth == fDepth) {
+                    fAnnotationDepth = -1;
+                    schemaDOM.endAnnotation(element, 
fCurrentAnnotationElement);
+                    schemaDOM.endElement();
+                } else { // inside a child of annotation
+                    schemaDOM.endAnnotationElement(element);
+                }
+            } else { // not in an annotation at all
+                if(element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && 
fGenerateSyntheticAnnotation) {
+                    boolean value = fHasNonSchemaAttributes.pop();
+                    boolean sawann = fSawAnnotation.pop();
+                    if (value && !sawann) {
+                        String schemaPrefix = 
fNamespaceContext.getPrefix(SchemaSymbols.URI_SCHEMAFORSCHEMA);
+                        QName annQName = new QName(schemaPrefix, 
SchemaSymbols.ELT_ANNOTATION, schemaPrefix + (schemaPrefix.length() == 
0?"":":") + SchemaSymbols.ELT_ANNOTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
+                        schemaDOM.startAnnotation(annQName, fEmptyAttr, 
fNamespaceContext);
+                        QName elemQName = new QName(schemaPrefix, 
SchemaSymbols.ELT_DOCUMENTATION, schemaPrefix + (schemaPrefix.length() == 
0?"":":") + SchemaSymbols.ELT_DOCUMENTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
+                        schemaDOM.startAnnotationElement(elemQName, 
fEmptyAttr);
+                        schemaDOM.characters(new 
XMLString("SYNTHETIC_ANNOTATION".toCharArray(), 0, 20 ));     
+                        schemaDOM.endSyntheticAnnotationElement(elemQName, 
false);
+                        schemaDOM.endSyntheticAnnotationElement(annQName, 
true);
+                    }
                 }
+                schemaDOM.endElement();
             }
-            schemaDOM.endElement();
+        }
+        else {
+            fIgnoreDepth--;
         }
         fDepth--;
-        
     }
     
     /**
@@ -425,7 +473,7 @@
      */
     public void ignorableWhitespace(XMLString text, Augmentations augs) throws 
XNIException {
         // unlikely to be called, but you never know...
-        if (fAnnotationDepth != -1 ) {
+        if (fAnnotationDepth != -1 && fIgnoreDepth == -1) {
             schemaDOM.characters(text);
         }
     }
@@ -440,7 +488,7 @@
      */
     public void startCDATA(Augmentations augs) throws XNIException {
         // only deal with CDATA boundaries within an annotation.
-        if (fAnnotationDepth != -1) {
+        if (fAnnotationDepth != -1 && fIgnoreDepth == -1) {
             schemaDOM.startAnnotationCDATA();
         }
     }
@@ -455,7 +503,7 @@
      */
     public void endCDATA(Augmentations augs) throws XNIException {
         // only deal with CDATA boundaries within an annotation.
-        if (fAnnotationDepth != -1) {
+        if (fAnnotationDepth != -1 && fIgnoreDepth == -1) {
             schemaDOM.endAnnotationCDATA();
         }
     }
@@ -478,7 +526,7 @@
      * @param state
      */
     public void setFeature(String featureId, boolean state){
-       config.setFeature(featureId, state);
+        config.setFeature(featureId, state);
     }
     
     /**
@@ -513,7 +561,7 @@
      * @param er XMLEntityResolver
      */
     public void setEntityResolver(XMLEntityResolver er) {
-       config.setEntityResolver(er);
+        config.setEntityResolver(er);
     }
     
     /**
@@ -530,16 +578,63 @@
      * Reset SchemaParsingConfig
      */
     public void reset() {
-       ((SchemaParsingConfig)config).reset();
+        ((SchemaParsingConfig)config).reset();
     }
     
     /**
      * ResetNodePool on SchemaParsingConfig
      */
     public void resetNodePool() {
-       ((SchemaParsingConfig)config).resetNodePool();
+        ((SchemaParsingConfig)config).resetNodePool();
+    }
+
+    public void setSupportedVersion(XSDecimal version) {
+        fSupportedVersion = version;
     }
     
+    private void checkSupportedVersion(QName element, XMLAttributes 
attributes) {
+        final int length = attributes.getLength();
+        for (int i = 0; i < length; ++i) {
+            String uri = attributes.getURI(i);
+            if (uri != null && uri == SchemaSymbols.URI_SCHEMAVERSION) {
+                String attrLocalName = attributes.getLocalName(i);
+                if (attrLocalName == SchemaSymbols.ATT_MINVERSION) {
+                    try {
+                        XSDecimal minVer = (XSDecimal) 
fDecimalDV.getActualValue(attributes.getValue(i), null);
+                        if (fDecimalDV.compare(minVer, fSupportedVersion) == 
1) {
+                            fIgnoreDepth++;
+                            return;
+                        }
+                    }
+                    catch (InvalidDatatypeValueException ide) {
+                        
fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
+                                "s4s-att-invalid-value",
+                                new Object[] {element.localpart, 
attrLocalName, ide.getMessage()},
+                                XMLErrorReporter.SEVERITY_ERROR);
+                    }
+                }
+                else if (attrLocalName == SchemaSymbols.ATT_MAXVERSION) {
+                    try {
+                        XSDecimal maxVer = (XSDecimal) 
fDecimalDV.getActualValue(attributes.getValue(i), null);
+                        if (fDecimalDV.compare(maxVer, fSupportedVersion) == 
-1) {
+                            fIgnoreDepth++;
+                            return;
+                        }
+                    }
+                    catch (InvalidDatatypeValueException ide) {
+                        
fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
+                                "s4s-att-invalid-value",
+                                new Object[] {element.localpart, 
attrLocalName, ide.getMessage()},
+                                XMLErrorReporter.SEVERITY_ERROR);
+                    }
+                }
+                else { //REVISIT: report error
+
+                }
+            }
+        }
+    }    
+
     /**
      * A simple boolean based stack.
      * 

Modified: 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/traversers/XSDHandler.java
URL: 
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/traversers/XSDHandler.java?rev=674512&r1=674511&r2=674512&view=diff
==============================================================================
--- 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/traversers/XSDHandler.java
 (original)
+++ 
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/traversers/XSDHandler.java
 Mon Jul  7 07:46:45 2008
@@ -27,6 +27,9 @@
 import org.apache.xerces.impl.Constants;
 import org.apache.xerces.impl.XMLEntityManager;
 import org.apache.xerces.impl.XMLErrorReporter;
+import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
+import org.apache.xerces.impl.dv.xs.DecimalDV;
+import org.apache.xerces.impl.dv.xs.TypeValidator;
 import org.apache.xerces.impl.xs.SchemaGrammar;
 import org.apache.xerces.impl.xs.SchemaNamespaceSupport;
 import org.apache.xerces.impl.xs.SchemaSymbols;
@@ -67,6 +70,7 @@
 import org.apache.xerces.xni.parser.XMLInputSource;
 import org.apache.xerces.xs.XSObject;
 import org.apache.xerces.xs.XSParticle;
+import org.apache.xerces.xs.datatypes.XSDecimal;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -247,7 +251,26 @@
     
     // Records which nodes are hidden when the input is a DOMInputSource.
     Hashtable fHiddenNodes = null;
+
+    // Conditional inclustion
+    private static final String XSD_VERSION_1_0 = "1.0";
+    private static final String XSD_VERSION_1_1 = "1.1";
+
+    private static final TypeValidator DECIMAL_DV = new DecimalDV();
+    private static final XSDecimal SUPPORTED_VERSION_1_0 = 
getSupportedVersion(XSD_VERSION_1_0);
+    private static final XSDecimal SUPPORTED_VERSION_1_1 = 
getSupportedVersion(XSD_VERSION_1_1);
+    private XSDecimal fSupportedVersion = SUPPORTED_VERSION_1_0;
     
+    private static XSDecimal getSupportedVersion(String version) {
+        XSDecimal result = null;
+        try {
+            result = (XSDecimal) DECIMAL_DV.getActualValue(version, null);
+        }
+        catch (InvalidDatatypeValueException ide) {
+        }
+        return result;
+    }
+
     // convenience methods
     private String null2EmptyString(String ns) {
         return ns == null ? XMLSymbols.EMPTY_STRING : ns;
@@ -385,6 +408,7 @@
     public XSDHandler(){
         fHiddenNodes = new Hashtable();       
         fSchemaParser = new SchemaDOMParser(new SchemaParsingConfig());
+        fSchemaParser.setSupportedVersion(fSupportedVersion); //REVISIT: pass 
to constructor?
     }
     
     // it should be possible to use the same XSDHandler to parse
@@ -2712,6 +2736,13 @@
      */
     public void setSchemaVersion(short version) {
         fSchemaVersion = version;
+        if (version < Constants.SCHEMA_VERSION_1_1) {
+            fSupportedVersion = SUPPORTED_VERSION_1_0;
+        }
+        else {
+            fSupportedVersion = SUPPORTED_VERSION_1_1;
+        }
+        fSchemaParser.setSupportedVersion(fSupportedVersion);
     }
 
     public short getSchemaVersion() {



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to