View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
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 com.sun.syndication.io.impl;
18  
19  import com.sun.syndication.feed.rss.Description;
20  import com.sun.syndication.feed.rss.Item;
21  import org.jdom.Document;
22  import org.jdom.Element;
23  import org.jdom.Namespace;
24  
25  import java.util.List;
26  
27  /***
28   */
29  public class RSS10Parser extends RSS090Parser {
30  
31      private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
32      private static final String RSS_URI = "http://purl.org/rss/1.0/";
33  
34      public RSS10Parser() {
35          this("rss_1.0");
36      }
37  
38      protected RSS10Parser(String type) {
39          super(type);
40      }
41  
42      /***
43       * Indicates if a JDom document is an RSS instance that can be parsed with the parser.
44       * <p/>
45       * It checks for RDF ("http://www.w3.org/1999/02/22-rdf-syntax-ns#") and
46       * RSS ("http://purl.org/rss/1.0/") namespaces being defined in the root element.
47       *
48       * @param document document to check if it can be parsed with this parser implementation.
49       * @return <b>true</b> if the document is RSS1., <b>false</b> otherwise.
50       */
51      public boolean isMyType(Document document) {
52          boolean ok = false;
53          Namespace rdfNS = Namespace.getNamespace(RDF_URI);
54          Namespace rssNS = Namespace.getNamespace(RSS_URI);
55  
56          Element rssRoot = document.getRootElement();
57          Namespace defaultNS = rssRoot.getNamespace();
58          List additionalNSs = rssRoot.getAdditionalNamespaces();
59  
60          ok = defaultNS!=null && defaultNS.equals(rdfNS);
61          if (ok) {
62              if (additionalNSs==null) {
63                  ok = false;
64              }
65              else {
66                  ok = false;
67                  for (int i=0;!ok && i<additionalNSs.size();i++) {
68                      ok = rssNS.equals(additionalNSs.get(i));
69                  }
70              }
71          }
72          return ok;
73      }
74  
75      /***
76       * Returns the namespace used by RSS elements in document of the RSS 1.0
77       * <P>
78       *
79       * @return returns "http://purl.org/rss/1.0/".
80       */
81      protected Namespace getRSSNamespace() {
82          return Namespace.getNamespace(RSS_URI);
83      }
84  
85      /***
86       * Parses an item element of an RSS document looking for item information.
87       * <p/>
88       * It first invokes super.parseItem and then parses and injects the description property if present.
89       * <p/>
90       *
91       * @param rssRoot the root element of the RSS document in case it's needed for context.
92       * @param eItem the item element to parse.
93       * @return the parsed RSSItem bean.
94       */
95      protected Item parseItem(Element rssRoot,Element eItem) {
96          Item item = super.parseItem(rssRoot,eItem);
97          Element e = eItem.getChild("description",getRSSNamespace());
98          if (e!=null) {
99              item.setDescription(parseItemDescription(rssRoot,e));
100         }
101         return item;
102     }
103 
104     protected Description parseItemDescription(Element rssRoot,Element eDesc) {
105         Description desc = new Description();
106         desc.setType("text/plain");
107         desc.setValue(eDesc.getText());
108         return desc;
109     }
110 
111 }