com.oxygenxml.validate.nvdl
Class ValidatorImpl

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.oxygenxml.validate.nvdl.ValidatorImpl
All Implemented Interfaces:
Validator, ContentHandler, DTDHandler, EntityResolver, ErrorHandler

 class ValidatorImpl
extends DefaultHandler
implements Validator

Implementation of a validator of XML documents against NVDL scripts.


Nested Class Summary
private static class ValidatorImpl.PrefixMapping
          Namespace context.
private static class ValidatorImpl.Program
          A program is a pair of mode usage and handler.
private  class ValidatorImpl.Section
          Store section information.
 
Field Summary
private  Vector activeHandlersAttributeIndexSets
          Sores the index sets for attributes for each active handler.
private  Hashtable attributeNamespaceIndexSets
          Stores index sets for attributed for each namespace.
private  boolean attributeNamespaceRejected
          Flag indicating if we had a reject action on attributes from this namespace.
private  Hashset attributeSchemas
          Attribute schemas for a namespace.
private  ValidatorImpl.Section currentSection
          Points to the current section.
private  ErrorHandler eh
          The error handler.
private  Stack elementsLocalNameStack
          Stores the element full names.
private  Attributes filteredAttributes
          Ee use this to compute only once the filtered attributes for a namespace, laysily when we will need them for the first time.
private  Localizer localizer
          Message localizer to report error messages from keys.
private  Locator locator
          Source locator.
protected static org.apache.log4j.Logger logger
          Logger
private static String NO_NS
          A value for really no namespace, that is different than any other value for any possible namespace including no namespace which is an empty string.
private  Hashset noResultActions
          keeps the no result actions for a section to avoid duplicating them as the same action can be specified by multiple programs in a section.
private  ValidatorImpl.PrefixMapping prefixMapping
          The current namespace context, points to the last prefix mapping the previous can be found on getParent and so on.
private  PropertyMap properties
          Properties.
private  Mode startMode
          The start mode for this NVDL script.
private  Triggers triggers
          Triggers.
private  Hashtable validatorHandlerCache
          A hashtable that keeps a stack of validators for schemas.
private static String VIRTUAL_ELEMENT_LOCAL_NAME
          The name for the dummy element that we use for attribute section validation.
private static String VIRTUAL_ELEMENT_URI
          The namespace for the virtual element that we use for attribute section validation.
 
Constructor Summary
ValidatorImpl(Mode mode, Triggers triggers, PropertyMap properties)
          Creates a NVDL validator.
 
Method Summary
 void characters(char[] ch, int start, int length)
          characters callback.
private  void cleanupHandler(ContentHandler vh)
          Cleanup a handler.
private  Validator createValidator(Schema schema)
          Get a validator for a schema.
 void endDocument()
          endDocument callback We should be in the initial section now so no op is required.
 void endElement(String uri, String localName, String qName)
          endElement callback
 void endPrefixMapping(String prefix)
          end prefix mapping callback
private  void endSection()
          End a section, its depth reached zero.
private static Attributes filterAttributes(IntSet indexSet, Attributes attributes)
          Get the filtered attributes.
 ContentHandler getContentHandler()
          Get the content handler for this NVDL validator.
 DTDHandler getDTDHandler()
          Get the DTD handler for this NVDL validator.
 void ignorableWhitespace(char[] ch, int start, int length)
          ignorable whitespace callback.
private  void initCurrentSection()
          Initializes the current session.
private  void initHandler(ContentHandler ch)
          Initialize a content handler.
private  boolean processAttributes(Attributes attributes)
          Processes the element attributes.
private  AttributeActionSet processAttributeSection(ModeUsage modeUsage, String ns, IntSet indexSet, Attributes attributes)
          Process an attributes section in a specific mode usage.
private  void releaseValidator(Schema schema, Validator vh)
          Releases a validator for a given schema.
 void reset()
          Reset the NVDL validator so it can be used further on other sources.
 void setDocumentLocator(Locator locator)
          Set document locator callback.
 void startElement(String uri, String localName, String qName, Attributes attributes)
          startElement callback.
 void startPrefixMapping(String prefix, String uri)
          start prefix mapping callback
private  void startSection(String uri)
          Start a new section on a given namespace.
