View Javadoc

1   package yarfraw.core.datamodel;
2   
3   import java.io.IOException;
4   import java.util.Locale;
5   
6   import javax.xml.parsers.ParserConfigurationException;
7   
8   import org.w3c.dom.Element;
9   import org.xml.sax.SAXException;
10  
11  import yarfraw.utils.ValidationUtils;
12  import yarfraw.utils.XMLUtils;
13  
14  /***
15   * This class is mapped to element of type xs:string as well as to text constructs element in Atom 1.0.
16   * <br/>
17   * Only the text content is used for Rss 1.0 and Rss 2.0, all other fields are ignored. 
18   * 
19   * <br/>
20   * for information about Atom 1.0, see http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html#rfc.section.4.1.2
21   * 
22   * @author jliang
23   *
24   */
25  public class Text extends AbstractBaseObject{
26    private static final long serialVersionUID = 20070927L;
27    public enum TextType{
28      text, html, xhtml
29    }
30    private TextType _type = TextType.text;
31    private Element _xhtmlDiv;
32    private String _text;
33    
34    public Text(){}
35    
36    /***
37     * This constructs a {@link Text} of type 'text'
38     */
39    public Text(String text){
40      _type = TextType.text;
41      setText(text);
42    }
43    
44    /***
45     * The text content of this text element.
46     * @return
47     */
48    public String getText() {
49      return _text;
50    }
51  
52    /***
53     * 
54     * @param text The text content of this text element.
55     * @return this
56     */
57    public Text setText(String text) {
58      _text = text;
59      return this;
60    }
61  
62  /***
63   * <b>Atom 1.0 only </b> <br/>
64   * The single xhtml div element if the text construct is an xhtml construct 
65   */
66    public Element getXhtmlDiv() {
67      return _xhtmlDiv;
68    }
69    /***
70     * <b>Atom 1.0 only </b> <br/>
71     * The single xhtml div element if the text construct is an xhtml construct 
72     */
73    public Text setXhtmlDiv(Element xhtmlDiv) {
74      _xhtmlDiv = xhtmlDiv;
75      _type = TextType.xhtml;
76      return this;
77    }
78    
79    /***
80     * <b>Atom 1.0 only </b> <br/>
81     * The single xhtml div element if the text construct is an xhtml construct.
82     * <br/>
83     * This method parses the input xhtml string into an {@link Element} and put it 
84     * to the xhtmlDiv field. Therefore it should be a single &lt;div> element.
85     * 
86     * @param xhtmlDiv any valid xhtml string
87     * @return this
88     * @throws SAXException
89     * @throws IOException
90     * @throws ParserConfigurationException
91     */
92    public Text setXhtmlDiv(String xhtmlDiv) throws SAXException, IOException, ParserConfigurationException {
93      return setXhtmlDiv(XMLUtils.parseXml(xhtmlDiv, false, false).getDocumentElement());
94    }
95  
96    /***
97     * Type of the text. 
98     * @param textType
99     */
100   public Text(TextType textType){
101     setType(textType);
102   }
103   
104   /***
105    * Type of the text.
106    * @return
107    */
108   public TextType getType() {
109     return _type;
110   }
111   /***
112    * Type of the text.
113    * @return
114    */
115   public Text setType(TextType type) {
116     _type = type;
117     return this;
118   }
119 
120 
121   /***
122    * <b>Atom 1.0 only</b><br/>
123    * Any element defined by this specification MAY have an xml:base attribute 
124    * [W3C.REC-xmlbase-20010627]. When xml:base is used in an Atom Document, 
125    * it serves the function described in section 5.1.1 of [RFC3986], establishing 
126    * the base URI (or IRI) for resolving any relative references found within the 
127    * effective scope of the xml:base attribute.
128    * @param base
129    * @return
130    */
131   public Text setBase(String base) {
132     _base = base;
133     return this;
134   }
135   /***
136    * <li>Rss 2.0 - &lt;language> element. 
137    * The language the channel is written in. This allows aggregators to group 
138    * all Italian language sites, for example, on a single page. A list of allowable 
139    * values for this element, as provided by Netscape, is here. You may also use values 
140    * defined by the W3C.
141    * Only &lt;channel> support this element.</li>
142    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
143    * Only &lt;channel> and &lt;item> support this element. </li>
144    * <li>Atom 1.0 - 'lang' attribute</li>
145    * <br/>
146    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
147    * @param lang
148    * @return
149    */
150   public Text setLang(String lang) {
151     _lang = lang;
152     return this;
153   }
154   
155   /***
156    * <li>Rss 2.0 - &lt;language> element. 
157    * The language the channel is written in. This allows aggregators to group 
158    * all Italian language sites, for example, on a single page. A list of allowable 
159    * values for this element, as provided by Netscape, is here. You may also use values 
160    * defined by the W3C.
161    * Only &lt;channel> support this element.</li>
162    * <li>Rss 1.0 - &lt;dc:language> element. A language of the intellectual content of the resource.
163    * Only &lt;channel> and &lt;item> support this element. </li>
164    * <li>Atom 1.0 - 'lang' attribute</li>
165    * <br/>
166    * Note: for Rss 2.0 and Rss 1.0, only &lt;channel> and &lt;item>
167    * @param lang
168    * @return
169    */
170   public Text setLang(Locale lang) {
171     _lang = lang.getLanguage();
172     return this;
173   }
174   @Override
175   public void validate(FeedFormat format) throws ValidationException {
176     ValidationUtils.validateNotNull("Text value should not be null", _text);
177   }
178 }