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 {
42 private static final String ELEM_PASSWORD = "password";
44 private static final String ELEM_USERNAME = "username";
46 private static final String ELEM_PORT = "port";
48 private static final String ELEM_HOST = "host";
50 // Formatting configuration.
51 private static final String ELEM_FORMAT = "format";
53 private static final String ELEM_TEXT = "text";
55 private static final String ELEM_HTML = "html";
58 // Mail server configuration.
60 private static final String ELEM_NOTIFICATION = "notification";
62 private static final String ELEM_SMTP = "smtp";
64 private static final String ELEM_SUBJECT = "subject";
66 private static final String ELEM_TO = "to";
68 private static final String ELEM_FROM = "from";
70 // Configuration of interesting programs.
72 private static final String ELEM_PROGRAM = "program";
74 private static final String ELEM_PATTERN = "match";
76 private static final String ELEM_ACTION = "action";
78 private static final String ELEM_CATEGORY = "category";
80 private static final String ACTION_NOTIFY = "notify";
82 private List<ProgramFilter> _filters;
84 private Notifier _notifier;
86 ProgramConfigurationParser() {
92 * Parses the condition used to match the desired programs.
95 * Input stream to parse from.
98 void parse(InputStream aStream) {
99 List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
101 SAXReader reader = new SAXReader();
102 Document document = reader.read(aStream);
104 Element root = document.getRootElement();
106 for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
107 Element program = (Element) i.next();
109 Element categoryElem = program.element(ELEM_CATEGORY);
110 String category = "";
111 if ( categoryElem != null ) {
112 category = categoryElem.getText().trim();
115 Element actionElem = program.element(ELEM_ACTION);
116 ProgramAction action = new RecordProgramAction();
117 if (actionElem != null) {
118 if (actionElem.getText().equals(ACTION_NOTIFY)) {
119 action = new InterestingProgramAction(category);
123 List<Condition<Program>> regexConditions =
124 new ArrayList<Condition<Program>>();
125 for (Iterator j = program.elementIterator(ELEM_PATTERN); j.hasNext(); ) {
126 Element patternElem = (Element)j.next();
127 String fieldName = "name";
128 Attribute fieldAttribute = patternElem.attribute("field");
129 if ( fieldAttribute != null ) {
130 fieldName = fieldAttribute.getText();
132 String pattern = ".*(" + patternElem.getText()
134 regexConditions.add(new PropertyRegexCondition<Program>(fieldName, pattern, true));
136 Condition<Program> condition = new AndCondition<Program>(regexConditions);
137 filters.add(new ProgramFilter(condition, action));
141 Element notifier = root.element(ELEM_NOTIFICATION);
142 _notifier = parseNotifier(notifier);
144 } catch (DocumentException e) {
145 throw new RuntimeException("Error parsing program configuraiton", e);
150 * Parses the notifier
153 private Notifier parseNotifier(Element aNotifier) {
154 String from = aNotifier.elementTextTrim(ELEM_FROM);
155 String to = aNotifier.elementTextTrim(ELEM_TO);
156 String subject = aNotifier.elementTextTrim(ELEM_SUBJECT);
158 Element smtp = aNotifier.element(ELEM_SMTP);
159 MailServer server = parseMailServer( smtp );
161 Element format = aNotifier.element(ELEM_FORMAT);
162 String htmlXslt = format.elementTextTrim(ELEM_HTML);
163 String textXslt = format.elementTextTrim(ELEM_TEXT);
165 return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
169 * Parses the mail server from the XML.
170 * @param aSmtp Mail server configuration.
171 * @return Mail server.
173 private MailServer parseMailServer( Element aSmtp ) {
174 String host = aSmtp.elementTextTrim(ELEM_HOST);
175 Element portElem = aSmtp.element(ELEM_PORT);
177 if ( portElem != null ) {
178 port = Integer.valueOf(portElem.getTextTrim());
180 String username = aSmtp.elementTextTrim(ELEM_USERNAME);
181 String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
183 MailServer server = new MailServer(host, port, username, password);
188 * Returns the list of program filters.
189 * @return Filter list.
191 public List<ProgramFilter> getFilters() {
196 * Returns the notifier to use.
199 public Notifier getNotifier() {