KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.ParseException JavaDoc;
40 import java.text.SimpleDateFormat JavaDoc;
41
42 import java.util.Date JavaDoc;
43
44 import org.jdom.Element;
45
46 /**
47  * A generic RSS Feed Item (includes the contents of an RSS feed between the
48  * &lt:item> and </item> tags).
49  *
50  * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
51  * @author Patrick Conant
52  */

53 public class Item implements Comparable JavaDoc {
54
55     private String JavaDoc title;
56     private String JavaDoc link;
57     private String JavaDoc description;
58     private Date JavaDoc publishDate;
59     private final SimpleDateFormat JavaDoc rssDateFormatter = new SimpleDateFormat JavaDoc(RSS.DATE_FORMAT);
60
61     public Item() {
62     }
63
64     /**
65      * Construct from a JDOM element.
66      */

67     public Item(Element itemNode) {
68         if (itemNode.getChild(RSS.NODE_ITEM_TITLE) != null) {
69             this.title = itemNode.getChild(RSS.NODE_ITEM_TITLE).getText();
70         }
71         if (itemNode.getChild(RSS.NODE_ITEM_LINK) != null) {
72             this.link = itemNode.getChild(RSS.NODE_ITEM_LINK).getText();
73         }
74         if (itemNode.getChild(RSS.NODE_ITEM_DESCRIPTION) != null) {
75             this.description = itemNode.getChild(RSS.NODE_ITEM_DESCRIPTION).getText();
76         }
77         if (itemNode.getChild(RSS.NODE_ITEM_PUBLISH_DATE) != null) {
78            try {
79                 this.publishDate = rssDateFormatter.parse(itemNode.getChild(RSS.NODE_ITEM_PUBLISH_DATE).getText());
80             } catch (ParseException JavaDoc pex) {
81                 // LOG?
82
this.publishDate = new Date JavaDoc(0);
83             }
84         }
85     }
86
87     public int compareTo(Object JavaDoc o) {
88         Item other = (Item) o;
89         return other.getPublishDate().compareTo(this.getPublishDate());
90     }
91
92     public String JavaDoc getTitle() {
93         return this.title;
94     }
95     public void setTitle(String JavaDoc title) {
96         this.title = title;
97     }
98     public String JavaDoc getDescription() {
99         return this.description;
100     }
101     public void setDescription(String JavaDoc description) {
102         this.description = description;
103     }
104     public String JavaDoc getLink() {
105         return this.link;
106     }
107     public void setLink(String JavaDoc link) {
108         this.link = link;
109     }
110     public Date JavaDoc getPublishDate() {
111         return this.publishDate;
112     }
113     public void setPublishDate(Date JavaDoc publishDate) {
114         this.publishDate = publishDate;
115     }
116
117     public String JavaDoc toXml() {
118         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
119
120         output.append(" <");
121         output.append(RSS.NODE_ITEM);
122         output.append(">\n");
123
124         //TITLE
125
output.append(" <");
126         output.append(RSS.NODE_ITEM_TITLE);
127         output.append(">");
128         if (this.getTitle() != null) {
129             output.append(this.getTitle());
130         }
131         output.append("</");
132         output.append(RSS.NODE_ITEM_TITLE);
133         output.append(">\n");
134
135         //LINK
136
output.append(" <");
137         output.append(RSS.NODE_ITEM_LINK);
138         output.append(">");
139         if (this.getLink() != null) {
140             output.append(this.getLink());
141         }
142         output.append("</");
143         output.append(RSS.NODE_ITEM_LINK);
144         output.append(">\n");
145
146
147         //DESCRIPTION
148
output.append(" <");
149         output.append(RSS.NODE_ITEM_DESCRIPTION);
150         output.append("><![CDATA[");
151         if (this.getDescription() != null) {
152             output.append(this.getDescription());
153         }
154         output.append("]]></");
155         output.append(RSS.NODE_ITEM_DESCRIPTION);
156         output.append(">\n");
157
158         //PUBLISH DATE
159
if (this.getPublishDate() != null) {
160             output.append(" <");
161             output.append(RSS.NODE_ITEM_PUBLISH_DATE);
162             output.append(">");
163             output.append(rssDateFormatter.format(this.getPublishDate()));
164             output.append("</");
165             output.append(RSS.NODE_ITEM_PUBLISH_DATE);
166             output.append(">\n");
167         }
168
169
170         //GUID & isPermaLink
171
output.append(" <");
172         output.append(RSS.NODE_ITEM_GUID);
173         output.append(" ");
174         output.append(RSS.ATTRIB_ITEM_IS_PERMA_LINK);
175         output.append("=\"true\">");
176         if (this.getLink() != null) {
177             output.append(this.getLink());
178         }
179         output.append("</");
180         output.append(RSS.NODE_ITEM_GUID);
181         output.append(">\n");
182
183         output.append(" </");
184         output.append(RSS.NODE_ITEM);
185         output.append(">\n");
186
187         return output.toString();
188     }
189 }
Popular Tags