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