1 26 28 package de.nava.informa.parsers; 29 30 import java.net.URL ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.jdom.Attribute; 38 import org.jdom.Element; 39 40 import de.nava.informa.core.ChannelBuilderIF; 41 import de.nava.informa.core.ChannelFormat; 42 import de.nava.informa.core.ChannelIF; 43 import de.nava.informa.core.ImageIF; 44 import de.nava.informa.core.ItemEnclosureIF; 45 import de.nava.informa.core.ItemIF; 46 import de.nava.informa.core.ItemSourceIF; 47 import de.nava.informa.core.ParseException; 48 import de.nava.informa.core.TextInputIF; 49 import de.nava.informa.utils.ParserUtils; 50 51 57 class RSS_0_91_Parser { 58 59 private static Log logger = LogFactory.getLog(RSS_0_91_Parser.class); 60 61 static ChannelIF parse(ChannelBuilderIF cBuilder, Element root) 62 throws ParseException { 63 if (cBuilder == null) { 64 throw new RuntimeException ( 65 "Without builder no channel can " + "be created."); 66 } 67 Date dateParsed = new Date (); 68 logger.debug("start parsing."); 69 70 Element channel = root.getChild("channel"); 72 if (channel == null) { 73 logger.warn("Channel element could not be retrieved from feed."); 74 throw new ParseException("No channel element found in feed."); 75 } 76 77 79 ChannelIF chnl = 81 cBuilder.createChannel(channel, channel.getChildTextTrim("title")); 82 83 chnl.setFormat(ChannelFormat.RSS_0_91); 84 85 chnl.setDescription(channel.getChildTextTrim("description")); 87 88 chnl.setSite(ParserUtils.getURL(channel.getChildTextTrim("link"))); 90 91 chnl.setLanguage(channel.getChildTextTrim("language")); 93 94 List items = channel.getChildren("item"); 96 Iterator i = items.iterator(); 97 while (i.hasNext()) { 98 Element item = (Element) i.next(); 99 100 Element elTitle = item.getChild("title"); 102 String strTitle = "<No Title>"; 103 if (elTitle != null) { 104 strTitle = elTitle.getTextTrim(); 105 } 106 if (logger.isDebugEnabled()) { 107 logger.debug("Item element found (" + strTitle + ")."); 108 } 109 110 Element elLink = item.getChild("link"); 112 String strLink = ""; 113 if (elLink != null) { 114 strLink = elLink.getTextTrim(); 115 } 116 117 Element elDesc = item.getChild("description"); 119 String strDesc = ""; 120 if (elDesc != null) { 121 strDesc = elDesc.getTextTrim(); 122 } 123 124 ItemIF rssItem = 126 cBuilder.createItem( 127 item, 128 chnl, 129 strTitle, 130 strDesc, 131 ParserUtils.getURL(strLink)); 132 rssItem.setFound(dateParsed); 133 134 Element source = item.getChild("source"); 136 if (source != null) { 137 String sourceName = source.getTextTrim(); 138 Attribute sourceAttribute = source.getAttribute("url"); 139 if (sourceAttribute != null) { 140 String location = sourceAttribute.getValue().trim(); 141 ItemSourceIF itemSource = 142 cBuilder.createItemSource(rssItem, sourceName, location, null); 143 rssItem.setSource(itemSource); 144 } 145 } 146 147 Element enclosure = item.getChild("enclosure"); 149 if (enclosure != null) { 150 URL location = null; 151 String type = null; 152 int length = -1; 153 Attribute urlAttribute = enclosure.getAttribute("url"); 154 if (urlAttribute != null) { 155 location = ParserUtils.getURL(urlAttribute.getValue().trim()); 156 } 157 Attribute typeAttribute = enclosure.getAttribute("type"); 158 if (typeAttribute != null) { 159 type = typeAttribute.getValue().trim(); 160 } 161 Attribute lengthAttribute = enclosure.getAttribute("length"); 162 if (lengthAttribute != null) { 163 try { 164 length = Integer.parseInt(lengthAttribute.getValue().trim()); 165 } catch (NumberFormatException e) { 166 logger.warn(e); 167 } 168 } 169 ItemEnclosureIF itemEnclosure = 170 cBuilder.createItemEnclosure(rssItem, location, type, length); 171 rssItem.setEnclosure(itemEnclosure); 172 } 173 } 174 175 Element image = channel.getChild("image"); 177 if (image != null) { 178 ImageIF rssImage = 179 cBuilder.createImage( 180 image.getChildTextTrim("title"), 181 ParserUtils.getURL(image.getChildTextTrim("url")), 182 ParserUtils.getURL(image.getChildTextTrim("link"))); 183 Element imgWidth = image.getChild("width"); 184 if (imgWidth != null) { 185 try { 186 rssImage.setWidth(Integer.parseInt(imgWidth.getTextTrim())); 187 } catch (NumberFormatException e) { 188 logger.warn(e); 189 } 190 } 191 Element imgHeight = image.getChild("height"); 192 if (imgHeight != null) { 193 try { 194 rssImage.setHeight(Integer.parseInt(imgHeight.getTextTrim())); 195 } catch (NumberFormatException e) { 196 logger.warn(e); 197 } 198 } 199 Element imgDescr = image.getChild("description"); 200 if (imgDescr != null) { 201 rssImage.setDescription(imgDescr.getTextTrim()); 202 } 203 chnl.setImage(rssImage); 204 } 205 206 Element txtinp = channel.getChild("textinput"); 208 if (txtinp != null) { 209 TextInputIF rssTextInput = 210 cBuilder.createTextInput( 211 txtinp.getChild("title").getTextTrim(), 212 txtinp.getChild("description").getTextTrim(), 213 txtinp.getChild("name").getTextTrim(), 214 ParserUtils.getURL(txtinp.getChild("link").getTextTrim())); 215 chnl.setTextInput(rssTextInput); 216 } 217 218 Element copyright = channel.getChild("copyright"); 220 if (copyright != null) { 221 chnl.setCopyright(copyright.getTextTrim()); 222 } 223 224 Element rating = channel.getChild("rating"); 226 if (rating != null) { 227 chnl.setRating(rating.getTextTrim()); 228 } 229 230 Element pubDate = channel.getChild("pubDate"); 232 if (pubDate != null) { 233 chnl.setPubDate(ParserUtils.getDate(pubDate.getTextTrim())); 234 } 235 236 Element lastBuildDate = channel.getChild("lastBuildDate"); 238 if (lastBuildDate != null) { 239 chnl.setLastBuildDate(ParserUtils.getDate(lastBuildDate.getTextTrim())); 240 } 241 242 Element docs = channel.getChild("docs"); 244 if (docs != null) { 245 chnl.setDocs(docs.getTextTrim()); 246 } 247 248 Element managingEditor = channel.getChild("managingEditor"); 250 if (managingEditor != null) { 251 chnl.setCreator(managingEditor.getTextTrim()); 252 } 253 254 Element webMaster = channel.getChild("webMaster"); 256 if (webMaster != null) { 257 chnl.setPublisher(webMaster.getTextTrim()); 258 } 259 260 Element cloud = channel.getChild("cloud"); 262 if (cloud != null) { 263 String _port = cloud.getAttributeValue("port"); 264 int port = -1; 265 if (_port != null) { 266 try { 267 port = Integer.parseInt(_port); 268 } catch (NumberFormatException e) { 269 logger.warn(e); 270 } 271 } 272 chnl.setCloud( 273 cBuilder.createCloud( 274 cloud.getAttributeValue("domain"), 275 port, 276 cloud.getAttributeValue("path"), 277 cloud.getAttributeValue("registerProcedure"), 278 cloud.getAttributeValue("protocol"))); 279 } 280 281 chnl.setLastUpdated(dateParsed); 282 285 return chnl; 286 } 287 288 } 289 | Popular Tags |