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