in src/org/apache/xalan/xsltc/trax/DOM2SAX.java [173:315]
private void parse(Node node) throws IOException, SAXException {
Node first = null;
if (node == null) return;
switch (node.getNodeType()) {
case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE
case Node.DOCUMENT_FRAGMENT_NODE:
case Node.DOCUMENT_TYPE_NODE :
case Node.ENTITY_NODE :
case Node.ENTITY_REFERENCE_NODE:
case Node.NOTATION_NODE :
// These node types are ignored!!!
break;
case Node.CDATA_SECTION_NODE:
final String cdata = node.getNodeValue();
if (_lex != null) {
_lex.startCDATA();
_sax.characters(cdata.toCharArray(), 0, cdata.length());
_lex.endCDATA();
}
else {
// in the case where there is no lex handler, we still
// want the text of the cdate to make its way through.
_sax.characters(cdata.toCharArray(), 0, cdata.length());
}
break;
case Node.COMMENT_NODE: // should be handled!!!
if (_lex != null) {
final String value = node.getNodeValue();
_lex.comment(value.toCharArray(), 0, value.length());
}
break;
case Node.DOCUMENT_NODE:
_sax.setDocumentLocator(this);
_sax.startDocument();
Node next = node.getFirstChild();
while (next != null) {
parse(next);
next = next.getNextSibling();
}
_sax.endDocument();
break;
case Node.ELEMENT_NODE:
String prefix;
List pushedPrefixes = new ArrayList();
final AttributesImpl attrs = new AttributesImpl();
final NamedNodeMap map = node.getAttributes();
final int length = map.getLength();
// Process all namespace declarations
for (int i = 0; i < length; i++) {
final Node attr = map.item(i);
final String qnameAttr = attr.getNodeName();
// Ignore everything but NS declarations here
if (qnameAttr.startsWith(XMLNS_PREFIX)) {
final String uriAttr = attr.getNodeValue();
final int colon = qnameAttr.lastIndexOf(':');
prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING;
if (startPrefixMapping(prefix, uriAttr)) {
pushedPrefixes.add(prefix);
}
}
}
// Process all other attributes
for (int i = 0; i < length; i++) {
final Node attr = map.item(i);
final String qnameAttr = attr.getNodeName();
// Ignore NS declarations here
if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
final String uriAttr = attr.getNamespaceURI();
final String localNameAttr = getLocalName(attr);
// Uri may be implicitly declared
if (uriAttr != null) {
final int colon = qnameAttr.lastIndexOf(':');
prefix = (colon > 0) ? qnameAttr.substring(0, colon) : EMPTYSTRING;
if (startPrefixMapping(prefix, uriAttr)) {
pushedPrefixes.add(prefix);
}
}
// Add attribute to list
attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr),
qnameAttr, "CDATA", attr.getNodeValue());
}
}
// Now process the element itself
final String qname = node.getNodeName();
final String uri = node.getNamespaceURI();
final String localName = getLocalName(node);
// Uri may be implicitly declared
if (uri != null) {
final int colon = qname.lastIndexOf(':');
prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
if (startPrefixMapping(prefix, uri)) {
pushedPrefixes.add(prefix);
}
}
// Generate SAX event to start element
if (_saxImpl != null) {
_saxImpl.startElement(uri, localName, qname, attrs, node);
}
else {
_sax.startElement(uri, localName, qname, attrs);
}
// Traverse all child nodes of the element (if any)
next = node.getFirstChild();
while (next != null) {
parse(next);
next = next.getNextSibling();
}
// Generate SAX event to close element
_sax.endElement(uri, localName, qname);
// Generate endPrefixMapping() for all pushed prefixes
final int nPushedPrefixes = pushedPrefixes.size();
for (int i = 0; i < nPushedPrefixes; i++) {
endPrefixMapping((String) pushedPrefixes.get(i));
}
break;
case Node.PROCESSING_INSTRUCTION_NODE:
_sax.processingInstruction(node.getNodeName(),
node.getNodeValue());
break;
case Node.TEXT_NODE:
final String data = node.getNodeValue();
_sax.characters(data.toCharArray(), 0, data.length());
break;
}
}