KickJava   Java API By Example, From Geeks To Geeks.

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


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: OPMLParser.java,v 1.2 2003/09/29 12:26:00 niko_schmuck Exp $
28

29 package de.nava.informa.parsers;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.Reader JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.Collection JavaDoc;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.jdom.Document;
41 import org.jdom.Element;
42 import org.jdom.JDOMException;
43 import org.jdom.input.SAXBuilder;
44 import org.xml.sax.InputSource JavaDoc;
45
46 import de.nava.informa.core.ParseException;
47 import de.nava.informa.core.UnsupportedFormatException;
48 import de.nava.informa.utils.NoOpEntityResolver;
49
50 /**
51  * OPML (Outline processor markup language) parser for to read in a collection
52  * of news channels (feeds) that will be made available as news channel object
53  * model.</p>
54  *
55  * Currently OPML version 1.1 is supported.
56  *
57  * @author Niko Schmuck
58  * @see de.nava.informa.core.FeedIF
59  */

60 public class OPMLParser {
61
62   private static Log logger = LogFactory.getLog(OPMLParser.class);
63
64   private OPMLParser() {
65   }
66
67   public static Collection JavaDoc parse(URL JavaDoc aURL) throws IOException JavaDoc, ParseException {
68     return parse(new InputSource JavaDoc(aURL.toExternalForm()), aURL);
69   }
70   
71   /**
72    * Reads in a news feed definition from the specified URL.
73    * @return A collection of <code>FeedIF</code> objects.
74    */

75   public static Collection JavaDoc parse(String JavaDoc url) throws IOException JavaDoc, ParseException {
76     URL JavaDoc aURL = null;
77     try {
78       aURL = new URL JavaDoc(url);
79     } catch (java.net.MalformedURLException JavaDoc e) {
80       logger.warn("Could not create URL for " + url);
81     }
82     return parse(new InputSource JavaDoc(url), aURL);
83   }
84   
85   public static Collection JavaDoc parse(Reader JavaDoc reader) throws IOException JavaDoc, ParseException {
86     return parse(new InputSource JavaDoc(reader), null);
87   }
88   
89   public static Collection JavaDoc parse(InputStream JavaDoc stream) throws IOException JavaDoc, ParseException {
90     return parse(new InputSource JavaDoc(stream), null);
91   }
92     
93   public static Collection JavaDoc parse(File JavaDoc aFile) throws IOException JavaDoc, ParseException {
94     URL JavaDoc aURL = null;
95     try {
96       aURL = aFile.toURL();
97     } catch (java.net.MalformedURLException JavaDoc e) {
98       throw new IOException JavaDoc("File " + aFile + " had invalid URL " +
99                             "representation.");
100     }
101     return parse(new InputSource JavaDoc(aURL.toExternalForm()), aURL);
102   }
103
104   public static Collection JavaDoc parse(InputSource JavaDoc inpSource,
105                                 URL JavaDoc baseLocation) throws IOException JavaDoc, ParseException {
106     // document reading without validation
107
SAXBuilder saxBuilder = new SAXBuilder(false);
108     // turn off DTD loading
109
saxBuilder.setEntityResolver(new NoOpEntityResolver());
110     try {
111       Document doc = saxBuilder.build(inpSource);
112       return parse(doc);
113     } catch (JDOMException e) {
114       throw new ParseException(e);
115     }
116   }
117
118   // ------------------------------------------------------------
119
// internal helper methods
120
// ------------------------------------------------------------
121

122   private static synchronized Collection JavaDoc parse(Document doc) throws ParseException {
123     
124     logger.debug("start parsing.");
125     // Get the root element (must be opml)
126
Element root = doc.getRootElement();
127     String JavaDoc rootElement = root.getName().toLowerCase();
128     // Decide which parser to use
129
if (rootElement.startsWith("opml")) {
130       String JavaDoc opmlVersion = root.getAttribute("version").getValue();
131       if (opmlVersion.indexOf("1.1") >= 0) {
132         logger.info("Collection uses OPML root element (Version 1.1).");
133         return OPML_1_1_Parser.parse(root);
134       }
135     }
136
137     // did not match anything
138
throw new UnsupportedFormatException("Unsupported OPML root element [" +
139                                          rootElement + "].");
140   }
141
142 }
143
Popular Tags