KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > rss > Feed


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.publishers.rss;
38
39 import java.io.BufferedWriter JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.Writer JavaDoc;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.List JavaDoc;
47
48 import org.jdom.Document;
49 import org.jdom.Element;
50 import org.jdom.JDOMException;
51 import org.jdom.input.SAXBuilder;
52
53 import org.apache.log4j.Logger;
54
55 /**
56  * The feed class acts as a generic RSS Feed (there's no CruiseControl-specific
57  * functionality in this class).
58  *
59  * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
60  * @author Patrick Conant
61  */

62 public class Feed {
63
64     private static final Logger LOG = Logger.getLogger(Feed.class);
65
66     private String JavaDoc channelTitle;
67     private String JavaDoc channelLink;
68     private String JavaDoc channelDescription;
69     private String JavaDoc channelLanguage = "en-US";
70
71     private int maxLength = 20;
72     private final List JavaDoc items = new ArrayList JavaDoc();
73
74     /**
75      * Constructor
76      *
77      * @param publishToFile the file to which the feed should be published.
78      */

79     public Feed(File JavaDoc publishToFile) {
80
81         // If we can read the file then load the RSSFeed settings from the existing file...
82
if (publishToFile.exists() && publishToFile.canRead()) {
83             try {
84
85                 SAXBuilder builder = new SAXBuilder();
86                 Document doc = builder.build(publishToFile);
87
88                 if (doc.getRootElement() != null
89                     && doc.getRootElement().getChild(RSS.NODE_CHANNEL) != null) {
90
91                     Element channelElement = doc.getRootElement().getChild(RSS.NODE_CHANNEL);
92                     if (channelElement.getChild(RSS.NODE_CHANNEL_TITLE) != null) {
93                         this.channelTitle =
94                             channelElement.getChild(RSS.NODE_CHANNEL_TITLE).getText().trim();
95                     }
96                     if (channelElement.getChild(RSS.NODE_CHANNEL_LINK) != null) {
97                         this.channelLink =
98                             channelElement.getChild(RSS.NODE_CHANNEL_LINK).getText().trim();
99                     }
100                     if (channelElement.getChild(RSS.NODE_CHANNEL_DESCRIPTION) != null) {
101                         this.channelDescription =
102                             channelElement.getChild(RSS.NODE_CHANNEL_DESCRIPTION).getText().trim();
103                     }
104
105                     if (channelElement.getChildren(RSS.NODE_ITEM) != null) {
106                         List JavaDoc itemNodes = channelElement.getChildren(RSS.NODE_ITEM);
107                         for (int i = 0; i < itemNodes.size(); i++) {
108                             this.items.add(new Item((Element) itemNodes.get(i)));
109                         }
110                     }
111                 } else {
112                     LOG.info("existing RSS file doesn't appear to be valid. Missnig root element or channel node.");
113                 }
114
115                 // Now sort the items from the old XML and remove enough to get to maxSize.
116
Collections.sort(this.items);
117                 while (this.items.size() > this.maxLength) {
118                     this.items.remove(this.items.size() - 1);
119                 }
120             } catch (JDOMException jex) {
121                 LOG.error("jdom exception while parsing existing RSS file " + publishToFile.getPath()
122                     + "; deleting file and starting over...", jex);
123                 publishToFile.delete();
124             } catch (IOException JavaDoc ioe) {
125                 LOG.error("IOException while reading existing RSS file " + publishToFile.getPath()
126                     + "; deleting file and starting over...", ioe);
127                 publishToFile.delete();
128             }
129         } else {
130             LOG.info("Unable to locate or read the existing RSS feed file.");
131         }
132     }
133
134
135
136     /**
137      * Set the title of the RSS feed. The title will go into the /rss/title
138      * element in the RSS XML.
139      *
140      * @param title the title of the RSS feed.
141      */

142     public void setTitle(String JavaDoc title) {
143         this.channelTitle = title;
144     }
145
146     /**
147      * Returns the title of the RSS feed. The title goes into the /rss/title
148      * element in the RSS XML.
149      *
150      * @return title the title of the RSS feed.
151      */

152     public String JavaDoc getTitle() {
153         return this.channelTitle;
154     }
155     public void setLink(String JavaDoc link) {
156         this.channelLink = link;
157     }
158     public String JavaDoc getLink() {
159         return this.channelLink;
160     }
161     public void setDescription(String JavaDoc description) {
162         this.channelDescription = description;
163     }
164     public String JavaDoc getDescription() {
165         return this.channelDescription;
166     }
167
168     public void setMaxLength(int max) {
169         this.maxLength = max;
170     }
171     public int getMaxLength() {
172         return this.maxLength;
173     }
174
175     public void addItem(Item item) {
176         synchronized (this.items) {
177             if (this.items.size() == this.maxLength) {
178                 this.items.remove(this.items.size() - 1);
179             }
180             this.items.add(0, item);
181         }
182     }
183
184     public List JavaDoc getItems() {
185         return this.items;
186     }
187
188     public void write(Writer JavaDoc wr) throws IOException JavaDoc {
189
190         BufferedWriter JavaDoc br = new BufferedWriter JavaDoc(wr);
191         br.write("<?xml version=\"1.0\" ?>\n");
192         br.write("<rss version=\"2.0\">\n");
193         br.write(" <channel>\n");
194         br.write(" <title>");
195         if (this.getTitle() != null) {
196             br.write(this.getTitle());
197         }
198         br.write("</title>\n");
199         br.write(" <link>");
200         if (this.getLink() != null) {
201             br.write(this.getLink());
202         }
203         br.write("</link>\n");
204         br.write(" <description>");
205         if (this.getDescription() != null) {
206             br.write(this.getDescription());
207         }
208         br.write("</description>\n");
209         br.write(" <language>");
210         br.write(this.channelLanguage);
211         br.write("</language>\n");
212
213         for (int i = 0; i < this.items.size(); i++) {
214             //write each item...
215
if (this.items.get(i) != null) {
216                 Item item = (Item) this.items.get(i);
217                 br.write(item.toXml());
218             }
219         }
220         br.write(" </channel>\n");
221         br.write("</rss>\n");
222         br.flush();
223     }
224 }
225
Popular Tags