View Javadoc

1   package yarfraw.core.datamodel;
2   
3   import java.io.IOException;
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Locale;
8   import java.util.Map;
9   
10  import javax.xml.namespace.QName;
11  import javax.xml.parsers.ParserConfigurationException;
12  
13  import org.w3c.dom.Element;
14  import org.xml.sax.SAXException;
15  
16  import yarfraw.utils.ValidationUtils;
17  import yarfraw.utils.XMLUtils;
18  
19  /***
20   * <li>Rss 1.0 & Rss 2.0 - only email filed is used, other fields are ignored.
21   * </li>
22   * <li>Atom 1.0 - person constructs
23   * <br/>http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html#rfc.section.3
24   * </li>
25   * @author jliang
26   *
27   */
28  public class Person extends AbstractBaseObject{
29    private static final long serialVersionUID = 20070927L;
30    private String _emailOrText;
31    private String _uri;
32    private String _name;
33    
34    public Person(){}
35    
36    /***
37     * Constructs a new Person object with the input string as its email or text content.
38     * @param emailOrText <li>Rss 1.0 - email address or text content of the corresponding person element.
39     * </li>
40     * <li>Rss 2.0 - should be an email address of the corresponding person element.
41     * </li>
42     * <li>Atom 1.0 - should be an email address of the corresponding person element.
43     * </li>
44     */
45    public Person(String emailOrText){  
46      _emailOrText = emailOrText;
47    }
48    
49    /***
50     * <li>Rss 1.0 - email address or text content of the corresponding person element.
51     * </li>
52     * <li>Rss 2.0 - should be an email address of the corresponding person element.
53     * </li>
54     * <li>Atom 1.0 - should be an email address of the corresponding person element.
55     * </li>
56     * @return
57     */
58    public String getEmailOrText() {
59      return _emailOrText;
60    }
61  
62    /***
63     * <li>Rss 1.0 - email address or text content of the corresponding person element.
64     * </li>
65     * <li>Rss 2.0 - should be an email address of the corresponding person element.
66     * </li>
67     * <li>Atom 1.0 - should be an email address of the corresponding person element.
68     * </li>
69     * @param emailOrText
70     */
71    public Person setEmailOrText(String emailOrText) {
72      _emailOrText = emailOrText;
73      return this;
74    }
75  
76    /***
77     * <b>Atom 1.0 only</b><br/>
78     * The "atom:uri" element's content conveys an IRI associated with the person. Person constructs MAY contain an atom:uri element, but MUST NOT contain more than one. The content of atom:uri in a Person construct MUST be an IRI reference [RFC3987].
79     * @return
80     */
81    public String getUri() {
82      return _uri;
83    }
84  /***
85   * <b>Atom 1.0 only</b><br/>
86   * The "atom:uri" element's content conveys an IRI associated with the person. Person constructs MAY contain an atom:uri element, but MUST NOT contain more than one. The content of atom:uri in a Person construct MUST be an IRI reference [RFC3987].
87   * @param uri any valid uri
88   * @return this
89   */
90    public Person setUri(String uri) {
91      _uri = uri;
92      return this;
93    }
94  
95    /***
96     * <b>Atom 1.0 only</b><br/>
97     * The "atom:name" element's content conveys a human-readable name for the person. The content of atom:name is Language-Sensitive. Person constructs MUST contain exactly one "atom:name" element.
98     * @return
99     */
100   public String getName() {
101     return _name;
102   }
103 
104   /***
105    * <b>Atom 1.0 only</b><br/>
106    * The "atom:name" element's content conveys a human-readable name for the person. The content of atom:name is Language-Sensitive. Person constructs MUST contain exactly one "atom:name" element.
107    * @param name value of the name element
108    * @return this
109    */
110   public Person setName(String name) {
111     _name = name;
112     return this;
113   }
114 
115   ////////////////////////Common setters///////////////////////
116   /***
117    * Any other attribute that is not in the RSS 2.0 specs.
118    */
119   public Person setOtherAttributes(Map<QName, String> otherAttributes) {
120     _otherAttributes = otherAttributes;
121     return this;
122   }
123   /***
124    * Add an attribute that is not in the RSS 2.0 specs.
125    */
126   public Person addOtherAttributes(QName namespace, String attribute) {
127     if(_otherAttributes == null){
128       _otherAttributes = new HashMap<QName, String>();
129     }
130     _otherAttributes.put(namespace, attribute);
131     return this;
132   }
133   
134   /***
135    * Other additional elements that are not in the Rss specs.<br/>
136    * **Note** The element should not have an empty namespace to avoid collision with the specs elements.
137    */
138   public Person setOtherElements(List<Element> otherElements) {
139     _otherElements = otherElements;
140     return this;
141   }
142   /***
143    * Add an element that is not specified in the Rss specs.<br/>
144    * **Note** The element should not have an empty namespace to avoid collision with the specs elements.
145    * @param element - any element
146    */
147   public Person addOtherElement(Element element){
148     if(_otherElements == null){
149       _otherElements = new ArrayList<Element>();
150     }
151     _otherElements.add(element);
152     return this;
153   }
154   
155   /***
156    * Add an element that is not specified in the Rss specs.<br/>
157    * **Note** The element should not have an empty namespace to avoid collision with the specs elements.
158    * 
159    * @param xmlString - any element
160    * @throws ParserConfigurationException 
161    * @throws IOException 
162    * @throws SAXException 
163    */
164   public Person addOtherElement(String xmlString) throws SAXException, IOException, ParserConfigurationException{
165     return addOtherElement(XMLUtils.parseXml(xmlString, false, false).getDocumentElement());
166   }
167   
168   /***
169    * <b>Atom 1.0 only</b><br/>
170    * Any element defined by this specification MAY have an xml:base attribute 
171    * [W3C.REC-xmlbase-20010627]. When xml:base is used in an Atom Document, 
172    * it serves the function described in section 5.1.1 of [RFC3986], establishing 
173    * the base URI (or IRI) for resolving any relative references found within the 
174    * effective scope of the xml:base attribute.
175    * @param base
176    * @return
177    */
178   public Person setBase(String base) {
179     _base = base;
180     return this;
181   }
182   /***
183    * <li>Rss 2.0 - &lt;language> element. 
184    * The language the channel is written in. This allows aggregators to group 
185    * all Italian language sites, for example, on a single page. A list of allowable 
186    * values for this element, as provided by Netscape, is here. You may also use values 
187    * defined by the W3C.
188    * Only &lt;channel> support this element.</li>
189    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
190    * Only &lt;channel> and &lt;item> support this element. </li>
191    * <li>Atom 1.0 - 'lang' attribute</li>
192    * <br/>
193    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
194    * @param lang
195    * @return
196    */
197   public Person setLang(String lang) {
198     _lang = lang;
199     return this;
200   }
201   
202   /***
203    * <li>Rss 2.0 - &lt;language> element. 
204    * The language the channel is written in. This allows aggregators to group 
205    * all Italian language sites, for example, on a single page. A list of allowable 
206    * values for this element, as provided by Netscape, is here. You may also use values 
207    * defined by the W3C.
208    * Only &lt;channel> support this element.</li>
209    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
210    * Only &lt;channel> and &lt;item> support this element. </li>
211    * <li>Atom 1.0 - 'lang' attribute</li>
212    * <br/>
213    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
214    * @param lang
215    * @return
216    */
217   public Person setLang(Locale lang) {
218     _lang = lang.getLanguage();
219     return this;
220   }
221   ////////////////////////Common setters///////////////////////
222   
223   
224   @Override
225   public void validate(FeedFormat format) throws ValidationException {
226     ValidationUtils.validateNotNull("EmailOrText should not be null", _emailOrText);
227   }
228   
229 }