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.kiss;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
24 import org.dom4j.Document;
25 import org.dom4j.DocumentException;
26 import org.dom4j.Element;
27 import org.dom4j.io.SAXReader;
28 import org.wamblee.conditions.Condition;
29 import org.wamblee.conditions.OrCondition;
32 * Parse the configuration of desired programs.
34 public class ProgramConfigurationParser {
37 private static final String ELEM_PROGRAM = "program";
38 private static final String ELEM_PATTERN = "name";
41 * Parses the condition used to match the desired programs.
44 * Input stream to parse from.
47 Condition<Program> parse(InputStream aStream) {
49 SAXReader reader = new SAXReader();
50 Document document = reader.read(aStream);
52 Element root = document.getRootElement();
53 List<Condition<Program>> conditions = new ArrayList<Condition<Program>>();
55 for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext(); ) {
56 Element program = (Element)i.next();
57 String pattern = ".*" + program.element(ELEM_PATTERN).getText() + ".*";
58 conditions.add(new ProgramNameMatcher(pattern));
60 return new OrCondition<Program>(conditions);
61 } catch (DocumentException e) {
62 throw new RuntimeException("Error parsing program configuraiton", e);