1eb971c22d7b32f258c43f5b5cb35d0a6cc66aaa
[utils] / crawler / kiss / src / main / java / org / wamblee / crawler / kiss / main / 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.main;
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.guide.Program;
33
34 /**
35  * Parse the configuration of desired programs.
36  *
37  * @author Erik Brakkee
38  */
39 class ProgramConfigurationParser {
40     private static final int DEFAULT_PRIORITY = 1; 
41
42     // Configuration of interesting programs.
43
44     private static final String ELEM_PROGRAM = "program";
45     
46     private static final String ELEM_PRIORITY = "priority";
47
48     private static final String ELEM_PATTERN = "match";
49
50     private static final String ELEM_ACTION = "action";
51
52     private static final String ELEM_CATEGORY = "category";
53
54     private static final String ACTION_NOTIFY = "notify";
55
56     private List<ProgramFilter> _filters;
57
58     ProgramConfigurationParser() {
59         _filters = null;
60     }
61
62     /**
63      * Parses the condition used to match the desired programs.
64      * 
65      * @param aStream
66      *            Input stream to parse from.
67      * @return Condition.
68      */
69     void parse(InputStream aStream) {
70         List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
71         try {
72             SAXReader reader = new SAXReader();
73             Document document = reader.read(aStream);
74
75             Element root = document.getRootElement();
76
77             for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
78                 Element program = (Element) i.next();
79
80                 Element categoryElem = program.element(ELEM_CATEGORY);
81                 String category = "";
82                 if (categoryElem != null) {
83                     category = categoryElem.getText().trim();
84                 }
85
86                 Element actionElem = program.element(ELEM_ACTION);
87                 int priority = DEFAULT_PRIORITY; 
88                 String priorityString = program.elementTextTrim(ELEM_PRIORITY);
89                 if ( priorityString != null ) { 
90                     priority = Integer.valueOf(priorityString);
91                 }
92                 ProgramAction action = new RecordProgramAction(priority);
93                 if (actionElem != null) {
94                     if (actionElem.getText().equals(ACTION_NOTIFY)) {
95                         action = new InterestingProgramAction(category);
96                     }
97                 }
98
99                 List<Condition<Program>> regexConditions = new ArrayList<Condition<Program>>();
100                 for (Iterator j = program.elementIterator(ELEM_PATTERN); j
101                         .hasNext();) {
102                     Element patternElem = (Element) j.next();
103                     String fieldName = "name";
104                     Attribute fieldAttribute = patternElem.attribute("field");
105                     if (fieldAttribute != null) {
106                         fieldName = fieldAttribute.getText();
107                     }
108                     String pattern = ".*(" + patternElem.getText() + ").*";
109                     regexConditions.add(new PropertyRegexCondition<Program>(
110                             fieldName, pattern, true));
111                 }
112                 Condition<Program> condition = new AndCondition<Program>(
113                         regexConditions);
114                 filters.add(new ProgramFilter(condition, action));
115             }
116             _filters = filters;
117         } catch (DocumentException e) {
118             throw new RuntimeException("Error parsing program configuraiton", e);
119         }
120     }
121
122     /**
123      * Returns the list of program filters.
124      * 
125      * @return Filter list.
126      */
127     public List<ProgramFilter> getFilters() {
128         return _filters;
129     }
130 }