X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=crawler%2Fkiss%2Fsrc%2Forg%2Fwamblee%2Fcrawler%2Fkiss%2FProgramConfigurationParser.java;h=3237ca79a449d140001b072d95fa266781fa644b;hb=3e7fabe4d28276a0c1c87069e6df43f1790b4fb8;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..3237ca79 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java @@ -21,21 +21,68 @@ 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_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 = "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"; + + private List _filters; + + private Notifier _notifier; + + ProgramConfigurationParser() { + _filters = null; + _notifier = null; + } /** * Parses the condition used to match the desired programs. @@ -44,22 +91,108 @@ 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(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 getFilters() { + return _filters; + } + + /** + * Returns the notifier to use. + * @return Notifier. + */ + public Notifier getNotifier() { + return _notifier; + } }