103ec0702c6caea299ca6583e265797c6e7cc8ee
[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
42     /**
43      * 
44      */
45     private static final int DEFAULT_SMTP_PORT = 25;
46
47     private static final String ELEM_PASSWORD = "password";
48
49     private static final String ELEM_USERNAME = "username";
50
51     private static final String ELEM_PORT = "port";
52
53     private static final String ELEM_HOST = "host";
54
55     // Formatting configuration.
56     private static final String ELEM_FORMAT = "format";
57
58     private static final String ELEM_TEXT = "text";
59
60     private static final String ELEM_HTML = "html";
61
62     // Mail server configuration.
63
64     private static final String ELEM_NOTIFICATION = "notification";
65
66     private static final String ELEM_SMTP = "smtp";
67
68     private static final String ELEM_SUBJECT = "subject";
69
70     private static final String ELEM_TO = "to";
71
72     private static final String ELEM_FROM = "from";
73
74     // Configuration of interesting programs.
75
76     private static final String ELEM_PROGRAM = "program";
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                 ProgramAction action = new RecordProgramAction(1);
121                 if (actionElem != null) {
122                     if (actionElem.getText().equals(ACTION_NOTIFY)) {
123                         action = new InterestingProgramAction(category);
124                     }
125                 }
126
127                 List<Condition<Program>> regexConditions = new ArrayList<Condition<Program>>();
128                 for (Iterator j = program.elementIterator(ELEM_PATTERN); j
129                         .hasNext();) {
130                     Element patternElem = (Element) j.next();
131                     String fieldName = "name";
132                     Attribute fieldAttribute = patternElem.attribute("field");
133                     if (fieldAttribute != null) {
134                         fieldName = fieldAttribute.getText();
135                     }
136                     String pattern = ".*(" + patternElem.getText() + ").*";
137                     regexConditions.add(new PropertyRegexCondition<Program>(
138                             fieldName, pattern, true));
139                 }
140                 Condition<Program> condition = new AndCondition<Program>(
141                         regexConditions);
142                 filters.add(new ProgramFilter(condition, action));
143             }
144             _filters = filters;
145
146             Element notifier = root.element(ELEM_NOTIFICATION);
147             _notifier = parseNotifier(notifier);
148
149         } catch (DocumentException e) {
150             throw new RuntimeException("Error parsing program configuraiton", e);
151         }
152     }
153
154     /**
155      * Parses the notifier
156      * 
157      * @return Notifier
158      */
159     private Notifier parseNotifier(Element aNotifier) {
160         String from = aNotifier.elementTextTrim(ELEM_FROM);
161         String to = aNotifier.elementTextTrim(ELEM_TO);
162         String subject = aNotifier.elementTextTrim(ELEM_SUBJECT);
163
164         Element smtp = aNotifier.element(ELEM_SMTP);
165         MailServer server = parseMailServer(smtp);
166
167         Element format = aNotifier.element(ELEM_FORMAT);
168         String htmlXslt = format.elementTextTrim(ELEM_HTML);
169         String textXslt = format.elementTextTrim(ELEM_TEXT);
170
171         return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
172     }
173
174     /**
175      * Parses the mail server from the XML.
176      * 
177      * @param aSmtp
178      *            Mail server configuration.
179      * @return Mail server.
180      */
181     private MailServer parseMailServer(Element aSmtp) {
182         String host = aSmtp.elementTextTrim(ELEM_HOST);
183         Element portElem = aSmtp.element(ELEM_PORT);
184         int port = DEFAULT_SMTP_PORT;
185         if (portElem != null) {
186             port = Integer.valueOf(portElem.getTextTrim());
187         }
188         String username = aSmtp.elementTextTrim(ELEM_USERNAME);
189         String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
190
191         MailServer server = new MailServer(host, port, username, password);
192         return server;
193     }
194
195     /**
196      * Returns the list of program filters.
197      * 
198      * @return Filter list.
199      */
200     public List<ProgramFilter> getFilters() {
201         return _filters;
202     }
203
204     /**
205      * Returns the notifier to use.
206      * 
207      * @return Notifier.
208      */
209     public Notifier getNotifier() {
210         return _notifier;
211     }
212 }