2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.crawler.kiss.main;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
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;
38 * Parse the configuration of desired programs.
40 class ProgramConfigurationParser {
41 private static final int DEFAULT_SMTP_PORT = 25;
43 private static final int DEFAULT_PRIORITY = 1;
45 private static final String ELEM_PASSWORD = "password";
47 private static final String ELEM_USERNAME = "username";
49 private static final String ELEM_PORT = "port";
51 private static final String ELEM_HOST = "host";
53 // Formatting configuration.
54 private static final String ELEM_FORMAT = "format";
56 private static final String ELEM_TEXT = "text";
58 private static final String ELEM_HTML = "html";
60 // Mail server configuration.
62 private static final String ELEM_NOTIFICATION = "notification";
64 private static final String ELEM_SMTP = "smtp";
66 private static final String ELEM_SUBJECT = "subject";
68 private static final String ELEM_TO = "to";
70 private static final String ELEM_FROM = "from";
72 // Configuration of interesting programs.
74 private static final String ELEM_PROGRAM = "program";
76 private static final String ELEM_PRIORITY = "priority";
78 private static final String ELEM_PATTERN = "match";
80 private static final String ELEM_ACTION = "action";
82 private static final String ELEM_CATEGORY = "category";
84 private static final String ACTION_NOTIFY = "notify";
86 private List<ProgramFilter> _filters;
88 private Notifier _notifier;
90 ProgramConfigurationParser() {
96 * Parses the condition used to match the desired programs.
99 * Input stream to parse from.
102 void parse(InputStream aStream) {
103 List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
105 SAXReader reader = new SAXReader();
106 Document document = reader.read(aStream);
108 Element root = document.getRootElement();
110 for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
111 Element program = (Element) i.next();
113 Element categoryElem = program.element(ELEM_CATEGORY);
114 String category = "";
115 if (categoryElem != null) {
116 category = categoryElem.getText().trim();
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);
125 ProgramAction action = new RecordProgramAction(priority);
126 if (actionElem != null) {
127 if (actionElem.getText().equals(ACTION_NOTIFY)) {
128 action = new InterestingProgramAction(category);
132 List<Condition<Program>> regexConditions = new ArrayList<Condition<Program>>();
133 for (Iterator j = program.elementIterator(ELEM_PATTERN); j
135 Element patternElem = (Element) j.next();
136 String fieldName = "name";
137 Attribute fieldAttribute = patternElem.attribute("field");
138 if (fieldAttribute != null) {
139 fieldName = fieldAttribute.getText();
141 String pattern = ".*(" + patternElem.getText() + ").*";
142 regexConditions.add(new PropertyRegexCondition<Program>(
143 fieldName, pattern, true));
145 Condition<Program> condition = new AndCondition<Program>(
147 filters.add(new ProgramFilter(condition, action));
151 Element notifier = root.element(ELEM_NOTIFICATION);
152 _notifier = parseNotifier(notifier);
154 } catch (DocumentException e) {
155 throw new RuntimeException("Error parsing program configuraiton", e);
160 * Parses the notifier
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);
169 Element smtp = aNotifier.element(ELEM_SMTP);
170 MailServer server = parseMailServer(smtp);
172 Element format = aNotifier.element(ELEM_FORMAT);
173 String htmlXslt = format.elementTextTrim(ELEM_HTML);
174 String textXslt = format.elementTextTrim(ELEM_TEXT);
176 return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
180 * Parses the mail server from the XML.
183 * Mail server configuration.
184 * @return Mail server.
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());
193 String username = aSmtp.elementTextTrim(ELEM_USERNAME);
194 String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
196 MailServer server = new MailServer(host, port, username, password);
201 * Returns the list of program filters.
203 * @return Filter list.
205 public List<ProgramFilter> getFilters() {
210 * Returns the notifier to use.
214 public Notifier getNotifier() {