1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | |
|
78 | 0 | Attribute attr = element.getAttribute( "lang" ); |
79 | |
|
80 | 0 | if ( attr != null ) |
81 | 0 | return attr.getValue(); |
82 | |
|
83 | |
|
84 | |
|
85 | 0 | Element lang = element.getChild( "language", NS.DC ); |
86 | |
|
87 | 0 | if ( lang != null ) |
88 | 0 | return lang.getText(); |
89 | |
|
90 | |
|
91 | |
|
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 | |
|
102 | |
|
103 | |
|
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 | |
|
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 | |
|
118 | |
|
119 | 0 | if ( result != null ) |
120 | 0 | result = result.trim(); |
121 | |
|
122 | 0 | return result; |
123 | |
|
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected static String getChildElementTextByName( FeedParserState state, |
132 | |
String name ) throws Exception { |
133 | |
|
134 | |
|
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 | |
|
145 | |
|
146 | 0 | if ( resultText != null ) |
147 | 0 | resultText = resultText.trim(); |
148 | |
|
149 | 0 | return resultText; |
150 | |
|
151 | |
} |
152 | |
|
153 | |
} |