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;link> 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 * For Channel: The URL to the HTML website corresponding to the channel.
030 * </p>
031 * 
032 * <p>
033 * For Item: The URL of the item.
034 * </p>
035 * 
036 * @author Bill Brown
037 * 
038 */
039public class Link implements Serializable {
040
041        /**
042         * 
043         */
044        private static final long serialVersionUID = 4258812777994100037L;
045
046        private final String link;
047
048        Link(String link) throws RSSpectException {
049
050                if (link == null || link.equals("")) {
051                        throw new RSSpectException("link SHOULD NOT be blank.");
052                }
053
054                String linkLocal = link.trim();
055                if (linkLocal.length() > 0
056                                && new URIScheme().contains(linkLocal.substring(0, linkLocal
057                                                .indexOf(":")))) {
058                        this.link = link;
059                } else {
060                        throw new RSSpectException("link elements must start with a valid "
061                                        + "Uniform Resource Identifer (URI) Schemes.  "
062                                        + "See http://www.iana.org. Yours started with: '" + link
063                                        + "'");
064                }
065        }
066
067        Link(Link link) {
068                this.link = link.link;
069        }
070
071        /**
072         * @return the link information.
073         */
074        public String getLink() {
075                return this.link;
076        }
077
078        /**
079         * Shows the contents of the &lt;link> element.
080         */
081        @Override
082        public String toString() {
083                return "<link>" + link + "</link>";
084        }
085        
086        @Override
087        public boolean equals(Object obj) {
088                if (obj == this) {
089                        return true;
090                }
091                if (!(obj instanceof Link)) {
092                        return false;
093                }
094                return this.toString().equals(obj.toString());
095        }
096        
097        @Override public int hashCode() {
098                return toString().hashCode();
099        }
100}