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 <source> 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 * The RSS channel that the item came from. <a href= 030 * "http://cyber.law.harvard.edu/rss/rss.html#ltsourcegtSubelementOfLtitemgt" 031 * >More</a>. 032 * </p> 033 * 034 * <p> 035 * <source> is an optional sub-element of <item>. 036 * </p> 037 * 038 * <p> 039 * Its value is the name of the RSS channel that the item came from, derived 040 * from its <title>. It has one required attribute, url, which links to the 041 * XMLization of the source. 042 * </p> 043 * 044 * <p> 045 * <source url="http://www.tomalak.org/links2.xml">Tomalak's 046 * Realm</source> 047 * </p> 048 * 049 * <p> 050 * The purpose of this element is to propagate credit for links, to publicize 051 * the sources of news items. It can be used in the Post command of an 052 * aggregator. It should be generated automatically when forwarding an item from 053 * an aggregator to a weblog authoring tool. 054 * </p> 055 * 056 * @author Bill Brown 057 * 058 */ 059public class Source implements Serializable { 060 061 /** 062 * 063 */ 064 private static final long serialVersionUID = -4585022053173517802L; 065 066 private final String source; 067 private final Attribute url; 068 069 Source(Attribute url, String source) throws RSSpectException { 070 // make sure the url attribute is present 071 if (url == null || !url.getName().equals("url")) { 072 throw new RSSpectException( 073 "source elements MUST contain a url attribute."); 074 } 075 this.url = new Attribute(url.getName(), url.getValue()); 076 077 if (source == null || source.equals("")) { 078 throw new RSSpectException("source SHOULD NOT be blank."); 079 } 080 this.source = source; 081 } 082 083 Source(Source source) { 084 this.url = source.getUrl(); 085 this.source = source.source; 086 } 087 088 /** 089 * @return the url attribute. 090 */ 091 public Attribute getUrl() { 092 return new Attribute(url); 093 } 094 095 /** 096 * @return the source data. 097 */ 098 public String getSource() { 099 return source; 100 } 101 102 /** 103 * Shows the contents of the <source> element. 104 */ 105 @Override 106 public String toString() { 107 return "<source" + url + " >" + source + "</source>"; 108 } 109 110 @Override 111 public boolean equals(Object obj) { 112 if (obj == this) { 113 return true; 114 } 115 if (!(obj instanceof Source)) { 116 return false; 117 } 118 return this.toString().equals(obj.toString()); 119 } 120 121 @Override public int hashCode() { 122 return toString().hashCode(); 123 } 124 125}