1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.extractor.microdata; |
19 | |
|
20 | |
import org.apache.any23.util.StringUtils; |
21 | |
|
22 | |
import java.net.MalformedURLException; |
23 | |
import java.net.URL; |
24 | |
import java.text.ParseException; |
25 | |
import java.text.SimpleDateFormat; |
26 | |
import java.util.Date; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class ItemPropValue { |
34 | |
|
35 | 0 | private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public enum Type { |
41 | 0 | Plain, |
42 | 0 | Link, |
43 | 0 | Date, |
44 | 0 | Nested |
45 | |
} |
46 | |
|
47 | |
public static Date parseDateTime(String dateStr) throws ParseException { |
48 | 0 | return sdf.parse(dateStr); |
49 | |
} |
50 | |
|
51 | |
public static String formatDateTime(Date in) { |
52 | 0 | return sdf.format(in); |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private final Object content; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private final Type type; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 0 | public ItemPropValue(Object content, Type type) { |
72 | 0 | if(content == null) { |
73 | 0 | throw new NullPointerException("content cannot be null."); |
74 | |
} |
75 | 0 | if(type == null) { |
76 | 0 | throw new NullPointerException("type cannot be null."); |
77 | |
} |
78 | 0 | if(type == Type.Nested && ! (content instanceof ItemScope) ) { |
79 | 0 | throw new IllegalArgumentException( |
80 | |
"content must be an " + ItemScope.class + " when type is " + Type.Nested |
81 | |
); |
82 | |
} |
83 | 0 | if(type == Type.Date && !(content instanceof Date) ) { |
84 | 0 | throw new IllegalArgumentException( |
85 | |
"content must be a " + Date.class.getName() + " whe type is " + Type.Date |
86 | |
); |
87 | |
} |
88 | 0 | if(content instanceof String && ((String) content).trim().length() == 0) { |
89 | 0 | throw new IllegalArgumentException("Invalid content '" + content + "'"); |
90 | |
} |
91 | 0 | this.content = content; |
92 | 0 | this.type = type; |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public Object getContent() { |
99 | 0 | return content; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public Type getType() { |
106 | 0 | return type; |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public boolean isPlain() { |
113 | 0 | return type == Type.Plain; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public boolean isLink() { |
120 | 0 | return type == Type.Link; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public boolean isDate() { |
127 | 0 | return type == Type.Date; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public boolean isNested() { |
134 | 0 | return type == Type.Nested; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public boolean isInteger() { |
141 | 0 | if(type != Type.Plain) return false; |
142 | |
try { |
143 | 0 | Integer.parseInt((String) content); |
144 | 0 | return true; |
145 | 0 | } catch (Exception e) { |
146 | 0 | return false; |
147 | |
} |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public boolean isFloat() { |
154 | 0 | if(type != Type.Plain) return false; |
155 | |
try { |
156 | 0 | Float.parseFloat((String) content); |
157 | 0 | return true; |
158 | 0 | } catch (Exception e) { |
159 | 0 | return false; |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public boolean isNumber() { |
167 | 0 | return isInteger() || isFloat(); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public int getAsInteger() { |
176 | 0 | return Integer.parseInt((String) content); |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public float getAsFloat() { |
185 | 0 | return Float.parseFloat((String) content); |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
public Date getAsDate() { |
195 | 0 | return (Date) content; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public URL getAsLink() { |
204 | |
try { |
205 | 0 | return new URL((String) content); |
206 | 0 | } catch (MalformedURLException murle) { |
207 | 0 | throw new IllegalStateException("Error while parsing URI.", murle); |
208 | |
} |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
public ItemScope getAsNested() { |
216 | 0 | return (ItemScope) content; |
217 | |
} |
218 | |
|
219 | |
public String toJSON() { |
220 | |
String contentStr; |
221 | 0 | if(content instanceof String) { |
222 | 0 | contentStr = "\"" + StringUtils.escapeAsJSONString((String) content) + "\""; |
223 | 0 | } else if(content instanceof Date) { |
224 | 0 | contentStr = "\"" + sdf.format((Date) content) + "\""; |
225 | |
} else { |
226 | 0 | contentStr = content.toString(); |
227 | |
} |
228 | |
|
229 | 0 | return String.format( "{ \"content\" : %s, \"type\" : \"%s\" }", contentStr, type ); |
230 | |
} |
231 | |
|
232 | |
@Override |
233 | |
public String toString() { |
234 | 0 | return toJSON(); |
235 | |
} |
236 | |
|
237 | |
@Override |
238 | |
public int hashCode() { |
239 | 0 | return content.hashCode() * type.hashCode() * 2; |
240 | |
} |
241 | |
|
242 | |
@Override |
243 | |
public boolean equals(Object obj) { |
244 | 0 | if(obj == null) { |
245 | 0 | return false; |
246 | |
} |
247 | 0 | if(obj == this) { |
248 | 0 | return true; |
249 | |
} |
250 | 0 | if(obj instanceof ItemPropValue) { |
251 | 0 | final ItemPropValue other = (ItemPropValue) obj; |
252 | 0 | return content.equals(other.content) && type.equals(other.type); |
253 | |
} |
254 | 0 | return false; |
255 | |
} |
256 | |
|
257 | |
} |