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