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.io.PrintStream;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
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;
36 * Parsing of the configuration from an XML file.
38 public class ConfigurationParser {
40 private static final String ELEM_URL = "url";
42 private static final String ELEM_TYPE = "type";
44 private static final String ELEM_PATTERN = "pattern";
46 private static final String ELEM_METHOD = "method";
48 private static final String ELEM_XSLT = "xslt";
50 private static final String ELEM_PARAM = "param";
52 private static final String AT_NAME = "name";
54 private static final String AT_VALUE = "value";
56 private static final String METHOD_POST = "post";
58 private static final String METHOD_GET = "get";
60 private static final int MAX_TRIES = 3;
62 private static final int MAX_DELAY = 5000;
64 private PrintStream _os;
66 public ConfigurationParser(PrintStream aOs) {
70 public Configuration parse(InputStream aStream) {
72 SAXReader reader = new SAXReader();
73 Document document = reader.read(aStream);
75 Element root = document.getRootElement();
76 List<UrlConfig> urlConfigs = parseUrlConfigs(root);
77 List<PageTypeConfig> pageTypeConfigs = parsePageTypeConfigs(root);
78 return new ConfigurationImpl(urlConfigs, pageTypeConfigs);
79 } catch (DocumentException e) {
80 throw new RuntimeException("Problem parsing config file", e);
88 private List<UrlConfig> parseUrlConfigs(Element aRoot) {
89 List<UrlConfig> configs = new ArrayList<UrlConfig>();
90 for (Iterator i = aRoot.elementIterator(ELEM_URL); i.hasNext();) {
91 Element url = (Element) i.next();
92 UrlConfig config = parseUrlConfig(url);
98 private List<PageTypeConfig> parsePageTypeConfigs(Element aRoot) {
99 List<PageTypeConfig> configs = new ArrayList<PageTypeConfig>();
100 for (Iterator i = aRoot.elementIterator(ELEM_TYPE); i.hasNext();) {
101 Element url = (Element) i.next();
102 PageTypeConfig config = parsePageTypeConfig(url);
108 private UrlConfig parseUrlConfig(Element aUrlElem) {
109 String pattern = aUrlElem.elementText(ELEM_PATTERN);
110 PageRequest request = parseRequestConfig(aUrlElem);
111 return new UrlConfig(pattern, request);
114 private PageTypeConfig parsePageTypeConfig(Element aTypeElem) {
115 String pattern = aTypeElem.elementText(ELEM_PATTERN);
116 PageRequest request = parseRequestConfig(aTypeElem);
117 return new PageTypeConfig(pattern, request);
124 private PageRequest parseRequestConfig(Element aUrlElem) {
125 String method = aUrlElem.elementText(ELEM_METHOD);
126 String xslt = aUrlElem.elementText(ELEM_XSLT);
127 List<NameValuePair> params = new ArrayList<NameValuePair>();
128 for (Iterator i = aUrlElem.elementIterator(ELEM_PARAM); i.hasNext();) {
129 Element paramElem = (Element) i.next();
130 NameValuePair param = parseParameter(paramElem);
134 NameValuePair[] paramsArray = params.toArray(new NameValuePair[0]);
136 if (METHOD_POST.equals(method)) {
137 request = new PostPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
139 } else if (METHOD_GET.equals(method) || method == null) {
140 request = new GetPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
143 throw new RuntimeException("Unknown request method '" + method
144 + "'. Only " + METHOD_GET + " and " + METHOD_POST
150 private NameValuePair parseParameter(Element aParam) {
151 String name = aParam.attributeValue(AT_NAME);
152 String value = aParam.attributeValue(AT_VALUE);
153 return new NameValuePair(name, value);