View Javadoc

1   /*
2    * $Header: /cvsroot/sinon/sinon/src/java/eteg/sinon/core/Catalog.java,v 1.2 2005/06/21 14:25:07 thiagohp Exp $
3    * $Revision: 1.2 $
4    * $Date: 2005/06/21 14:25:07 $
5    * $Author: thiagohp $
6    *
7    * =============================================================================
8    *
9    * Copyright 2004-2005 Eteg Internet Ltda. (http://www.eteg.com.br)
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   *
15   *     http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   */
23  package eteg.sinon.core;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Properties;
28  
29  /***
30   * Class that represents a catalog in Sinon. In other words, a catalog is a
31   * definition of how the extraction of some data is made in some Web site.
32   *
33   * @author <a href="mailto:thiagohp at users.sourceforge.net">Thiago H. de Paula Figueiredo</a>
34   * @author Last modified by $Author: thiagohp $
35   * @version $Revision: 1.2 $
36   */
37  public class Catalog {
38  
39      /***
40       * Catalog identifier.
41       */
42      private String id;
43  
44      /***
45       * Catalog specific configurations.
46       */
47      private CollectorConfiguration configuration;
48  
49      /***
50       * List of parameter sets. These are global in this catalog scope.
51       */
52      private List parameterSets;
53  
54      /***
55       * List of pages of this catalog.
56       */
57      private List pages;
58  
59      /***
60       * List of initial pages of this catalog.
61       */
62      private List startPages;
63  
64      /***
65       * List of global data extractions of this catalog.
66       */
67      private List dataExtractions;
68  
69      /***
70       * Catalog properties.
71       */
72      private Properties properties;
73  
74      /***
75       * Contructors with no parameters.
76       */
77      public Catalog() {
78  
79          id = null;
80          configuration = null;
81          parameterSets = new ArrayList();
82          pages = new ArrayList();
83          startPages = new ArrayList();
84          dataExtractions = new ArrayList();
85          properties = new Properties();
86  
87      }
88  
89      /***
90       * Returns the value of the <code>id</code> property.
91       * @return a <code>String</code>.
92       */
93      public String getId() {
94          return id;
95      }
96  
97      /***
98       * Sets the value of the <code>id</code> property.
99       * @param id the new <code>id</code> value.
100      */
101     public void setId(String id) {
102         this.id = id;
103     }
104 
105     /***
106      * Returns the value of the <code>configuration</code> property.
107      * @return a {@link CollectorConfiguration}.
108      */
109     public CollectorConfiguration getConfiguration() {
110         return configuration;
111     }
112 
113     /***
114      * Sets the value of the <code>configuration</code> property.
115      * @param configuration the new {@link CollectorConfiguration} value.
116      */
117     public void setConfiguration(CollectorConfiguration configuration) {
118         this.configuration = configuration;
119     }
120 
121     /***
122      * Adds a {@link ParameterSet} to this catalog.
123      * @param parameterSet a {@link ParameterSet} instance.
124      */
125     public void addParameterSet(ParameterSet parameterSet) {
126         parameterSets.add(parameterSet);
127     }
128 
129     /***
130      * Removes a {@link ParameterSet} from this catalog.
131      * @param parameterSet a {@link ParameterSet} instance.
132      */
133     public void removeParameterSet(ParameterSet parameterSet) {
134         parameterSets.remove(parameterSet);
135     }
136 
137     /***
138      * Returns the value of the <code>parameterSets</code> property.
139      * @return a {@link ParameterSet} array.
140      */
141     public ParameterSet[] getParameterSets() {
142 
143         ParameterSet[] parameterSet =
144                 new ParameterSet[parameterSets.size()];
145 
146         parameterSets.toArray(parameterSet);
147 
148         return parameterSet;
149 
150     }
151 
152     /***
153      * Returns the {@link ParameterSet} whose identifier is <code>id</code>.
154      * @param id a <code>String</code>.
155      * @return a {@link ParameterSet} instance or <code>null</code> if
156      * there is no {@link ParameterSet} whose identifier is <code>id</code>.
157      * @throws IllegalArgumentException if <code>id</code> is
158      * <code>null</code>.
159      */
160     public ParameterSet getParameterSet(String id) {
161 
162         if (id == null) {
163 
164             throw new IllegalArgumentException(
165                     "Parameter id can't be null");
166 
167         }
168 
169         final int size = parameterSets.size();
170         ParameterSet parameterSet;
171 
172         for (int i = 0; i < size; i++) {
173 
174             parameterSet = (ParameterSet) parameterSets.get(i);
175 
176             if (id.equals(parameterSet.getId())) {
177                 return parameterSet;
178             }
179 
180         }
181 
182         return null;
183 
184     }
185 
186     /***
187      * Adds a {@link Page} to this catalog.
188      * @param page a {@link Page} instance.
189      */
190     public void addPage(Page page) {
191         pages.add(page);
192     }
193 
194     /***
195      * Removes a {@link Page} from this catalog.
196      * @param page a {@link Page} instance.
197      */
198     public void removePage(Page page) {
199         pages.remove(page);
200     }
201 
202     /***
203      * Returns the value of the <code>pages</code> property.
204      * @return a <code>String</code>.
205      */
206     public Page[] getPages() {
207 
208         Page[] pageArray = new Page[pages.size()];
209 
210         pages.toArray(pageArray);
211 
212         return pageArray;
213 
214     }
215 
216     /***
217      * Returns the {@link Page} whose identifier is <code>id</code>.
218      * @param id a <code>String</code>.
219      * @return a {@link Page} instance or <code>null</code> if
220      * there is no {@link Page} whose identifier is <code>id</code>.
221      * @throws IllegalArgumentException if <code>id</code> is
222      * <code>null</code>.
223      */
224     public Page getPage(String id) {
225 
226         if (id == null) {
227             throw new IllegalArgumentException("Parameter id can't be null.");
228         }
229 
230         final int size = pages.size();
231         Page page;
232 
233         for (int i = 0; i < size; i++) {
234 
235             page = (Page) pages.get(i);
236 
237             if (id.equals(page.getId())) {
238                 return page;
239             }
240 
241         }
242 
243         return null;
244 
245     }
246 
247     /***
248      * Adds a {@link StartPage} to this catalog.
249      * @param startPage a {@link StartPage} instance.
250      */
251     public void addStartPage(StartPage startPage) {
252         startPages.add(startPage);
253     }
254 
255     /***
256      * Removes a {@link StartPage} from this catalog.
257      * @param startPage a {@link StartPage} instance.
258      */
259     public void removeStartPage(StartPage startPage) {
260         startPages.remove(startPage);
261     }
262 
263     /***
264      * Returns the value of the <code>startPages</code> property.
265      * @return a <code>String</code>.
266      */
267     public StartPage[] getStartPages() {
268 
269         StartPage[] startPageArray = new StartPage[startPages.size()];
270 
271         startPages.toArray(startPageArray);
272 
273         return startPageArray;
274 
275     }
276 
277     /***
278      * Adds a global {@link DataExtraction} to this catalog.
279      * @param dataExtraction a {@link DataExtraction} instance..
280      */
281     public void addDataExtraction(DataExtraction dataExtraction) {
282         dataExtractions.add(dataExtraction);
283     }
284 
285     /***
286      * Removes a global {@link DataExtraction} from this catalog.
287      * @param dataExtraction a {@link DataExtraction} instance..
288      */
289     public void removeDataExtraction(DataExtraction dataExtraction) {
290         dataExtractions.remove(dataExtraction);
291     }
292 
293     /***
294      * Returns the {@link DataExtraction} whose identifier is <code>id</code>.
295      * @param id a <code>String</code>.
296      * @return a {@link DataExtraction} instance or <code>null</code> if
297      * there is no {@link DataExtraction} whose identifier is <code>id</code>.
298      * @throws IllegalArgumentException if <code>id</code> is
299      * <code>null</code>.
300      */
301     public DataExtraction getDataExtraction(String id) {
302 
303         final int size = dataExtractions.size();
304         DataExtraction dataExtraction;
305 
306         for (int i = 0; i < size; i++) {
307 
308             dataExtraction = (DataExtraction) dataExtractions.get(i);
309 
310             if (id.equals(dataExtraction.getId())) {
311                 return dataExtraction;
312             }
313 
314         }
315 
316         return null;
317 
318     }
319 
320     /***
321      * Returns the value of the <code>dataExtractions</code> property.
322      * @return a {@link DataExtraction} array.
323      */
324     public DataExtraction[] getDataExtractions() {
325 
326         DataExtraction[] dataExtractionArray =
327                 new DataExtraction[dataExtractions.size()];
328 
329         dataExtractions.toArray(dataExtractionArray);
330 
331         return dataExtractionArray;
332 
333     }
334 
335     /***
336      * Returns the value of the <code>properties</code> property.
337      * @return a <code>Properties</code> instance.
338      */
339     public Properties getProperties() {
340         return properties;
341     }
342 
343 }