stylesheets are now searched in the classpath.
authorErik Brakkee <erik@brakkee.org>
Sat, 25 Mar 2006 21:03:17 +0000 (21:03 +0000)
committerErik Brakkee <erik@brakkee.org>
Sat, 25 Mar 2006 21:03:17 +0000 (21:03 +0000)
13 files changed:
crawler/basic/src/org/wamblee/crawler/AbstractPageRequest.java
crawler/kiss/src/channel-overview.xsl [moved from crawler/kiss/conf/kiss/channel-overview.xsl with 100% similarity]
crawler/kiss/src/channel-right-now.xsl [moved from crawler/kiss/conf/kiss/channel-right-now.xsl with 100% similarity]
crawler/kiss/src/channels-favorites.xsl [moved from crawler/kiss/conf/kiss/channels-favorites.xsl with 100% similarity]
crawler/kiss/src/identity.xsl [moved from crawler/kiss/conf/kiss/identity.xsl with 100% similarity]
crawler/kiss/src/login.xsl [moved from crawler/kiss/conf/kiss/login.xsl with 100% similarity]
crawler/kiss/src/org/wamblee/crawler/kiss/notification/MailNotifier.java
crawler/kiss/src/program-info.xsl [moved from crawler/kiss/conf/kiss/program-info.xsl with 100% similarity]
crawler/kiss/src/recorded.xsl [moved from crawler/kiss/conf/kiss/recorded.xsl with 100% similarity]
crawler/kiss/src/reportToHtml.xsl [moved from crawler/kiss/conf/kiss/reportToHtml.xsl with 100% similarity]
crawler/kiss/src/reportToText.xsl [moved from crawler/kiss/conf/kiss/reportToText.xsl with 100% similarity]
crawler/kiss/src/utilities.xsl [moved from crawler/kiss/conf/kiss/utilities.xsl with 100% similarity]
support/src/org/wamblee/xml/XSLT.java

index baf9510b8c2ee9d560d1e888fbb58b8bd5b7b170..66627ca523faa4c56a112811e7ccdf9381107825 100644 (file)
@@ -17,7 +17,6 @@
 package org.wamblee.crawler;
 
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.IOException;
 
 import javax.xml.transform.OutputKeys;
@@ -40,7 +39,7 @@ 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.io.FileResource;
+import org.wamblee.xml.ClasspathUriResolver;
 import org.wamblee.xml.DOMUtility;
 import org.wamblee.xml.XSLT;
 
@@ -155,8 +154,9 @@ public abstract class AbstractPageRequest implements PageRequest {
             aMethod = executeWithRedirects(aClient, aMethod);
             byte[] xhtmlData = getXhtml(aMethod);
 
-            Document transformed = new XSLT().transform(xhtmlData,
-                    new FileResource(new File(_xslt)));
+            XSLT xsltProcessor = new XSLT(new ClasspathUriResolver());
+            Document transformed = xsltProcessor.transform(xhtmlData,
+                    xsltProcessor.resolve(_xslt));
             ByteArrayOutputStream os = new ByteArrayOutputStream(); 
             Transformer transformer = TransformerFactory.newInstance()
                     .newTransformer();
index 47c3edd59c19d3be265f25403a8062b667986be6..536135b28508288f65f42bf82f3ea3089584d66c 100644 (file)
@@ -4,7 +4,6 @@
  */
 package org.wamblee.crawler.kiss.notification;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Date;
@@ -16,8 +15,7 @@ import javax.xml.transform.TransformerException;
 import org.apache.commons.mail.EmailException;
 import org.apache.commons.mail.HtmlEmail;
 import org.dom4j.Element;
-import org.wamblee.io.FileResource;
-import org.wamblee.io.InputResource;
+import org.wamblee.xml.ClasspathUriResolver;
 import org.wamblee.xml.XSLT;
 
 /**
@@ -80,10 +78,8 @@ public class MailNotifier implements Notifier {
             mail.setSentDate(new Date());
             mail.setSubject(_subject);
 
-            String htmlText = transformReport(aReport, new FileResource(
-                    new File(_htmlXslt)));
-            String plainText = transformReport(aReport, new FileResource(
-                    new File(_textXslt)));
+            String htmlText = transformReport(aReport, _htmlXslt);
+            String plainText = transformReport(aReport, _textXslt);
 
             mail.setHtmlMsg(htmlText);
             mail.setTextMsg(plainText);
@@ -113,9 +109,10 @@ public class MailNotifier implements Notifier {
      * @throws TransformerException
      *             In case of problems transforming.
      */
-    private String transformReport(Element aReport, InputResource aXslt)
+    private String transformReport(Element aReport, String aXslt)
             throws IOException, TransformerException {
         String reportXmlText = aReport.asXML();
-        return new XSLT().textTransform(reportXmlText.getBytes(), aXslt);
+        XSLT xsltProcessor = new XSLT(new ClasspathUriResolver());
+        return xsltProcessor.textTransform(reportXmlText.getBytes(), xsltProcessor.resolve(aXslt));
     }
 }
