001 /* 002 * Copyright 1999,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.feedparser; 018 019 import java.io.FileInputStream; 020 021 import org.jdom.Element; 022 023 /** 024 * This FeedParser implementation is based on JDOM and Jaxen and is based around 025 * XPath and JDOM iteration. While the implementation is straight forward it 026 * has not been optimized for performance. A SAX based parser would certainly 027 * be less memory intensive but with the downside of being harder to develop. 028 * 029 * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a> 030 * @version $Id: Test.java 373614 2006-01-30 22:31:21Z mvdb $ 031 */ 032 public class Test extends DefaultFeedParserListener 033 implements FeedParserListener, 034 ModContentFeedParserListener, 035 XHTMLFeedParserListener { 036 037 // **** FeedParserListener interface **************************************** 038 039 /** 040 * Called prior to event parsing to signal the parsing of a new feed. 041 * 042 * 043 */ 044 public void init() { 045 System.out.println( "init()" ); 046 } 047 048 public void setContext( Object context ) {} 049 050 /** 051 * Called when a channel item is found. 052 * 053 * 054 */ 055 public void onChannel( FeedParserState state, 056 String title, 057 String link, 058 String description ) { 059 060 System.out.println( "onChannel: " ); 061 System.out.println( "\t title: " + title ); 062 System.out.println( "\t link: " + link ); 063 System.out.println( "\t description: " + description ); 064 065 } 066 public void onChannelEnd() { 067 System.out.println( "onChannelEnd()" ); 068 } 069 070 /** 071 * Called when an RSS image is found. 072 * 073 * 074 */ 075 public void onImage( FeedParserState state, 076 String title, 077 String link, 078 String url ) { 079 080 System.out.println( "onImage: " ); 081 System.out.println( "\t title: " + title ); 082 System.out.println( "\t link: " + link ); 083 System.out.println( "\t url: " + url ); 084 085 } 086 087 public void onImageEnd() { 088 System.out.println( "onImageEnd()" ); 089 } 090 091 /** 092 * Called when an RSS item or Atom entry is found. 093 * 094 * 095 */ 096 public void onItem( FeedParserState state, 097 String title, 098 String link, 099 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