1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package eteg.sinon.core;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /***
29 * Class that represents a data extraction from a page.
30 * Classe que implementa a extração de um determinado dado de uma página.
31 *
32 * @author <a href="mailto:thiagohp at users.sourceforge.net">Thiago H. de Paula Figueiredo</a>
33 * @author Last modified by $Author: thiagohp $
34 * @version $Revision: 1.2 $
35 */
36 public class DataExtraction implements Step {
37
38 /***
39 * Property that is, at the same time, the data extraction identifier
40 * and the name of the variable that will receive the extracted data.
41 */
42 private String id;
43
44 /***
45 * Tells if the extracted value must be trimmed after it is extracted.
46 * (using <code>String.trim()</code>).
47 * Default: true.
48 */
49 private boolean trimValueNeeded;
50
51 /***
52 * Tells if this data extraction needs the current position in page to
53 * be reset.
54 * Default: false.
55 */
56 private boolean resetPositionNeeded;
57
58 /***
59 * Tells if the variable that will receive the extracted data is
60 * simple (just one value) or multivalued (a list of values).
61 */
62 private boolean multivalued;
63
64 /***
65 * Tells if this data extraction must stop when one of its positioning
66 * steps fails.
67 * Default: true
68 */
69 private boolean failOnError;
70
71 /***
72 * Tells what value the data extraction property must receive if
73 * some positioning step fails. Ignored if <code>failOnError</code> is
74 * <code>true</code>.
75 * Default: empty string.
76 */
77 private String valueOnError;
78
79 /***
80 * Ordered list of the extraction steps that comprise this data extraction.
81 */
82 private List steps;
83
84 /***
85 * Tells if this object is a copy of another.
86 */
87 private boolean copy;
88
89 /***
90 * Tells if the variable of this data extraction accepts duplicate
91 * values. If not, duplicate values are ignored. Only used if the
92 * variable of this data extraction is multivalued.
93 * Default: true.
94 */
95 private boolean allowDuplicates;
96
97 /***
98 * Constructor without parameters.
99 */
100 public DataExtraction() {
101
102 id = null;
103 trimValueNeeded = true;
104 resetPositionNeeded = false;
105 steps = new ArrayList();
106 copy = false;
107 multivalued = false;
108 failOnError = true;
109 valueOnError = "";
110 allowDuplicates = true;
111
112 }
113
114 /***
115 * Returns the value of the <code>id</code> property.
116 * @return a <code>String</code>.
117 */
118 public String getId() {
119 return id;
120 }
121
122 /***
123 * Sets the value of the <code>id</code> property.
124 * @param id the new <code>id</code> value.
125 */
126 public void setId(String id) {
127 this.id = id;
128 }
129
130 /***
131 * Returns the value of the <code>trimValueNeeded</code> property.
132 * @return a <code>boolean</code>.
133 */
134 public boolean isTrimValueNeeded() {
135 return trimValueNeeded;
136 }
137
138 /***
139 * Sets the value of the <code>trimValueNeeded</code> property.
140 * @param trimValueNeeded the new <code>trimValueNeeded</code> value.
141 */
142 public void setTrimValueNeeded(boolean trimValueNeeded) {
143 this.trimValueNeeded = trimValueNeeded;
144 }
145
146 /***
147 * Returns the value of the <code>resetPositionNeeded</code> property.
148 * @return a <code>boolean</code>.
149 */
150 public boolean isResetPositionNeeded() {
151 return resetPositionNeeded;
152 }
153
154 /***
155 * Sets the value of the <code>resetPositionNeeded</code> property.
156 * @param resetPositionNeeded the new <code>resetPositionNeeded</code>
157 * value.
158 */
159 public void setResetPositionNeeded(boolean resetPositionNeeded) {
160 this.resetPositionNeeded = resetPositionNeeded;
161 }
162
163 /***
164 * Returns the value of the <code>multivalued</code> property.
165 * @return a <code>boolean</code>.
166 */
167 public boolean isMultivalued() {
168 return multivalued;
169 }
170
171 /***
172 * Sets the value of the <code>multiValued</code> property.
173 * @param multiValued the new <code>multivalued</code> value.
174 */
175 public void setMultivalued(boolean multiValued) {
176 this.multivalued = multiValued;
177 }
178
179 /***
180 * Returns the value of the <code>failOnError</code> property.
181 * @return a <code>boolean</code>.
182 */
183 public boolean isFailOnError() {
184 return failOnError;
185 }
186
187 /***
188 * Sets the value of the <code>failOnError</code> property.
189 * @param failOnError the new <code>failOnError</code> value.
190 */
191 public void setFailOnError(boolean failOnError) {
192 this.failOnError = failOnError;
193 }
194
195 /***
196 * Returns the value of the <code>valueOnError</code> property.
197 * @return a <code>String</code>.
198 */
199 public String getValueOnError() {
200 return valueOnError;
201 }
202
203 /***
204 * Sets the value of the <code>valueOnError</code> property.
205 * @param valueOnError the new <code>valueOnError</code> value.
206 */
207 public void setValueOnError(String valueOnError) {
208 this.valueOnError = valueOnError;
209 }
210
211 /***
212 * Adds a {@link Step} to this data extraction.
213 * @param step a {@link Step} instance.
214 * @throws IllegalArgumentException if <code>step</code> is
215 * <code>null</code>.
216 */
217 public void addStep(Step step) {
218
219 if (step == null) {
220 throw new IllegalArgumentException("Parameter step cannot be null");
221 }
222
223 steps.add(step);
224
225 }
226
227 /***
228 * Returns the value of the <code>steps</code> property.
229 * @return a {@link Step} array.
230 */
231 public Step[] getSteps() {
232
233 Step[] stepArray = new Step[steps.size()];
234 steps.toArray(stepArray);
235
236 return stepArray;
237
238 }
239
240 /***
241 * Returns the value of the <code>allowDuplicates</code> property.
242 * @return a <code>boolean</code>.
243 */
244 public boolean isAllowDuplicates() {
245 return allowDuplicates;
246 }
247
248 /***
249 * Sets the value of the <code>allowDuplicates</code> property.
250 * @param allowDuplicates the new <code>allowDuplicates</code> value.
251 */
252 public void setAllowDuplicates(boolean allowDuplicates) {
253 this.allowDuplicates = allowDuplicates;
254 }
255
256 /***
257 * Creates and returns a copy of this object.
258 * @return a {@link DataExtraction} instance.
259 */
260 public DataExtraction copy() {
261
262 DataExtraction copy = new DataExtraction();
263
264 copy.setId(this.id);
265 copy.setResetPositionNeeded(this.resetPositionNeeded);
266 copy.setTrimValueNeeded(this.trimValueNeeded);
267 copy.setMultivalued(this.multivalued);
268 copy.copy = true;
269 copy.steps.addAll(this.steps);
270
271 return copy;
272
273 }
274
275 /***
276 * Returns <code>true</code>.
277 * @return <code>true</code>.
278 */
279 public boolean isDataExtraction() {
280 return true;
281 }
282
283 /***
284 * Returns <code>false</code>.
285 * @return <code>false</code>.
286 */
287 public boolean isPositioningStep() {
288 return false;
289 }
290
291 /***
292 * Returns <code>false</code>.
293 * @return <code>false</code>.
294 */
295 public boolean isAction() {
296 return false;
297 }
298
299 /***
300 * Returns <code>false</code>.
301 * @return <code>false</code>.
302 */
303 public boolean isLoop() {
304 return false;
305 }
306
307 }