Interface for triggers that react to document-related events. Document triggers may have two roles:
- before the document is stored, updated or removed, the trigger's {@link #prepare(int,DBBroker,Txn,XmldbURI,DocumentImpl) prepare} method is called. The trigger code may take any action desired, for example, to ensure referential integrity on the database, issue XUpdate commands on other documents in the database...
- the trigger also functions as a filter: the trigger interface extends SAX {@link org.xml.sax.ContentHandler content handler} and{@link org.xml.sax.ext.LexicalHandler lexical handler}. It will thus receive any SAX events generated by the SAX parser. The default implementation just forwards the SAX events to the indexer, i.e. the output content handler. However, a trigger may also alter the received SAX events before it forwards them to the indexer, for example, by applying a stylesheet.
The DocumentTrigger interface is also called for binary resources. However, in this case, the trigger can not function as a filter and the SAX-related methods are useless. Only {@link #prepare(int,DBBroker,Txn,XmldbURI,DocumentImpl)} and{@link #finish(int,DBBroker,Txn,XmldbURI,DocumentImpl)} will be called. To determine if the document is a binary resource,call {@link org.exist.dom.DocumentImpl#getResourceType()}. The general contract for a trigger is as follows:
- configuration phase: whenever the collection loads its configuration file, the trigger's {@link #configure(DBBroker,Collection,Map) configure} methodwill be called once.
- pre-parse phase: before parsing the source document, the collection will call the trigger's {@link #prepare(int,DBBroker,Txn,XmldbURI,DocumentImpl) prepare} method once for each document to be stored, removed or updated. The trigger may throw a TriggerException if the current action should be aborted.
- validation phase: during the validation phase, the document is parsed once by the SAX parser. During this phase, the trigger may decide to throw a SAXException to report a problem. Validation will fail and the action is aborted.
- storage phase: the document is again parsed by the SAX parser. The trigger will still receive all SAX events, but it is not allowed to throw an exception. Throwing an exception during the storage phase will result in an invalid document in the database. Use {@link #isValidating() isValidating} in your code to check that you'rein validation phase.
- finalization: the method {@link #finish(int,DBBroker,Txn,XmldbURI,DocumentImpl)} is called. At this point, the documenthas already been stored and is ready to be used or - for {@link #REMOVE_DOCUMENT_EVENT} - has been removed.
@author wolf