01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.commons.betwixt.examples.rss;
19:
20: import java.io.File;
21: import java.util.Iterator;
22:
23: import org.apache.commons.betwixt.io.BeanReader;
24:
25: /**
26: * <p>Example application using Betwixt to process RSS 0.91.
27: * The intention is to provide illumination and education
28: * rather than providing a .</p>
29: *
30: * @author Robert Burrell Donkin
31: * @version $Revision: 438373 $ $Date: 2006-08-30 06:17:21 +0100 (Wed, 30 Aug 2006) $
32: */
33:
34: public class RSSApplication {
35:
36: /**
37: *
38: */
39: public static void main(String args[]) throws Exception {
40: if (args.length != 1) {
41: System.err.println("Usage: <filename>");
42: System.exit(1);
43: }
44:
45: RSSApplication rssApplication = new RSSApplication();
46: System.out.println(rssApplication.plainTextSummary(args[0]));
47: System.exit(0);
48: }
49:
50: private BeanReader reader = new BeanReader();
51:
52: public RSSApplication() throws Exception {
53: configure();
54: }
55:
56: private void configure() throws Exception {
57: reader.registerBeanClass(Channel.class);
58: }
59:
60: public String plainTextSummary(String filename) throws Exception {
61: return plainTextSummary(new File(filename));
62: }
63:
64: public String plainTextSummary(File file) throws Exception {
65: Channel channel = (Channel) reader.parse(file);
66: return plainTextSummary(channel);
67: }
68:
69: public String plainTextSummary(Channel channel) {
70: StringBuffer buffer = new StringBuffer();
71: buffer.append("channel: ");
72: buffer.append(channel.getTitle());
73: buffer.append('\n');
74: buffer.append("url: ");
75: buffer.append(channel.getLink());
76: buffer.append('\n');
77: buffer.append("copyright: ");
78: buffer.append(channel.getCopyright());
79: buffer.append('\n');
80:
81: for (Iterator it = channel.getItems().iterator(); it.hasNext();) {
82: Item item = (Item) it.next();
83: buffer.append('\n');
84: buffer.append("title: ");
85: buffer.append(item.getTitle());
86: buffer.append('\n');
87: buffer.append("link: ");
88: buffer.append(item.getLink());
89: buffer.append('\n');
90: buffer.append("description: ");
91: buffer.append(item.getDescription());
92: buffer.append('\n');
93: }
94:
95: return buffer.toString();
96: }
97: }
|