(no commit message)
[utils] / crawler / basic / src / org / wamblee / crawler / impl / ConfigurationParser.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.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.commons.httpclient.NameValuePair;
25 import org.dom4j.Document;
26 import org.dom4j.DocumentException;
27 import org.dom4j.Element;
28 import org.dom4j.io.SAXReader;
29 import org.wamblee.crawler.Configuration;
30 import org.wamblee.crawler.GetPageRequest;
31 import org.wamblee.crawler.PageRequest;
32 import org.wamblee.crawler.PostPageRequest;
33
34 /**
35  * Parsing of the configuration from an XML file.
36  */
37 public class ConfigurationParser {
38
39     private static final String ELEM_URL = "url";
40
41     private static final String ELEM_TYPE = "type";
42
43     private static final String ELEM_PATTERN = "pattern";
44
45     private static final String ELEM_METHOD = "method";
46
47     private static final String ELEM_XSLT = "xslt";
48
49     private static final String ELEM_PARAM = "param";
50
51     private static final String AT_NAME = "name";
52
53     private static final String AT_VALUE = "value";
54
55     private static final String METHOD_POST = "post";
56
57     private static final String METHOD_GET = "get";
58
59     private static final int MAX_TRIES = 3;
60
61     private static final int MAX_DELAY = 100;
62
63     /**
64      * Constructs the configuration parser. 
65      */
66     public ConfigurationParser() {
67         // Empty
68     }
69
70     /**
71      * Parses the configuration from an input stream.
72      * @param aStream Input file. 
73      * @return Configuration. 
74      */
75     public Configuration parse(InputStream aStream) {
76         try {
77             SAXReader reader = new SAXReader();
78             Document document = reader.read(aStream);
79
80             Element root = document.getRootElement();
81             List<UrlConfig> urlConfigs = parseUrlConfigs(root);
82             List<PageTypeConfig> pageTypeConfigs = parsePageTypeConfigs(root);
83             return new ConfigurationImpl(urlConfigs, pageTypeConfigs);
84         } catch (DocumentException e) {
85             throw new RuntimeException("Problem parsing config file", e);
86         }
87     }
88
89     /**
90      * Parses the URL-based configuration. 
91      * @param aRoot Root of the configuration file document. 
92      * @return List of URL-based configurations. 
93      */
94     private List<UrlConfig> parseUrlConfigs(Element aRoot) {
95         List<UrlConfig> configs = new ArrayList<UrlConfig>();
96         for (Iterator i = aRoot.elementIterator(ELEM_URL); i.hasNext();) {
97             Element url = (Element) i.next();
98             UrlConfig config = parseUrlConfig(url);
99             configs.add(config);
100         }
101         return configs;
102     }
103
104     /**
105      * Parses the page type based configurations. 
106      * @param aRoot Root of the configuration file document. 
107      * @return LIst of page type based configurations. 
108      */
109     private List<PageTypeConfig> parsePageTypeConfigs(Element aRoot) {
110         List<PageTypeConfig> configs = new ArrayList<PageTypeConfig>();
111         for (Iterator i = aRoot.elementIterator(ELEM_TYPE); i.hasNext();) {
112             Element url = (Element) i.next();
113             PageTypeConfig config = parsePageTypeConfig(url);
114             configs.add(config);
115         }
116         return configs;
117     }
118
119     /**
120      * Parses a URL-based configuration. 
121      * @param aUrlElem Configuration element. 
122      * @return Configuration. 
123      */
124     private UrlConfig parseUrlConfig(Element aUrlElem) {
125         String pattern = aUrlElem.elementText(ELEM_PATTERN);
126         PageRequest request = parseRequestConfig(aUrlElem);
127         return new UrlConfig(pattern, request);
128     }
129
130     /**
131      * Parses a page type based configuration. 
132      * @param aTypeElem Configuration element. 
133      * @return Configuration. 
134      */
135     private PageTypeConfig parsePageTypeConfig(Element aTypeElem) {
136         String pattern = aTypeElem.elementText(ELEM_PATTERN);
137         PageRequest request = parseRequestConfig(aTypeElem);
138         return new PageTypeConfig(pattern, request);
139     }
140
141     /**
142      * Parses a request configuration describing how to execute requests. 
143      * @param aElem Configuration element. 
144      * @return Page request. 
145      */
146     private PageRequest parseRequestConfig(Element aElem) {
147         String method = aElem.elementText(ELEM_METHOD);
148         String xslt = aElem.elementText(ELEM_XSLT);
149         List<NameValuePair> params = new ArrayList<NameValuePair>();
150         for (Iterator i = aElem.elementIterator(ELEM_PARAM); i.hasNext();) {
151             Element paramElem = (Element) i.next();
152             NameValuePair param = parseParameter(paramElem);
153             params.add(param);
154         }
155
156         NameValuePair[] paramsArray = params.toArray(new NameValuePair[0]);
157         PageRequest request;
158         if (METHOD_POST.equals(method)) {
159             request = new PostPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
160                     xslt);
161         } else if (METHOD_GET.equals(method) || method == null) {
162             request = new GetPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
163                     xslt);
164         } else {
165             throw new RuntimeException("Unknown request method '" + method
166                     + "'. Only " + METHOD_GET + " and " + METHOD_POST
167                     + " are supported");
168         }
169         return request;
170     }
171
172     /**
173      * Parses a parameter definition. 
174      * @param aParam Parameter. 
175      * @return Name value pair describing a parameter. 
176      */
177     private NameValuePair parseParameter(Element aParam) {
178         String name = aParam.attributeValue(AT_NAME);
179         String value = aParam.attributeValue(AT_VALUE);
180         return new NameValuePair(name, value);
181     }
182 }