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.Arrays;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.List;
26 import javax.mail.internet.InternetAddress;
28 import org.apache.commons.mail.SimpleEmail;
29 import org.dom4j.Attribute;
30 import org.dom4j.Document;
31 import org.dom4j.DocumentException;
32 import org.dom4j.Element;
33 import org.dom4j.io.SAXReader;
34 import org.wamblee.conditions.AndCondition;
35 import org.wamblee.conditions.Condition;
36 import org.wamblee.conditions.PropertyRegexCondition;
39 * Parse the configuration of desired programs.
41 class ProgramConfigurationParser {
43 private static final String ELEM_PROGRAM = "program";
45 private static final String ELEM_PATTERN = "match";
47 private static final String ELEM_ACTION = "action";
49 private static final String ELEM_CATEGORY = "category";
51 private static final String ACTION_NOTIFY = "notify";
53 private List<ProgramFilter> _filters;
55 private Notifier _notifier;
57 ProgramConfigurationParser() {
63 * Parses the condition used to match the desired programs.
66 * Input stream to parse from.
69 void parse(InputStream aStream) {
70 List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
72 SAXReader reader = new SAXReader();
73 Document document = reader.read(aStream);
75 Element root = document.getRootElement();
77 for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
78 Element program = (Element) i.next();
80 Element categoryElem = program.element(ELEM_CATEGORY);
82 if ( categoryElem != null ) {
83 category = categoryElem.getText().trim();
86 Element actionElem = program.element(ELEM_ACTION);
87 ProgramAction action = new RecordProgramAction();
88 if (actionElem != null) {
89 if (actionElem.getText().equals(ACTION_NOTIFY)) {
90 action = new InterestingProgramAction(category);
94 List<Condition<Program>> regexConditions =
95 new ArrayList<Condition<Program>>();
96 for (Iterator j = program.elementIterator(ELEM_PATTERN); j.hasNext(); ) {
97 Element patternElem = (Element)j.next();
98 String fieldName = "name";
99 Attribute fieldAttribute = patternElem.attribute("field");
100 if ( fieldAttribute != null ) {
101 fieldName = fieldAttribute.getText();
103 String pattern = ".*(" + patternElem.getText()
105 regexConditions.add(new PropertyRegexCondition<Program>(fieldName, pattern, true));
107 Condition<Program> condition = new AndCondition<Program>(regexConditions);
108 filters.add(new ProgramFilter(condition, action));
112 Element notifier = root.element("notification");
113 _notifier = parseNotifier(notifier);
115 } catch (DocumentException e) {
116 throw new RuntimeException("Error parsing program configuraiton", e);
121 * Parses the notifier
124 private Notifier parseNotifier(Element aNotifier) {
125 String from = aNotifier.elementTextTrim("from");
126 String to = aNotifier.elementTextTrim("to");
127 String subject = aNotifier.elementTextTrim("subject");
129 Element smtp = aNotifier.element("smtp");
130 MailServer server = parseMailServer( smtp );
132 Element format = aNotifier.element("format");
133 String htmlXslt = format.elementTextTrim("html");
134 String textXslt = format.elementTextTrim("text");
136 return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
143 private MailServer parseMailServer( Element smtp ) {
144 String host = smtp.elementTextTrim("host");
145 Element portElem = smtp.element("port");
147 if ( portElem != null ) {
148 port = Integer.valueOf(portElem.getTextTrim());
150 String username = smtp.elementTextTrim("username");
151 String password = smtp.elementTextTrim("password");
153 MailServer server = new MailServer(host, port, username, password);
158 * Returns the list of program filters.
159 * @return Filter list.
161 public List<ProgramFilter> getFilters() {
166 * Returns the notifier to use.
169 public Notifier getNotifier() {