Coverage Report - org.apache.commons.feedparser.MetaFeedParser
 
Classes in this File Line Coverage Branch Coverage Complexity
MetaFeedParser
0%
0/80
0%
0/34
4
 
 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.util.Date;
 20  
 
 21  
 import org.apache.commons.feedparser.tools.ISO8601DateParser;
 22  
 import org.apache.commons.feedparser.tools.RFC822DateParser;
 23  
 import org.jdom.Element;
 24  
 import org.jdom.Namespace;
 25  
 
 26  
 /**
 27  
  * Handles parsing RSS metadata including dates
 28  
  *
 29  
  * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
 30  
  * @version $Id: MetaFeedParser.java 373879 2006-01-31 17:23:15Z mvdb $
 31  
  */
 32  0
 public class MetaFeedParser extends BaseParser {
 33  
 
 34  
     /**
 35  
      * 
 36  
      */
 37  
     public static void parse( FeedParserListener listener,
 38  
                               FeedParserState state ) throws FeedParserException {
 39  
 
 40  
         //FIXME: this should be refactored into a new class called
 41  
         //MetaFeedParser to be used by both Atom and RSS.  Also the date
 42  
         //handling below needs to be generic.
 43  
         
 44  0
         if ( listener instanceof MetaFeedParserListener == false )
 45  0
             return;
 46  
 
 47  0
         MetaFeedParserListener mfp = (MetaFeedParserListener)listener;
 48  
 
 49  0
         parseDate( state, mfp );
 50  
         
 51  
         //FIXME: make sure RSS .9 is working and 0.91.  I just need to confirm
 52  
         //but I think they are working correctly
 53  0
         parseGUID( state, mfp );
 54  
 
 55  0
         parseAuthor( state, mfp );
 56  
 
 57  0
         parseComments( state, mfp );
 58  
 
 59  0
         parseCommentsFeed( state, mfp );
 60  
         
 61  0
     }
 62  
 
 63  
     private static void parseComments( FeedParserState state,
 64  
                                        MetaFeedParserListener listener )
 65  
         throws FeedParserException {
 66  
 
 67  0
         Element element = state.current.getChild( "comments" );
 68  
 
 69  0
         if ( element != null ) {
 70  
 
 71  0
             String resource = element.getText();
 72  0
             listener.onComments( state, resource );
 73  0
             listener.onCommentsEnd();
 74  
             
 75  
         }
 76  
         
 77  0
     }
 78  
 
 79  
     private static void parseCommentsFeed( FeedParserState state,
 80  
                                            MetaFeedParserListener listener )
 81  
         throws FeedParserException {
 82  
 
 83  0
         Element element = state.current.getChild( "commentRSS", NS.WFW );
 84  
 
 85  0
         if ( element != null ) {
 86  
 
 87  0
             String resource = element.getText();
 88  0
             listener.onCommentsFeed( state, resource );
 89  0
             listener.onCommentsFeedEnd();
 90  
             
 91  
         }
 92  
 
 93  0
     }
 94  
 
 95  
     private static void parseAuthor( FeedParserState state,
 96  
                                      MetaFeedParserListener listener )
 97  
         throws FeedParserException {
 98  
 
 99  0
         Element element = null;
 100  
 
 101  0
         String name = null;
 102  0
         String email = null;
 103  0
         String resource = null;
 104  
 
 105  
         try {
 106  
             
 107  
             //atom:author
 108  
 
 109  0
             element = state.current.getChild( "author", NS.ATOM );
 110  
 
 111  0
             if ( element != null ) {
 112  0
                 name = selectText( "atom:name", element );
 113  0
                 email = selectText( "atom:email", element );
 114  0
                 resource = selectText( "atom:uri", element );
 115  
             }
 116  
 
 117  
             //dc:creator (RSS 1.0)
 118  0
             element = state.current.getChild( "creator", NS.DC );
 119  
 
 120  0
             if ( element != null )
 121  0
                 name = element.getText();
 122  
                 
 123  
             //author (RSS 2.0)
 124  0
             element = state.current.getChild( "author" );
 125  
 
 126  0
             if ( element != null )
 127  0
                 name = element.getText();
 128  
 
 129  0
             if ( name != null && ! "".equals( name ) ) {
 130  
 
 131  0
                 listener.onAuthor( state,
 132  
                                    name,
 133  
                                    email,
 134  
                                    resource );
 135  
                 
 136  0
                 listener.onAuthorEnd();
 137  
 
 138  
             }
 139  
 
 140  0
         } catch ( Exception e ) {
 141  0
             throw new FeedParserException( e );
 142  0
         }
 143  
 
 144  0
     }
 145  
 
 146  
     private static void parseGUID( FeedParserState state,
 147  
                                    MetaFeedParserListener listener )
 148  
         throws FeedParserException {
 149  
 
 150  0
         Element id = null;
 151  
 
 152  0
         String guid = null;
 153  0
         boolean isPermalink = false;
 154  
         
 155  0
         id = state.current.getChild( "id", NS.ATOM );
 156  
 
 157  0
         if ( id != null ) {
 158  0
             guid = id.getText();
 159  
         }
 160  
 
 161  0
         id = state.current.getChild( "guid" );
 162  
 
 163  0
         if ( id != null ) {
 164  
 
 165  0
             guid = id.getText();
 166  0
             isPermalink = "true".equals( id.getAttributeValue( "isPermalink" ) );
 167  
             
 168  
         }
 169  
 
 170  0
         if ( guid != null ) {
 171  
 
 172  0
             listener.onGUID( state,
 173  
                              guid,
 174  
                              isPermalink );
 175  
             
 176  0
             listener.onGUIDEnd();
 177  
 
 178  
         }
 179  
         
 180  0
     }
 181  
 
 182  
     private static void parseDate( FeedParserState state,
 183  
                                    MetaFeedParserListener listener )
 184  
         throws FeedParserException {
 185  
 
 186  0
         if ( parseDate( "date", NS.DC, listener, state ) )
 187  0
             return;
 188  
 
 189  
         // http://www.mnot.net/drafts/draft-nottingham-atom-format-02.html#rfc.section.4.13.8
 190  
         // 
 191  
         // The "atom:created" element is a Date construct that indicates the
 192  
         // time that the entry was created. atom:entry elements MAY contain
 193  
         // an atom:created element, but MUST NOT contain more than one.
 194  
 
 195  
         // The content of an atom:created element MUST have a time zone
 196  
         // whose value SHOULD be "UTC".
 197  
         
 198  0
         if ( parseDate( "created", NS.ATOM, listener, state ) )
 199  0
             return;
 200  
 
 201  
         // http://www.mnot.net/drafts/draft-nottingham-atom-format-02.html#rfc.section.4.13.8
 202  
         //            
 203  
         // If atom:created is not present, its content MUST considered to be
 204  
         // the same as that of atom:modified.
 205  
 
 206  0
         if ( parseDate( "modified", NS.ATOM, listener, state ) )
 207  0
             return;
 208  
 
 209  
         //support RSS 2.0 and RSS 0.9x dates.
 210  
 
 211  0
         if ( parseDate( "pubDate", null, listener, state, false ) )
 212  0
             return;
 213  
 
 214  0
     }
 215  
     
 216  
     private static boolean parseDate( String name,
 217  
                                       Namespace ns,
 218  
                                       MetaFeedParserListener listener,
 219  
                                       FeedParserState state ) {
 220  
 
 221  0
         return parseDate( name, ns, listener, state, true );
 222  
 
 223  
     }
 224  
     
 225  
     private static boolean parseDate( String name,
 226  
                                       Namespace ns,
 227  
                                       MetaFeedParserListener listener,
 228  
                                       FeedParserState state,
 229  
                                       boolean ISO8601 ) {
 230  
         try { 
 231  
             //ok.  Support dc:date
 232  0
             String v = state.current.getChildText( name, ns );
 233  
     
 234  0
             if ( v != null ) {
 235  
                     
 236  0
                 Date d = null;
 237  0
                 if ( ISO8601 ) {
 238  0
                     d = ISO8601DateParser.parse( v );
 239  
                 } else {
 240  0
                     d = RFC822DateParser.parse( v );
 241  
                 }
 242  
 
 243  0
                 listener.onCreated( state, d );
 244  0
                 listener.onCreatedEnd();
 245  
 
 246  0
                 return true;
 247  
             }
 248  0
         } catch ( Throwable t ) {
 249  
             // ignore the exception, so we can just move on
 250  0
         }
 251  
 
 252  0
         return false;
 253  
 
 254  
     }
 255  
 
 256  
 }