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