library names now start with wamblee- to make them unique.
[utils] / crawler / kiss / src / 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 import org.wamblee.crawler.kiss.notification.MailNotifier;
34 import org.wamblee.crawler.kiss.notification.MailServer;
35 import org.wamblee.crawler.kiss.notification.Notifier;
36 import org.wamblee.xml.XslTransformer;
37
38 /**
39  * Parse the configuration of desired programs.
40  */
41 class ProgramConfigurationParser {
42     private static final int DEFAULT_SMTP_PORT = 25;
43     
44     private static final int DEFAULT_PRIORITY = 1; 
45
46     private static final String ELEM_PASSWORD = "password";
47
48     private static final String ELEM_USERNAME = "username";
49
50     private static final String ELEM_PORT = "port";
51
52     private static final String ELEM_HOST = "host";
53
54     // Formatting configuration.
55     private static final String ELEM_FORMAT = "format";
56
57     private static final String ELEM_TEXT = "text";
58
59     private static final String ELEM_HTML = "html";
60
61     // Mail server configuration.
62
63     private static final String ELEM_NOTIFICATION = "notification";
64
65     private static final String ELEM_SMTP = "smtp";
66
67     private static final String ELEM_SUBJECT = "subject";
68
69     private static final String ELEM_TO = "to";
70
71     private static final String ELEM_FROM = "from";
72
73     // Configuration of interesting programs.
74
75     private static final String ELEM_PROGRAM = "program";
76     
77     private static final String ELEM_PRIORITY = "priority";
78
79     private static final String ELEM_PATTERN = "match";
80
81     private static final String ELEM_ACTION = "action";
82
83     private static final String ELEM_CATEGORY = "category";
84
85     private static final String ACTION_NOTIFY = "notify";
86
87     private List<ProgramFilter> _filters;
88     
89     private XslTransformer _transformer;
90
91     private Notifier _notifier;
92
93     ProgramConfigurationParser(XslTransformer aTransformer) {
94         _filters = null;
95         _notifier = null;
96         _transformer = aTransformer;
97     }
98
99     /**
100      * Parses the condition used to match the desired programs.
101      * 
102      * @param aStream
103      *            Input stream to parse from.
104      * @return Condition.
105      */
106     void parse(InputStream aStream) {
107         List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
108         try {
109             SAXReader reader = new SAXReader();
110             Document document = reader.read(aStream);
111
112             Element root = document.getRootElement();
113
114             for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
115                 Element program = (Element) i.next();
116
117                 Element categoryElem = program.element(ELEM_CATEGORY);
118                 String category = "";
119                 if (categoryElem != null) {
120                     category = categoryElem.getText().trim();
121                 }
122
123                 Element actionElem = program.element(ELEM_ACTION);
124                 int priority = DEFAULT_PRIORITY; 
125                 String priorityString = program.elementTextTrim(ELEM_PRIORITY);
126                 if ( priorityString != null ) { 
127                     priority = Integer.valueOf(priorityString);
128                 }
129                 ProgramAction action = new RecordProgramAction(priority);
130                 if (actionElem != null) {
131                     if (actionElem.getText().equals(ACTION_NOTIFY)) {
132                         action = new InterestingProgramAction(category);
133                     }
134                 }
135
136                 List<Condition<Program>> regexConditions = new ArrayList<Condition<Program>>();
137                 for (Iterator j = program.elementIterator(ELEM_PATTERN); j
138                         .hasNext();) {
139                     Element patternElem = (Element) j.next();
140                     String fieldName = "name";
141                     Attribute fieldAttribute = patternElem.attribute("field");
142                     if (fieldAttribute != null) {
143                         fieldName = fieldAttribute.getText();
144                     }
145                     String pattern = ".*(" + patternElem.getText() + ").*";
146                     regexConditions.add(new PropertyRegexCondition<Program>(
147                             fieldName, pattern, true));
148                 }
149                 Condition<Program> condition = new AndCondition<Program>(
150                         regexConditions);
151                 filters.add(new ProgramFilter(condition, action));
152             }
153             _filters = filters;
154
155             Element notifier = root.element(ELEM_NOTIFICATION);
156             _notifier = parseNotifier(notifier);
157
158         } catch (DocumentException e) {
159             throw new RuntimeException("Error parsing program configuraiton", e);
160         }
161     }
162
163     /**
164      * Parses the notifier
165      * 
166      * @return Notifier
167      */
168     private Notifier parseNotifier(Element aNotifier) {
169         String from = aNotifier.elementTextTrim(ELEM_FROM);
170         String to = aNotifier.elementTextTrim(ELEM_TO);
171         String subject = aNotifier.elementTextTrim(ELEM_SUBJECT);
172
173         Element smtp = aNotifier.element(ELEM_SMTP);
174         MailServer server = parseMailServer(smtp);
175
176         Element format = aNotifier.element(ELEM_FORMAT);
177         String htmlXslt = format.elementTextTrim(ELEM_HTML);
178         String textXslt = format.elementTextTrim(ELEM_TEXT);
179
180         return new MailNotifier(from, to, subject, htmlXslt, textXslt, server, _transformer);
181     }
182
183     /**
184      * Parses the mail server from the XML.
185      * 
186      * @param aSmtp
187      *            Mail server configuration.
188      * @return Mail server.
189      */
190     private MailServer parseMailServer(Element aSmtp) {
191         String host = aSmtp.elementTextTrim(ELEM_HOST);
192         Element portElem = aSmtp.element(ELEM_PORT);
193         int port = DEFAULT_SMTP_PORT;
194         if (portElem != null) {
195             port = Integer.valueOf(portElem.getTextTrim());
196         }
197         String username = aSmtp.elementTextTrim(ELEM_USERNAME);
198         String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
199
200         return new MailServer(host, port, username, password);
201     }
202
203     /**
204      * Returns the list of program filters.
205      * 
206      * @return Filter list.
207      */
208     public List<ProgramFilter> getFilters() {
209         return _filters;
210     }
211
212     /**
213      * Returns the notifier to use.
214      * 
215      * @return Notifier.
216      */
217     public Notifier getNotifier() {
218         return _notifier;
219     }
220 }