(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.Iterator;
22 import java.util.List;
23
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
33 /**
34  * Parse the configuration of desired programs.
35  */
36 class ProgramConfigurationParser {
37
38     private static final String ELEM_PASSWORD = "password";
39
40     private static final String ELEM_USERNAME = "username";
41
42     private static final String ELEM_PORT = "port";
43
44     private static final String ELEM_HOST = "host";
45
46     // Formatting configuration.
47     private static final String ELEM_FORMAT = "format";
48     
49     private static final String ELEM_TEXT = "text";
50
51     private static final String ELEM_HTML = "html";
52
53  
54     // Mail server configuration.
55
56     private static final String ELEM_NOTIFICATION = "notification";
57     
58     private static final String ELEM_SMTP = "smtp";
59
60     private static final String ELEM_SUBJECT = "subject";
61
62     private static final String ELEM_TO = "to";
63
64     private static final String ELEM_FROM = "from";
65
66     // Configuration of interesting programs.
67
68     private static final String ELEM_PROGRAM = "program";
69
70     private static final String ELEM_PATTERN = "match";
71
72     private static final String ELEM_ACTION = "action";
73     
74     private static final String ELEM_CATEGORY = "category";
75
76     private static final String ACTION_NOTIFY = "notify";
77     
78     private List<ProgramFilter> _filters; 
79     
80     private Notifier _notifier; 
81     
82     ProgramConfigurationParser() { 
83         _filters = null; 
84         _notifier = null; 
85     }
86
87     /**
88      * Parses the condition used to match the desired programs.
89      * 
90      * @param aStream
91      *            Input stream to parse from.
92      * @return Condition.
93      */
94     void parse(InputStream aStream) {
95         List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
96         try {
97             SAXReader reader = new SAXReader();
98             Document document = reader.read(aStream);
99
100             Element root = document.getRootElement();
101
102             for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
103                 Element program = (Element) i.next();
104
105                 Element categoryElem = program.element(ELEM_CATEGORY);
106                 String category = "";
107                 if ( categoryElem != null ) { 
108                     category = categoryElem.getText().trim(); 
109                 }
110                 
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);
116                     }
117                 }
118               
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(); 
127                     }
128                     String pattern = ".*(" + patternElem.getText()
129                     + ").*";
130                     regexConditions.add(new PropertyRegexCondition<Program>(fieldName, pattern, true));
131                 }
132                 Condition<Program> condition = new AndCondition<Program>(regexConditions);
133                 filters.add(new ProgramFilter(condition, action));
134             }
135             _filters = filters;
136             
137             Element notifier = root.element(ELEM_NOTIFICATION);
138             _notifier = parseNotifier(notifier);
139             
140         } catch (DocumentException e) {
141             throw new RuntimeException("Error parsing program configuraiton", e);
142         }
143     }
144     
145     /**
146      * Parses the notifier
147      * @return Notifier
148      */
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);
153         
154         Element smtp = aNotifier.element(ELEM_SMTP);
155         MailServer server = parseMailServer( smtp );
156         
157         Element format = aNotifier.element(ELEM_FORMAT);
158         String htmlXslt = format.elementTextTrim(ELEM_HTML);
159         String textXslt = format.elementTextTrim(ELEM_TEXT);
160         
161         return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
162     }
163
164     /**
165      * Parses the mail server from the XML.
166      * @param aSmtp Mail server configuration.
167      * @return Mail server.
168      */
169     private MailServer parseMailServer( Element aSmtp ) {
170         String host = aSmtp.elementTextTrim(ELEM_HOST);
171         Element portElem = aSmtp.element(ELEM_PORT);
172         int port = 25; 
173         if ( portElem != null ) {
174             port = Integer.valueOf(portElem.getTextTrim());
175         }
176         String username = aSmtp.elementTextTrim(ELEM_USERNAME);
177         String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
178         
179         MailServer server = new MailServer(host, port, username, password);
180         return server;
181     }
182     
183     /**
184      * Returns the list of program filters.
185      * @return Filter list.
186      */
187     public List<ProgramFilter> getFilters() { 
188         return _filters;
189     }
190     
191     /**
192      * Returns the notifier to use. 
193      * @return Notifier.
194      */
195     public Notifier getNotifier() { 
196         return _notifier; 
197     }
198 }