Coverage Report - org.apache.commons.feedparser.OPMLFeedParser
 
Classes in this File Line Coverage Branch Coverage Complexity
OPMLFeedParser
0%
0/40
0%
0/12
3.667
 
 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.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.jaxen.jdom.JDOMXPath;
 23  
 import org.jdom.Element;
 24  
 
 25  
 /**
 26  
  * Handles parsing OPML.
 27  
  *
 28  
  * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
 29  
  * @version $Id: OPMLFeedParser.java 373614 2006-01-30 22:31:21Z mvdb $
 30  
  */
 31  0
 public class OPMLFeedParser {
 32  
 
 33  
     /**
 34  
      * Parse this feed.
 35  
      *
 36  
      * 
 37  
      */
 38  
     public static void parse( FeedParserListener listener,
 39  
                               org.jdom.Document doc ) throws FeedParserException {
 40  
 
 41  
         try {
 42  
                 
 43  0
             FeedParserState state = new FeedParserState();
 44  
 
 45  
             //will result in an incorrect interface if the caller isn't using the
 46  
             //system correctly.
 47  
 
 48  0
             FeedVersion v = new FeedVersion();
 49  0
             v.isOPML = true;
 50  0
             listener.onFeedVersion( v );
 51  
 
 52  0
             listener.init();
 53  
 
 54  0
             FeedDirectoryParserListener fdpl = (FeedDirectoryParserListener)listener;
 55  
 
 56  
             //this should be the root directory.
 57  0
             JDOMXPath xpath = new JDOMXPath( "/opml/body/outline" );
 58  0
             List list = xpath.selectNodes( doc );
 59  
 
 60  0
             Iterator i = list.iterator();
 61  0
             while ( i.hasNext() ) {
 62  
 
 63  0
                 Element child = (Element)i.next();
 64  0
                 onOutline( fdpl, state, child );
 65  
                 
 66  0
             }
 67  
             
 68  0
             listener.finished();
 69  
 
 70  0
         } catch ( Throwable t ) { throw new FeedParserException( t ); }
 71  
 
 72  0
     }
 73  
 
 74  
     private static void onOutlineFolder( FeedDirectoryParserListener listener,
 75  
                                          FeedParserState state,
 76  
                                          Element current ) throws Exception {
 77  
 
 78  0
         JDOMXPath xpath = new JDOMXPath( "outline" );
 79  0
         List list = xpath.selectNodes( current );
 80  
 
 81  0
         Iterator i = list.iterator();
 82  0
         while ( i.hasNext() ) {
 83  
 
 84  0
             Element child = (Element)i.next();
 85  0
             onOutline( listener, state, child );
 86  
                 
 87  0
         }
 88  
 
 89  0
     }
 90  
         
 91  
     private static void onOutline( FeedDirectoryParserListener listener,
 92  
                                    FeedParserState state,
 93  
                                    Element current ) throws Exception {
 94  
 
 95  0
         String title = current.getAttributeValue( "title" );
 96  
 
 97  0
         if ( title == null )
 98  0
             title = current.getAttributeValue( "text" );
 99  
 
 100  0
         String weblog = current.getAttributeValue( "htmlUrl" );
 101  0
         String description = current.getAttributeValue( "description" );
 102  0
         String feed = current.getAttributeValue( "xmlUrl" );
 103  
 
 104  0
         if ( weblog == null )
 105  0
             weblog = feed;
 106  
         
 107  0
         if ( feed != null ) {
 108  0
             listener.onItem( state, title, weblog, description, feed );
 109  0
         } else if ( title != null ) {
 110  
             //this is almost certainly a folder
 111  
             
 112  0
             listener.onFolder( state, title );
 113  0
             onOutlineFolder( listener, state, current );
 114  0
             listener.onFolderEnd();
 115  
                     
 116  
         }
 117  
 
 118  0
     }
 119  
 
 120  
 }
 121