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