01: /*
02: * Created on Mar 7, 2005
03: */
04: package com.sun.portal.wireless.htmlconversion;
05:
06: import java.util.HashMap;
07:
08: import org.xml.sax.Attributes;
09: import org.xml.sax.InputSource;
10: import org.xml.sax.SAXException;
11: import org.xml.sax.SAXParseException;
12: import org.xml.sax.helpers.DefaultHandler;
13:
14: /**
15: * SAX handler for XHTML documents.
16: *
17: * @author ashwin.mathew@sun.com
18: */
19: public class XhtmlParserCallback extends DefaultHandler {
20:
21: private GenericHtmlParserCallback callback;
22:
23: public XhtmlParserCallback(GenericHtmlParserCallback callback) {
24: this .callback = callback;
25: }
26:
27: /* (non-Javadoc)
28: * @see org.xml.sax.ContentHandler#characters(char[], int, int)
29: */
30: public void characters(char[] text, int start, int length)
31: throws SAXException {
32: callback.handleText(text, start, length);
33: }
34:
35: /* (non-Javadoc)
36: * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
37: */
38: public void endElement(String uri, String localName, String qName)
39: throws SAXException {
40: callback.endTag(qName);
41: }
42:
43: /* (non-Javadoc)
44: * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
45: */
46: public void error(SAXParseException exception) throws SAXException {
47: exception.printStackTrace();
48: throw exception;
49: }
50:
51: /* (non-Javadoc)
52: * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
53: */
54: public void startElement(String uri, String localName,
55: String qName, Attributes attributes) throws SAXException {
56: if (callback.getState().isBypassTagProcessing()) {
57: return;
58: }
59:
60: HashMap attrs = new HashMap();
61: for (int i = 0; i < attributes.getLength(); i++) {
62: String aqName = attributes.getQName(i);
63: String auri = attributes.getURI(i);
64: String alocalName = attributes.getLocalName(i);
65:
66: String value = attributes.getValue(i).trim();
67:
68: // Set key, value on HashMap
69: attrs.put(alocalName, value);
70: }
71:
72: callback.startTag(localName, attrs);
73: }
74:
75: /* (non-Javadoc)
76: * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
77: */
78: public void warning(SAXParseException exception)
79: throws SAXException {
80: exception.printStackTrace();
81: }
82:
83: public InputSource resolveEntity(String publicId, String systemId)
84: throws SAXException {
85: InputSource source = null;
86:
87: try {
88: source = super .resolveEntity(publicId, systemId);
89: } catch (Exception ex) {
90: // In case the entity cannot be resolved (e.g., XHTML DTD cannot
91: // be referenced), parsing should continue.
92: source = null;
93: }
94:
95: return source;
96: }
97:
98: }
|