KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > rss > Channel


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
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
18 package org.apache.commons.digester.rss;
19
20 import java.io.OutputStream JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28
29 /**
30  * <p>Implementation object representing a <strong>channel</strong> in the
31  * <em>Rich Site Summary</em> DTD, version 0.91. This class may be subclassed
32  * to further specialize its behavior.</p>
33  */

34
35 public class Channel implements Serializable JavaDoc {
36
37
38     // ----------------------------------------------------- Instance Variables
39

40
41     /**
42      * The set of items associated with this Channel.
43      */

44     protected ArrayList JavaDoc items = new ArrayList JavaDoc();
45
46
47     /**
48      * The set of skip days for this channel.
49      */

50     protected ArrayList JavaDoc skipDays = new ArrayList JavaDoc();
51
52
53     /**
54      * The set of skip hours for this channel.
55      */

56     protected ArrayList JavaDoc skipHours = new ArrayList JavaDoc();
57
58
59     // ------------------------------------------------------------- Properties
60

61
62     /**
63      * The channel copyright (1-100 characters).
64      */

65     protected String JavaDoc copyright = null;
66
67     public String JavaDoc getCopyright() {
68         return (this.copyright);
69     }
70
71     public void setCopyright(String JavaDoc copyright) {
72         this.copyright = copyright;
73     }
74
75
76     /**
77      * The channel description (1-500 characters).
78      */

79     protected String JavaDoc description = null;
80
81     public String JavaDoc getDescription() {
82         return (this.description);
83     }
84
85     public void setDescription(String JavaDoc description) {
86         this.description = description;
87     }
88
89
90     /**
91      * The channel description file URL (1-500 characters).
92      */

93     protected String JavaDoc docs = null;
94
95     public String JavaDoc getDocs() {
96         return (this.docs);
97     }
98
99     public void setDocs(String JavaDoc docs) {
100         this.docs = docs;
101     }
102
103
104     /**
105      * The image describing this channel.
106      */

107     protected Image image = null;
108
109     public Image getImage() {
110         return (this.image);
111     }
112
113     public void setImage(Image image) {
114         this.image = image;
115     }
116
117
118     /**
119      * The channel language (2-5 characters).
120      */

121     protected String JavaDoc language = null;
122
123     public String JavaDoc getLanguage() {
124         return (this.language);
125     }
126
127     public void setLanguage(String JavaDoc language) {
128         this.language = language;
129     }
130
131
132     /**
133      * The channel last build date (1-100 characters).
134      */

135     protected String JavaDoc lastBuildDate = null;
136
137     public String JavaDoc getLastBuildDate() {
138         return (this.lastBuildDate);
139     }
140
141     public void setLastBuildDate(String JavaDoc lastBuildDate) {
142         this.lastBuildDate = lastBuildDate;
143     }
144
145
146     /**
147      * The channel link (1-500 characters).
148      */

149     protected String JavaDoc link = null;
150
151     public String JavaDoc getLink() {
152         return (this.link);
153     }
154
155     public void setLink(String JavaDoc link) {
156         this.link = link;
157     }
158
159
160     /**
161      * The managing editor (1-100 characters).
162      */

163     protected String JavaDoc managingEditor = null;
164
165     public String JavaDoc getManagingEditor() {
166         return (this.managingEditor);
167     }
168
169     public void setManagingEditor(String JavaDoc managingEditor) {
170         this.managingEditor = managingEditor;
171     }
172
173
174     /**
175      * The channel publication date (1-100 characters).
176      */

177     protected String JavaDoc pubDate = null;
178
179     public String JavaDoc getPubDate() {
180         return (this.pubDate);
181     }
182
183     public void setPubDate(String JavaDoc pubDate) {
184         this.pubDate = pubDate;
185     }
186
187
188     /**
189      * The channel rating (20-500 characters).
190      */

191     protected String JavaDoc rating = null;
192
193     public String JavaDoc getRating() {
194         return (this.rating);
195     }
196
197     public void setRating(String JavaDoc rating) {
198         this.rating = rating;
199     }
200
201
202     /**
203      * The text input description for this channel.
204      */

205     protected TextInput textInput = null;
206
207     public TextInput getTextInput() {
208         return (this.textInput);
209     }
210
211     public void setTextInput(TextInput textInput) {
212         this.textInput = textInput;
213     }
214
215
216     /**
217      * The channel title (1-100 characters).
218      */

219     protected String JavaDoc title = null;
220
221     public String JavaDoc getTitle() {
222         return (this.title);
223     }
224
225     public void setTitle(String JavaDoc title) {
226         this.title = title;
227     }
228
229
230     /**
231      * The RSS specification version number used to create this Channel.
232      */

233     protected double version = 0.91;
234
235     public double getVersion() {
236         return (this.version);
237     }
238
239     public void setVersion(double version) {
240         this.version = version;
241     }
242
243
244     /**
245      * The webmaster email address (1-100 characters).
246      */

247     protected String JavaDoc webMaster = null;
248
249     public String JavaDoc getWebMaster() {
250         return (this.webMaster);
251     }
252
253     public void setWebMaster(String JavaDoc webMaster) {
254         this.webMaster = webMaster;
255     }
256
257
258     // --------------------------------------------------------- Public Methods
259

260
261     /**
262      * Add an additional item.
263      *
264      * @param item The item to be added
265      */

266     public void addItem(Item item) {
267         synchronized (items) {
268             items.add(item);
269         }
270     }
271
272
273     /**
274      * Add an additional skip day name.
275      *
276      * @param skipDay The skip day to be added
277      */

278     public void addSkipDay(String JavaDoc skipDay) {
279         synchronized (skipDays) {
280             skipDays.add(skipDay);
281         }
282     }
283
284
285     /**
286      * Add an additional skip hour name.
287      *
288      * @param skipHour The skip hour to be added
289      */

290     public void addSkipHour(String JavaDoc skipHour) {
291         synchronized (skipHours) {
292             skipHours.add(skipHour);
293         }
294     }
295
296
297     /**
298      * Return the items for this channel.
299      */

300     public Item[] findItems() {
301         synchronized (items) {
302             Item items[] = new Item[this.items.size()];
303             return ((Item[]) this.items.toArray(items));
304         }
305     }
306
307
308     /**
309      * Return the items for this channel.
310      */

311     public Item[] getItems() {
312         return findItems();
313     }
314
315
316     /**
317      * Return the skip days for this channel.
318      */

319     public String JavaDoc[] findSkipDays() {
320         synchronized (skipDays) {
321             String JavaDoc skipDays[] = new String JavaDoc[this.skipDays.size()];
322             return ((String JavaDoc[]) this.skipDays.toArray(skipDays));
323         }
324     }
325
326
327     /**
328      * Return the skip hours for this channel.
329      */

330     public String JavaDoc[] getSkipHours() {
331         return findSkipHours();
332     }
333
334
335     /**
336      * Return the skip hours for this channel.
337      */

338     public String JavaDoc[] findSkipHours() {
339         synchronized (skipHours) {
340             String JavaDoc skipHours[] = new String JavaDoc[this.skipHours.size()];
341             return ((String JavaDoc[]) this.skipHours.toArray(skipHours));
342         }
343     }
344
345
346     /**
347      * Return the skip days for this channel.
348      */

349     public String JavaDoc[] getSkipDays() {
350         return findSkipDays();
351     }
352
353
354     /**
355      * Remove an item for this channel.
356      *
357      * @param item The item to be removed
358      */

359     public void removeItem(Item item) {
360         synchronized (items) {
361             items.remove(item);
362         }
363     }
364
365
366     /**
367      * Remove a skip day for this channel.
368      *
369      * @param skipDay The skip day to be removed
370      */

371     public void removeSkipDay(String JavaDoc skipDay) {
372         synchronized (skipDays) {
373             skipDays.remove(skipDay);
374         }
375     }
376
377
378     /**
379      * Remove a skip hour for this channel.
380      *
381      * @param skipHour The skip hour to be removed
382      */

383     public void removeSkipHour(String JavaDoc skipHour) {
384         synchronized (skipHours) {
385             skipHours.remove(skipHour);
386         }
387     }
388
389
390     /**
391      * Render this channel as XML conforming to the RSS 0.91 specification,
392      * to the specified output stream, with no indication of character
393      * encoding.
394      *
395      * @param stream The output stream to write to
396      */

397     public void render(OutputStream JavaDoc stream) {
398
399         try {
400             render(stream, null);
401         } catch (UnsupportedEncodingException JavaDoc e) {
402             ; // Can not happen
403
}
404
405     }
406
407
408     /**
409      * Render this channel as XML conforming to the RSS 0.91 specification,
410      * to the specified output stream, with the specified character encoding.
411      *
412      * @param stream The output stream to write to
413      * @param encoding The character encoding to declare, or <code>null</code>
414      * for no declaration
415      *
416      * @exception UnsupportedEncodingException if the named encoding
417      * is not supported
418      */

419     public void render(OutputStream JavaDoc stream, String JavaDoc encoding)
420             throws UnsupportedEncodingException JavaDoc {
421
422         PrintWriter JavaDoc pw = null;
423         if (encoding == null) {
424             pw = new PrintWriter JavaDoc(stream);
425         } else {
426             pw = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(stream, encoding));
427         }
428         render(pw, encoding);
429         pw.flush();
430
431     }
432
433
434     /**
435      * Render this channel as XML conforming to the RSS 0.91 specification,
436      * to the specified writer, with no indication of character encoding.
437      *
438      * @param writer The writer to render output to
439      */

