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;
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.
* 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();
Condition<Program> condition = new AndCondition<Program>(regexConditions);
filters.add(new ProgramFilter(condition, action));
}
- return filters;
+ _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<ProgramFilter> getFilters() {
+ return _filters;
+ }
+
+ /**
+ * Returns the notifier to use.
+ * @return Notifier.
+ */
+ public Notifier getNotifier() {
+ return _notifier;
+ }
}