KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > parsers > OPML_1_1_Parser


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: OPML_1_1_Parser.java,v 1.2 2003/09/25 20:50:06 niko_schmuck Exp $
28

29 package de.nava.informa.parsers;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.jdom.Attribute;
40 import org.jdom.Element;
41
42 import de.nava.informa.core.FeedIF;
43 import de.nava.informa.core.ParseException;
44 import de.nava.informa.impl.basic.Feed;
45 import de.nava.informa.utils.ParserUtils;
46
47 /**
48  * Parser which reads in document according to the OPML 1.1 specification
49  * and generates a collection of <code>FeedIF</code> instances.
50  *
51  * @author Niko Schmuck
52  */

53 class OPML_1_1_Parser {
54
55   private static Log logger = LogFactory.getLog(OPML_1_1_Parser.class);
56
57   static Collection JavaDoc parse(Element root) throws ParseException {
58
59     Collection JavaDoc feedColl = new ArrayList JavaDoc();
60     
61     Date JavaDoc dateParsed = new Date JavaDoc();
62     logger.debug("start parsing.");
63
64     // Get the head element (only one should occur)
65
Element headElem = root.getChild("head");
66     String JavaDoc title = headElem.getChildTextTrim("title");
67     
68     // Get the body element (only one occurs)
69
Element bodyElem = root.getChild("body");
70
71     // 1..n outline elements
72
List JavaDoc feeds = bodyElem.getChildren("outline");
73     Iterator JavaDoc i = feeds.iterator();
74     while (i.hasNext()) {
75       Element feedElem = (Element) i.next();
76       // get title attribute
77
Attribute attrTitle = feedElem.getAttribute("title");
78       String JavaDoc strTitle = "[No Title]";
79       if (attrTitle != null) {
80         strTitle = attrTitle.getValue();
81       }
82       FeedIF feed = new Feed(strTitle);
83       if (logger.isDebugEnabled()) {
84         logger.debug("Feed element found (" + strTitle + ").");
85       }
86       // get text attribute
87
Attribute attrText = feedElem.getAttribute("text");
88       String JavaDoc strText = "[No Text]";
89       if (attrText != null) {
90         strText = attrText.getValue();
91       }
92       feed.setText(strText);
93       // get attribute type (for example: 'rss')
94
Attribute attrType = feedElem.getAttribute("type");
95       String JavaDoc strType = "text/xml";
96       if (attrType != null) {
97         strType = attrType.getValue();
98       }
99       feed.setContentType(strType);
100       
101       // TODO: handle attribute version (for example: 'RSS')
102

103       // get attribute xmlUrl
104
Attribute attrXmlUrl = feedElem.getAttribute("xmlUrl");
105       if (attrXmlUrl != null && attrXmlUrl.getValue() != null) {
106         feed.setLocation(ParserUtils.getURL(attrXmlUrl.getValue()));
107       }
108       // get attribute htmllUrl
109
Attribute attrHtmlUrl = feedElem.getAttribute("htmlUrl");
110       if (attrHtmlUrl != null && attrHtmlUrl.getValue() != null) {
111         feed.setSite(ParserUtils.getURL(attrHtmlUrl.getValue()));
112       }
113       // set current date
114
feed.setDateFound(dateParsed);
115       // add feed to collection
116
feedColl.add(feed);
117     }
118
119     return feedColl;
120   }
121   
122 }
123
Popular Tags