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