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.WireFeed;
20  import com.sun.syndication.feed.rss.Category;
21  import com.sun.syndication.feed.rss.Channel;
22  import com.sun.syndication.feed.rss.Cloud;
23  import com.sun.syndication.feed.rss.Description;
24  import com.sun.syndication.feed.rss.Enclosure;
25  import com.sun.syndication.feed.rss.Item;
26  import com.sun.syndication.feed.rss.Source;
27  import org.jdom.Element;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   */
34  public class RSS092Parser extends RSS091UserlandParser {
35  
36      public RSS092Parser() {
37          this("rss_0.92");
38      }
39  
40      protected RSS092Parser(String type) {
41          super(type);
42      }
43  
44      protected String getRSSVersion() {
45              return "0.92";
46      }
47  
48      protected WireFeed parseChannel(Element rssRoot)  {
49          Channel channel = (Channel) super.parseChannel(rssRoot);
50  
51          Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
52          Element eCloud = eChannel.getChild("cloud",getRSSNamespace());
53          if (eCloud!=null) {
54              Cloud cloud = new Cloud();
55              String att = eCloud.getAttributeValue("domain");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
56              if (att!=null) {
57                  cloud.setDomain(att);
58              }
59              att = eCloud.getAttributeValue("port");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
60              if (att!=null) {
61                  cloud.setPort(Integer.parseInt(att.trim()));
62              }
63              att = eCloud.getAttributeValue("path");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
64              if (att!=null) {
65                  cloud.setPath(att);
66              }
67              att = eCloud.getAttributeValue("registerProcedure");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
68              if (att!=null) {
69                  cloud.setRegisterProcedure(att);
70              }
71              att = eCloud.getAttributeValue("protocol");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
72              if (att!=null) {
73                  cloud.setProtocol(att);
74              }
75              channel.setCloud(cloud);
76          }
77          return channel;
78      }
79  
80      protected Item parseItem(Element rssRoot,Element eItem) {
81          Item item = super.parseItem(rssRoot,eItem);
82  
83          Element e = eItem.getChild("source",getRSSNamespace());
84          if (e!=null) {
85              Source source = new Source();
86              String url = e.getAttributeValue("url");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
87              source.setUrl(url);
88              source.setValue(e.getText());
89              item.setSource(source);
90          }
91  
92          // 0.92 allows one enclosure occurrence, 0.93 multiple
93          // just saving to write some code.
94          List eEnclosures = eItem.getChildren("enclosure");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
95          if (eEnclosures.size()>0) {
96              List enclosures = new ArrayList();
97              for (int i=0;i<eEnclosures.size();i++) {
98                  e = (Element) eEnclosures.get(i);
99  
100                 Enclosure enclosure = new Enclosure();
101                 String att = e.getAttributeValue("url");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
102                 if (att!=null) {
103                     enclosure.setUrl(att);
104                 }
105                 att = e.getAttributeValue("length");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
106                 if (att!=null && att.trim().length()>0) {
107                     enclosure.setLength(Long.parseLong(att.trim()));
108                 }
109                 att = e.getAttributeValue("type");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
110                 if (att!=null) {
111                     enclosure.setType(att);
112                 }
113                 enclosures.add(enclosure);
114             }
115             item.setEnclosures(enclosures);
116         }
117 
118         List eCats = eItem.getChildren("category");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
119         item.setCategories(parseCategories(eCats));
120 
121         return item;
122     }
123 
124     protected List parseCategories(List eCats) {
125         List cats = null;
126         if (eCats.size()>0) {
127             cats = new ArrayList();
128             for (int i=0;i<eCats.size();i++) {
129                 Category cat = new Category();
130                 Element e = (Element) eCats.get(i);
131                 String att = e.getAttributeValue("domain");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
132                 if (att!=null) {
133                     cat.setDomain(att);
134                 }
135                 cat.setValue(e.getText());
136                 cats.add(cat);
137             }
138         }
139         return cats;
140     }
141 
142     protected Description parseItemDescription(Element rssRoot,Element eDesc) {
143         Description desc = super.parseItemDescription(rssRoot,eDesc);
144         desc.setType("text/html");
145         return desc;
146     }
147 
148 }