01: /*
02: * Created on Feb 9, 2005
03: *
04: */
05: package com.sun.portal.wireless.htmlconversion;
06:
07: import javax.swing.text.MutableAttributeSet;
08: import javax.swing.text.html.HTML.Tag;
09: import javax.swing.text.html.HTMLEditorKit.ParserCallback;
10:
11: import java.util.Enumeration;
12: import java.util.HashMap;
13:
14: /**
15: * The parser callback class, which handles events generated by the HTML parser.
16: *
17: * @author ashwin.mathew@sun.com
18: */
19: public class HtmlParserCallback extends ParserCallback {
20:
21: private GenericHtmlParserCallback callback;
22:
23: /**
24: *
25: */
26: public HtmlParserCallback(GenericHtmlParserCallback callback) {
27: super ();
28:
29: this .callback = callback;
30: }
31:
32: /* (non-Javadoc)
33: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, int)
34: */
35: public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
36: startTag(t, a, pos, false);
37: }
38:
39: /* (non-Javadoc)
40: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleEndTag(javax.swing.text.html.HTML.Tag, int)
41: */
42: public void handleEndTag(Tag t, int pos) {
43: callback.endTag(t.toString());
44: }
45:
46: /* (non-Javadoc)
47: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleSimpleTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, int)
48: */
49: public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
50: startTag(t, a, pos, true);
51: callback.endTag(t.toString());
52: }
53:
54: /* (non-Javadoc)
55: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleText(char[], int)
56: */
57: public void handleText(char[] data, int pos) {
58: callback.handleText(data, 0, data.length);
59: }
60:
61: /* (non-Javadoc)
62: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleError(java.lang.String, int)
63: */
64: public void handleError(String errorMsg, int pos) {
65: }
66:
67: // Encapsulates logic for both handleStartTag() and handleSimpleTag().
68: // Note that the isSimple argument is not being used at this time, but is
69: // being kept as a useful placeholder, in case behaviour needs to change
70: // based on the calling method.
71: private void startTag(Tag t, MutableAttributeSet attributeSet,
72: int pos, boolean isSimple) {
73: if (callback.getState().isBypassTagProcessing()) {
74: return;
75: }
76:
77: HashMap attributes = new HashMap();
78:
79: Enumeration names = attributeSet.getAttributeNames();
80: while (names.hasMoreElements()) {
81: Object nameO = names.nextElement();
82: String name = nameO.toString();
83: String value = attributeSet.getAttribute(nameO).toString()
84: .trim();
85: attributes.put(name, value);
86: }
87:
88: callback.startTag(t.toString(), attributes);
89: }
90:
91: }
|