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   * This maps to both <guid> in Rss and <id> in Atom.
13   * Note: it is not supported by Rss 1.0 format.
14   * 
15   * <p/>
16   * Rss 2.0 description:
17   * <br/>
18   * {@link Guid} is an optional sub-element of {@link ItemEntry}.<br/>
19   * guid stands for globally unique identifier. It's a string that uniquely identifies the item. When present, an aggregator may choose to use this string to determine if an item is new.
20   * &lt;guid>http://some.server.com/weblogItem3207&lt;/guid>
21   * <p/>
22   * There are no rules for the syntax of a guid. Aggregators must view them as a string. It's up to the source of the feed to establish the uniqueness of the string.
23   * <p/>
24   * If the guid element has an attribute named "isPermaLink" with a value of true, 
25   * the reader may assume that it is a permalink to the item, that is, 
26   * a url that can be opened in a Web browser, that points to the full item described by the 
27   * {@link ItemEntry} element.
28   * <br/> 
29   * An example:
30   * &lt;guid isPermaLink="true">http://inessential.com/2002/09/01.php#a2&lt;/guid>
31   * <p/>
32   * isPermaLink is optional, its default value is true. If its value is false, the guid may not be assumed to be a url, or a url to anything in particular.
33   * <p/>
34   * 
35   * Atom 1.0 description:
36   * <br/>
37   * The "atom:id" element conveys a permanent, universally unique identifier for an entry or feed.
38   * 
39   * @author jliang
40   *
41   */
42  public class Id extends AbstractBaseObject{
43    private static final long serialVersionUID = 20070927L;
44    private String _idValue;
45    private Boolean _isPermaLink = true;
46  
47    public Id(){super();}
48    
49    public Id(String idValue){
50      this();
51      setIdValue(idValue);
52    }
53    
54    /***
55     * @return A value that uniquely identify a {@link ChannelFeed} or a {@link ItemEntry}. 
56     */
57    public String getIdValue() {
58      return _idValue;
59    }
60  
61    /***
62     * @param idValue - A value that uniquely identify a {@link ChannelFeed} or a {@link ItemEntry}.
63     * @return - this
64     */
65    public Id setIdValue(String idValue) {
66      _idValue = idValue;
67      return this;
68    }
69  
70    /***
71     * This field is only used by Rss 2.0.
72     * 
73     * @return - If true, the reader may assume that it is a permalink to the item, that is, 
74     * a url that can be opened in a Web browser, that points to the full item described by the
75     * {@link ItemEntry} element.
76     */
77    public Boolean isPermaLink() {
78      return _isPermaLink;
79    }
80    /***
81     * This field is only used by Rss 2.0.
82     * 
83     * @param isPermaLink If true, the reader may assume that it is a permalink to the item, that is, 
84     * a url that can be opened in a Web browser, that points to the full item described by the
85     * {@link ItemEntry} element.
86     * @return this
87     */
88    public Id setPermaLink(Boolean isPermaLink) {
89        _isPermaLink = isPermaLink == null || isPermaLink;
90        return this;
91    }
92    
93    ////////////////////////Common setters///////////////////////
94    /***
95     * Any other attribute that is not in the RSS 2.0 specs.
96     */
97    public Id setOtherAttributes(Map<QName, String> otherAttributes) {
98      _otherAttributes = otherAttributes;
99      return this;
100   }
101   /***
102    * Add an attribute that is not in the RSS 2.0 specs.
103    */
104   public Id addOtherAttributes(QName namespace, String attribute) {
105     if(_otherAttributes == null){
106       _otherAttributes = new HashMap<QName, String>();
107     }
108     _otherAttributes.put(namespace, attribute);
109     return this;
110   }
111   
112 
113   /***
114    * <b>Atom 1.0 only</b><br/>
115    * Any element defined by this specification MAY have an xml:base attribute 
116    * [W3C.REC-xmlbase-20010627]. When xml:base is used in an Atom Document, 
117    * it serves the function described in section 5.1.1 of [RFC3986], establishing 
118    * the base URI (or IRI) for resolving any relative references found within the 
119    * effective scope of the xml:base attribute.
120    * @param base
121    * @return
122    */
123   public Id setBase(String base) {
124     _base = base;
125     return this;
126   }
127   /***
128    * <li>Rss 2.0 - &lt;language> element. 
129    * The language the channel is written in. This allows aggregators to group 
130    * all Italian language sites, for example, on a single page. A list of allowable 
131    * values for this element, as provided by Netscape, is here. You may also use values 
132    * defined by the W3C.
133    * Only &lt;channel> support this element.</li>
134    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
135    * Only &lt;channel> and &lt;item> support this element. </li>
136    * <li>Atom 1.0 - 'lang' attribute</li>
137    * <br/>
138    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
139    * @param lang
140    * @return
141    */
142   public Id setLang(String lang) {
143     _lang = lang;
144     return this;
145   }
146   /***
147    * <li>Rss 2.0 - &lt;language> element. 
148    * The language the channel is written in. This allows aggregators to group 
149    * all Italian language sites, for example, on a single page. A list of allowable 
150    * values for this element, as provided by Netscape, is here. You may also use values 
151    * defined by the W3C.
152    * Only &lt;channel> support this element.</li>
153    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
154    * Only &lt;channel> and &lt;item> support this element. </li>
155    * <li>Atom 1.0 - 'lang' attribute</li>
156    * <br/>
157    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
158    * @param lang
159    * @return
160    */
161   public Id setLang(Locale lang) {
162     _lang = lang.getLanguage();
163     return this;
164   }
165   ////////////////////////Common setters///////////////////////
166   @Override
167   public void validate(FeedFormat format) throws ValidationException {
168     if(format == FeedFormat.RSS10){
169       return;
170     }
171     ValidationUtils.validateNotNull("Id Value should not be null", _idValue);
172     if(format == FeedFormat.ATOM10){
173       ValidationUtils.validateUri("Id value should be an valid url", _idValue);
174     }
175   }
176 }