View Javadoc

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  public class BaseParser {
33  
34      protected static void doLocale( FeedParserState state,
35                                      FeedParserListener listener,
36                                      Element element ) throws Exception {
37  
38          if ( element == null )
39              return;
40  
41          if ( state.metaFeedParserlistener == null )
42              return;
43  
44          String l = getLocaleString( element );
45          
46          if ( l != null ) {
47  
48              Locale locale = RFC3066LocaleParser.parse( l );
49  
50              if ( locale != null )
51                  state.metaFeedParserlistener.onLocale( state, locale );
52  
53          }
54  
55      }
56  
57      protected static void doLocaleEnd( FeedParserState state,
58                                         FeedParserListener listener,
59                                         Element element )
60          throws Exception {
61  
62          if ( element == null )
63              return;
64  
65          if ( state.metaFeedParserlistener == null )
66              return;
67  
68          String l = getLocaleString( element );
69  
70          if ( l != null ) 
71              state.metaFeedParserlistener.onLocaleEnd();
72  
73      }
74  
75      protected static String getLocaleString( Element element ) {
76  
77          //hm.. crap. how do we get the 'xml' namespace here?
78          Attribute attr = element.getAttribute( "lang" );
79  
80          if ( attr != null )
81              return attr.getValue();
82          
83          //when stil null see that we have dc:language
84  
85          Element lang = element.getChild( "language", NS.DC );
86  
87          if ( lang != null )
88              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          lang = element.getChild( "language" );
93  
94          if ( lang != null )
95              return lang.getText();
96  
97          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         JDOMXPath xpath = new JDOMXPath( query );
107         xpath.setNamespaceContext( NS.context );
108         
109         //perform onChannel method...  (title, link, description)
110         Element e = (Element)xpath.selectSingleNode( element );
111 
112         if ( e == null )
113             return null;
114 
115         String result = e.getText();
116 
117         //The normalize method of XML SHOULD take care of this but for some
118         //reason it doesnt.
119         if ( result != null )
120             result = result.trim();
121 
122         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         JDOMXPath xpath = new JDOMXPath( "descendant::*[local-name() = '" + name + "']" );
137         Object resultNode = xpath.selectSingleNode( state.current );
138 
139         String resultText = null;
140 
141         if ( resultNode != null )
142             resultText = ((Element)resultNode).getText();
143 
144         //The normalize method of XML SHOULD take care of this but for some
145         //reason it doesnt.
146         if ( resultText != null )
147             resultText = resultText.trim();
148 
149         return resultText;
150         
151     }
152 
153 }