Minor improvements; Changed location of xsl files
authorn.hoffmann <n.hoffmann@localhost>
Tue, 24 May 2011 15:33:31 +0000 (15:33 +0000)
committern.hoffmann <n.hoffmann@localhost>
Tue, 24 May 2011 15:33:31 +0000 (15:33 +0000)
.gitattributes
cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/RemoteXMLEntityFactory.java
cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/AbstractPublishOutputModule.java
cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/IPublishOutputModule.java
cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/odf/OdfOutputModule.java
cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/pdf/PdfOutputModule.java
cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/taxpub/TaxPubOutputModule.java
cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/xml/XMLOutputModule.java
cdmlib-print/src/main/resources/stylesheets/odf/cdmToOdfText.xsl [moved from cdmlib-print/src/main/resources/eu/etaxonomy/cdm/print/out/odf/cdmToOdfText.xsl with 100% similarity]
cdmlib-print/src/main/resources/stylesheets/pdf/cdmToPdf.xsl [moved from cdmlib-print/src/main/resources/eu/etaxonomy/cdm/print/out/pdf/cdmToPdf.xsl with 100% similarity]
cdmlib-print/src/test/java/eu/etaxonomy/cdm/print/out/pdf/PdfOutputModuleTest.java

index b519139869bcc663174a7efd8591db6b3b29d11a..501da90f7e11517194f2d5c24d6add617507d477 100644 (file)
@@ -1369,10 +1369,10 @@ cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/odf/StylesFactory.java -te
 cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/pdf/PdfOutputModule.java -text
 cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/taxpub/TaxPubOutputModule.java -text
 cdmlib-print/src/main/java/eu/etaxonomy/cdm/print/out/xml/XMLOutputModule.java -text
-cdmlib-print/src/main/resources/eu/etaxonomy/cdm/print/out/odf/cdmToOdfText.xsl -text
-cdmlib-print/src/main/resources/eu/etaxonomy/cdm/print/out/pdf/cdmToPdf.xsl -text
 cdmlib-print/src/main/resources/eu/etaxonomy/cdm/print/package.html -text
 cdmlib-print/src/main/resources/log4j.properties -text
+cdmlib-print/src/main/resources/stylesheets/odf/cdmToOdfText.xsl -text
+cdmlib-print/src/main/resources/stylesheets/pdf/cdmToPdf.xsl -text
 cdmlib-print/src/site/apt/download.apt -text
 cdmlib-print/src/site/apt/formats.apt -text
 cdmlib-print/src/site/apt/getting-started.apt -text
index 895378d9fb118f4bd7da30909b91110c19cbc6e7..fef5c2adcd7ffa54add2b06a3c305668180ebfb6 100644 (file)
@@ -36,7 +36,7 @@ import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
 
 /**
  * Implementation of an IXMLEntityFactory that is connected to a CDM Community Server on 
- * a remote machine. API call will be executed by accessing the the servers REST API.
+ * a remote machine. API call will be executed by accessing the servers REST API.
  * 
  * @author n.hoffmann
  * @created Apr 6, 2010
index bdb75c719e26a54326eeed6903c4b62152b79246..22b1f041d67e83828163b0950eccf373c5ed2c75 100644 (file)
 package eu.etaxonomy.cdm.print.out;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
+import java.util.List;
+
+import javax.imageio.stream.FileImageInputStream;
 
 import org.apache.log4j.Logger;
 import org.jdom.Document;
 
+import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.common.IProgressMonitor;
 
 /**
@@ -35,6 +47,16 @@ public abstract class AbstractPublishOutputModule implements IPublishOutputModul
         * The date format used by {@link #generateFilenameWithDate(String, String)}
         */
        public static final String DATE_FORMAT_NOW = "yyyyMMdd-HHmm";
