2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.crawler.impl;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
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;
35 * Parsing of the configuration from an XML file.
37 public class ConfigurationParser {
39 private static final String ELEM_URL = "url";
41 private static final String ELEM_TYPE = "type";
43 private static final String ELEM_PATTERN = "pattern";
45 private static final String ELEM_METHOD = "method";
47 private static final String ELEM_XSLT = "xslt";
49 private static final String ELEM_PARAM = "param";
51 private static final String AT_NAME = "name";
53 private static final String AT_VALUE = "value";
55 private static final String METHOD_POST = "post";
57 private static final String METHOD_GET = "get";
59 private static final int MAX_TRIES = 3;
61 private static final int MAX_DELAY = 100;
64 * Constructs the configuration parser.
66 public ConfigurationParser() {
71 * Parses the configuration from an input stream.
72 * @param aStream Input file.
73 * @return Configuration.
75 public Configuration parse(InputStream aStream) {
77 SAXReader reader = new SAXReader();
78 Document document = reader.read(aStream);
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);
90 * Parses the URL-based configuration.
91 * @param aRoot Root of the configuration file document.
92 * @return List of URL-based configurations.
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);
105 * Parses the page type based configurations.
106 * @param aRoot Root of the configuration file document.
107 * @return LIst of page type based configurations.
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);
120 * Parses a URL-based configuration.
121 * @param aUrlElem Configuration element.
122 * @return Configuration.
124 private UrlConfig parseUrlConfig(Element aUrlElem) {
125 String pattern = aUrlElem.elementText(ELEM_PATTERN);
126 PageRequest request = parseRequestConfig(aUrlElem);
127 return new UrlConfig(pattern, request);
131 * Parses a page type based configuration.
132 * @param aTypeElem Configuration element.
133 * @return Configuration.
135 private PageTypeConfig parsePageTypeConfig(Element aTypeElem) {
136 String pattern = aTypeElem.elementText(ELEM_PATTERN);
137 PageRequest request = parseRequestConfig(aTypeElem);
138 return new PageTypeConfig(pattern, request);
142 * Parses a request configuration describing how to execute requests.
143 * @param aElem Configuration element.
144 * @return Page request.
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);
156 NameValuePair[] paramsArray = params.toArray(new NameValuePair[0]);
158 if (METHOD_POST.equals(method)) {
159 request = new PostPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
161 } else if (METHOD_GET.equals(method) || method == null) {
162 request = new GetPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
165 throw new RuntimeException("Unknown request method '" + method
166 + "'. Only " + METHOD_GET + " and " + METHOD_POST
173 * Parses a parameter definition.
174 * @param aParam Parameter.
175 * @return Name value pair describing a parameter.
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);