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 java.net.MalformedURLException; |
21 | |
import java.net.URL; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Arrays; |
24 | |
import java.util.Collection; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class ItemScope extends Item { |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private final Map<String, List<ItemProp>> properties; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private final String id; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private final String[] refs; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private final URL type; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private final String itemId; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public ItemScope(String xpath, ItemProp[] itemProps, String id, String[] refs, String type, String itemId) { |
72 | 0 | super(xpath); |
73 | |
|
74 | 0 | if (itemProps == null) { |
75 | 0 | throw new NullPointerException("itemProps list cannot be null."); |
76 | |
} |
77 | 0 | if (type != null) { |
78 | |
try { |
79 | 0 | this.type = new URL(type); |
80 | 0 | } catch (MalformedURLException murle) { |
81 | 0 | throw new IllegalArgumentException("Invalid type '" + type + "', must be a valid URL."); |
82 | 0 | } |
83 | |
} else { |
84 | 0 | this.type = null; |
85 | |
} |
86 | 0 | this.id = id; |
87 | 0 | this.refs = refs; |
88 | 0 | this.itemId = itemId; |
89 | |
|
90 | 0 | final Map<String, List<ItemProp>> tmpProperties = new HashMap<String, List<ItemProp>>(); |
91 | 0 | for (ItemProp itemProp : itemProps) { |
92 | 0 | final String propName = itemProp.getName(); |
93 | 0 | List<ItemProp> propList = tmpProperties.get(propName); |
94 | 0 | if (propList == null) { |
95 | 0 | propList = new ArrayList<ItemProp>(); |
96 | 0 | tmpProperties.put(propName, propList); |
97 | |
} |
98 | 0 | propList.add(itemProp); |
99 | |
} |
100 | 0 | final Map<String, List<ItemProp>> properties = new HashMap<String, List<ItemProp>>(); |
101 | 0 | for (Map.Entry<String, List<ItemProp>> propertiesEntry : tmpProperties.entrySet()) { |
102 | 0 | properties.put( |
103 | |
propertiesEntry.getKey(), |
104 | |
|
105 | |
propertiesEntry.getValue() |
106 | |
); |
107 | |
} |
108 | |
|
109 | 0 | this.properties = properties; |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public Map<String, List<ItemProp>> getProperties() { |
116 | 0 | return properties; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public String getId() { |
123 | 0 | return id; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public String[] getRefs() { |
130 | 0 | return refs; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public URL getType() { |
137 | 0 | return type; |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public String getItemId() { |
144 | 0 | return itemId; |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
public String toJSON() { |
149 | 0 | StringBuilder sb = new StringBuilder(); |
150 | |
int i, j; |
151 | 0 | final Collection<List<ItemProp>> itemPropsList = properties.values(); |
152 | 0 | j = 0; |
153 | 0 | for (List<ItemProp> itemProps : itemPropsList) { |
154 | 0 | i = 0; |
155 | 0 | for (ItemProp itemProp : itemProps) { |
156 | 0 | sb.append(itemProp); |
157 | 0 | if (i < itemProps.size() - 1) { |
158 | 0 | sb.append(", "); |
159 | |
} |
160 | 0 | i++; |
161 | |
} |
162 | 0 | if (j < itemPropsList.size() - 1) { |
163 | 0 | sb.append(", "); |
164 | |
} |
165 | 0 | j++; |
166 | |
} |
167 | 0 | return String.format( |
168 | |
"{ " + |
169 | |
"\"xpath\" : \"%s\", \"id\" : %s, \"refs\" : %s, \"type\" : %s, \"itemid\" : %s, \"properties\" : [ %s ]" + |
170 | |
" }", |
171 | |
getXpath(), |
172 | |
id == null ? null : "\"" + id + "\"", |
173 | |
refs == null ? null : toJSON(refs), |
174 | |
type == null ? null : "\"" + type + "\"", |
175 | |
itemId == null ? null : "\"" + itemId + "\"", |
176 | |
sb.toString() |
177 | |
); |
178 | |
} |
179 | |
|
180 | |
@Override |
181 | |
public String toString() { |
182 | 0 | return toJSON(); |
183 | |
} |
184 | |
|
185 | |
@Override |
186 | |
public int hashCode() { |
187 | 0 | return |
188 | |
(properties == null ? 1 : properties.hashCode()) * |
189 | |
(id == null ? 1 : id.hashCode()) * 2 * |
190 | |
(refs == null ? 1 : refs.hashCode()) * 3 * |
191 | |
(type == null ? 1 : type.hashCode()) * 5 * |
192 | |
(itemId == null ? 1 : itemId.hashCode()); |
193 | |
|
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public boolean equals(Object obj) { |
198 | 0 | if (obj == null) { |
199 | 0 | return false; |
200 | |
} |
201 | 0 | if (obj == this) { |
202 | 0 | return true; |
203 | |
} |
204 | 0 | if (obj instanceof ItemScope) { |
205 | 0 | final ItemScope other = (ItemScope) obj; |
206 | 0 | return |
207 | |
super.getXpath().equals(other.getXpath()) |
208 | |
&& |
209 | |
(properties == null ? other.properties == null : properties.equals(other.properties)) |
210 | |
&& |
211 | |
(id == null ? other.id == null : id.equals(other.id)) |
212 | |
&& |
213 | |
(refs == null ? other.refs == null : Arrays.equals(refs, other.refs)) |
214 | |
&& |
215 | |
(type == null ? other.type == null : type.equals(other.type)) |
216 | |
&& |
217 | |
(itemId == null ? other.itemId == null : itemId.equals(other.itemId)); |
218 | |
} |
219 | 0 | return false; |
220 | |
} |
221 | |
|
222 | |
protected void acquireProperty(ItemProp itemProp) { |
223 | 0 | List<ItemProp> itemProps = properties.get(itemProp.getName()); |
224 | 0 | if (itemProps == null) { |
225 | 0 | itemProps = new ArrayList<ItemProp>(); |
226 | 0 | properties.put(itemProp.getName(), itemProps); |
227 | |
} |
228 | 0 | if (!itemProps.contains(itemProp)) itemProps.add(itemProp); |
229 | 0 | } |
230 | |
|
231 | |
protected void disownProperty(ItemProp itemProp) { |
232 | 0 | List<ItemProp> propList = properties.get(itemProp.getName()); |
233 | 0 | if (propList != null) propList.remove(itemProp); |
234 | 0 | } |
235 | |
|
236 | |
private String toJSON(String[] in) { |
237 | 0 | StringBuilder sb = new StringBuilder(); |
238 | 0 | sb.append('['); |
239 | 0 | for (int i = 0; i < in.length; i++) { |
240 | 0 | sb.append("\""); |
241 | 0 | sb.append(in[i]); |
242 | 0 | sb.append("\""); |
243 | 0 | if (i < in.length - 1) { |
244 | 0 | sb.append(", "); |
245 | |
} |
246 | |
} |
247 | 0 | sb.append(']'); |
248 | 0 | return sb.toString(); |
249 | |
} |
250 | |
|
251 | |
} |