View Javadoc

1   /*
2    * Copyright 1999,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  package org.apache.commons.feedparser;
18  
19  import java.io.FileInputStream;
20  
21  import org.jdom.Element;
22  
23  /**
24   * This FeedParser implementation is based on JDOM and Jaxen and is based around
25   * XPath and JDOM iteration.  While the implementation is straight forward it
26   * has not been optimized for performance.  A SAX based parser would certainly
27   * be less memory intensive but with the downside of being harder to develop.  
28   *
29   * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
30   * @version $Id: Test.java 373614 2006-01-30 22:31:21Z mvdb $
31   */
32  public class Test extends DefaultFeedParserListener
33      implements FeedParserListener,
34                 ModContentFeedParserListener,
35                 XHTMLFeedParserListener {
36  
37      // **** FeedParserListener interface ****************************************
38  
39      /**
40       * Called prior to event parsing to signal the parsing of a new feed.
41       *
42       * 
43       */
44      public void init() {
45          System.out.println( "init()" );
46      }
47  
48      public void setContext( Object context ) {}
49  
50      /**
51       * Called when a channel item is found.
52       *
53       * 
54       */
55      public void onChannel( FeedParserState state,
56                             String title,
57                             String link,
58                             String description ) {
59  
60          System.out.println( "onChannel: " );
61          System.out.println( "\t title: " + title );
62          System.out.println( "\t link: " + link );
63          System.out.println( "\t description: " + description );
64  
65      }
66      public void onChannelEnd() {
67          System.out.println( "onChannelEnd()" );
68      }
69      
70      /**
71       * Called when an RSS image is found.
72       *
73       * 
74       */
75      public void onImage( FeedParserState state,
76                           String title,
77                           String link,
78                           String url ) {
79  
80          System.out.println( "onImage: " );
81          System.out.println( "\t title: " + title );
82          System.out.println( "\t link: " + link );
83          System.out.println( "\t url: " + url );
84  
85      }
86  
87      public void onImageEnd() {
88          System.out.println( "onImageEnd()" );
89      }
90  
91      /**
92       * Called when an RSS item or Atom entry is found. 
93       *
94       * 
95       */
96      public void onItem( FeedParserState state,
97                          String title,
98                          String link,
99                          String description,
100                         String permalink ) {
101 
102         System.out.println( "onItem: " );
103         System.out.println( "\t title: " + title );
104         System.out.println( "\t link: " + link );
105         System.out.println( "\t description: " + description );
106 
107     }
108 
109     public void onItemEnd() {
110 
111         System.out.println( "onItemEnd()" );
112 
113     }
114 
115     public void finished() {
116         System.out.println( "finished()" );
117     }
118 
119     // **** ModContentFeedParserListener interface ******************************
120 
121     public void onContentEncoded( FeedParserState state,
122                                   String value ) {
123 
124         System.out.println( "onContentEncoded: " + value.length() + " bytes" );
125         
126     }
127 
128     public void onContentEncodedEnd() {
129         System.out.println( "onContentEncodedEnd" );
130     }
131 
132     public void onContentItem( FeedParserState state,
133                                String format,
134                                String encoding,
135                                Element value ) {
136 
137         System.out.println( "onContentItem" );
138         
139     }
140 
141     public void onContentItemEnd() {
142         System.out.println( "onContentItemEnd" );
143     }
144 
145     // **** XHTMLBodyFeedParserListener interface *******************************
146 
147     public void onXHTMLBody( FeedParserState state, Element value ) {
148         System.out.println( "onXHTMLBody" );
149     }
150 
151     public void onXHTMLBodyEnd() {
152         System.out.println( "onXHTMLBodyEnd" );
153     }
154 
155     public static void main( String[] args ) {
156 
157         try { 
158             
159             FeedParser parser = FeedParserFactory.newFeedParser();
160 
161             //String path = "/home/burton/.records/rss/index.rss";
162             //String path = "/projects/newsmonster/xml-tests/aaronsw.xml";
163             //String path = "/projects/newsmonster/xml-tests/intertwingly.rss2";
164 
165             String path = "/projects/newsmonster/xml-tests/feedster.xml";
166             
167             parser.parse( new Test(), new FileInputStream( path ), path );
168             
169         } catch ( Throwable t ) {
170             
171             t.printStackTrace();
172             
173         }
174         
175     }
176 
177 }
178