private  void validateAttributes(Schema schema, Attributes attributes)
          Validates a set of attributes with an attribute schema.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
error, fatalError, notationDecl, processingInstruction, resolveEntity, skippedEntity, startDocument, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static org.apache.log4j.Logger logger
Logger


VIRTUAL_ELEMENT_URI

private static final String VIRTUAL_ELEMENT_URI
The namespace for the virtual element that we use for attribute section validation. We the http://purl.oclc.org/dsdl/nvdl/ns/instance/1.0 namespace.

See Also:
Constant Field Values

VIRTUAL_ELEMENT_LOCAL_NAME

private static final String VIRTUAL_ELEMENT_LOCAL_NAME
The name for the dummy element that we use for attribute section validation. We use "virtualElement"

See Also:
Constant Field Values

NO_NS

private static final String NO_NS
A value for really no namespace, that is different than any other value for any possible namespace including no namespace which is an empty string.

See Also:
Constant Field Values

eh

private final ErrorHandler eh
The error handler.


properties

private final PropertyMap properties
Properties.


triggers

private final Triggers triggers
Triggers. Specifies elements that start a new section.


locator

private Locator locator
Source locator.


currentSection

private ValidatorImpl.Section currentSection
Points to the current section.


prefixMapping

private ValidatorImpl.PrefixMapping prefixMapping
The current namespace context, points to the last prefix mapping the previous can be found on getParent and so on.


validatorHandlerCache

private final Hashtable validatorHandlerCache
A hashtable that keeps a stack of validators for schemas.


localizer

private final Localizer localizer
Message localizer to report error messages from keys.


noResultActions

private final Hashset noResultActions
keeps the no result actions for a section to avoid duplicating them as the same action can be specified by multiple programs in a section.


attributeNamespaceIndexSets

private final Hashtable attributeNamespaceIndexSets
Stores index sets for attributed for each namespace.


activeHandlersAttributeIndexSets

private final Vector activeHandlersAttributeIndexSets
Sores the index sets for attributes for each active handler. The index set specifies what attributes should be given to what handlers.


attributeSchemas

private final Hashset attributeSchemas
Attribute schemas for a namespace. It is used to avoid validating twice the set of attributes from a namespace with the same schema.


attributeNamespaceRejected

private boolean attributeNamespaceRejected
Flag indicating if we had a reject action on attributes from this namespace. Useful to avoid reporting the same error multiple times.


filteredAttributes

private Attributes filteredAttributes
Ee use this to compute only once the filtered attributes for a namespace, laysily when we will need them for the first time.


startMode

private final Mode startMode
The start mode for this NVDL script.


elementsLocalNameStack

private final Stack elementsLocalNameStack
Stores the element full names. Used for triggers.

Constructor Detail

ValidatorImpl

ValidatorImpl(Mode mode,
              Triggers triggers,
              PropertyMap properties)
Creates a NVDL validator. The initial mode is specified by the mode parameter. Initializes the current section.

Parameters:
mode - The start mode.
properties - Validation properties.
Method Detail

initCurrentSection

private void initCurrentSection()
Initializes the current session. Creates a section for a dummy namespace (differnet of "", that is no namespace). Adds as child mode usage for this a mode usage with start mode as current mode and that uses start mode. No content handler is set on addChildMode.


setDocumentLocator

public void setDocumentLocator(Locator locator)
Set document locator callback.

Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class DefaultHandler
Parameters:
locator - The document locator.

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws SAXException
characters callback. Dispatch it to all active handlers from the current section.

Specified by:
characters in interface ContentHandler
Overrides:
characters in class DefaultHandler
Throws:
SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws SAXException
ignorable whitespace callback. Dispatch it to all active handlers from the current section.

Specified by:
ignorableWhitespace in interface ContentHandler
Overrides:
ignorableWhitespace in class DefaultHandler
Throws:
SAXException

startElement

public void startElement(String uri,
                         String localName,
                         String qName,
                         Attributes attributes)
                  throws SAXException
startElement callback.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultHandler
Parameters:
uri - The element namespace.
localName - The element local name.
qName - The element qualified name.
attributes - The attributes for this element.
Throws:
SAXException

filterAttributes

private static Attributes filterAttributes(IntSet indexSet,
                                           Attributes attributes)
