(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / ProgramConfigurationParser.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16
17 package org.wamblee.crawler.kiss;
18
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;
25
26 import javax.mail.internet.InternetAddress;
27
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;
37
38 /**
39  * Parse the configuration of desired programs.
40  */
41 class ProgramConfigurationParser {
42
43     private static final String ELEM_PROGRAM = "program";
44
45     private static final String ELEM_PATTERN = "match";
46
47     private static final String ELEM_ACTION = "action";
48     
49     private static final String ELEM_CATEGORY = "category";
50
51     private static final String ACTION_NOTIFY = "notify";
52     
53     private List<ProgramFilter> _filters; 
54     
55     private Notifier _notifier; 
56     
57     ProgramConfigurationParser() { 
58         _filters = null; 
59         _notifier = null; 
60     }
61
62     /**
63      * Parses the condition used to match the desired programs.
64      * 
65      * @param aStream
66      *            Input stream to parse from.
67      * @return Condition.
68      */
69     void parse(InputStream aStream) {
70         List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
71         try {
72             SAXReader reader = new SAXReader();
73             Document document = reader.read(aStream);
74
75             Element root = document.getRootElement();
76
77             for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
78                 Element program = (Element) i.next();
79
80                 Element categoryElem = program.element(ELEM_CATEGORY);
81                 String category = "";
82                 if ( categoryElem != null ) { 
83                     category = categoryElem.getText().trim(); 
84                 }
85                 
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);
91                     }
92                 }
93               
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(); 
102                     }
103                     String pattern = ".*(" + patternElem.getText()
104                     + ").*";
105                     regexConditions.add(new PropertyRegexCondition<Program>(fieldName, pattern, true));
106                 }
107                 Condition<Program> condition = new AndCondition<Program>(regexConditions);
108                 filters.add(new ProgramFilter(condition, action));
109             }
110             _filters = filters;
111             
112             Element notifier = root.element("notification");
113             _notifier = parseNotifier(notifier);
114             
115         } catch (DocumentException e) {
116             throw new RuntimeException("Error parsing program configuraiton", e);
117         }
118     }
119     
120     /**
121      * Parses the notifier
122      * @return Notifier
123      */
124     private Notifier parseNotifier(Element aNotifier) { 
125         String from = aNotifier.elementTextTrim("from");
126         String to = aNotifier.elementTextTrim("to");
127         String subject = aNotifier.elementTextTrim("subject");
128         
129         Element smtp = aNotifier.element("smtp");
130         MailServer server = parseMailServer( smtp );
131         
132         Element format = aNotifier.element("format");
133         String htmlXslt = format.elementTextTrim("html");
134         String textXslt = format.elementTextTrim("text");
135         
136         return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
137     }
138
139     /**
140      * @param smtp
141      * @return
142      */
143     private MailServer parseMailServer( Element smtp ) {
144         String host = smtp.elementTextTrim("host");
145         Element portElem = smtp.element("port");
146         int port = 25; 
147         if ( portElem != null ) {
148             port = Integer.valueOf(portElem.getTextTrim());
149         }
150         String username = smtp.elementTextTrim("username");
151         String password = smtp.elementTextTrim("password");
152         
153         MailServer server = new MailServer(host, port, username, password);
154         return server;
155     }
156     
157     /**
158      * Returns the list of program filters.
159      * @return Filter list.
160      */
161     public List<ProgramFilter> getFilters() { 
162         return _filters;
163     }
164     
165     /**
166      * Returns the notifier to use. 
167      * @return Notifier.
168      */
169     public Notifier getNotifier() { 
170         return _notifier; 
171     }
172 }