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