View Javadoc

1   package yarfraw.core.datamodel;
2   
3   import java.util.HashMap;
4   import java.util.Locale;
5   import java.util.Map;
6   
7   import javax.xml.namespace.QName;
8   
9   import yarfraw.utils.ValidationUtils;
10  
11  /***
12   * 
13   * <li>Rss 2.0 - 
14   * {@link Image} is an optional sub-element of {@link Image}, 
15   * which contains three required and three optional sub-elements.
16   * <p>
17   * <code>url</code> is the URI of a GIF, JPEG or PNG image that represents the channel.
18   * <p>
19   * <code>title</code> describes the image, it's used in the ALT attribute of the HTML 
20   * &lt;img> tag when the channel is rendered in HTML.
21   * <p>
22   * <code>link</code> is the URI of the site, when the channel is rendered, 
23   * the image is a link to the site. 
24   * (Note, in practice the image &lt;title> and &lt;link> should have the same value as the channel's 
25   * &lt;title> and &lt;link>.
26   * <p>
27   * Optional elements include <code>width</code> and <code>height</code>, indicating the width and height of 
28   * the image in pixels.
29   * <p> 
30   * <code>description</code> contains text that is included in the TITLE attribute 
31   * of the link formed around the image in the HTML rendering.
32   * <p>
33   * Maximum value for width is 144, default value is 88.
34   * Maximum value for height is 400, default value is 31.
35   * </li>
36   * 
37   * <li> Rss 1.0 -
38   * An image to be associated with an HTML rendering of the channel. 
39   * This image should be of a format supported by the majority of Web browsers. 
40   * While the later 0.91 specification allowed for a width of 1-144 and height of 1-400, 
41   * convention (and the 0.9 specification) dictate 88x31.
42   * </li>
43   * <li>Atom 1.0 - 
44   * The "atom:icon" element's content is an IRI reference [RFC3987] 
45   * which identifies an image which provides iconic visual identification for a feed.
46   * <br/>
47   * The "atom:logo" element's content is an IRI reference [RFC3987] which identifies an image which provides visual identification for a feed.
48   * </li>
49   * @author jliang
50   *
51   */
52  public class Image extends AbstractBaseObject{
53    private static final long serialVersionUID = 20070927L;
54    private String _url;
55    private String _title;
56    private String _link;
57    private Integer _width =88;
58    private Integer _height = 31;
59    private String _description;  
60    public Image(){}
61  
62    public Image(String url, String title, String link, Integer width, Integer height,
63        String description){
64      super();
65      setUrl(url);
66      _title = title;
67      setLink(link);
68      setHeight(height);
69      setWidth(width);
70      _description = description;
71    }
72  
73    /***
74     * <li>Rss 2.0 -
75     * <code>url</code> is the URI of a GIF, JPEG or PNG image that represents the channel. 
76     * if url is not an valid url
77     * </li>
78     * <li>Rss 1.0 -
79     * The URL of the image to used in the "src" attribute of the channel's image tag when rendered as HTML. 
80     * </li> 
81     * <li>
82     * Atom 1.0 -
83     * url of the image
84     * </li>
85     * @param url - any valid url
86     * @return this
87     */
88    public Image setUrl(String url){
89      _url = url;
90      return this;
91    }
92    
93    /***
94     * <li>Rss 2.0 - 
95     * <code>title</code> describes the image, it's used in the ALT attribute of the HTML 
96     * &lt;img> tag when the channel is rendered in HTML.
97     * </li>
98     * <li>Rss 1.0 -
99     * The alternative text ("alt" attribute) associated with the channel's image tag when rendered as HTML.
100    * </li>
101    * <li>
102    * Atom 1.0 - not supported, this field is ignored.
103    * </li>
104    */
105   public String getTitle() {
106     return _title;
107   }
108 
109   /***
110    * <li>Rss 2.0 - 
111    * <code>title</code> describes the image, it's used in the ALT attribute of the HTML 
112    * &lt;img> tag when the channel is rendered in HTML.
113    * </li>
114    * <li>Rss 1.0 -
115    * The alternative text ("alt" attribute) associated with the channel's image tag when rendered as HTML.
116    * </li>
117    * <li>
118    * Atom 1.0 - not supported, this field is ignored.
119    * </li>
120    * @param title - string value of the title
121    * @return this
122    */
123   public Image setTitle(String title) {
124     _title = title;
125     return this;
126   }
127   
128   /***
129    * <li> Rss 2.0 -
130    * <code>link</code> is the URI of the site, when the channel is rendered, 
131     * the image is a link to the site. 
132     * (Note, in practice the image &lt;title> and &lt;link> should have the same value as the channel's 
133     * &lt;title> and &lt;link>.
134     *</li>   
135     *<li> Rss 1.0 - 
136     *The URL to which an HTML rendering of the channel image will link. This, as with the channel's title link, is commonly the parent site's home or news page.
137     *</li>
138     *<li>
139    * Atom 1.0 - not supported, this field is ignored.
140    * </li> 
141   */  
142   public Image setLink(String link){
143     _link = link;
144     return this;
145   }
146   
147   
148   /***
149    * <li>Rss 2.0 -
150    * <code>url</code> is the URI of a GIF, JPEG or PNG image that represents the channel. 
151    * if url is not an valid url
152    * </li>
153    * <li>Rss 1.0 -
154    * The URL of the image to used in the "src" attribute of the channel's image tag when rendered as HTML. 
155    * </li> 
156    * <li>
157    * Atom 1.0 - url of the image
158    * </li>
159    * @return url of the image
160    */
161   public String getUrl() {
162     return _url;
163   }
164 
165   /***
166    * <li> Rss 2.0 -
167    * <code>link</code> is the URI of the site, when the channel is rendered, 
168     * the image is a link to the site. 
169     * (Note, in practice the image &lt;title> and &lt;link> should have the same value as the channel's 
170     * &lt;title> and &lt;link>.
171     *</li>   
172     *<li> Rss 1.0 - 
173     *The URL to which an HTML rendering of the channel image will link. This, as with the channel's title link, is commonly the parent site's home or news page.
174     *</li>
175     *<li>
176    * Atom 1.0 - not supported, this field is ignored.
177    * </li> 
178    * @return
179    */
180   public String getLink() {
181     return _link;
182   }
183 
184   /***
185    * This field is only used by Rss 2.0, it is ignored by other {@link FeedFormat}<br/>
186    * Optional elements include <code>width</code> and <code>height</code>, indicating the width and height of 
187    * the image in pixels.
188    */
189   public Integer getWidth() {
190     return _width;
191   }
192   /***
193    * <b>This field is only used by Rss 2.0, it is ignored by other {@link FeedFormat}</b><br/>
194    * Optional elements include <code>width</code> and <code>height</code>, indicating the width and height of 
195    * the image in pixels.
196  * Maximum value for width is 144, default value is 88. 
197    */  
198   public Image setWidth(Integer width) {
199     if(width == null){
200       _width = 88;
201       return this;
202     }
203     
204     _width = width;
205     return this;
206   }
207   /***
208    * <b>This field is only used by Rss 2.0, it is ignored by other {@link FeedFormat}</b><br/>
209    * Optional elements include <code>width</code> and <code>height</code>, indicating the width and height of 
210    * the image in pixels.
211    */  
212   public Integer getHeight() {
213     return _height;
214   }
215   /***
216    * <b>This field is only used by Rss 2.0, it is ignored by other {@link FeedFormat}</b><br/>
217    * Optional elements include <code>width</code> and <code>height</code>, indicating the width and height of 
218    * the image in pixels.
219    * 
220  * Maximum value for height is 400, default value is 31.
221    */  
222   public Image setHeight(Integer height) {
223     if(height == null){
224       _width = 31;
225       return this;
226     }
227     _height = height;
228     return this;
229   }
230   /***
231    * <b>This field is only used by Rss 2.0, it is ignored by other {@link FeedFormat}</b><br/>
232    * <code>description</code> contains text that is included in the TITLE attribute 
233    * of the link formed around the image in the HTML rendering.
234    */  
235   public String getDescription() {
236     return _description;
237   }
238   /***
239    * <b>This field is only used by Rss 2.0, it is ignored by other {@link FeedFormat}</b><br/>
240    * <code>description</code> contains text that is included in the TITLE attribute 
241    * of the link formed around the image in the HTML rendering.
242    */    
243   public Image setDescription(String description) {
244     _description = description;
245     return this;
246   }
247   
248   ////////////////////////Common setters///////////////////////
249   /***
250    * Any other attribute that is not in the RSS 2.0 specs.
251    */
252   public Image setOtherAttributes(Map<QName, String> otherAttributes) {
253     _otherAttributes = otherAttributes;
254     return this;
255   }
256   /***
257    * Add an attribute that is not in the RSS 2.0 specs.
258    */
259   public Image addOtherAttributes(QName namespace, String attribute) {
260     if(_otherAttributes == null){
261       _otherAttributes = new HashMap<QName, String>();
262     }
263     _otherAttributes.put(namespace, attribute);
264     return this;
265   }
266 
267   /***
268    * <b>Atom 1.0 only</b><br/>
269    * Any element defined by this specification MAY have an xml:base attribute 
270    * [W3C.REC-xmlbase-20010627]. When xml:base is used in an Atom Document, 
271    * it serves the function described in section 5.1.1 of [RFC3986], establishing 
272    * the base URI (or IRI) for resolving any relative references found within the 
273    * effective scope of the xml:base attribute.
274    * @param base
275    * @return
276    */
277   public Image setBase(String base) {
278     _base = base;
279     return this;
280   }
281   /***
282    * <li>Rss 2.0 - &lt;language> element. 
283    * The language the channel is written in. This allows aggregators to group 
284    * all Italian language sites, for example, on a single page. A list of allowable 
285    * values for this element, as provided by Netscape, is here. You may also use values 
286    * defined by the W3C.
287    * Only &lt;channel> support this element.</li>
288    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
289    * Only &lt;channel> and &lt;item> support this element. </li>
290    * <li>Atom 1.0 - 'lang' attribute</li>
291    * <br/>
292    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
293    * @param lang
294    * @return
295    */
296   public Image setLang(String lang) {
297     _lang = lang;
298     return this;
299   }
300   
301   /***
302    * <li>Rss 2.0 - &lt;language> element. 
303    * The language the channel is written in. This allows aggregators to group 
304    * all Italian language sites, for example, on a single page. A list of allowable 
305    * values for this element, as provided by Netscape, is here. You may also use values 
306    * defined by the W3C.
307    * Only &lt;channel> support this element.</li>
308    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
309    * Only &lt;channel> and &lt;item> support this element. </li>
310    * <li>Atom 1.0 - 'lang' attribute</li>
311    * <br/>
312    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
313    * @param lang
314    * @return
315    */
316   public Image setLang(Locale lang) {
317     _lang = lang.getLanguage();
318     return this;
319   }
320   /***
321    * <b>Rss 1.0 only</b><br/>
322    * @param resource
323    * @return
324    */
325   public Image setResource(String resource) {
326     _resource = resource;
327     return this;
328   }
329   /***
330    * <b>Rss 1.0 only</b><br/>
331    * @param about
332    * @return
333    */
334   public Image setAbout(String about) {
335     _about = about;
336     return this;
337   }
338   ////////////////////////Common setters///////////////////////
339   @Override
340   public void validate(FeedFormat format) throws ValidationException {
341     if(format == FeedFormat.RSS20){
342       ValidationUtils.validateNotNull("Image: All required fields in the Image object should be not null", _url, _title, _link);
343       ValidationUtils.validateUri("Url or link is not a valid URI", _url, _link);
344       if(_width > 144 || _width < 0){
345         throw new ValidationException("[Image] Maximum value for width is 144, according to RSS20 specs");
346       }
347       if(_height > 400 ||_height < 0){
348         throw new ValidationException("[Image] Maximum value for height is 400, according to RSS20 specs");
349       }
350     }else{
351       ValidationUtils.validateNotNull("Image: url should not be null", _url);
352       ValidationUtils.validateUri("Url is not a valid URI", _url);
353     }
354     
355     if(format == FeedFormat.RSS10){
356       ValidationUtils.validateNotNull("attribute 'about' is required", getAbout());
357     }
358   }
359 }