+
+       private FilenameFilter filter = new FilenameFilter() {
+               
+               @Override
+               public boolean accept(File dir, String fileName) {
+                       return fileName.substring(fileName.length() - 3).equals("xsl");
+               }
+       };
+
+       private File xslt;
                
        /**
         * Generates a string containing the current date followed by the given name.
@@ -79,4 +101,73 @@ public abstract class AbstractPublishOutputModule implements IPublishOutputModul
                logger.trace(message);
                progressMonitor.subTask(message);
        }
+       
+
+       @Override
+       public List<File> getStylesheets() throws IOException {
+               List<File> stylesheets = new ArrayList<File>();
+               
+               for(File directory : getStylesheetLocations()){
+                       if(directory.exists() && directory.isDirectory()){
+                               stylesheets.addAll(getStylesheetsByLocation(directory));
+                       }else{
+                               logger.info(String.format("Tried to read styleshets from '%s', but it does not exist or is not a directory", directory));
+                       }
+               }
+               
+               return stylesheets;
+               
+       }
+       
+       private List<File> getStylesheetLocations() throws IOException {
+               List<File> locationList = new ArrayList<File>();
+               
+               String l = File.separator;
+               
+               URL shippedStylesheetsResource = AbstractPublishOutputModule.class.getResource("/stylesheets/pdf/");            
+               File shippedStylesheetsDir = new File(shippedStylesheetsResource.getFile());
+               locationList.add(shippedStylesheetsDir);
+               
+               // TODO this should be configured in a central place, see #2387
+               String cdmlibHomeDir = CdmUtils.getHomeDir() + l + ".cdmLibrary";
+               
+               File userdir = new File(cdmlibHomeDir + l + "stylesheets"  + l + getOutputFileSuffix());
+               locationList.add(userdir);
+               
+               return locationList;
+       }
+       
+       @Override
+       public File getXslt() {
+               return xslt;
+       }
+       
+       @Override
+       public void setXslt(File xslt){
+               this.xslt = xslt;
+       }
+       
+       public InputStream getXsltInputStream(){
+               if(getXslt() == null){
+                       return getDefaultXsltInputStream();
+               }
+               
+               try {
+                       return new FileInputStream(getXslt());
+               } catch (FileNotFoundException e) {
+                       logger.error(e);
+               }
+               return null;
+       }
+       
+       protected InputStream getDefaultXsltInputStream() {
+               return null;
+       }
+
+       public List<File> getStylesheetsByLocation(File stylesheetFolder){
+               
+               File[] stylesheets = stylesheetFolder.listFiles(filter);
+               
+               return Arrays.asList(stylesheets);
+       }
 }
index 08a555f17f4717dd8cb95e287dd91a73cf3a9c91..4664120ce1dc7ebb7b91d9052a7712f70d676fb0 100644 (file)
@@ -11,6 +11,8 @@
 package eu.etaxonomy.cdm.print.out;
 
 import java.io.File;
+import java.io.IOException;
+import java.util.List;
 
 import org.jdom.Document;
 
@@ -45,4 +47,24 @@ public interface IPublishOutputModule {
         */
        public void output(Document document, File targetFolder, IProgressMonitor progressMonitor);
        
+       /**
+        * Returns all available stylesheets for this output module. Search path will include the users ~/.cdmLibrary directory
+        * to allow for custom stylesheets
+        * 
+        * @return a set of xsl files
+        * @throws IOException TODO
+        */
+       public List<File> getStylesheets() throws IOException;
+       
+       /** 
+        * @return the xslt file associated with this output module
+        */
+       public File getXslt();
+       
+       /** 
+        * Associate an xsl  file to be used by this output module
+        * 
+        * @param xslt a file
+        */
+       public void setXslt(File xslt);
 }
index 3247693aff87c633907b3dcf7f55ca4327ffcff8..156f18efe84c7ecb51a43d61746c55e29d539f3f 100644 (file)
@@ -4,7 +4,9 @@
 package eu.etaxonomy.cdm.print.out.odf;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 import org.jdom.Document;
