Coverage Report - org.apache.commons.feedparser.HTMLFeedParser
 
Classes in this File Line Coverage Branch Coverage Complexity
HTMLFeedParser
0%
0/33
0%
0/2
2.8
HTMLFeedParser$1
0%
0/19
0%
0/8
2.8
 
 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.HashSet;
 20  
 
 21  
 import org.apache.commons.feedparser.impl.DebugFeedParserListener;
 22  
 import org.apache.commons.feedparser.locate.AnchorParser;
 23  
 import org.apache.commons.feedparser.locate.AnchorParserException;
 24  
 import org.apache.commons.feedparser.locate.AnchorParserListener;
 25  
 
 26  
 /**
 27  
  *
 28  
  * Experimental class to play with supporting XFN.  HTML parsing in general is
 29  
  * interesting because I could start with the AnchorParser and move to an HTML
 30  
  * parser but that might be too generic.
 31  
  * 
 32  
  * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
 33  
  * @version $Id: HTMLFeedParser.java 373614 2006-01-30 22:31:21Z mvdb $
 34  
  */
 35  0
 public class HTMLFeedParser extends BaseParser {
 36  
 
 37  0
     public static final HashSet XFN_RELATIONS = new HashSet();
 38  
     
 39  
     public static void parse( String content, final FeedParserListener listener ) throws Exception {
 40  
 
 41  0
         if ( listener instanceof FeedDirectoryParserListener == false )
 42  0
             return;
 43  
 
 44  
         //FIXME: only convert to using XFN if these types of links are detected.
 45  
         //If its just a plain XHTML file then we shouldn't use this interface.
 46  
         //Also FeedVersion needs to be called.
 47  
         
 48  0
         final FeedDirectoryParserListener directoryParserLisener =
 49  
             (FeedDirectoryParserListener)listener;
 50  
 
 51  0
         directoryParserLisener.init();
 52  
         
 53  0
         final FeedParserState state = new FeedParserState();
 54  
         
 55  0
         AnchorParserListener alistener = new AnchorParserListener() {
 56  
 
 57  0
                 public void setContext( Object context ) {}
 58  
 
 59  0
                 public Object getResult() { return null; }
 60  
 
 61  
                 public boolean onAnchor( String href, String rel, String title )
 62  
                     throws AnchorParserException {
 63  
 
 64  
                     try {
 65  
                         
 66  0
                         if ( rel == null || "".equals( rel ) )
 67  0
                             return true;
 68  
 
 69  
                         //right now these aren't valid here
 70  0
                         String description = null;
 71  0
                         String feed = null;
 72  
                     
 73  
                         //FIXME: only include onItem when we have at least ONE XFN
 74  
                         //relations that valid.
 75  
 
 76  0
                         directoryParserLisener.onItem( state, title, href, description, feed );
 77  
                     
 78  0
                         String[] rels = rel.split( " " );
 79  
 
 80  0
                         for ( int i = 0; i < rels.length; ++i ) {
 81  
 
 82  0
                             String current = rels[i];
 83  
 
 84  
                             //FIXME: when this current rel is NOT part of any XFN
 85  
                             //spec we should not be using the feed parser listener
 86  
                             //because it might just be a nofollow link or such.
 87  
 
 88  0
                             boolean isXFriendRel = XFN_RELATIONS.contains( current );
 89  
 
 90  0
                             if ( isXFriendRel ) {
 91  
                         
 92  0
                                 directoryParserLisener.onRelation( state,
 93  
                                                                    current );
 94  
                             
 95  0
                                 directoryParserLisener.onRelationEnd();
 96  
 
 97  
                             }
 98  
                         
 99  
                         }
 100  
 
 101  0
                         directoryParserLisener.onItemEnd();
 102  
                     
 103  
                         //split this into individual rels... then call them.
 104  
                     
 105  0
                         return true;
 106  
 
 107  0
                     } catch ( Exception e ) {
 108  0
                         throw new AnchorParserException( e );
 109  
                     }
 110  
 
 111  
                 }
 112  
 
 113  
             };
 114  
 
 115  0
         AnchorParser.parse( content, alistener );
 116  
 
 117  0
         directoryParserLisener.finished();
 118  
 
 119  0
     }
 120  
 
 121  
     public static void main( String[] args ) throws Exception {
 122  
 
 123  0
         FeedParserListener listener = new DebugFeedParserListener();
 124  
         
 125  0
         parse( "<a href='http://jane-blog.example.org/' rel='sweetheart date met'>Jane</a> ",
 126  
                listener );
 127  
         
 128  0
     }
 129  
 
 130  
     static {
 131  
 
 132  0
         XFN_RELATIONS.add( "contact" );
 133  0
         XFN_RELATIONS.add( "acquaintance" );
 134  0
         XFN_RELATIONS.add( "friend" );
 135  0
         XFN_RELATIONS.add( "met" );
 136  0
         XFN_RELATIONS.add( "co-worker" );
 137  0
         XFN_RELATIONS.add( "colleague" );
 138  0
         XFN_RELATIONS.add( "co-resident" );
 139  0
         XFN_RELATIONS.add( "neighbor" );
 140  0
         XFN_RELATIONS.add( "child" );
 141  0
         XFN_RELATIONS.add( "parent" );
 142  0
         XFN_RELATIONS.add( "sibling" );
 143  0
         XFN_RELATIONS.add( "spouse" );
 144  0
         XFN_RELATIONS.add( "kin" );
 145  0
         XFN_RELATIONS.add( "muse" );
 146  0
         XFN_RELATIONS.add( "crush" );
 147  0
         XFN_RELATIONS.add( "date" );
 148  0
         XFN_RELATIONS.add( "sweetheart" );
 149  0
         XFN_RELATIONS.add( "me" );
 150  
         
 151  0
     }
 152  
     
 153  
 }
 154