b8d5c67863adcd9406c3bae2fc76d6f11f86b829
[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.Attribute;
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.conditions.AndCondition;
30 import org.wamblee.conditions.Condition;
31 import org.wamblee.conditions.PropertyRegexCondition;
32 import org.wamblee.crawler.kiss.ProgramFilter.ProgramAction;
33
34 /**
35  * Parse the configuration of desired programs.
36  */
37 class ProgramConfigurationParser {
38
39     private static final String ELEM_PROGRAM = "program";
40
41     private static final String ELEM_PATTERN = "match";
42
43     private static final String ELEM_ACTION = "action";
44
45     private static final String ACTION_NOTIFY = "notify";
46
47     /**
48      * Parses the condition used to match the desired programs.
49      * 
50      * @param aStream
51      *            Input stream to parse from.
52      * @return Condition.
53      */
54     List<ProgramFilter> parse(InputStream aStream) {
55         List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
56         try {
57             SAXReader reader = new SAXReader();
58             Document document = reader.read(aStream);
59
60             Element root = document.getRootElement();
61
62             for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
63                 Element program = (Element) i.next();
64
65                 Element actionElem = program.element(ELEM_ACTION);
66                 ProgramAction action = ProgramAction.RECORD;
67                 if (actionElem != null) {
68                     if (actionElem.getText().equals(ACTION_NOTIFY)) {
69                         action = ProgramAction.NOTIFY;
70                     }
71                 }
72                 List<Condition<Program>> regexConditions = 
73                     new ArrayList<Condition<Program>>();
74                 for (Iterator j = program.elementIterator(ELEM_PATTERN); j.hasNext(); ) {
75                     Element patternElem = (Element)j.next();
76                     String fieldName = "name"; 
77                     Attribute fieldAttribute = patternElem.attribute("field"); 
78                     if ( fieldAttribute != null ) { 
79                         fieldName = fieldAttribute.getText(); 
80                     }
81                     String pattern = ".*" + patternElem.getText()
82                     + ".*";
83                     regexConditions.add(new PropertyRegexCondition<Program>(fieldName, pattern, true));
84                 }
85                 Condition<Program> condition = new AndCondition<Program>(regexConditions);
86                 filters.add(new ProgramFilter(condition, action));
87             }
88             return filters;
89         } catch (DocumentException e) {
90             throw new RuntimeException("Error parsing program configuraiton", e);
91         }
92     }
93 }