@@ -27,18 +29,16 @@ public class OdfOutputModule extends AbstractPublishOutputModule {
        private static final Logger logger = Logger
                .getLogger(OdfOutputModule.class);
        
-       public static String STYLESHEET_RESOURCE_DEFAULT = "/eu/etaxonomy/cdm/print/out/odf/cdmToOdfText.xsl";
+       public static String STYLESHEET_RESOURCE_DEFAULT = "/stylesheets/odf/cdmToOdfText.xsl";
        
        private DocumentCreator documentCreator;
        private Transformator transformator;
-
-       private InputStream stylesheet;
        
        public OdfOutputModule() {
-               stylesheet = OdfOutputModule.class.getResourceAsStream(STYLESHEET_RESOURCE_DEFAULT);
+               InputStream xslt = getXsltInputStream();
                documentCreator = new DocumentCreator();
                try {
-                       transformator = new Transformator(stylesheet);  
+                       transformator = new Transformator(xslt);        
                } 
                catch (XSLTransformException e) {
                        logger.error("XSLTransformException while creating ODF output module", e);
@@ -75,5 +75,9 @@ public class OdfOutputModule extends AbstractPublishOutputModule {
        public String getOutputFileSuffix() {
                return "odf";
        }
-
+       
+       @Override
+       protected InputStream getDefaultXsltInputStream() {
+               return OdfOutputModule.class.getResourceAsStream(STYLESHEET_RESOURCE_DEFAULT);
+       }
 }
index 1977fadb55e33f70b6f251dc4be78a81a5223098..25e016154c98f9b8185799e5ca41539b0983c634 100644 (file)
@@ -14,6 +14,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Set;
 
 import javax.xml.transform.Result;
 import javax.xml.transform.Transformer;
@@ -44,7 +45,7 @@ public class PdfOutputModule extends AbstractPublishOutputModule {
        private static final Logger logger = Logger
                        .getLogger(PdfOutputModule.class);
        
-       public static String STYLESHEET_RESOURCE_DEFAULT = "/eu/etaxonomy/cdm/print/out/pdf/cdmToPdf.xsl";
+       public static String STYLESHEET_RESOURCE_DEFAULT = "/stylesheets/pdf/cdmToPdf.xsl";
                
        /*
         * (non-Javadoc)
@@ -74,7 +75,7 @@ public class PdfOutputModule extends AbstractPublishOutputModule {
                    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
 
                    // Setup XSLT
-                   InputStream xslt = getXslt();
+                   InputStream xslt = getXsltInputStream();
                    TransformerFactory factory = TransformerFactory.newInstance();
                    Transformer transformer = factory.newTransformer(new StreamSource(xslt));
 
@@ -100,7 +101,7 @@ public class PdfOutputModule extends AbstractPublishOutputModule {
                return "pdf";
        }
        
-       public InputStream getXslt(){
+       public InputStream getDefaultXsltInputStream(){
                return PdfOutputModule.class.getResourceAsStream(STYLESHEET_RESOURCE_DEFAULT);
        }
 }
index 73baf3835c20af52c880276da340583c26084dfd..9b2286d014ad78c51edb74695ce1ab29f080e407 100644 (file)
@@ -11,6 +11,8 @@
 package eu.etaxonomy.cdm.print.out.taxpub;
 
 import java.io.File;
+import java.io.IOException;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 import org.jdom.Document;
index e7c39f9bcccb0d7c1f252db92397a64c9ea03715..eaac2f38195f85ff2f35b070467ff91e3c068e62 100644 (file)
@@ -14,6 +14,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 import org.jdom.Document;
index 42c44be4c941f10a68f0b9097325c38c852012d1..234bf2e8be55b72e2a275b4d8158edb07b59ccde 100644 (file)
 
 package eu.etaxonomy.cdm.print.out.pdf;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.List;
 
 import org.junit.Test;
 
+import eu.etaxonomy.cdm.print.out.AbstractPublishOutputModule;
+
 
 /**
  * @author n.hoffmann
@@ -24,13 +34,31 @@ public class PdfOutputModuleTest {
 
        
        /**
+        * @throws IOException 
         * 
         */
        @Test
-       public void testGetXslt() {
+       public void testGetXslt() throws IOException {
                PdfOutputModule outputModule = new PdfOutputModule();
                
-               assertNotNull(outputModule.getXslt());
+               InputStream xslt = outputModule.getXsltInputStream();
+               
+               assertNotNull(xslt);
+               assertTrue(xslt.available() > 0);
        }
        
+       @Test
+       public void testGetStylesheetByLocation() throws IOException {
+               PdfOutputModule outputModule = new PdfOutputModule();
+               
+               URL shippedStylesheetsResource = AbstractPublishOutputModule.class.getResource("/stylesheets/pdf/");            
+               File shippedStylesheetsDir = new File(shippedStylesheetsResource.getFile());
+               
+               List<File> stylesheets = outputModule.getStylesheetsByLocation(shippedStylesheetsDir);
+               
+               assertNotNull("There should be stylesheets", stylesheets);
+               assertEquals("There should be one stylesheet", 1, stylesheets.size());
+       }
+       
+       
 }