support for parameters on actions.
[utils] / crawler / basic / src / org / wamblee / crawler / impl / ActionImpl.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.crawler.impl;
18
19 import org.apache.commons.httpclient.NameValuePair;
20 import org.dom4j.Element;
21 import org.wamblee.crawler.Action;
22 import org.wamblee.crawler.Crawler;
23 import org.wamblee.crawler.Page;
24 import org.wamblee.crawler.PageException;
25 import org.wamblee.crawler.PageType;
26
27 /**
28  * Action implementation.
29  */
30 public class ActionImpl implements Action {
31
32     private Crawler _crawler;
33
34     private Element _content;
35
36     private String _name;
37
38     private String _reference;
39
40     private PageType _type;
41     
42     private NameValuePair[] _parameters; 
43
44     /**
45      * Constructs the action.
46      * 
47      * @param aCrawler
48      *            Crawler to use.
49      * @param aContent
50      *            Content of the action element in the page where the action
51      *            occurs.
52      * @param aName
53      *            Name of the action.
54      * @param aReference
55      *            URL of the reference.
56      * @param aParameters Parameters to use for the action. 
57      */
58     public ActionImpl(Crawler aCrawler, Element aContent, String aName,
59             String aReference, NameValuePair[] aParameters) {
60         _crawler = aCrawler;
61         _content = aContent;
62         _name = aName;
63         _reference = aReference;
64         _type = null;
65         _parameters = aParameters; 
66     }
67
68     /**
69      * Constructs the action.
70      * 
71      * @param aCrawler
72      *            Crawler to use.
73      * @param aContent
74      *            Content of the action element in the page where the action
75      *            occurs.
76      * @param aName
77      *            Name of the action.
78      * @param aReference
79      *            URL of the reference.
80      * @param aType
81      *            Type of the referenced page.
82      * @param aParameters Parameters to use. 
83      */
84     public ActionImpl(Crawler aCrawler, Element aContent, String aName,
85             String aReference, PageType aType, NameValuePair[] aParameters) {
86         _crawler = aCrawler;
87         _content = aContent;
88         _name = aName;
89         _reference = aReference;
90         _type = aType;
91         _parameters = aParameters; 
92     }
93
94     /*
95      * (non-Javadoc)
96      * 
97      * @see org.wamblee.crawler.Action#getName()
98      */
99     public String getName() {
100         return _name;
101     }
102
103     /*
104      * (non-Javadoc)
105      * 
106      * @see org.wamblee.crawler.Action#execute()
107      */
108     public Page execute() throws PageException {
109         if (_type == null) {
110             return _crawler.getPage(_reference, _parameters);
111         }
112         return _crawler.getPage(_reference, _parameters, _type);
113     }
114
115     /*
116      * (non-Javadoc)
117      * 
118      * @see org.wamblee.crawler.Action#getContent()
119      */
120     public Element getContent() {
121         return _content;
122     }
123     
124     /* (non-Javadoc)
125      * @see java.lang.Object#equals(java.lang.Object)
126      */
127     @Override
128     public boolean equals(Object obj) {
129         if ( !(obj instanceof ActionImpl )) { 
130             return false; 
131         }
132         ActionImpl action = (ActionImpl)obj; 
133         return _reference.equals(action._reference) && 
134                _type.equals(action._type);
135     }
136 }