Get the filtered attributes. It checks if we want all the attributes and in that case returns the initial attributes, otherwise creates a FilteredAttributes instance based on the index set and on the attributes.

Parameters:
indexSet - The set with the indexes of the attributes we want to keep.
attributes - The list of attributes
Returns:
the attributes containing only those whose indexes are in the indexSet.

processAttributes

private boolean processAttributes(Attributes attributes)
                           throws SAXException
Processes the element attributes.

Parameters:
attributes - The element attributes
Returns:
true if we need to filter attributes when we pass them to the active handlers, false if we can just pass the initial attributes to all the active content handlers
Throws:
SAXException

processAttributeSection

private AttributeActionSet processAttributeSection(ModeUsage modeUsage,
                                                   String ns,
                                                   IntSet indexSet,
                                                   Attributes attributes)
                                            throws SAXException
Process an attributes section in a specific mode usage.

Parameters:
modeUsage - The mode usage
ns - The attribute section namespace
indexSet - The indexes of the attributes in the given namespace
attributes - All the attributes
Returns:
The set of attribute actions
Throws:
SAXException

validateAttributes

private void validateAttributes(Schema schema,
                                Attributes attributes)
                         throws SAXException
Validates a set of attributes with an attribute schema.

Parameters:
schema - The attributes schema.
attributes - The attributes to be validated
Throws:
SAXException

startSection

private void startSection(String uri)
                   throws SAXException
Start a new section on a given namespace. Called from startElement when we encounter an element whose namepsace does not match the current section namespace or if we get an element declared as a new section trigger in the NVDL script.

Parameters:
uri - The new namespace.
Throws:
SAXException

initHandler

private void initHandler(ContentHandler ch)
                  throws SAXException
Initialize a content handler. This content handler will receive the document fragment starting at the current element. Therefore we need to set a locator, call startDocument and give the current namespace content to that content handler.

Parameters:
ch - The content handler.
Throws:
SAXException

endElement

public void endElement(String uri,
                       String localName,
                       String qName)
                throws SAXException
endElement callback

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler
Parameters:
uri - The namespace uri
localName - The element local name
qName - The element qualified name
Throws:
SAXException

endSection

private void endSection()
                 throws SAXException
End a section, its depth reached zero.

Throws:
SAXException

cleanupHandler

private void cleanupHandler(ContentHandler vh)
                     throws SAXException
Cleanup a handler. Remove proxy namespace mappings calling endPrefixMapping and calls also endDocument to signal that the source was ended.

Parameters:
vh - The validator content handler to clean up.
Throws:
SAXException

endDocument

public void endDocument()
                 throws SAXException
endDocument callback We should be in the initial section now so no op is required.

Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class DefaultHandler
Throws:
SAXException

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String uri)
                        throws SAXException
start prefix mapping callback

Specified by:
startPrefixMapping in interface ContentHandler
Overrides:
startPrefixMapping in class DefaultHandler
Throws:
SAXException

endPrefixMapping

public void endPrefixMapping(String prefix)
                      throws SAXException
end prefix mapping callback

Specified by:
endPrefixMapping in interface ContentHandler
Overrides:
endPrefixMapping in class DefaultHandler
Throws:
SAXException

createValidator

private Validator createValidator(Schema schema)
Get a validator for a schema. If we already have a validator for this schema available in cache then we will use it and remove it from cache. At the end it will be added back to the cache through releaseValidator.

Parameters:
schema - The schema we need a validaor for.
Returns:
A Validator for the given schema.

releaseValidator

private void releaseValidator(Schema schema,
                              Validator vh)
Releases a validator for a given schema. Put that validator in the cache so that further actions to validate against this schema will be able to use this validator instead of creating a new one.

Parameters:
schema - The schema the validator validates against
vh - The validator.

reset

public void reset()
Reset the NVDL validator so it can be used further on other sources.

Specified by:
reset in interface Validator

getContentHandler

public ContentHandler getContentHandler()
Get the content handler for this NVDL validator.

Specified by:
getContentHandler in interface Validator
Returns:
a ContentHandler, never null
See Also:
Validator.reset()

getDTDHandler

public DTDHandler getDTDHandler()
Get the DTD handler for this NVDL validator.

Specified by:
getDTDHandler in interface Validator
Returns:
a DTDHandler, maybe null if DTD information is not significant to the Validator