1 package yarfraw.core.datamodel;
2
3 import java.io.Serializable;
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 import java.util.Map.Entry;
10
11 import javax.xml.namespace.QName;
12
13 import org.apache.commons.lang.builder.EqualsBuilder;
14 import org.apache.commons.lang.builder.HashCodeBuilder;
15 import org.apache.commons.lang.builder.ToStringBuilder;
16 import org.apache.commons.lang.builder.ToStringStyle;
17 import org.w3c.dom.Element;
18
19 import yarfraw.utils.XMLUtils;
20 /***
21 * An abstract base object for the core data model
22 */
23 abstract class AbstractBaseObject implements Serializable{
24 protected String _base;
25 protected String _lang;
26 protected String _resource;
27 protected String _about;
28
29
30 protected Map<QName, String> _otherAttributes = new HashMap<QName, String>(0);
31 protected List<Element> _otherElements = new ArrayList<Element>(0);
32
33 /***
34 * This maps to the 'base' attribute that is common in all Atom 1.0 elements.
35 * Other {@link FeedFormat} will ignore this attribute.
36 *
37 * @return - attribute value.
38 */
39 public String getBase() {
40 return _base;
41 }
42 /***
43 * The language attribute indicates the language that is used by the enclosed
44 * element.
45 *
46 * @return - attribute value.
47 */
48 public String getLang() {
49 return _lang;
50 }
51
52 /***
53 * The language attribute indicates the language that is used by the enclosed
54 * element.
55 *
56 * @return - a new Locale Object by parsing the lang attribute.
57 */
58 public Locale getLangAsLocale() {
59 if(_lang == null)
60 return null;
61
62 return new Locale(_lang);
63 }
64
65 /***
66 * This maps to the optional 'resource' attribute that present in some Rss 1.0 elements.
67 * Other {@link FeedFormat} will ignore this attribute.
68 *
69 * @return - attribute value.
70 */
71 public String getResource() {
72 return _resource;
73 }
74
75 /***
76 * This maps to the required 'about' attribute that present of all second level elements
77 * (channel, image, item, and textinput).
78 * Other {@link FeedFormat} will ignore this attribute.
79 *
80 * @return - attribute value.
81 */
82 public String getAbout() {
83 return _about;
84 }
85
86 /***
87 * Other additional elements that are not in the Rss specs.
88 */
89 public List<Element> getOtherElements() {
90 return _otherElements;
91 }
92
93 /***
94 * Search through the other element list and return the first element that matches
95 * both input the namespaceURI and the localName.
96 *
97 * @param namespaceURI - namespaceURI of the element to be search for
98 * @param localName - localName of the element
99 * @return - null if no matching element is found,
100 * the matching element otherwise.
101 */
102 public Element getElementByNS(String namespaceURI, String localName){
103 return XMLUtils.getElementByNS(_otherElements, namespaceURI, localName);
104 }
105
106 /***
107 * Search through the other element list and return the FIRST element that matches
108 * the input localName.
109 *
110 * @param localName - localName of the element
111 * @return - null if no matching element is found,
112 * the matching element otherwise.
113 */
114 public Element getElementByLocalName(String localName){
115 return XMLUtils.getElementByLocalName(_otherElements, localName);
116 }
117
118 /***
119 * Any other attribute that is not in the RSS specs.
120 */
121 public Map<QName, String> getOtherAttributes() {
122 return _otherAttributes;
123 }
124
125 /***
126 * Search for attributes that are not in the spec by its local name.
127 * @param localName localName of the attribute
128 * @return null if attribute is not found, the value of the attribute otherwise
129 */
130 public String getAttributeValueByLocalName(String localName){
131 if(_otherAttributes != null && localName != null){
132 for(Entry<QName, String> e : _otherAttributes.entrySet()){
133 if(localName.equals(e.getKey().getLocalPart())){
134 return e.getValue();
135 }
136 }
137 }
138 return null;
139 }
140 /***
141 * Search for attributes that are not in the spec by its {@link QName}.
142 * @param name {@link QName} of the attribute
143 * @return null if attribute is not found, the value of the attribute otherwise
144 */
145 public String getAttributeValueByQName(QName name){
146 if(_otherAttributes != null){
147 return _otherAttributes.get(name);
148 }
149 return null;
150 }
151
152 @Override
153 public String toString(){
154 return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
155 }
156 @Override
157 public boolean equals(Object other){
158 return EqualsBuilder.reflectionEquals(this, other);
159 }
160 @Override
161 public int hashCode(){
162 return HashCodeBuilder.reflectionHashCode(this);
163 }
164
165 public abstract void validate(FeedFormat format) throws ValidationException;
166 }