Hi there,
I am just new to this mailing list, sorry if this question has been asked
before (I could not find the archives of this list at archive.covalent.net).
I am using the SAX 2 parser part of Xerces-J 1.3.0, so the call-back methods
part, and have two questions.
I would like to be SAX 2 compliant, but also want to detect and process
default attributes. I could not find that in SAX 2, but maybe I overlooked
something? What I currently do is register an extra SAX 1 compliant handler
specifically for the default attributes, like so:
org.xml.sax.XMLReader parser = getParser(validate, namespaces);
parser.setContentHandler(docHandler);
// DEFAULT ATTR FIX
((Parser) parser).setDocumentHandler(
new DefaultAttributeHandler(docHandler));
The docHandler handles all the content in the SAX 2 way, the
DefaultAttributeHandler only looks out for default attributes.
private class DefaultAttributeHandler extends HandlerBase {
...(some declarations)
public void startElement(java.lang.String name,
AttributeList attributes) throws SAXException {
if (attributes instanceof org.apache.xerces.framework.XMLAttrList) {
org.apache.xerces.framework.XMLAttrList attrs =
(org.apache.xerces.framework.XMLAttrList) attributes;
// determine the unspecified (default) attributes
unSpecified.clear();
for (int i = 0; i < attrs.getLength() ; i++) {
if (! attrs.isSpecified(i)) {
unSpecified.addElement(attrs.getName(i));
}
}
docHandler.setDefaultAttributeNames(unSpecified);
}
}
}
You see, I looked at the Xerces sources and found out that the SAX 1
AttributeList parameter is actually a xerces XMLAttrList (unlike the SAX 2
Attributes), which has an isSpecified method. I can then use the callback
method to inform the actual contenthandler of this. Needless to say, I do
not like my solution (it brings me back to the deprecated SAX 1 api, and if
the xerces SAXParser.startElement implementation would e.g. change the order
in which the fDocumentHandler and fContentHandler are called it would stop
working).
So, could anybody please tell me an easier way to detect default attributes?
While I'm asking I have a second question: Is there any way to detect the
internal subset while parsing (so when the document both refers to a
external DTD and has some internal declarations, it is all passed as one big
DTD through the SAX 2 callback methods, unless, again, I am mistaken)?
Thanks ever so much in advance for your possible answers,
--Sander.