001: /*---------------------------------------------------------------------------*\
002: $Id: ParseTest.java 7041 2007-09-09 01:04:47Z bmc $
003: ---------------------------------------------------------------------------
004: This software is released under a BSD-style license:
005:
006: Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions are
010: met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. The end-user documentation included with the redistribution, if any,
016: must include the following acknowlegement:
017:
018: "This product includes software developed by Brian M. Clapper
019: (bmc@clapper.org, http://www.clapper.org/bmc/). That software is
020: copyright (c) 2004-2007 Brian M. Clapper."
021:
022: Alternately, this acknowlegement may appear in the software itself,
023: if wherever such third-party acknowlegements normally appear.
024:
025: 3. Neither the names "clapper.org", "curn", nor any of the names of the
026: project contributors may be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact bmc@clapper.org.
029:
030: 4. Products derived from this software may not be called "curn", nor may
031: "clapper.org" appear in their names without prior written permission
032: of Brian M. Clapper.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
036: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
037: NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
038: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
039: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
040: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
041: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: \*---------------------------------------------------------------------------*/
045:
046: package org.clapper.curn.parser;
047:
048: import org.clapper.util.io.WordWrapWriter;
049: import org.clapper.util.text.TextUtil;
050: import java.io.*;
051: import java.util.*;
052: import java.lang.reflect.*;
053:
054: public class ParseTest {
055: private static WordWrapWriter out = new WordWrapWriter(System.out);
056:
057: private ParseTest() {
058: // Nothing to do
059: }
060:
061: public static void main(String args[]) {
062: try {
063: runTest(args);
064: }
065:
066: catch (Exception ex) {
067: ex.printStackTrace(); // NOPMD
068: System.exit(1);
069: }
070:
071: System.exit(0);
072: }
073:
074: private static void runTest(String args[])
075: throws ClassNotFoundException, NoSuchMethodException,
076: InvocationTargetException, IllegalAccessException,
077: InstantiationException, FileNotFoundException, IOException,
078: RSSParserException {
079: if (args.length < 2) {
080: System.err.println("Usage: java "
081: + ParseTest.class.getName()
082: + " parserClass XMLfile [XMLfile] ...");
083: System.exit(1);
084: }
085:
086: Class<?> parserClass = Class.forName(args[0]);
087: Constructor constructor = parserClass.getConstructor();
088: RSSParser parser = (RSSParser) constructor.newInstance();
089:
090: for (int i = 1; i < args.length; i++) {
091: out.println();
092: out.println(args[i] + ":");
093: out.println();
094:
095: File f = new File(args[i]);
096: FileInputStream is = new FileInputStream(f);
097: RSSChannel channel = parser.parseRSSFeed(f.toURI().toURL(),
098: is, null);
099: if (channel != null)
100: show(channel);
101: }
102: }
103:
104: private static void show(RSSChannel channel) {
105: out.println("Channel title: " + channel.getTitle());
106: out.println("Channel link: " + channel.getURL());
107:
108: Collection<String> authors = channel.getAuthors();
109: String s = null;
110: if (authors != null)
111: s = TextUtil.join(authors, ", ");
112:
113: out.println("Channel author: " + ((s == null) ? "<null>" : s));
114: out.println("RSS version: " + channel.getRSSFormat());
115: out.print("Channel date: ");
116:
117: Date date = channel.getPublicationDate();
118: if (date != null)
119: out.println(date);
120: else
121: out.println("<null>");
122:
123: for (Iterator it = channel.getItems().iterator(); it.hasNext();) {
124: RSSItem item = (RSSItem) it.next();
125:
126: out.println();
127: out.println("Item title: " + item.getTitle());
128:
129: out.print("Item categories: ");
130: Collection<String> categories = item.getCategories();
131: if ((categories == null) || (categories.size() == 0))
132: out.println("<none>");
133: else
134: out.println(TextUtil.join(categories, ", "));
135:
136: out.print("Item author(s): ");
137: authors = item.getAuthors();
138: if ((authors == null) || (authors.size() == 0))
139: out.println("<none>");
140: else
141: out.println(TextUtil.join(authors, ", "));
142:
143: out.println("Item link: " + item.getURL());
144:
145: out.print("Item date: ");
146: date = item.getPublicationDate();
147: s = "<null>";
148: if (date != null)
149: s = date.toString();
150: out.println(s);
151:
152: s = item.getSummary();
153: if (s != null) {
154: out.setPrefix("Item desc: ");
155: out.println(s);
156: out.setPrefix(null);
157: }
158: }
159: }
160: }
|