X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=crawler%2Fkiss%2Fsrc%2Forg%2Fwamblee%2Fcrawler%2Fkiss%2FProgramConfigurationParser.java;h=b75d3e058b765f4a470ed39675b9d8b416adeddc;hb=6c499198b6f0e13d9ee02cbfcb547952b7c6af67;hp=71719799c2162296902da68e8f6c7612382fc5b0;hpb=33225df8dfc5d87809fd0923b585c74245ad26b8;p=utils diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java index 71719799..b75d3e05 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java @@ -18,24 +18,46 @@ package org.wamblee.crawler.kiss; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; import java.util.Iterator; import java.util.List; +import javax.mail.internet.InternetAddress; + +import org.apache.commons.mail.SimpleEmail; +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 = "match"; + + private static final String ELEM_ACTION = "action"; + private static final String ELEM_CATEGORY = "category"; + + private static final String ACTION_NOTIFY = "notify"; - private static final String ELEM_PROGRAM = "program"; - private static final String ELEM_PATTERN = "name"; + private List _filters; + + private Notifier _notifier; + + ProgramConfigurationParser() { + _filters = null; + _notifier = null; + } /** * Parses the condition used to match the desired programs. @@ -44,22 +66,107 @@ public class ProgramConfigurationParser { * Input stream to parse from. * @return Condition. */ - Condition parse(InputStream aStream) { + void parse(InputStream aStream) { + List filters = new ArrayList(); try { SAXReader reader = new SAXReader(); Document document = reader.read(aStream); Element root = document.getRootElement(); - List> conditions = new ArrayList>(); - 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)); + for (Iterator i = root.elementIterator(ELEM_PROGRAM); i.hasNext();) { + Element program = (Element) i.next(); + + 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> regexConditions = + new ArrayList>(); + 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(fieldName, pattern, true)); + } + Condition condition = new AndCondition(regexConditions); + filters.add(new ProgramFilter(condition, action)); } - return new OrCondition(conditions); + _filters = filters; + + Element notifier = root.element("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("from"); + String to = aNotifier.elementTextTrim("to"); + String subject = aNotifier.elementTextTrim("subject"); + + Element smtp = aNotifier.element("smtp"); + MailServer server = parseMailServer( smtp ); + + Element format = aNotifier.element("format"); + String htmlXslt = format.elementTextTrim("html"); + String textXslt = format.elementTextTrim("text"); + + return new MailNotifier(from, to, subject, htmlXslt, textXslt, server); + } + + /** + * @param smtp + * @return + */ + private MailServer parseMailServer( Element smtp ) { + String host = smtp.elementTextTrim("host"); + Element portElem = smtp.element("port"); + int port = 25; + if ( portElem != null ) { + port = Integer.valueOf(portElem.getTextTrim()); + } + String username = smtp.elementTextTrim("username"); + String password = smtp.elementTextTrim("password"); + + MailServer server = new MailServer(host, port, username, password); + return server; + } + + /** + * Returns the list of program filters. + * @return Filter list. + */ + public List getFilters() { + return _filters; + } + + /** + * Returns the notifier to use. + * @return Notifier. + */ + public Notifier getNotifier() { + return _notifier; + } }