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