support for parameters on actions.
[utils] / crawler / basic / src / org / wamblee / crawler / impl / ActionImpl.java
index d0fe0806d424dd6b1c74e3c64aa4621e6bfbc8f0..ab7068cbda32ee8e8895a6677a2c6db431c8f60f 100644 (file)
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 
 package org.wamblee.crawler.impl;
 
+import org.apache.commons.httpclient.NameValuePair;
 import org.dom4j.Element;
 import org.wamblee.crawler.Action;
 import org.wamblee.crawler.Crawler;
 import org.wamblee.crawler.Page;
+import org.wamblee.crawler.PageException;
 import org.wamblee.crawler.PageType;
 
 /**
- * 
+ * Action implementation.
  */
 public class ActionImpl implements Action {
+
+    private Crawler _crawler;
+
+    private Element _content;
+
+    private String _name;
+
+    private String _reference;
+
+    private PageType _type;
     
-    private Crawler _crawler; 
-    private Element _content; 
-    private String _name; 
-    private String _reference; 
-    private PageType _type; 
-    
-    public ActionImpl(Crawler aCrawler, Element aContent, String aName, String aReference) {
-        _crawler = aCrawler; 
-        _content = aContent; 
+    private NameValuePair[] _parameters; 
+
+    /**
+     * Constructs the action.
+     * 
+     * @param aCrawler
+     *            Crawler to use.
+     * @param aContent
+     *            Content of the action element in the page where the action
+     *            occurs.
+     * @param aName
+     *            Name of the action.
+     * @param aReference
+     *            URL of the reference.
+     * @param aParameters Parameters to use for the action. 
+     */
+    public ActionImpl(Crawler aCrawler, Element aContent, String aName,
+            String aReference, NameValuePair[] aParameters) {
+        _crawler = aCrawler;
+        _content = aContent;
         _name = aName;
         _reference = aReference;
-        _type = null; 
+        _type = null;
+        _parameters = aParameters; 
     }
-    
-    public ActionImpl(Crawler aCrawler, Element aContent, String aName, String aReference, PageType aType) {
+
+    /**
+     * Constructs the action.
+     * 
+     * @param aCrawler
+     *            Crawler to use.
+     * @param aContent
+     *            Content of the action element in the page where the action
+     *            occurs.
+     * @param aName
+     *            Name of the action.
+     * @param aReference
+     *            URL of the reference.
+     * @param aType
+     *            Type of the referenced page.
+     * @param aParameters Parameters to use. 
+     */
+    public ActionImpl(Crawler aCrawler, Element aContent, String aName,
+            String aReference, PageType aType, NameValuePair[] aParameters) {
         _crawler = aCrawler;
-        _content = aContent; 
+        _content = aContent;
         _name = aName;
-        _reference = aReference; 
-        _type = aType; 
+        _reference = aReference;
+        _type = aType;
+        _parameters = aParameters; 
     }
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
+     * 
      * @see org.wamblee.crawler.Action#getName()
      */
     public String getName() {
         return _name;
     }
-    
-    /* (non-Javadoc)
+
+    /*
+     * (non-Javadoc)
+     * 
      * @see org.wamblee.crawler.Action#execute()
      */
-    public Page execute() {
-        if ( _type == null) {
-            return _crawler.getPage(_reference);
+    public Page execute() throws PageException {
+        if (_type == null) {
+            return _crawler.getPage(_reference, _parameters);
         }
-        return _crawler.getPage(_reference, _type);
+        return _crawler.getPage(_reference, _parameters, _type);
     }
-    
-    /* (non-Javadoc)
+
+    /*
+     * (non-Javadoc)
+     * 
      * @see org.wamblee.crawler.Action#getContent()
      */
     public Element getContent() {
-        return _content; 
+        return _content;
+    }
+    
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if ( !(obj instanceof ActionImpl )) { 
+            return false; 
+        }
+        ActionImpl action = (ActionImpl)obj; 
+        return _reference.equals(action._reference) && 
+               _type.equals(action._type);
     }
 }