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;
36 import org.wamblee.xml.XslTransformer;
39 * Parse the configuration of desired programs.
41 class ProgramConfigurationParser {
42 private static final int DEFAULT_SMTP_PORT = 25;
44 private static final int DEFAULT_PRIORITY = 1;
46 private static final String ELEM_PASSWORD = "password";
48 private static final String ELEM_USERNAME = "username";
50 private static final String ELEM_PORT = "port";
52 private static final String ELEM_HOST = "host";
54 // Formatting configuration.
55 private static final String ELEM_FORMAT = "format";
57 private static final String ELEM_TEXT = "text";
59 private static final String ELEM_HTML = "html";
61 // Mail server configuration.
63 private static final String ELEM_NOTIFICATION = "notification";
65 private static final String ELEM_SMTP = "smtp";
67 private static final String ELEM_SUBJECT = "subject";
69 private static final String ELEM_TO = "to";
71 private static final String ELEM_FROM = "from";
73 // Configuration of interesting programs.
75 private static final String ELEM_PROGRAM = "program";
77 private static final String ELEM_PRIORITY = "priority";
79 private static final String ELEM_PATTERN = "match";
81 private static final String ELEM_ACTION = "action";
83 private static final String ELEM_CATEGORY = "category";
85 private static final String ACTION_NOTIFY = "notify";
87 private List<ProgramFilter> _filters;
89 private XslTransformer _transformer;
91 private Notifier _notifier;
93 ProgramConfigurationParser(XslTransformer aTransformer) {
96 _transformer = aTransformer;
100 * Parses the condition used to match the desired programs.
103 * Input stream to parse from.
106 void parse(InputStream aStream) {
107 List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
109 SAXReader reader = new SAXReader();
110 Document document = reader.read(aStream);
112 Element root = document.getRootElement();
114 for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
115 Element program = (Element) i.next();
117 Element categoryElem = program.element(ELEM_CATEGORY);
118 String category = "";
119 if (categoryElem != null) {
120 category = categoryElem.getText().trim();
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);
129 ProgramAction action = new RecordProgramAction(priority);
130 if (actionElem != null) {
131 if (actionElem.getText().equals(ACTION_NOTIFY)) {
132 action = new InterestingProgramAction(category);
136 List<Condition<Program>> regexConditions = new ArrayList<Condition<Program>>();
137 for (Iterator j = program.elementIterator(ELEM_PATTERN); j
139 Element patternElem = (Element) j.next();
140 String fieldName = "name";
141 Attribute fieldAttribute = patternElem.attribute("field");
142 if (fieldAttribute != null) {
143 fieldName = fieldAttribute.getText();
145 String pattern = ".*(" + patternElem.getText() + ").*";
146 regexConditions.add(new PropertyRegexCondition<Program>(
147 fieldName, pattern, true));
149 Condition<Program> condition = new AndCondition<Program>(
151 filters.add(new ProgramFilter(condition, action));
155 Element notifier = root.element(ELEM_NOTIFICATION);
156 _notifier = parseNotifier(notifier);
158 } catch (DocumentException e) {
159 throw new RuntimeException("Error parsing program configuraiton", e);
164 * Parses the notifier
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);
173 Element smtp = aNotifier.element(ELEM_SMTP);
174 MailServer server = parseMailServer(smtp);
176 Element format = aNotifier.element(ELEM_FORMAT);
177 String htmlXslt = format.elementTextTrim(ELEM_HTML);
178 String textXslt = format.elementTextTrim(ELEM_TEXT);
180 return new MailNotifier(from, to, subject, htmlXslt, textXslt, server, _transformer);
184 * Parses the mail server from the XML.
187 * Mail server configuration.
188 * @return Mail server.
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());
197 String username = aSmtp.elementTextTrim(ELEM_USERNAME);
198 String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
200 return new MailServer(host, port, username, password);
204 * Returns the list of program filters.
206 * @return Filter list.
208 public List<ProgramFilter> getFilters() {
213 * Returns the notifier to use.
217 public Notifier getNotifier() {