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