org.merlotxml.util.xml.xerces
Class SchemaIdentityConstraintValidator

java.lang.Object
  extended byorg.merlotxml.util.xml.xerces.SchemaIdentityConstraintValidator

public class SchemaIdentityConstraintValidator
extends java.lang.Object

Validates Identity Constraints - Unique, Key and KeyRef - for XML Schema. Xerces validates these during initial parsing, but not dynamically. In other words, if a value changes that causes a duplicate Key Xerces won't pick this up.

The validation that Xerces does during parsing can be repeated by calling Document.normalizeDocument(), but this takes too much time, because the entire document is revalidated. Errors could be caught and linked to nodes using an org.w3c.dom.DOMErrorHandler. This approach works, but is too slow to do with each change in value.

This class implements the same functionality that Xerces uses during parsing, but in a dynamic way.

See org.apache.xerces.impl.xs.XMLSchemaValidator.ValueStoreCache

This class is instanciated from parseValidXMLStream

Author:
Evert Hoff

Nested Class Summary
 class SchemaIdentityConstraintValidator.DynamicValueStore
          Similar to org.apache.xerces.impl.xs.XMLSchemaValidator.ValueStoreCache except that the Xerces ValueStores are not dynamic - they were intended to validate only once during parsing.
 
Field Summary
protected  int debugLevel
          Utility to minimise the output during debugging.
 
Constructor Summary
SchemaIdentityConstraintValidator(org.w3c.dom.Document document, SchemaGrammarDocumentImpl grammarDocument)
          Create and initialize this validator with the current value of each field in the document.
 
Method Summary
protected  void debug(int level, java.lang.String msg)
          Utility for debugging.
 java.lang.String getErrorMessage(FieldNode node)
          Retrieves the current error message associated with this FieldNode.
 FieldNode[] getIdentityConstraintFieldNodePeers(FieldNode node)
          Finds the FieldNodes for the other fields that form part of the same Key, KeyRef or Unique as this one.
 boolean getIsKeyRefField(GrammarSimpleType simpleType)
          Used to determine is the field represented by this simpleType is of type KeyRef.
 FieldNode[][] getPossibleReferenceFieldNodes(FieldNode keyRefNode)
          Gets the valid possible key nodes that this keyref node may refer to.
 org.w3c.dom.Element getSelectorElement(FieldNode fieldNode)
          Follows the reverse of the Field's XPath to get back from the Field to the Selector element that served as the context element for the Field's XPath statement.
 void setValue(FieldNode node, java.lang.String value)
          Updates the value of this FieldNode in the value store and checks for duplicates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debugLevel

protected int debugLevel
Utility to minimise the output during debugging.

Constructor Detail

SchemaIdentityConstraintValidator

public SchemaIdentityConstraintValidator(org.w3c.dom.Document document,
                                         SchemaGrammarDocumentImpl grammarDocument)
Create and initialize this validator with the current value of each field in the document.

Method Detail

debug

protected void debug(int level,
                     java.lang.String msg)
Utility for debugging.

Parameters:
level - 0=no debugging, 1 little bit, 2 more, etc.
msg - The message to print.

setValue

public void setValue(FieldNode node,
                     java.lang.String value)
Updates the value of this FieldNode in the value store and checks for duplicates. If a duplicate or other error is encountered, the error is stored for this FieldNode so that the next call to getErrorMessage will be able to retrieve the error.

Parameters:
node - The field for which the value is being set.
value - The new value.

getErrorMessage

public java.lang.String getErrorMessage(FieldNode node)
Retrieves the current error message associated with this FieldNode.

Parameters:
node -
Returns:
String

getIdentityConstraintFieldNodePeers

public FieldNode[] getIdentityConstraintFieldNodePeers(FieldNode node)
Finds the FieldNodes for the other fields that form part of the same Key, KeyRef or Unique as this one.

Parameters:
node - One of the fields that is part of the key.
Returns:
FieldNode[] All the fields that are part of this key.

getSelectorElement

public org.w3c.dom.Element getSelectorElement(FieldNode fieldNode)
Follows the reverse of the Field's XPath to get back from the Field to the Selector element that served as the context element for the Field's XPath statement.

Parameters:
fieldNode - The field for which the selector is sought.

getPossibleReferenceFieldNodes

public FieldNode[][] getPossibleReferenceFieldNodes(FieldNode keyRefNode)
Gets the valid possible key nodes that this keyref node may refer to. But, it also returns the possible nodes that the peers to this field node may refer to.

Parameters:
keyRefNode - A field in the key.
Returns:
FieldNode[possibility][field] The values for all fields for each option.

getIsKeyRefField

public boolean getIsKeyRefField(GrammarSimpleType simpleType)
Used to determine is the field represented by this simpleType is of type KeyRef. Sometimes, the simple type will be of type IDREF, but this is not guaranteed, because this is just kept in Schema for backward compatibility. So, a different way of determining if a simple type is a KeyRef is required.

Parameters:
simpleType -
Returns:
boolean