71719799c2162296902da68e8f6c7612382fc5b0
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / ProgramConfigurationParser.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.kiss;
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.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;
30
31 /**
32  * Parse the configuration of desired programs.
33  */
34 public class ProgramConfigurationParser {
35     
36     
37     private static final String ELEM_PROGRAM = "program";
38     private static final String ELEM_PATTERN = "name";
39
40     /**
41      * Parses the condition used to match the desired programs.
42      * 
43      * @param aStream
44      *            Input stream to parse from.
45      * @return Condition.
46      */
47     Condition<Program> parse(InputStream aStream) {
48         try {
49             SAXReader reader = new SAXReader();
50             Document document = reader.read(aStream);
51
52             Element root = document.getRootElement();
53             List<Condition<Program>> conditions = new ArrayList<Condition<Program>>();
54
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)); 
59             }
60             return new OrCondition<Program>(conditions);
61         } catch (DocumentException e) {
62             throw new RuntimeException("Error parsing program configuraiton", e);
63         }
64     }
65 }