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;
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;
34 * Parse the configuration of desired programs.
36 class ProgramConfigurationParser {
38 private static final String ELEM_PASSWORD = "password";
40 private static final String ELEM_USERNAME = "username";
42 private static final String ELEM_PORT = "port";
44 private static final String ELEM_HOST = "host";
46 // Formatting configuration.
47 private static final String ELEM_FORMAT = "format";
49 private static final String ELEM_TEXT = "text";
51 private static final String ELEM_HTML = "html";
54 // Mail server configuration.
56 private static final String ELEM_NOTIFICATION = "notification";
58 private static final String ELEM_SMTP = "smtp";
60 private static final String ELEM_SUBJECT = "subject";
62 private static final String ELEM_TO = "to";
64 private static final String ELEM_FROM = "from";
66 // Configuration of interesting programs.
68 private static final String ELEM_PROGRAM = "program";
70 private static final String ELEM_PATTERN = "match";
72 private static final String ELEM_ACTION = "action";
74 private static final String ELEM_CATEGORY = "category";
76 private static final String ACTION_NOTIFY = "notify";
78 private List<ProgramFilter> _filters;
80 private Notifier _notifier;
82 ProgramConfigurationParser() {
88 * Parses the condition used to match the desired programs.
91 * Input stream to parse from.
94 void parse(InputStream aStream) {
95 List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
97 SAXReader reader = new SAXReader();
98 Document document = reader.read(aStream);
100 Element root = document.getRootElement();
102 for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
103 Element program = (Element) i.next();
105 Element categoryElem = program.element(ELEM_CATEGORY);
106 String category = "";
107 if ( categoryElem != null ) {
108 category = categoryElem.getText().trim();
111 Element actionElem = program.element(ELEM_ACTION);
112 ProgramAction action = new RecordProgramAction();
113 if (actionElem != null) {
114 if (actionElem.getText().equals(ACTION_NOTIFY)) {
115 action = new InterestingProgramAction(category);
119 List<Condition<Program>> regexConditions =
120 new ArrayList<Condition<Program>>();
121 for (Iterator j = program.elementIterator(ELEM_PATTERN); j.hasNext(); ) {
122 Element patternElem = (Element)j.next();
123 String fieldName = "name";
124 Attribute fieldAttribute = patternElem.attribute("field");
125 if ( fieldAttribute != null ) {
126 fieldName = fieldAttribute.getText();
128 String pattern = ".*(" + patternElem.getText()
130 regexConditions.add(new PropertyRegexCondition<Program>(fieldName, pattern, true));
132 Condition<Program> condition = new AndCondition<Program>(regexConditions);
133 filters.add(new ProgramFilter(condition, action));
137 Element notifier = root.element(ELEM_NOTIFICATION);
138 _notifier = parseNotifier(notifier);
140 } catch (DocumentException e) {
141 throw new RuntimeException("Error parsing program configuraiton", e);
146 * Parses the notifier
149 private Notifier parseNotifier(Element aNotifier) {
150 String from = aNotifier.elementTextTrim(ELEM_FROM);
151 String to = aNotifier.elementTextTrim(ELEM_TO);
152 String subject = aNotifier.elementTextTrim(ELEM_SUBJECT);
154 Element smtp = aNotifier.element(ELEM_SMTP);
155 MailServer server = parseMailServer( smtp );
157 Element format = aNotifier.element(ELEM_FORMAT);
158 String htmlXslt = format.elementTextTrim(ELEM_HTML);
159 String textXslt = format.elementTextTrim(ELEM_TEXT);
161 return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
165 * Parses the mail server from the XML.
166 * @param aSmtp Mail server configuration.
167 * @return Mail server.
169 private MailServer parseMailServer( Element aSmtp ) {
170 String host = aSmtp.elementTextTrim(ELEM_HOST);
171 Element portElem = aSmtp.element(ELEM_PORT);
173 if ( portElem != null ) {
174 port = Integer.valueOf(portElem.getTextTrim());
176 String username = aSmtp.elementTextTrim(ELEM_USERNAME);
177 String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
179 MailServer server = new MailServer(host, port, username, password);
184 * Returns the list of program filters.
185 * @return Filter list.
187 public List<ProgramFilter> getFilters() {
192 * Returns the notifier to use.
195 public Notifier getNotifier() {