001/**
002 * Copyright 2011 Bill Brown
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.colorfulsoftware.rss;
017
018import java.io.Serializable;
019
020/**
021 * <p>
022 * The &lt;image> element.
023 * </p>
024 * <p>
025 * From the <a href="http://cyber.law.harvard.edu/rss/rss.html">RSS 2.0
026 * specification</a>...
027 * </p>
028 * <p>
029 * Specifies a GIF, JPEG or PNG image that can be displayed with the channel.
030 * More info <a href=
031 * "http://cyber.law.harvard.edu/rss/rss.html#ltimagegtSubelementOfLtchannelgt"
032 * >here</a>.
033 * </p>
034 * 
035 * <p>
036 * &lt;image> is an optional sub-element of &lt;channel>, which contains three
037 * required and three optional sub-elements.
038 * </p>
039 * 
040 * <p>
041 * &lt;url> is the URL of a GIF, JPEG or PNG image that represents the channel.
042 * </p>
043 * 
044 * <p>
045 * &lt;title> describes the image, it's used in the ALT attribute of the HTML
046 * &lt;img> tag when the channel is rendered in HTML.
047 * </p>
048 * 
049 * <p>
050 * &lt;link> is the URL of the site, when the channel is rendered, the image is
051 * a link to the site. (Note, in practice the image &lt;title> and &lt;link>
052 * should have the same value as the channel's &lt;title> and &lt;link>.
053 * </p>
054 * 
055 * <p>
056 * Optional elements include &lt;width> and &lt;height>, numbers, indicating the
057 * width and height of the image in pixels. &lt;description> contains text that
058 * is included in the TITLE attribute of the link formed around the image in the
059 * HTML rendering.
060 * </p>
061 * 
062 * <p>
063 * Maximum value for width is 144, default value is 88.
064 * </p>
065 * 
066 * <p>
067 * Maximum value for height is 400, default value is 31.
068 * </p>
069 * 
070 * @author Bill Brown
071 * 
072 */
073public class Image implements Serializable {
074
075        private static final long serialVersionUID = -812074455770644390L;
076
077        /* required sub elements */
078        private final URL url;
079
080        private final Title title;
081
082        private final Link link;
083
084        /* optional sub elements */
085        private final Width width;
086
087        private final Height height;
088
089        private final Description description;
090
091        Image(URL url, Title title, Link link, Width width, Height height,
092                        Description description) throws RSSpectException {
093                // make sure id is present
094                if (url == null) {
095                        throw new RSSpectException(
096                                        "image elements MUST contain a url element.");
097                }
098                this.url = new URL(url);
099
100                // make sure title is present
101                if (title == null) {
102                        throw new RSSpectException(
103                                        "image elements MUST contain a title element.");
104                }
105                this.title = new Title(title.getTitle());
106
107                // make sure updated is present
108                if (link == null) {
109                        throw new RSSpectException(
110                                        "image elements MUST contain a link element.");
111                }
112                this.link = new Link(link);
113
114                this.width = (width == null) ? null : new Width(width);
115                this.height = (height == null) ? null : new Height(height);
116                this.description = (description == null) ? null : new Description(
117                                description.getDescription());
118        }
119
120        Image(Image image) {
121                this.url = image.getUrl();
122                this.title = image.getTitle();
123                this.link = image.getLink();
124                this.width = image.getWidth();
125                this.height = image.getHeight();
126                this.description = image.getDescription();
127        }
128
129        /**
130         * @return the url element.
131         */
132        public URL getUrl() {
133                return new URL(url);
134        }
135
136        /**
137         * @return the title element.
138         */
139        public Title getTitle() {
140                return new Title(title);
141        }
142
143        /**
144         * @return the link element.
145         */
146        public Link getLink() {
147                return new Link(link);
148
149        }
150
151        /**
152         * @return the width element.
153         */
154        public Width getWidth() {
155                return (width == null) ? null : new Width(width);
156        }
157
158        /**
159         * @return the height element.
160         */
161        public Height getHeight() {
162                return (height == null) ? null : new Height(height);
163        }
164
165        /**
166         * @return the description element.
167         */
168        public Description getDescription() {
169                return (description == null) ? null : new Description(description
170                                .getDescription());
171        }
172
173        /**
174         * Shows the contents of the &lt;image> element.
175         */
176        @Override
177        public String toString() {
178                StringBuilder sb = new StringBuilder("<image>" + url + title + link);
179
180                if (width != null) {
181                        sb.append(width);
182                }
183
184                if (height != null) {
185                        sb.append(height);
186                }
187
188                if (description != null) {
189                        sb.append(description);
190                }
191
192                sb.append("</image>");
193                return sb.toString();
194        }
195        
196        @Override
197        public boolean equals(Object obj) {
198                if (obj == this) {
199                        return true;
200                }
201                if (!(obj instanceof Image)) {
202                        return false;
203                }
204                return this.toString().equals(obj.toString());
205        }
206        
207        @Override public int hashCode() {
208                return toString().hashCode();
209        }
210}