index ae603976b6de126e8a1ac22a3880fb76d1599f7c..7ffe63b3ff132d0a8cbaf49dca6610f837acd794 100644 (file)
@@ -18,8 +18,8 @@ package org.wamblee.xml;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
@@ -27,25 +27,60 @@ import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Document;
-import org.wamblee.io.InputResource;
+import org.wamblee.io.FileResource;
 
 /**
  * XSLT utilities.
  */
 public class XSLT {
 
+    private TransformerFactory _factory;
+
+    /**
+     * Constructs the URL resolver.
+     * 
+     * @param aResolver
+     *            URI resolver to use.
+     */
+    public XSLT(URIResolver aResolver) {
+        _factory = TransformerFactory.newInstance();
+        _factory.setURIResolver(aResolver);
+    }
+
     /**
      * Constructs the XSLT processor.
      * 
      */
     public XSLT() {
-        // Empty.
+        _factory = TransformerFactory.newInstance();
+    }
+
+    /**
+     * Resolves an XSLT based on URI. 
+     * @param aXslt XSLT to resolve, 
+     * @return Source for the XSLT
+     * @throws TransformerException In case the XSLT cannot be found. 
+     */
+    public Source resolve(String aXslt) throws TransformerException {
+        URIResolver resolver = _factory.getURIResolver();
+        if (resolver == null) {
+            if (new File(aXslt).canRead()) {
+                try {
+                    return new StreamSource(new FileResource(new File(aXslt))
+                            .getInputStream());
+                } catch (IOException e) {
+                    throw new TransformerException(e.getMessage(), e);
+                }
+            }
+        }
+        return resolver.resolve(aXslt, "");
     }
 
     /**
@@ -62,7 +97,7 @@ public class XSLT {
      * @throws TransformerException
      *             In case transformation fails.
      */
-    public Document transform(Document aDocument, InputResource aXslt)
+    public Document transform(Document aDocument, Source aXslt)
             throws IOException, TransformerException {
         Source source = new DOMSource(aDocument);
         DOMResult result = new DOMResult();
@@ -83,24 +118,28 @@ public class XSLT {
      * @throws TransformerException
      *             In case transformation fails.
      */
-    public Document transform(byte[] aDocument, InputResource aXslt)
+    public Document transform(byte[] aDocument, Source aXslt)
             throws IOException, TransformerException {
         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
         DOMResult result = new DOMResult();
         transform(source, result, aXslt);
         return (Document) result.getNode();
     }
-    
+
     /**
-     * Transforms a document to a text output. This supports XSLT transformations
-     * that result in text documents. 
-     * @param aDocument Document to transform.
-     * @param aXslt XSL transformation. 
-     * @return Transformed document.  
+     * Transforms a document to a text output. This supports XSLT
+     * transformations that result in text documents.
+     * 
+     * @param aDocument
+     *            Document to transform.
+     * @param aXslt
+     *            XSL transformation.
+     * @return Transformed document.
      */
-    public String textTransform(byte[] aDocument, InputResource aXslt) throws IOException, TransformerException { 
+    public String textTransform(byte[] aDocument, Source aXslt)
+            throws IOException, TransformerException {
         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
-        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
         StreamResult result = new StreamResult(os);
         transform(source, result, aXslt);
         return new String(os.toByteArray());
@@ -120,22 +159,14 @@ public class XSLT {
      * @throws TransformerException
      *             In case transformation fails.
      */
-    public void transform(Source aSource, Result aResult, InputResource aXslt)
+    public void transform(Source aSource, Result aResult, Source aXslt)
             throws IOException, TransformerException {
-        InputStream xslt = null;
         try {
-            xslt = aXslt.getInputStream();
-            Source xsltSource = new StreamSource(xslt);
-            Transformer transformer = TransformerFactory.newInstance()
-                    .newTransformer(xsltSource);
+            Transformer transformer = _factory.newTransformer(aXslt);
             transformer.transform(aSource, aResult);
         } catch (TransformerConfigurationException e) {
             throw new RuntimeException(
                     "Configuration problem of XSLT transformation", e);
-        } finally {
-            if (xslt != null) {
-                xslt.close();
-            }
         }
     }
 }