d478ed4b73f1a11181d46d16aa57cab7114710dd
[utils] / crawler / basic / src / org / wamblee / crawler / impl / PageImpl.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 java.util.ArrayList;
20 import java.util.List;
21
22 import org.dom4j.DocumentHelper;
23 import org.dom4j.Element;
24 import org.dom4j.XPath;
25 import org.wamblee.crawler.Action;
26 import org.wamblee.crawler.Crawler;
27 import org.wamblee.crawler.Page;
28 import org.wamblee.crawler.PageType;
29
30 /**
31  * Page implementation.
32  */
33 public class PageImpl implements Page {
34
35     private static final String ELEM_NAME = "action";
36
37     private static final String ATT_NAME = "name";
38
39     private static final String ATT_HREF = "reference";
40
41     private static final String ATT_TYPE = "type";
42
43     private Crawler _crawler;
44
45     private Element _content;
46
47     private Action[] _actions;
48
49     /**
50      * Constructs a page.
51      * 
52      * @param aContent
53      */
54     public PageImpl(Crawler aCrawler, Element aContent) {
55         _crawler = aCrawler;
56         _content = aContent;
57         _actions = computeActions();
58     }
59
60     /*
61      * (non-Javadoc)
62      * 
63      * @see org.wamblee.crawler.Page#getLinkNames()
64      */
65     private Action[] computeActions() {
66         XPath xpath = DocumentHelper.createXPath(ELEM_NAME);
67         List<Element> results = (List<Element>) xpath.selectNodes(_content);
68         List<Action> names = new ArrayList<Action>();
69         for (Element elem : results) {
70             String name = elem.attributeValue(ATT_NAME);
71             String href = elem.attributeValue(ATT_HREF);
72             String type = elem.attributeValue(ATT_TYPE);
73             if (type == null) {
74                 names.add(new ActionImpl(_crawler, elem, name, href));
75             } else {
76                 names.add(new ActionImpl(_crawler, elem, name, href,
77                         new PageType(type)));
78             }
79         }
80         return names.toArray(new Action[0]);
81     }
82
83     /*
84      * (non-Javadoc)
85      * 
86      * @see org.wamblee.crawler.Page#getContent()
87      */
88     public Element getContent() {
89         return _content;
90     }
91
92     /*
93      * (non-Javadoc)
94      * 
95      * @see org.wamblee.crawler.Page#getActions()
96      */
97     public Action[] getActions() {
98         return _actions;
99     }
100
101     /*
102      * (non-Javadoc)
103      * 
104      * @see org.wamblee.crawler.Page#getAction(java.lang.String)
105      */
106     public Action getAction(String aName) {
107         List<Action> results = new ArrayList<Action>();
108         for (Action action : _actions) {
109             if (action.getName().equals(aName)) {
110                 results.add(action);
111             }
112         }
113         if (results.size() == 0) {
114             return null;
115         }
116         if (results.size() > 1) {
117             throw new RuntimeException("Duplicate action '" + aName + "'");
118         }
119         return results.get(0);
120     }
121 }