From 77ec7c5daa6c54d447ba6b58dde246cea9586173 Mon Sep 17 00:00:00 2001 From: erik Date: Sat, 25 Mar 2006 21:23:12 +0000 Subject: [PATCH] Now using dependency injection for the XslTransformer instead of constructing it every time it is needed. --- .../src/org/wamblee/crawler/AbstractPageRequest.java | 11 ++++++----- .../src/org/wamblee/crawler/GetPageRequest.java | 5 +++-- .../src/org/wamblee/crawler/PostPageRequest.java | 5 +++-- crawler/basic/src/org/wamblee/crawler/impl/App.java | 3 ++- .../wamblee/crawler/impl/ConfigurationParser.java | 11 +++++++---- .../org/wamblee/crawler/kiss/main/KissCrawler.java | 12 ++++++++---- .../kiss/main/ProgramConfigurationParser.java | 8 ++++++-- .../crawler/kiss/notification/MailNotifier.java | 10 ++++++---- 8 files changed, 41 insertions(+), 24 deletions(-) diff --git a/crawler/basic/src/org/wamblee/crawler/AbstractPageRequest.java b/crawler/basic/src/org/wamblee/crawler/AbstractPageRequest.java index 7a3755fe..5cb4fae6 100644 --- a/crawler/basic/src/org/wamblee/crawler/AbstractPageRequest.java +++ b/crawler/basic/src/org/wamblee/crawler/AbstractPageRequest.java @@ -39,7 +39,6 @@ import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Document; import org.w3c.tidy.Tidy; -import org.wamblee.xml.ClasspathUriResolver; import org.wamblee.xml.DOMUtility; import org.wamblee.xml.XslTransformer; @@ -59,6 +58,8 @@ public abstract class AbstractPageRequest implements PageRequest { private NameValuePair[] _params; private String _xslt; + + private XslTransformer _transformer; /** * Constructs the request. @@ -73,7 +74,7 @@ public abstract class AbstractPageRequest implements PageRequest { * XSLT used to convert the response. */ protected AbstractPageRequest(int aMaxTries, int aMaxDelay, - NameValuePair[] aParams, String aXslt) { + NameValuePair[] aParams, String aXslt, XslTransformer aTransformer) { if (aParams == null) { throw new IllegalArgumentException("aParams is null"); } @@ -84,6 +85,7 @@ public abstract class AbstractPageRequest implements PageRequest { _maxDelay = aMaxDelay; _params = aParams; _xslt = aXslt; + _transformer = aTransformer; } /* @@ -154,9 +156,8 @@ public abstract class AbstractPageRequest implements PageRequest { aMethod = executeWithRedirects(aClient, aMethod); byte[] xhtmlData = getXhtml(aMethod); - XslTransformer xsltProcessor = new XslTransformer(new ClasspathUriResolver()); - Document transformed = xsltProcessor.transform(xhtmlData, - xsltProcessor.resolve(_xslt)); + Document transformed = _transformer.transform(xhtmlData, + _transformer.resolve(_xslt)); ByteArrayOutputStream os = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance() .newTransformer(); diff --git a/crawler/basic/src/org/wamblee/crawler/GetPageRequest.java b/crawler/basic/src/org/wamblee/crawler/GetPageRequest.java index 1d92b024..9bc25608 100644 --- a/crawler/basic/src/org/wamblee/crawler/GetPageRequest.java +++ b/crawler/basic/src/org/wamblee/crawler/GetPageRequest.java @@ -25,6 +25,7 @@ import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.w3c.dom.Document; +import org.wamblee.xml.XslTransformer; /** * Gets a page by issueing a get request. @@ -38,8 +39,8 @@ public class GetPageRequest extends AbstractPageRequest { * @param aParams Request parameters to use. * @param aXslt XSLT to use. */ - public GetPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams, String aXslt) { - super(aMaxTries, aMaxDelay, aParams, aXslt); + public GetPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams, String aXslt, XslTransformer aTransformer) { + super(aMaxTries, aMaxDelay, aParams, aXslt, aTransformer); } /* diff --git a/crawler/basic/src/org/wamblee/crawler/PostPageRequest.java b/crawler/basic/src/org/wamblee/crawler/PostPageRequest.java index db1f2eb9..598afe05 100644 --- a/crawler/basic/src/org/wamblee/crawler/PostPageRequest.java +++ b/crawler/basic/src/org/wamblee/crawler/PostPageRequest.java @@ -24,6 +24,7 @@ import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.w3c.dom.Document; +import org.wamblee.xml.XslTransformer; /** * Retrieving pages using the post method. @@ -37,8 +38,8 @@ public class PostPageRequest extends AbstractPageRequest { * @param aParams Request parameters to use. * @param aXslt XSLT to use. */ - public PostPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams, String aXslt) { - super(aMaxTries, aMaxDelay, aParams, aXslt); + public PostPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams, String aXslt, XslTransformer aTransformer) { + super(aMaxTries, aMaxDelay, aParams, aXslt, aTransformer); } /* diff --git a/crawler/basic/src/org/wamblee/crawler/impl/App.java b/crawler/basic/src/org/wamblee/crawler/impl/App.java index d4ca4709..f9b9bd45 100644 --- a/crawler/basic/src/org/wamblee/crawler/impl/App.java +++ b/crawler/basic/src/org/wamblee/crawler/impl/App.java @@ -11,6 +11,7 @@ import org.wamblee.crawler.Configuration; import org.wamblee.crawler.Crawler; import org.wamblee.crawler.Page; import org.wamblee.crawler.PageException; +import org.wamblee.xml.XslTransformer; /* * Copyright 2005 the original author or authors. @@ -54,7 +55,7 @@ public final class App { String configFileName = aArgs[0]; String starturl = aArgs[1]; - ConfigurationParser parser = new ConfigurationParser(); + ConfigurationParser parser = new ConfigurationParser(new XslTransformer()); InputStream configFile = new FileInputStream(new File(configFileName)); Configuration config = parser.parse(configFile); diff --git a/crawler/basic/src/org/wamblee/crawler/impl/ConfigurationParser.java b/crawler/basic/src/org/wamblee/crawler/impl/ConfigurationParser.java index 792d0d34..7e15d4a3 100644 --- a/crawler/basic/src/org/wamblee/crawler/impl/ConfigurationParser.java +++ b/crawler/basic/src/org/wamblee/crawler/impl/ConfigurationParser.java @@ -30,6 +30,7 @@ import org.wamblee.crawler.Configuration; import org.wamblee.crawler.GetPageRequest; import org.wamblee.crawler.PageRequest; import org.wamblee.crawler.PostPageRequest; +import org.wamblee.xml.XslTransformer; /** * Parsing of the configuration from an XML file. @@ -59,12 +60,14 @@ public class ConfigurationParser { private static final int MAX_TRIES = 3; private static final int MAX_DELAY = 100; + + private XslTransformer _transformer; /** * Constructs the configuration parser. */ - public ConfigurationParser() { - // Empty + public ConfigurationParser(XslTransformer aTransformer) { + _transformer = aTransformer; } /** @@ -157,10 +160,10 @@ public class ConfigurationParser { PageRequest request; if (METHOD_POST.equals(method)) { request = new PostPageRequest(MAX_TRIES, MAX_DELAY, paramsArray, - xslt); + xslt, _transformer); } else if (METHOD_GET.equals(method) || method == null) { request = new GetPageRequest(MAX_TRIES, MAX_DELAY, paramsArray, - xslt); + xslt, _transformer); } else { throw new RuntimeException("Unknown request method '" + method + "'. Only " + METHOD_GET + " and " + METHOD_POST diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/main/KissCrawler.java b/crawler/kiss/src/org/wamblee/crawler/kiss/main/KissCrawler.java index c522131d..ad8de402 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/main/KissCrawler.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/main/KissCrawler.java @@ -46,6 +46,8 @@ import org.wamblee.crawler.kiss.guide.Time; import org.wamblee.crawler.kiss.guide.TimeInterval; import org.wamblee.crawler.kiss.notification.NotificationException; import org.wamblee.crawler.kiss.notification.Notifier; +import org.wamblee.xml.ClasspathUriResolver; +import org.wamblee.xml.XslTransformer; /** * The KiSS crawler for automatic recording of interesting TV shows. @@ -117,11 +119,13 @@ public class KissCrawler { try { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("127.0.0.1", 3128); + + XslTransformer transformer = new XslTransformer(new ClasspathUriResolver()); - Crawler crawler = createCrawler(aCrawlerConfig, client); + Crawler crawler = createCrawler(aCrawlerConfig, client, transformer); InputStream programConfigFile = new FileInputStream(new File( aProgramConfig)); - ProgramConfigurationParser parser = new ProgramConfigurationParser(); + ProgramConfigurationParser parser = new ProgramConfigurationParser(transformer); parser.parse(programConfigFile); List programFilters = parser.getFilters(); @@ -177,8 +181,8 @@ public class KissCrawler { * In case configuration files cannot be found. */ private Crawler createCrawler(String aCrawlerConfig, - HttpClient aClient) throws FileNotFoundException { - ConfigurationParser parser = new ConfigurationParser(); + HttpClient aClient, XslTransformer aTransformer) throws FileNotFoundException { + ConfigurationParser parser = new ConfigurationParser(aTransformer); InputStream crawlerConfigFile = new FileInputStream(new File( aCrawlerConfig)); Configuration config = parser.parse(crawlerConfigFile); 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 c16f4f93..c5fd5602 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java @@ -33,6 +33,7 @@ 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. @@ -84,12 +85,15 @@ class ProgramConfigurationParser { private static final String ACTION_NOTIFY = "notify"; private List _filters; + + private XslTransformer _transformer; private Notifier _notifier; - ProgramConfigurationParser() { + ProgramConfigurationParser(XslTransformer aTransformer) { _filters = null; _notifier = null; + _transformer = aTransformer; } /** @@ -173,7 +177,7 @@ class ProgramConfigurationParser { 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); } /** diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/notification/MailNotifier.java b/crawler/kiss/src/org/wamblee/crawler/kiss/notification/MailNotifier.java index 8d2ccf97..558a7738 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/notification/MailNotifier.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/notification/MailNotifier.java @@ -15,7 +15,6 @@ import javax.xml.transform.TransformerException; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.dom4j.Element; -import org.wamblee.xml.ClasspathUriResolver; import org.wamblee.xml.XslTransformer; /** @@ -35,6 +34,8 @@ public class MailNotifier implements Notifier { private String _textXslt; private MailServer _server; + + private XslTransformer _transformer; /** * Constructs the notifier. @@ -51,15 +52,17 @@ public class MailNotifier implements Notifier { * XSLT file to transform the report into text. * @param aServer * Mail server to use. + * @param aTransformer Transformer to use. */ public MailNotifier(String aFrom, String aTo, String aSubject, - String aHtmlXslt, String aTextXslt, MailServer aServer) { + String aHtmlXslt, String aTextXslt, MailServer aServer, XslTransformer aTransformer) { _from = aFrom; _to = aTo; _subject = aSubject; _htmlXslt = aHtmlXslt; _textXslt = aTextXslt; _server = aServer; + _transformer = aTransformer; } /* @@ -112,7 +115,6 @@ public class MailNotifier implements Notifier { private String transformReport(Element aReport, String aXslt) throws IOException, TransformerException { String reportXmlText = aReport.asXML(); - XslTransformer xsltProcessor = new XslTransformer(new ClasspathUriResolver()); - return xsltProcessor.textTransform(reportXmlText.getBytes(), xsltProcessor.resolve(aXslt)); + return _transformer.textTransform(reportXmlText.getBytes(), _transformer.resolve(aXslt)); } } -- 2.31.1