(no commit message)
[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             }
76             else { 
77                 names.add(new ActionImpl(_crawler, elem, name, href, 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     /* (non-Javadoc)
93      * @see org.wamblee.crawler.Page#getActions()
94      */
95     public Action[] getActions() {
96         return _actions;
97     }
98     
99     /*
100      *  (non-Javadoc)
101      * @see org.wamblee.crawler.Page#getAction(java.lang.String)
102      */
103     public Action getAction(String aName) {
104         List<Action> results = new ArrayList<Action>();
105         for (Action action: _actions) { 
106             if ( action.getName().equals(aName)) {
107                 results.add(action);
108             }
109         }
110         if (results.size() == 0) {
111             return null;
112         }
113         if (results.size() > 1) {
114             throw new RuntimeException("Duplicate link '" + aName + "'");
115         }
116         return results.get(0);
117     }
118 }