34972c59b214074ae58ef05217016a910ee9aa90
[utils] / crawler / basic / src / main / java / 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  * @author Erik Brakkee
31  */
32 public class ActionImpl implements Action {
33
34     private Crawler _crawler;
35
36     private Element _content;
37
38     private String _name;
39
40     private String _reference;
41
42     private PageType _type;
43     
44     private NameValuePair[] _parameters; 
45
46     /**
47      * Constructs the action.
48      * 
49      * @param aCrawler
50      *            Crawler to use.
51      * @param aContent
52      *            Content of the action element in the page where the action
53      *            occurs.
54      * @param aName
55      *            Name of the action.
56      * @param aReference
57      *            URL of the reference.
58      * @param aParameters Parameters to use for the action. 
59      */
60     public ActionImpl(Crawler aCrawler, Element aContent, String aName,
61             String aReference, NameValuePair[] aParameters) {
62         _crawler = aCrawler;
63         _content = aContent;
64         _name = aName;
65         _reference = aReference;
66         _type = null;
67         _parameters = aParameters; 
68     }
69
70     /**
71      * Constructs the action.
72      * 
73      * @param aCrawler
74      *            Crawler to use.
75      * @param aContent
76      *            Content of the action element in the page where the action
77      *            occurs.
78      * @param aName
79      *            Name of the action.
80      * @param aReference
81      *            URL of the reference.
82      * @param aType
83      *            Type of the referenced page.
84      * @param aParameters Parameters to use. 
85      */
86     public ActionImpl(Crawler aCrawler, Element aContent, String aName,
87             String aReference, PageType aType, NameValuePair[] aParameters) {
88         _crawler = aCrawler;
89         _content = aContent;
90         _name = aName;
91         _reference = aReference;
92         _type = aType;
93         _parameters = aParameters; 
94     }
95
96     /*
97      * (non-Javadoc)
98      * 
99      * @see org.wamblee.crawler.Action#getName()
100      */
101     public String getName() {
102         return _name;
103     }
104
105     /*
106      * (non-Javadoc)
107      * 
108      * @see org.wamblee.crawler.Action#execute()
109      */
110     public Page execute() throws PageException {
111         if (_type == null) {
112             return _crawler.getPage(_reference, _parameters);
113         }
114         return _crawler.getPage(_reference, _parameters, _type);
115     }
116
117     /*
118      * (non-Javadoc)
119      * 
120      * @see org.wamblee.crawler.Action#getContent()
121      */
122     public Element getContent() {
123         return _content;
124     }
125     
126     /* (non-Javadoc)
127      * @see java.lang.Object#equals(java.lang.Object)
128      */
129     @Override
130     public boolean equals(Object obj) {
131         if ( !(obj instanceof ActionImpl )) { 
132             return false; 
133         }
134         ActionImpl action = (ActionImpl)obj; 
135         return _reference.equals(action._reference) && 
136                _type.equals(action._type);
137     }
138 }