(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / ProgramConfigurationParser.java
index 5615ced2f6973d973c4cbcc623f35fa0a5f4bbc4..e9ba272e3ec33fe25497c6637d87974af91075f8 100644 (file)
@@ -21,21 +21,29 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
+import org.dom4j.Attribute;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
 import org.dom4j.Element;
 import org.dom4j.io.SAXReader;
+import org.wamblee.conditions.AndCondition;
 import org.wamblee.conditions.Condition;
-import org.wamblee.conditions.OrCondition;
+import org.wamblee.conditions.PropertyRegexCondition;
 
 /**
  * Parse the configuration of desired programs.
  */
-public class ProgramConfigurationParser {
+class ProgramConfigurationParser {
 
     private static final String ELEM_PROGRAM = "program";
 
-    private static final String ELEM_PATTERN = "name";
+    private static final String ELEM_PATTERN = "match";
+
+    private static final String ELEM_ACTION = "action";
+    
+    private static final String ELEM_CATEGORY = "category";
+
+    private static final String ACTION_NOTIFY = "notify";
 
     /**
      * Parses the condition used to match the desired programs.
@@ -44,21 +52,48 @@ public class ProgramConfigurationParser {
      *            Input stream to parse from.
      * @return Condition.
      */
-    Condition<Program> parse(InputStream aStream) {
+    List<ProgramFilter> parse(InputStream aStream) {
+        List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
         try {
             SAXReader reader = new SAXReader();
             Document document = reader.read(aStream);
 
             Element root = document.getRootElement();
-            List<Condition<Program>> conditions = new ArrayList<Condition<Program>>();
 
             for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) {
                 Element program = (Element) i.next();
-                String pattern = ".*" + program.element(ELEM_PATTERN).getText()
-                        + ".*";
-                conditions.add(new ProgramNameMatcher(pattern));
+
+                Element categoryElem = program.element(ELEM_CATEGORY);
+                String category = "";
+                if ( categoryElem != null ) { 
+                    category = categoryElem.getText().trim(); 
+                }
+                
+                Element actionElem = program.element(ELEM_ACTION);
+                ProgramAction action = new RecordProgramAction();
+                if (actionElem != null) {
+                    if (actionElem.getText().equals(ACTION_NOTIFY)) {
+                        action = new InterestingProgramAction(category);
+                    }
+                }
+              
+                List<Condition<Program>> regexConditions = 
+                    new ArrayList<Condition<Program>>();
+                for (Iterator j = program.elementIterator(ELEM_PATTERN); j.hasNext(); ) {
+                    Element patternElem = (Element)j.next();
+                    String fieldName = "name"; 
+                    Attribute fieldAttribute = patternElem.attribute("field"); 
+                    if ( fieldAttribute != null ) { 
+                        fieldName = fieldAttribute.getText(); 
+                    }
+                    String pattern = ".*(" + patternElem.getText()
+                    + ").*";
+                    regexConditions.add(new PropertyRegexCondition<Program>(fieldName, pattern, true));
+                }
+                Condition<Program> condition = new AndCondition<Program>(regexConditions);
+                filters.add(new ProgramFilter(condition, action));
             }
-            return new OrCondition<Program>(conditions);
+            return filters;
         } catch (DocumentException e) {
             throw new RuntimeException("Error parsing program configuraiton", e);
         }