440     public void render(Writer JavaDoc writer) {
441
442         render(writer, null);
443
444     }
445
446
447     /**
448      * Render this channel as XML conforming to the RSS 0.91 specification,
449      * to the specified writer, indicating the specified character encoding.
450      *
451      * @param writer The writer to render output to
452      * @param encoding The character encoding to declare, or <code>null</code>
453      * for no declaration
454      */

455     public void render(Writer JavaDoc writer, String JavaDoc encoding) {
456
457         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(writer);
458         render(pw, encoding);
459         pw.flush();
460
461     }
462
463
464     /**
465      * Render this channel as XML conforming to the RSS 0.91 specification,
466      * to the specified writer, with no indication of character encoding.
467      *
468      * @param writer The writer to render output to
469      */

470     public void render(PrintWriter JavaDoc writer) {
471
472         render(writer, null);
473
474     }
475
476
477     /**
478      * Render this channel as XML conforming to the RSS 0.91 specification,
479      * to the specified writer, indicating the specified character encoding.
480      *
481      * @param writer The writer to render output to
482      * @param encoding The character encoding to declare, or <code>null</code>
483      * for no declaration
484      */

485     public void render(PrintWriter JavaDoc writer, String JavaDoc encoding) {
486
487         writer.print("<?xml version=\"1.0\"");
488         if (encoding != null) {
489             writer.print(" encoding=\"");
490             writer.print(encoding);
491             writer.print("\"");
492         }
493         writer.println("?>");
494         writer.println();
495
496         writer.println("<!DOCTYPE rss PUBLIC");
497         writer.println(" \"-//Netscape Communications//DTD RSS 0.91//EN\"");
498         writer.println(" \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">");
499         writer.println();
500
501         writer.println("<rss version=\"0.91\">");
502         writer.println();
503
504         writer.println(" <channel>");
505         writer.println();
506
507         writer.print(" <title>");
508         writer.print(title);
509         writer.println("</title>");
510
511         writer.print(" <description>");
512         writer.print(description);
513         writer.println("</description>");
514
515         writer.print(" <link>");
516         writer.print(link);
517         writer.println("</link>");
518
519         writer.print(" <language>");
520         writer.print(language);
521         writer.println("</language>");
522
523         if (rating != null) {
524             writer.print(" <rating>");
525             writer.print(rating);
526             writer.println("</rating>");
527         }
528
529         if (copyright != null) {
530             writer.print(" <copyright>");
531             writer.print(copyright);
532             writer.print("</copyright>");
533         }
534
535
536         if (pubDate != null) {
537             writer.print(" <pubDate>");
538             writer.print(pubDate);
539             writer.println("</pubDate>");
540         }
541
542         if (lastBuildDate != null) {
543             writer.print(" <lastBuildDate>");
544             writer.print(lastBuildDate);
545             writer.println("</lastBuildDate>");
546         }
547
548         if (docs != null) {
549             writer.print(" <docs>");
550             writer.print(docs);
551             writer.println("</docs>");
552         }
553
554         if (managingEditor != null) {
555             writer.print(" <managingEditor>");
556             writer.print(managingEditor);
557             writer.println("</managingEditor>");
558         }
559
560         if (webMaster != null) {
561             writer.print(" <webMaster>");
562             writer.print(webMaster);
563             writer.println("</webMaster>");
564         }
565
566         writer.println();
567
568         if (image != null) {
569             image.render(writer);
570             writer.println();
571         }
572
573         if (textInput != null) {
574             textInput.render(writer);
575             writer.println();
576         }
577
578         String JavaDoc skipDays[] = findSkipDays();
579         if (skipDays.length > 0) {
580             writer.println(" <skipDays>");
581             for (int i = 0; i < skipDays.length; i++) {
582                 writer.print(" <skipDay>");
583                 writer.print(skipDays[i]);
584                 writer.println("</skipDay>");
585             }
586             writer.println(" </skipDays>");
587         }
588
589         String JavaDoc skipHours[] = findSkipHours();
590         if (skipHours.length > 0) {
591             writer.println(" <skipHours>");
592             for (int i = 0; i < skipHours.length; i++) {
593                 writer.print(" <skipHour>");
594                 writer.print(skipHours[i]);
595                 writer.println("</skipHour>");
596             }
597             writer.println(" </skipHours>");
598             writer.println();
599         }
600
601         Item items[] = findItems();
602         for (int i = 0; i < items.length; i++) {
603             items[i].render(writer);
604             writer.println();
605         }
606
607         writer.println(" </channel>");
608         writer.println();
609
610         writer.println("</rss>");
611
612     }
613
614
615 }
616
Popular Tags