Author: knoaman
Date: Mon Feb 14 15:33:49 2011
New Revision: 1070524
URL: http://svn.apache.org/viewvc?rev=1070524&view=rev
Log:
Handle duplicate ID values
- XML Schema 1.1 allows an element to have multiple attributes of type ID. More
than one attribute can have the same ID value which is not considered a
duplicate.
- From XML Schema 1.0 (clarified in 1.1), child elements with simple type ID
can have the same value (which identifies the parent element), and these values
are not considered duplicate
Added:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/IDContext.java
(with props)
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XSValidationState.java
(with props)
Modified:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ConfigurableValidationState.java
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ValidationState.java
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLSchemaValidator.java
Modified:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ConfigurableValidationState.java
URL:
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ConfigurableValidationState.java?rev=1070524&r1=1070523&r2=1070524&view=diff
==============================================================================
---
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ConfigurableValidationState.java
(original)
+++
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ConfigurableValidationState.java
Mon Feb 14 15:33:49 2011
@@ -26,12 +26,12 @@ package org.apache.xerces.impl.validatio
* @author Peter McCracken, IBM
* @version $Id$
*/
-public final class ConfigurableValidationState extends ValidationState {
+public class ConfigurableValidationState extends ValidationState {
/**
* Whether to check for ID/IDREF errors
*/
- private boolean fIdIdrefChecking;
+ protected boolean fIdIdrefChecking;
/**
* Whether to check for unparsed entity errors
@@ -54,7 +54,7 @@ public final class ConfigurableValidatio
* @param setting true to turn on error checking,
* false to turn off error checking
*/
- public void setIdIdrefChecking(boolean setting) {
+ public final void setIdIdrefChecking(boolean setting) {
fIdIdrefChecking = setting;
}
@@ -63,7 +63,7 @@ public final class ConfigurableValidatio
* @param setting true to turn on error checking,
* false to turn off error checking
*/
- public void setUnparsedEntityChecking(boolean setting) {
+ public final void setUnparsedEntityChecking(boolean setting) {
fUnparsedEntityChecking = setting;
}
@@ -72,7 +72,7 @@ public final class ConfigurableValidatio
* @return null, if ID/IDREF checking is turned off
* otherwise, returns the value of the super implementation
*/
- public String checkIDRefID() {
+ public final String checkIDRefID() {
return (fIdIdrefChecking) ? super.checkIDRefID() : null;
}
@@ -90,7 +90,7 @@ public final class ConfigurableValidatio
* @return true, if unparsed entity checking is turned off
* otherwise, returns the value of the super implementation
*/
- public boolean isEntityDeclared(String name) {
+ public final boolean isEntityDeclared(String name) {
return (fUnparsedEntityChecking) ? super.isEntityDeclared(name) : true;
}
@@ -99,7 +99,7 @@ public final class ConfigurableValidatio
* @return true, if unparsed entity checking is turned off
* otherwise, returns the value of the super implementation
*/
- public boolean isEntityUnparsed(String name) {
+ public final boolean isEntityUnparsed(String name) {
return (fUnparsedEntityChecking) ? super.isEntityUnparsed(name) : true;
}
@@ -117,7 +117,7 @@ public final class ConfigurableValidatio
* Adds the IDREF, if ID/IDREF checking is enabled.
* @param name the IDREF to add
*/
- public void addIdRef(String name) {
+ public final void addIdRef(String name) {
if (fIdIdrefChecking) {
super.addIdRef(name);
}
Modified:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ValidationState.java
URL:
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ValidationState.java?rev=1070524&r1=1070523&r2=1070524&view=diff
==============================================================================
---
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ValidationState.java
(original)
+++
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/validation/ValidationState.java
Mon Feb 14 15:33:49 2011
@@ -97,13 +97,16 @@ public class ValidationState implements
String key;
while (iter.hasNext()) {
key = (String) iter.next();
- if (!fIdTable.containsKey(key)) {
+ if (!containsID(key)) {
return key;
}
}
return null;
}
+ protected boolean containsID(String name) {
+ return fIdTable.containsKey(name);
+ }
public void reset () {
fExtraChecking = true;
fFacetChecking = true;
Added:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/IDContext.java
URL:
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/IDContext.java?rev=1070524&view=auto
==============================================================================
---
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/IDContext.java
(added)
+++
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/IDContext.java
Mon Feb 14 15:33:49 2011
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xerces.impl.xs;
+
+
+/**
+ * @xerces.internal
+ *
+ * XML Schema 1.1 - ID value handling
+ *
+ * @version $Id$
+ */
+final class IDContext {
+
+ private static final int INITIAL_MATCH_SIZE = 16;
+
+ /** Table that stores the ID depth matched for a given element depth */
+ private int[] fElementIDTable = new int[INITIAL_MATCH_SIZE];
+
+ /** ID Depth */
+ private int fIDDepth = 0;
+
+ /**
+ * Current ID Scope
+ *
+ * Represents the id matched to current element (attribute handling) or to
+ * its parent (simple content handling)
+ */
+ private int fIDScope = 0;
+
+ /** Current Element scope
+ *
+ * Represents the current element depth (attribute handling) or its parent
+ * (simple content handling)
+ */
+ private int fElementScope = -1;
+
+ /** Element depth */
+ private int fElementDepth = 0;
+
+ // ID Mapping Table
+
+ /** Default table size. */
+ final private int fTableSize = 101;
+
+ /** Buckets. */
+ final private Entry[] fBuckets;
+
+ /** Number of elements. */
+ private int fNum = 0;
+
+ IDContext() {
+ fBuckets = new Entry[fTableSize];
+ }
+
+ void clear() {
+ if (fNum > 0) {
+ for (int i=0; i<fTableSize; i++) {
+ fBuckets[i] = null;
+ }
+ }
+
+ fElementDepth = fIDDepth = fNum = 0;
+ fElementScope = fIDScope = -1;
+ }
+
+ void setCurrentScopeToParent() {
+ if (fElementScope > 0) {
+ fIDScope = fElementIDTable[--fElementScope];
+ }
+ else {
+ fElementScope = fIDScope = -1;
+ }
+ }
+
+ void popContext() {
+ --fElementDepth;
+ }
+
+ void pushContext() {
+ if (fElementDepth == fElementIDTable.length) {
+ resizeElementDepthIDTable();
+ }
+ fElementScope = fElementDepth++;
+ fElementIDTable[fElementScope] = fIDScope = 0;
+ }
+
+ private void resizeElementDepthIDTable() {
+ final int newSize = fElementDepth << 1;
+ final int[] newArray = new int[newSize];
+
+ System.arraycopy(fElementIDTable, 0, newArray, 0, fElementDepth);
+ fElementIDTable = newArray;
+ }
+
+ boolean isDeclared(String id) {
+ final int idDepth = get(id);
+ // did not find a match in the table
+ if (idDepth == -1) {
+ // root element with simple content of type ID is invalid
+ return fIDScope == -1;
+ }
+
+ return idDepth != fIDScope;
+ }
+
+ boolean containsID(String name) {
+ int bucket = (name.hashCode() & 0x7FFFFFFF) % fTableSize;
+ for (Entry entry = fBuckets[bucket]; entry != null; entry =
entry.next) {
+ if (name.equals(entry.key))
+ return true;
+ }
+ return false;
+ }
+
+ private int get(String key) {
+ int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize;
+ for (Entry entry = fBuckets[bucket]; entry != null; entry =
entry.next) {
+ if (key.equals(entry.key))
+ return entry.value;
+ }
+ return -1;
+ }
+
+ void add(String id) {
+ final int bucket = (id.hashCode() & 0x7FFFFFFF) % fTableSize;
+ Entry entry = search(id, bucket);
+
+ if (entry == null) {
+ if (fElementIDTable[fElementScope] == 0) {
+ fElementIDTable[fElementScope] = fIDScope = ++fIDDepth;
+ }
+ entry = new Entry(id, fIDScope, fBuckets[bucket]);
+ fBuckets[bucket] = entry;
+ fNum++;
+ }
+ }
+
+ private Entry search(String id, int bucket) {
+ for (Entry entry = fBuckets[bucket]; entry != null; entry =
entry.next) {
+ if (id.equals(entry.key)) {
+ return entry;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * This class is a key table entry. Each entry acts as a node
+ * in a linked list.
+ */
+ private static final class Entry {
+ /** key/value */
+ public String key;
+ public int value;
+
+ /** The next entry. */
+ public Entry next;
+
+ public Entry() {
+ key = null;
+ value = -1;
+ next = null;
+ }
+
+ public Entry(String key, int value, Entry next) {
+ this.key = key;
+ this.value = value;
+ this.next = next;
+ }
+ } // entry
+}
Propchange:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/IDContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/IDContext.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLSchemaValidator.java
URL:
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLSchemaValidator.java?rev=1070524&r1=1070523&r2=1070524&view=diff
==============================================================================
---
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLSchemaValidator.java
(original)
+++
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLSchemaValidator.java
Mon Feb 14 15:33:49 2011
@@ -38,7 +38,6 @@ import org.apache.xerces.impl.dv.Validat
import org.apache.xerces.impl.dv.XSSimpleType;
import org.apache.xerces.impl.dv.xs.TypeValidatorHelper;
import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl;
-import org.apache.xerces.impl.validation.ConfigurableValidationState;
import org.apache.xerces.impl.validation.ValidationManager;
import org.apache.xerces.impl.validation.ValidationState;
import org.apache.xerces.impl.xs.identity.Field;
@@ -405,7 +404,9 @@ public class XMLSchemaValidator
* While parsing a document, keep the location of the document.
*/
private XMLLocator fLocator;
-
+
+ private IDContext fIDContext = null;
+
/**
* A wrapper of the standard error reporter. We'll store all schema errors
* in this wrapper object, so that we can get all errors (error codes) of
@@ -524,7 +525,7 @@ public class XMLSchemaValidator
// updated during reset
protected ValidationManager fValidationManager = null;
- protected ConfigurableValidationState fValidationState = new
ConfigurableValidationState();
+ protected XSValidationState fValidationState = new XSValidationState();
protected XMLGrammarPool fGrammarPool;
// schema location property values
@@ -642,6 +643,15 @@ public class XMLSchemaValidator
fSchemaLoader.setSchemaVersion((String)value);
fSchemaVersion = fSchemaLoader.getSchemaVersion();
fXSConstraints = fSchemaLoader.getXSConstraints();
+ if (fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
+ if (fIDContext == null) {
+ fIDContext = new IDContext();
+ }
+ fValidationState.setIDContext(fIDContext);
+ }
+ else {
+ fValidationState.setIDContext(null);
+ }
}
} // setProperty(String,Object)
@@ -1516,6 +1526,11 @@ public class XMLSchemaValidator
}
fEntityResolver = (XMLEntityResolver)
componentManager.getProperty(ENTITY_MANAGER);
+
+ // reset ID Context
+ if (fIDContext != null) {
+ fIDContext.clear();
+ }
final TypeValidatorHelper typeValidatorHelper =
TypeValidatorHelper.getInstance(fSchemaVersion);
@@ -2369,6 +2384,12 @@ public class XMLSchemaValidator
}
}
+ // Push the ID context
+ // attributes of type ID identifies the current element
+ if (fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
+ fIDContext.pushContext();
+ }
+
processAttributes(element, attributes, attrGrp);
// add default attributes
@@ -2376,6 +2397,12 @@ public class XMLSchemaValidator
addDefaultAttributes(element, attributes, attrGrp);
}
+ // Set ID scope to parent
+ // simple content of an element identifies the parent of the element
+ if (fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
+ fIDContext.setCurrentScopeToParent();
+ }
+
// delegate to 'type alternative' validator subcomponent
if (fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
fTypeAlternativeValidator.handleStartElement(fCurrentElemDecl,
attributes);
@@ -2473,6 +2500,11 @@ public class XMLSchemaValidator
// now validate the content of the element
processElementContent(element);
+
+ // Pop the ID context
+ if (fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
+ fIDContext.popContext();
+ }
if (fIDCChecking) {
// Element Locally Valid (Element)
Added:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XSValidationState.java
URL:
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XSValidationState.java?rev=1070524&view=auto
==============================================================================
---
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XSValidationState.java
(added)
+++
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XSValidationState.java
Mon Feb 14 15:33:49 2011
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xerces.impl.xs;
+
+import org.apache.xerces.impl.validation.ConfigurableValidationState;
+
+/**
+ * @xerces.internal
+ *
+ * @version $Id$
+ */
+final class XSValidationState extends ConfigurableValidationState {
+
+ private IDContext fIDContext;
+
+ public XSValidationState() {
+ super();
+ }
+
+ public void addId(String name) {
+ if (fIDContext == null) {
+ super.addId(name);
+ }
+ else if (fIdIdrefChecking) {
+ fIDContext.add(name);
+ }
+ }
+
+ public boolean isIdDeclared(String name) {
+ if (fIDContext == null) {
+ return super.isIdDeclared(name);
+ }
+ return (fIdIdrefChecking) ? fIDContext.isDeclared(name) : false;
+ }
+
+ void setIDContext(IDContext idContext) {
+ fIDContext = idContext;
+ }
+
+ protected boolean containsID(String name) {
+ return (fIDContext == null) ? super.containsID(name) :
fIDContext.containsID(name);
+ }
+}
Propchange:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XSValidationState.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XSValidationState.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]