Database Reference
In-Depth Information
without modifications). When the events are forwarded, they are sent to the
vali‐
dator
, which ensures the resultant document is well formed and valid according
to any configured schemas. If you choose to throw a
SAXException
(or any
Runti
meException
) from one of your SAX event handler methods, then you are effec‐
tively indicating to eXist that you consider the document to be invalid, which
stops the validation phase and aborts the store process.
Storage
If the validation phase completes, eXist then enters the storage phase. The entire
document is parsed for a second time, and the generated SAX events are again
sent to your trigger, which is responsible for forwarding them (with or without
modifications). When the events are forwarded, they are sent to the database
storage engine, which is responsible for writing the document into the database.
If you choose to throw a
SAXException
(or any
RuntimeException
) from one of
your SAX event handler methods, you are signaling to eXist that there was a
problem with the store process, and eXist will abort the store and roll back the
transaction.
You can determine which phase your SAX event handler methods are being called in
by calling the function
isValidating
from the
super
FilteringTrigger
class. An
interesting effect of having different validation and storage phases is that you can
modify the document stream in a different manner in each phase. This offers some
interesting possibilities, such as allowing the validation phase to pass, and then
rewriting the document into another form before it is stored.
Consider the
startElement
method from a fictional implementation of
Filtering
Trigger
shown in
Example 16-12
. This example shows you how you can
drop
ele‐
ments,
rename
elements, and
create
new elements as the document is being stored.
Example 16-12. Event handling in a filtertrigger
@Override
public
void
startElement
(
final
String
namespaceURI
,
final
String
localName
,
final
String
qname
,
final
Attributes
attributes
)
throws
SAXException
{
if
(
localName
.
equals
(
"author"
))
{
//drop an element
}
else
if
(
localName
.
equals
(
"color"
))
{
//rename an element
super
.
startElement
(
namespaceURI
,
"colour"
,
qname
.
replace
(
"color"
,
"colour"
),
attributes
);
}
else
if
(
localName
.
equals
(
"isbn"
))
{
//encapsulate an element
super
.
startElement
(
namespaceURI
,
"reference"
,
"reference"
,
null
);
super
.
startElement
(
namespaceURI
,
localName
,
qname
,
attributes
);
Search WWH ::
Custom Search