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;
21  import java.io.OutputStreamWriter;
22  import java.io.PrintWriter;
23  import java.io.Serializable;
24  import java.io.UnsupportedEncodingException;
25  import java.io.Writer;
26  import java.util.ArrayList;
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 {
36  
37  
38      // ----------------------------------------------------- Instance Variables
39  
40  
41      /***
42       * The set of items associated with this Channel.
43       */
44      protected ArrayList items = new ArrayList();
45  
46  
47      /***
48       * The set of skip days for this channel.
49       */
50      protected ArrayList skipDays = new ArrayList();
51  
52  
53      /***
54       * The set of skip hours for this channel.
55       */
56      protected ArrayList skipHours = new ArrayList();
57  
58  
59      // ------------------------------------------------------------- Properties
60  
61  
62      /***
63       * The channel copyright (1-100 characters).
64       */
65      protected String copyright = null;
66  
67      public String getCopyright() {
68          return (this.copyright);
69      }
70  
71      public void setCopyright(String copyright) {
72          this.copyright = copyright;
73      }
74  
75  
76      /***
77       * The channel description (1-500 characters).
78       */
79      protected String description = null;
80  
81      public String getDescription() {
82          return (this.description);
83      }
84  
85      public void setDescription(String description) {
86          this.description = description;
87      }
88  
89  
90      /***
91       * The channel description file URL (1-500 characters).
92       */
93      protected String docs = null;
94  
95      public String getDocs() {
96          return (this.docs);
97      }
98  
99      public void setDocs(String 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 language = null;
122 
123     public String getLanguage() {
124         return (this.language);
125     }
126 
127     public void setLanguage(String language) {
128         this.language = language;
129     }
130 
131 
132     /***
133      * The channel last build date (1-100 characters).
134      */
135     protected String lastBuildDate = null;
136 
137     public String getLastBuildDate() {
138         return (this.lastBuildDate);
139     }
140 
141     public void setLastBuildDate(String lastBuildDate) {
142         this.lastBuildDate = lastBuildDate;
143     }
144 
145 
146     /***
147      * The channel link (1-500 characters).
148      */
149     protected String link = null;
150 
151     public String getLink() {
152         return (this.link);
153     }
154 
155     public void setLink(String link) {
156         this.link = link;
157     }
158 
159 
160     /***
161      * The managing editor (1-100 characters).
162      */
163     protected String managingEditor = null;
164 
165     public String getManagingEditor() {
166         return (this.managingEditor);
167     }
168 
169     public void setManagingEditor(String managingEditor) {
170         this.managingEditor = managingEditor;
171     }
172 
173 
174     /***
175      * The channel publication date (1-100 characters).
176      */
177     protected String pubDate = null;
178 
179     public String getPubDate() {
180         return (this.pubDate);
181     }
182 
183     public void setPubDate(String pubDate) {
184         this.pubDate = pubDate;
185     }
186 
187 
188     /***
189      * The channel rating (20-500 characters).
190      */
191     protected String rating = null;
192 
193     public String getRating() {
194         return (this.rating);
195     }
196 
197     public void setRating(String 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 title = null;
220 
221     public String getTitle() {
222         return (this.title);
223     }
224 
225     public void setTitle(String 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 webMaster = null;
248 
249     public String getWebMaster() {
250         return (this.webMaster);
251     }
252 
253     public void setWebMaster(String 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 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 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[] findSkipDays() {
320         synchronized (skipDays) {
321             String skipDays[] = new String[this.skipDays.size()];
322             return ((String[]) this.skipDays.toArray(skipDays));
323         }
324     }
325 
326 
327     /***
328      * Return the skip hours for this channel.
329      */
330     public String[] getSkipHours() {
331         return findSkipHours();
332     }
333 
334 
335     /***
336      * Return the skip hours for this channel.
337      */
338     public String[] findSkipHours() {
339         synchronized (skipHours) {
340             String skipHours[] = new String[this.skipHours.size()];
341             return ((String[]) this.skipHours.toArray(skipHours));
342         }
343     }
344 
345 
346     /***
347      * Return the skip days for this channel.
348      */
349     public String[] 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 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 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 stream) {
398 
399         try {
400             render(stream, null);
401         } catch (UnsupportedEncodingException 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 stream, String encoding)
420             throws UnsupportedEncodingException {
421 
422         PrintWriter pw = null;
423         if (encoding == null) {
424             pw = new PrintWriter(stream);
425         } else {
426             pw = new PrintWriter(new OutputStreamWriter(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 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 writer, String encoding) {
456 
457         PrintWriter pw = new PrintWriter(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 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 writer, String 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 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 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 }