1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  /**
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: 438373 $ $Date: 2006-08-30 06:17:21 +0100 (Wed, 30 Aug 2006) $
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  }