Coverage Report - org.apache.commons.feedparser.BaseParser
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseParser
0%
0/46
0%
0/28
5.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.Locale;
 20  
 
 21  
 import org.apache.commons.feedparser.tools.RFC3066LocaleParser;
 22  
 import org.jaxen.jdom.JDOMXPath;
 23  
 import org.jdom.Attribute;
 24  
 import org.jdom.Element;
 25  
 
 26  
 /**
 27  
  * Basic parser support common between RSS, Atom, and FOAF feed impls.
 28  
  * 
 29  
  * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
 30  
  * @version $Id: BaseParser.java 373614 2006-01-30 22:31:21Z mvdb $
 31  
  */
 32  0
 public class BaseParser {
 33  
 
 34  
     protected static void doLocale( FeedParserState state,
 35  
                                     FeedParserListener listener,
 36  
                                     Element element ) throws Exception {
 37  
 
 38  0
         if ( element == null )
 39  0
             return;
 40  
 
 41  0
         if ( state.metaFeedParserlistener == null )
 42  0
             return;
 43  
 
 44  0
         String l = getLocaleString( element );
 45  
         
 46  0
         if ( l != null ) {
 47  
 
 48  0
             Locale locale = RFC3066LocaleParser.parse( l );
 49  
 
 50  0
             if ( locale != null )
 51  0
                 state.metaFeedParserlistener.onLocale( state, locale );
 52  
 
 53  
         }
 54  
 
 55  0
     }
 56  
 
 57  
     protected static void doLocaleEnd( FeedParserState state,
 58  
                                        FeedParserListener listener,
 59  
                                        Element element )
 60  
         throws Exception {
 61  
 
 62  0
         if ( element == null )
 63  0
             return;
 64  
 
 65  0
         if ( state.metaFeedParserlistener == null )
 66  0
             return;
 67  
 
 68  0
         String l = getLocaleString( element );
 69  
 
 70  0
         if ( l != null ) 
 71  0
             state.metaFeedParserlistener.onLocaleEnd();
 72  
 
 73  0
     }
 74  
 
 75  
     protected static String getLocaleString( Element element ) {
 76  
 
 77  
         //hm.. crap. how do we get the 'xml' namespace here?
 78  0
         Attribute attr = element.getAttribute( "lang" );
 79  
 
 80  0
         if ( attr != null )
 81  0
             return attr.getValue();
 82  
         
 83  
         //when stil null see that we have dc:language
 84  
 
 85  0
         Element lang = element.getChild( "language", NS.DC );
 86  
 
 87  0
         if ( lang != null )
 88  0
             return lang.getText();
 89  
 
 90  
         //fall over to just using "language" and if it isn't a local string we
 91  
         //won't parse it.  This is for RSS 0.91 and RSS 2.0 content.
 92  0
         lang = element.getChild( "language" );
 93  
 
 94  0
         if ( lang != null )
 95  0
             return lang.getText();
 96  
 
 97  0
         return null;
 98  
 
 99  
     }
 100  
 
 101  
     // **** shared code for all parsers *****************************************
 102  
 
 103  
     //FIXME: unify this with RSSFeedParser.getChildElementTextByName
 104  
     protected static String selectText( String query, Element element ) throws Exception {
 105  
 
 106  0
         JDOMXPath xpath = new JDOMXPath( query );
 107  0
         xpath.setNamespaceContext( NS.context );
 108  
         
 109  
         //perform onChannel method...  (title, link, description)
 110  0
         Element e = (Element)xpath.selectSingleNode( element );
 111  
 
 112  0
         if ( e == null )
 113  0
             return null;
 114  
 
 115  0
         String result = e.getText();
 116  
 
 117  
         //The normalize method of XML SHOULD take care of this but for some
 118  
         //reason it doesnt.
 119  0
         if ( result != null )
 120  0
             result = result.trim();
 121  
 
 122  0
         return result;
 123  
 
 124  
     }
 125  
 
 126  
     /**
 127  
      * Regardless of namespace, get the child node text by name or null if it is not found.
 128  
      *
 129  
      * 
 130  
      */
 131  
     protected static String getChildElementTextByName( FeedParserState state,
 132  
                                                     String name ) throws Exception {
 133  
 
 134  
         //FIXME: this can be rewritten to use getChild()
 135  
         
 136  0
         JDOMXPath xpath = new JDOMXPath( "descendant::*[local-name() = '" + name + "']" );
 137  0
         Object resultNode = xpath.selectSingleNode( state.current );
 138  
 
 139  0
         String resultText = null;
 140  
 
 141  0
         if ( resultNode != null )
 142  0
             resultText = ((Element)resultNode).getText();
 143  
 
 144  
         //The normalize method of XML SHOULD take care of this but for some
 145  
         //reason it doesnt.
 146  0
         if ( resultText != null )
 147  0
             resultText = resultText.trim();
 148  
 
 149  0
         return resultText;
 150  
         
 151  
     }
 152  
 
 153  
 }