001: package org.methodize.nntprss.rss.parser;
002:
003: /* -----------------------------------------------------------
004: * nntp//rss - a bridge between the RSS world and NNTP clients
005: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
006: *
007: * email: nntprss@methodize.org
008: * mail: Methodize Solutions
009: * PO Box 3865
010: * Grand Central Station
011: * New York NY 10163
012: *
013: * This file is part of nntp//rss
014: *
015: * nntp//rss is free software; you can redistribute it
016: * and/or modify it under the terms of the GNU General
017: * Public License as published by the Free Software Foundation;
018: * either version 2 of the License, or (at your option) any
019: * later version.
020: *
021: * This program is distributed in the hope that it will be
022: * useful, but WITHOUT ANY WARRANTY; without even the implied
023: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
024: * PURPOSE. See the GNU General Public License for more
025: * details.
026: *
027: * You should have received a copy of the GNU General Public
028: * License along with this program; if not, write to the
029: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
030: * Boston, MA 02111-1307 USA
031: * ----------------------------------------------------- */
032:
033: import javax.swing.text.MutableAttributeSet;
034: import javax.swing.text.html.HTML;
035: import javax.swing.text.html.HTMLEditorKit;
036: import javax.swing.text.html.HTML.Tag;
037:
038: import org.methodize.nntprss.rss.Item;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.Text;
042:
043: /**
044: * @author Jason Brome <jason@methodize.org>
045: * @version $Id: ParserCallback.java,v 1.1 2003/03/22 16:32:02 jasonbrome Exp $
046: */
047: public class ParserCallback extends HTMLEditorKit.ParserCallback {
048:
049: private boolean inChannel = false;
050: private boolean inItem = false;
051: private String currentText = null;
052: private Item currentItem = null;
053: private Element currentItemElm = null;
054:
055: private Element rootElm = null;
056: private Element channelElm = null;
057: private Document doc = null;
058:
059: private ParserCallback() {
060: }
061:
062: public ParserCallback(Document doc) {
063: this .doc = doc;
064: this .rootElm = doc.getDocumentElement();
065: this .channelElm = (Element) rootElm.getElementsByTagName(
066: "channel").item(0);
067: }
068:
069: /**
070: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleComment(char[], int)
071: */
072: public void handleComment(char[] data, int pos) {
073: // System.out.println("Comment: "
074: // + new String(data));
075: }
076:
077: /**
078: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleEndOfLineString(String)
079: */
080: public void handleEndOfLineString(String eol) {
081: // System.out.println("EOL: " + eol.toString());
082: }
083:
084: private void createTextChild(Element parent, String element,
085: String value) {
086:
087: Element elm = doc.createElement(element);
088: Text elmTextElm = doc.createTextNode(value);
089: elm.appendChild(elmTextElm);
090: parent.appendChild(elm);
091: }
092:
093: /**
094: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleEndTag(Tag, int)
095: */
096: public void handleEndTag(Tag t, int pos) {
097: // System.out.println("EndTag: " + t.toString());
098:
099: String tagName = t.toString();
100: if (tagName.equals("item")) {
101: inItem = false;
102: if (currentItemElm != null) {
103: rootElm.appendChild(currentItemElm);
104: currentItemElm = null;
105: }
106: } else if (tagName.equals("channel")) {
107: inChannel = false;
108: } else {
109: if (inItem) {
110: if (currentItemElm == null) {
111: currentItemElm = doc.createElement("item");
112: }
113:
114: if (tagName.equals("title")) {
115: createTextChild(currentItemElm, "title",
116: currentText);
117: } else if (tagName.equals("link")) {
118: createTextChild(currentItemElm, "link", currentText);
119: } else if (tagName.equals("description")) {
120: createTextChild(currentItemElm, "description",
121: currentText);
122: } else if (tagName.equals("comments")) {
123: createTextChild(currentItemElm, "comments",
124: currentText);
125: }
126:
127: currentText = null;
128: } else if (inChannel) {
129: if (tagName.equals("title")) {
130: createTextChild(channelElm, "title", currentText);
131: } else if (tagName.equals("link")) {
132: createTextChild(channelElm, "link", currentText);
133: } else if (tagName.equals("description")) {
134: createTextChild(channelElm, "description",
135: currentText);
136: } else if (tagName.equals("managingEditor")) {
137: createTextChild(channelElm, "managingEditor",
138: currentText);
139: }
140:
141: }
142: }
143:
144: }
145:
146: /**
147: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleError(String, int)
148: */
149: public void handleError(String errorMsg, int pos) {
150: // System.out.println("Error: " + errorMsg.toString());
151: }
152:
153: /**
154: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleSimpleTag(Tag, MutableAttributeSet, int)
155: */
156: public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
157: // System.out.println("SimpleTag: " + t.toString());
158: }
159:
160: /**
161: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleStartTag(Tag, MutableAttributeSet, int)
162: */
163: public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
164: // System.out.println("Tag: " + t.toString());
165: // System.out.println("Atts List: " + a.toString());
166:
167: String tagName = t.toString();
168: if (tagName.equals("item")) {
169: inItem = true;
170: } else if (tagName.equals("channel")) {
171: inChannel = true;
172: } else if (tagName.equals("rss")) {
173: String version = (String) a
174: .getAttribute(HTML.Attribute.VERSION);
175: rootElm.setAttribute("version", version);
176: } else if (tagName.equals("rdf")) {
177: rootElm.setAttribute("version", "RDF");
178: }
179: }
180:
181: /**
182: * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleText(char[], int)
183: */
184: public void handleText(char[] data, int pos) {
185: currentText = new String(data);
186: // System.out.println("Text: "
187: // + currentText);
188: }
189:
190: }
|