1 16 17 18 package org.apache.commons.digester.rss; 19 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 26 import org.apache.commons.digester.Digester; 27 import org.apache.commons.logging.LogFactory; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 30 31 32 45 46 public class RSSDigester extends Digester { 47 48 49 51 52 53 55 56 59 protected boolean configured = false; 60 61 62 66 protected static final String registrations[] = { 67 "-//Netscape Communications//DTD RSS 0.9//EN", 68 "/org/apache/commons/digester/rss/rss-0.9.dtd", 69 "-//Netscape Communications//DTD RSS 0.91//EN", 70 "/org/apache/commons/digester/rss/rss-0.91.dtd", 71 }; 72 73 74 76 77 81 protected String channelClass = "org.apache.commons.digester.rss.Channel"; 82 83 public String getChannelClass() { 84 return (this.channelClass); 85 } 86 87 public void setChannelClass(String channelClass) { 88 this.channelClass = channelClass; 89 } 90 91 92 96 protected String imageClass = "org.apache.commons.digester.rss.Image"; 97 98 public String getImageClass() { 99 return (this.imageClass); 100 } 101 102 public void setImageClass(String imageClass) { 103 this.imageClass = imageClass; 104 } 105 106 107 111 protected String itemClass = "org.apache.commons.digester.rss.Item"; 112 113 public String getItemClass() { 114 return (this.itemClass); 115 } 116 117 public void setItemClass(String itemClass) { 118 this.itemClass = itemClass; 119 } 120 121 122 126 protected String textInputClass = 127 "org.apache.commons.digester.rss.TextInput"; 128 129 public String getTextInputClass() { 130 return (this.textInputClass); 131 } 132 133 public void setTextInputClass(String textInputClass) { 134 this.textInputClass = textInputClass; 135 } 136 137 138 140 141 150 public Object parse(File file) throws IOException , SAXException { 151 152 configure(); 153 return (super.parse(file)); 154 155 } 156 157 158 168 public Object parse(InputSource input) throws IOException , SAXException { 169 170 configure(); 171 return (super.parse(input)); 172 173 } 174 175 176 186 public Object parse(InputStream input) throws IOException , SAXException { 187 188 configure(); 189 return (super.parse(input)); 190 191 } 192 193 194 204 public Object parse(String uri) throws IOException , SAXException { 205 206 configure(); 207 return (super.parse(uri)); 208 209 } 210 211 212 214 215 216 218 219 222 protected void configure() { 223 224 if (configured) { 225 return; 226 } 227 228 for (int i = 0; i < registrations.length; i += 2) { 230 URL url = this.getClass().getResource(registrations[i + 1]); 231 if (url != null) { 232 register(registrations[i], url.toString()); 233 } 234 } 235 236 238 addObjectCreate("rss/channel", channelClass); 240 addCallMethod("rss/channel/copyright", "setCopyright", 0); 241 addCallMethod("rss/channel/description", "setDescription", 0); 242 addCallMethod("rss/channel/docs", "setDocs", 0); 243 addCallMethod("rss/channel/language", "setLanguage", 0); 244 addCallMethod("rss/channel/lastBuildDate", "setLastBuildDate", 0); 245 addCallMethod("rss/channel/link", "setLink", 0); 246 addCallMethod("rss/channel/managingEditor", "setManagingEditor", 0); 247 addCallMethod("rss/channel/pubDate", "setPubDate", 0); 248 addCallMethod("rss/channel/rating", "setRating", 0); 249 addCallMethod("rss/channel/skipDays/day", "addSkipDay", 0); 250 addCallMethod("rss/channel/skipHours/hour", "addSkipHour", 0); 251 addCallMethod("rss/channel/title", "setTitle", 0); 252 addCallMethod("rss/channel/webMaster", "setWebMaster", 0); 253 254 addObjectCreate("rss/channel/image", imageClass); 256 addSetNext("rss/channel/image", "setImage", 257 "org.apache.commons.digester.rss.Image"); 258 addCallMethod("rss/channel/image/description", "setDescription", 0); 259 addCallMethod("rss/channel/image/height", "setHeight", 0, 260 new Class []{ Integer.TYPE }); 261 addCallMethod("rss/channel/image/link", "setLink", 0); 262 addCallMethod("rss/channel/image/title", "setTitle", 0); 263 addCallMethod("rss/channel/image/url", "setURL", 0); 264 addCallMethod("rss/channel/image/width", "setWidth", 0, 265 new Class []{ Integer.TYPE }); 266 267 addObjectCreate("rss/channel/item", itemClass); 269 addSetNext("rss/channel/item", "addItem", 270 "org.apache.commons.digester.rss.Item"); 271 addCallMethod("rss/channel/item/description", "setDescription", 0); 272 addCallMethod("rss/channel/item/link", "setLink", 0); 273 addCallMethod("rss/channel/item/title", "setTitle", 0); 274 275 addObjectCreate("rss/channel/textinput", textInputClass); 277 addSetNext("rss/channel/textinput", "setTextInput", 278 "org.apache.commons.digester.rss.TextInput"); 279 addCallMethod("rss/channel/textinput/description", 280 "setDescription", 0); 281 addCallMethod("rss/channel/textinput/link", "setLink", 0); 282 addCallMethod("rss/channel/textinput/name", "setName", 0); 283 addCallMethod("rss/channel/textinput/title", "setTitle", 0); 284 285 configured = true; 287 288 } 289 290 291 293 294 300 public static void main(String args[]) { 301 302 try { 303 System.out.println("RSSDigester Test Program"); 304 System.out.println("Opening input stream ..."); 305 InputStream is = RSSDigester.class.getResourceAsStream 306 ("/org/apache/commons/digester/rss/rss-example.xml"); 307 System.out.println("Creating new digester ..."); 308 RSSDigester digester = new RSSDigester(); 309 if ((args.length > 0) && (args[0].equals("-debug"))) { 310 digester.setLogger(LogFactory.getLog("RSSDigester")); 311 } 312 System.out.println("Parsing input stream ..."); 313 Channel channel = (Channel) digester.parse(is); 314 System.out.println("Closing input stream ..."); 315 is.close(); 316 System.out.println("Dumping channel info ..."); 317 channel.render(System.out); 318 } catch (Exception e) { 319 System.out.println("-->Exception"); 320 e.printStackTrace(System.out); 321 } 322 323 } 324 325 326 } 327 | Popular Tags |