(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / ProgramConfigurationParser.java
index e9ba272e3ec33fe25497c6637d87974af91075f8..3237ca79a449d140001b072d95fa266781fa644b 100644 (file)
@@ -35,6 +35,36 @@ import org.wamblee.conditions.PropertyRegexCondition;
  */
 class ProgramConfigurationParser {
 
+    private static final String ELEM_PASSWORD = "password";
+
+    private static final String ELEM_USERNAME = "username";
+
+    private static final String ELEM_PORT = "port";
+
+    private static final String ELEM_HOST = "host";
+
+    // Formatting configuration.
+    private static final String ELEM_FORMAT = "format";
+    
+    private static final String ELEM_TEXT = "text";
+
+    private static final String ELEM_HTML = "html";
+
+    // Mail server configuration.
+
+    private static final String ELEM_NOTIFICATION = "notification";
+    
+    private static final String ELEM_SMTP = "smtp";
+
+    private static final String ELEM_SUBJECT = "subject";
+
+    private static final String ELEM_TO = "to";
+
+    private static final String ELEM_FROM = "from";
+
+    // Configuration of interesting programs.
+
     private static final String ELEM_PROGRAM = "program";
 
     private static final String ELEM_PATTERN = "match";
@@ -44,6 +74,15 @@ class ProgramConfigurationParser {
     private static final String ELEM_CATEGORY = "category";
 
     private static final String ACTION_NOTIFY = "notify";
+    
+    private List<ProgramFilter> _filters; 
+    
+    private Notifier _notifier; 
+    
+    ProgramConfigurationParser() { 
+        _filters = null; 
+        _notifier = null; 
+    }
 
     /**
      * Parses the condition used to match the desired programs.
@@ -52,7 +91,7 @@ class ProgramConfigurationParser {
      *            Input stream to parse from.
      * @return Condition.
      */
-    List<ProgramFilter> parse(InputStream aStream) {
+    void parse(InputStream aStream) {
         List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
         try {
             SAXReader reader = new SAXReader();
@@ -93,9 +132,67 @@ class ProgramConfigurationParser {
                 Condition<Program> condition = new AndCondition<Program>(regexConditions);
                 filters.add(new ProgramFilter(condition, action));
             }
-            return filters;
+            _filters = filters;
+            
+            Element notifier = root.element(ELEM_NOTIFICATION);
+            _notifier = parseNotifier(notifier);
+            
         } catch (DocumentException e) {
             throw new RuntimeException("Error parsing program configuraiton", e);
         }
     }
+    
+    /**
+     * Parses the notifier
+     * @return Notifier
+     */
+    private Notifier parseNotifier(Element aNotifier) { 
+        String from = aNotifier.elementTextTrim(ELEM_FROM);
+        String to = aNotifier.elementTextTrim(ELEM_TO);
+        String subject = aNotifier.elementTextTrim(ELEM_SUBJECT);
+        
+        Element smtp = aNotifier.element(ELEM_SMTP);
+        MailServer server = parseMailServer( smtp );
+        
+        Element format = aNotifier.element(ELEM_FORMAT);
+        String htmlXslt = format.elementTextTrim(ELEM_HTML);
+        String textXslt = format.elementTextTrim(ELEM_TEXT);
+        
+        return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
+    }
+
+    /**
+     * Parses the mail server from the XML.
+     * @param aSmtp Mail server configuration.
+     * @return Mail server.
+     */
+    private MailServer parseMailServer( Element aSmtp ) {
+        String host = aSmtp.elementTextTrim(ELEM_HOST);
+        Element portElem = aSmtp.element(ELEM_PORT);
+        int port = 25; 
+        if ( portElem != null ) {
+            port = Integer.valueOf(portElem.getTextTrim());
+        }
+        String username = aSmtp.elementTextTrim(ELEM_USERNAME);
+        String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
+        
+        MailServer server = new MailServer(host, port, username, password);
+        return server;
+    }
+    
+    /**
+     * Returns the list of program filters.
+     * @return Filter list.
+     */
+    public List<ProgramFilter> getFilters() { 
+        return _filters;
+    }
+    
+    /**
+     * Returns the notifier to use. 
+     * @return Notifier.
+     */
+    public Notifier getNotifier() { 
+        return _notifier; 
+    }
 }