X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=crawler%2Fkiss%2Fsrc%2Forg%2Fwamblee%2Fcrawler%2Fkiss%2Fmain%2FProgramConfigurationParser.java;h=2e22f351baca0ac126d5f5afbf288f54c956fa01;hb=40b576e854e9b21f090015dfc1814df6c0dde2ca;hp=24d7d9b3f93dda8f1e933e08f7dc01c1d925190d;hpb=d85bc24e068a68a54786fae5dc71573607b3b0cb;p=utils diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java index 24d7d9b3..2e22f351 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java @@ -33,11 +33,15 @@ import org.wamblee.crawler.kiss.guide.Program; import org.wamblee.crawler.kiss.notification.MailNotifier; import org.wamblee.crawler.kiss.notification.MailServer; import org.wamblee.crawler.kiss.notification.Notifier; +import org.wamblee.xml.XslTransformer; /** * Parse the configuration of desired programs. */ class ProgramConfigurationParser { + private static final int DEFAULT_SMTP_PORT = 25; + + private static final int DEFAULT_PRIORITY = 1; private static final String ELEM_PASSWORD = "password"; @@ -49,16 +53,15 @@ class ProgramConfigurationParser { // 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"; @@ -70,22 +73,27 @@ class ProgramConfigurationParser { // Configuration of interesting programs. private static final String ELEM_PROGRAM = "program"; + + private static final String ELEM_PRIORITY = "priority"; 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 List _filters; - - private Notifier _notifier; - - ProgramConfigurationParser() { - _filters = null; - _notifier = null; + private XslTransformer _transformer; + + private Notifier _notifier; + + ProgramConfigurationParser(XslTransformer aTransformer) { + _filters = null; + _notifier = null; + _transformer = aTransformer; } /** @@ -108,95 +116,105 @@ class ProgramConfigurationParser { Element categoryElem = program.element(ELEM_CATEGORY); String category = ""; - if ( categoryElem != null ) { - category = categoryElem.getText().trim(); + if (categoryElem != null) { + category = categoryElem.getText().trim(); } - + Element actionElem = program.element(ELEM_ACTION); - ProgramAction action = new RecordProgramAction(); + int priority = DEFAULT_PRIORITY; + String priorityString = program.elementTextTrim(ELEM_PRIORITY); + if ( priorityString != null ) { + priority = Integer.valueOf(priorityString); + } + ProgramAction action = new RecordProgramAction(priority); 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(); + + 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)); + String pattern = ".*(" + patternElem.getText() + ").*"; + regexConditions.add(new PropertyRegexCondition( + fieldName, pattern, true)); } - Condition condition = new AndCondition(regexConditions); + Condition condition = new AndCondition( + regexConditions); filters.add(new ProgramFilter(condition, action)); } _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) { + 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 ); - + 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); + + return new MailNotifier(from, to, subject, htmlXslt, textXslt, server, _transformer); } /** * Parses the mail server from the XML. - * @param aSmtp Mail server configuration. + * + * @param aSmtp + * Mail server configuration. * @return Mail server. */ - private MailServer parseMailServer( Element aSmtp ) { + private MailServer parseMailServer(Element aSmtp) { String host = aSmtp.elementTextTrim(ELEM_HOST); Element portElem = aSmtp.element(ELEM_PORT); - int port = 25; - if ( portElem != null ) { + int port = DEFAULT_SMTP_PORT; + 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; + + return new MailServer(host, port, username, password); } - + /** * Returns the list of program filters. + * * @return Filter list. */ - public List getFilters() { + public List getFilters() { return _filters; } - + /** - * Returns the notifier to use. + * Returns the notifier to use. + * * @return Notifier. */ - public Notifier getNotifier() { - return _notifier; + public Notifier getNotifier() { + return _notifier; } }