changeset 533:87cb3dc2aa12 digilibPDF

improved pdf generation (not done yet)
author robcast
date Fri, 09 Apr 2010 18:22:05 +0200
parents 6d66b4ed7f07
children e758a49258e8
files servlet/src/digilib/image/DocuImage.java servlet/src/digilib/image/DocuImageImpl.java servlet/src/digilib/image/ImageLoaderDocuImage.java servlet/src/digilib/image/JAIDocuImage.java servlet/src/digilib/image/JAIImageLoaderDocuImage.java servlet/src/digilib/servlet/DigilibConfiguration.java servlet/src/digilib/servlet/DigilibPDFWorker.java servlet/src/digilib/servlet/NumRange.java servlet/src/digilib/servlet/PDFCache.java servlet/src/digilib/servlet/PDFJobInformation.java servlet/src/digilib/servlet/PDFMaker.java
diffstat 11 files changed, 319 insertions(+), 106 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/image/DocuImage.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/image/DocuImage.java	Fri Apr 09 18:22:05 2010 +0200
@@ -220,5 +220,11 @@
      * Returns a list of supported image formats
      */
 	public Iterator<String> getSupportedFormats();
+	
+	/**
+	 * returns the underlying image as java.awt.Image (if possible, or null)
+	 * @return
+	 */
+	public java.awt.Image getAwtImage();
 
 }
--- a/servlet/src/digilib/image/DocuImageImpl.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/image/DocuImageImpl.java	Fri Apr 09 18:22:05 2010 +0200
@@ -20,9 +20,11 @@
 
 package digilib.image;
 
+import java.awt.Image;
 import java.awt.Rectangle;
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.io.RandomAccessFile;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -161,6 +163,48 @@
 		List<String> empty = new LinkedList<String>();
 		return empty.iterator();
 	}
-	
+
+    @Override
+    public void crop(int xoff, int yoff, int width, int height)
+            throws ImageOpException {
+        // TODO Auto-generated method stub
+        
+    }
+
+    @Override
+    public Image getAwtImage() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public int getHeight() {
+        // TODO Auto-generated method stub
+        return 0;
+    }
 
+    @Override
+    public int getWidth() {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    @Override
+    public void loadImage(ImageFile f) throws FileOpException {
+        // TODO Auto-generated method stub
+        
+    }
+
+    @Override
+    public void scale(double scaleX, double scaleY) throws ImageOpException {
+        // TODO Auto-generated method stub
+        
+    }
+
+    @Override
+    public void writeImage(String mt, OutputStream ostream)
+            throws FileOpException {
+        // TODO Auto-generated method stub
+    }
+	
 }
--- a/servlet/src/digilib/image/ImageLoaderDocuImage.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/image/ImageLoaderDocuImage.java	Fri Apr 09 18:22:05 2010 +0200
@@ -562,7 +562,7 @@
 		img = null;
 	}
 
-	public Image getImage(){
+	public Image getAwtImage(){
 		return (Image) img;
 	}
 	
--- a/servlet/src/digilib/image/JAIDocuImage.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/image/JAIDocuImage.java	Fri Apr 09 18:22:05 2010 +0200
@@ -20,6 +20,7 @@
 
 package digilib.image;
 
+import java.awt.Image;
 import java.awt.Rectangle;
 import java.awt.RenderingHints;
 import java.awt.image.RenderedImage;
--- a/servlet/src/digilib/image/JAIImageLoaderDocuImage.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/image/JAIImageLoaderDocuImage.java	Fri Apr 09 18:22:05 2010 +0200
@@ -20,6 +20,7 @@
 
 package digilib.image;
 
+import java.awt.Image;
 import java.awt.Rectangle;
 import java.awt.image.renderable.ParameterBlock;
 import java.io.File;
@@ -166,7 +167,13 @@
 		}
 	}
 
-	/* (non-Javadoc)
+	@Override
+    public Image getAwtImage() {
+        // TODO Auto-generated method stub
+        return (Image) img;
+    }
+
+    /* (non-Javadoc)
 	 * @see java.lang.Object#finalize()
 	 */
 	protected void finalize() throws Throwable {
--- a/servlet/src/digilib/servlet/DigilibConfiguration.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/servlet/DigilibConfiguration.java	Fri Apr 09 18:22:05 2010 +0200
@@ -151,6 +151,10 @@
 		newParameter("worker-threads", new Integer(1), null, 'f');
 		// max number of waiting threads
 		newParameter("max-waiting-threads", new Integer(0), null, 'f');
+        // PDF generation temp directory
+        newParameter("pdf-temp-dir", "pdf_temp", null, 'f');
+        // PDF generation cache directory
+        newParameter("pdf-cache-dir", "pdf_cache", null, 'f');
 
 	}
 
--- a/servlet/src/digilib/servlet/DigilibPDFWorker.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/servlet/DigilibPDFWorker.java	Fri Apr 09 18:22:05 2010 +0200
@@ -34,7 +34,6 @@
 import com.itextpdf.text.pdf.PdfWriter;
 
 import digilib.image.DocuImage;
-import digilib.image.ImageLoaderDocuImage;
 import digilib.image.ImageOpException;
 import digilib.io.FileOpException;
 
@@ -50,37 +49,34 @@
 
 	private Document doc = null;
 
-	private String filename = null;
+	private File outputfile = null;
 	
 	private PDFJobInformation job_info = null;
 	
-	public DigilibPDFWorker(DigilibConfiguration dlConfig, PDFJobInformation pdfji, String filename) {
+	public DigilibPDFWorker(DigilibConfiguration dlConfig, PDFJobInformation pdfji, File outputfile) {
 		super();
 		// TODO dlConfig 
 		this.dlConfig = dlConfig;
 		this.job_info = pdfji;
-		this.filename = filename;
+		this.outputfile = outputfile;
 	}
 
 	public void run() {
 		// create document object
 		doc = new Document(PageSize.A4, 0,0,0,0);
 		PdfWriter docwriter = null;
-		File output_file = new File(filename);
 		FileOutputStream fos;
 		
 		try {
-			fos = new FileOutputStream(output_file);
+			fos = new FileOutputStream(outputfile);
 		} catch (FileNotFoundException e1) {
 			// TODO Auto-generated catch block
 			logger.error(e1.getMessage());
 			e1.printStackTrace();
 			return;
 		}
-
 		
 		long start_time = System.currentTimeMillis();
-
 		
 		try {
 			docwriter = PdfWriter.getInstance(doc, fos);
@@ -90,51 +86,41 @@
 			doc.open();
 
 			addTitlePage();
-
 			
-			logger.debug("- "+filename+" doc.open()ed ("+(System.currentTimeMillis()-start_time) + "ms)");
+			logger.debug("- "+outputfile+" doc.open()ed ("+(System.currentTimeMillis()-start_time) + "ms)");
 			start_time = System.currentTimeMillis();
-			
 
 			Integer[] pgs = job_info.getPageNrs();//get_pgs();
 
 			for(Integer p: pgs){
-				logger.debug(" - adding Image "+p+" to " + filename);
+				logger.debug(" - adding Image "+p+" to " + outputfile);
 				addImage(p);
-				logger.debug(" - done adding Image "+p+" to " + filename);
+				logger.debug(" - done adding Image "+p+" to " + outputfile);
 			}
 			
-			logger.debug(" - done adding all Images to " + filename);
-
+			logger.debug(" - done adding all Images to " + outputfile);
 			
-			
-		}
-		catch(Exception e) {
+		} catch(Exception e) {
 			logger.error(e.getMessage());
 			error = e;
 			return;
-		}
-		
-		finally {
+		} finally {
 			if (doc!=null){
 				doc.close();
-				logger.debug("- "+filename+" doc.close() ("+(System.currentTimeMillis()-start_time) + "ms)");
+				logger.debug("- "+outputfile+" doc.close() ("+(System.currentTimeMillis()-start_time) + "ms)");
 			}
 			if (docwriter!=null){
 				docwriter.close();
 			}
 		}
 
-		
-		
 		try {
 			fos.flush();
 		} catch (IOException e) {
 			logger.error(e.getMessage());
 			e.printStackTrace();
 			error = e;
-		}
-		finally{
+		} finally{
 			if(fos!=null){
 				try {
 					fos.close();
@@ -167,9 +153,7 @@
 		doc.add(titlepage.getPageContents());
 		doc.newPage();
 	}
-	
-	
-	
+		
 	/**
 	 * add the image with page number 'pn' to the document.
 	 * 
@@ -182,18 +166,16 @@
 		// create image worker
 		DigilibImageWorker image_worker = new DigilibImageWorker(dlConfig, null, iji);
 		try {
-			ImageLoaderDocuImage img = (ImageLoaderDocuImage) image_worker.render();
+			DocuImage img = image_worker.render();
 
-			Image theimg = Image.getInstance(img.getImage(),null);
+			Image pdfimg = Image.getInstance(img.getAwtImage(),null);
 			
-			float docW = PageSize.A4.getWidth() - 2*PageSize.A4.getBorder(); 
-			float docH= PageSize.A4.getHeight()- 2*PageSize.A4.getBorder();
-
+			float docW = PageSize.A4.getWidth() - 2 * PageSize.A4.getBorder(); 
+			float docH = PageSize.A4.getHeight() - 2 * PageSize.A4.getBorder();
 			
-			theimg.scaleToFit(docW,docH);
+			pdfimg.scaleToFit(docW,docH);
 			
-			
-			doc.add(theimg);
+			doc.add(pdfimg);
 			
 		} catch (FileOpException e) {
 			logger.error(e.getMessage());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servlet/src/digilib/servlet/NumRange.java	Fri Apr 09 18:22:05 2010 +0200
@@ -0,0 +1,164 @@
+/**
+ * 
+ */
+package digilib.servlet;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @author casties
+ * 
+ */
+public class NumRange implements Iterable<Integer> {
+
+    private Integer start = 1;
+    private Integer end = Integer.MAX_VALUE;
+    private List<Integer> list = null;
+    private Integer maxnum = null;
+
+    /**
+     * @param start
+     * @param end
+     */
+    public NumRange(Integer start, Integer end) {
+        this.start = start;
+        this.end = end;
+    }
+
+    /**
+     * @param range
+     */
+    public NumRange(String range) {
+        parseString(range);
+    }
+
+    /**
+     * @param range
+     */
+    public NumRange(String range, Integer max) {
+        this.maxnum = max;
+        parseString(range);
+    }
+
+
+    public void parseString(String pages) {
+
+        ArrayList<Integer> pgs = new ArrayList<Integer>();
+
+        String intervals[] = pages.split(",");
+
+        // convert the page-interval-strings into a list containing every single
+        // page
+        for (String interval : intervals) {
+            if (interval.contains("-")) {
+                String nums[] = interval.split("-");
+                int start = Integer.valueOf(nums[0]);
+                if (nums.length > 1) {
+                    // second number is end of range
+                    int end = Integer.valueOf(nums[1]);
+                    for (int i = start; i <= end; i++) {
+                        // add all numbers to list
+                        pgs.add(i);
+                    }
+                } else {
+                    // second number missing: range to infinity
+                    pgs.add(start);
+                    pgs.add(Integer.MAX_VALUE);
+                }
+            } else {
+                // single number
+                pgs.add(Integer.valueOf(interval));
+            }
+        }
+        if (intervals.length > 1) {
+            Collections.sort(pgs);
+        }
+        list = pgs;
+    }
+
+    public int getStart() {
+        if (list == null) {
+            return start;
+        } else {
+            return list.get(0);
+        }
+    }
+
+    public int getEnd() {
+        Integer last;
+        if (list == null) {
+            last = end;
+        } else {
+            last = list.get(list.size() - 1);
+        }
+        if (maxnum == null) {
+            return last;
+        } else {
+            return Math.min(last, maxnum);
+        }
+    }
+
+    @Override
+    public Iterator<Integer> iterator() {
+        if (list == null) {
+            // return count-based iterator
+            return new Iterator<Integer>() {
+                // anonymous inner Iterator class
+                private int num = getStart();
+                private int end = getEnd();
+
+                @Override
+                public boolean hasNext() {
+                    return (num < end);
+                }
+
+                @Override
+                public Integer next() {
+                    return num++;
+                }
+
+                @Override
+                public void remove() {
+                    // don't do this
+                }
+            };
+        } else {
+            // return list-based iterator
+            return new Iterator<Integer>() {
+                // anonymous inner Iterator class
+                private int listidx = 0;
+                private int listend = list.size();
+                private int num = getStart();
+                private int end = getEnd();
+
+                @Override
+                public boolean hasNext() {
+                    return (num < end);
+                }
+
+                @Override
+                public Integer next() {
+                    if (listidx < listend) {
+                        num = list.get(listidx++);
+                        return num;
+                    } else {
+                        return num++;
+                    }
+                }
+
+                @Override
+                public void remove() {
+                    // don't do this
+                }
+            };
+        }
+    }
+
+    public void setMaxnum(Integer maxnum) {
+        this.maxnum = maxnum;
+    }
+
+}
--- a/servlet/src/digilib/servlet/PDFCache.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/servlet/PDFCache.java	Fri Apr 09 18:22:05 2010 +0200
@@ -23,15 +23,16 @@
  *
  */
 
+@SuppressWarnings("serial")
 public class PDFCache extends RequestHandler {
 
 	private DigilibConfiguration dlConfig = null;
 	
-	public static String global_instance = "digilib.servlet.PDFCache";
+	public static String instanceKey = "digilib.servlet.PDFCache";
 	
-	private String cache_directory = "cache/";  
+	private File cache_directory = new File("cache");  
 	
-	private String temp_directory = "pdf_temp/";
+	private File temp_directory = new File("pdf_temp");
 	
 	private static String JSP_WIP = "/pdf/wip.jsp";
 	
@@ -63,38 +64,39 @@
                 + version + ") *****");
 
 		context = config.getServletContext();
-		
 		dlConfig = (DigilibConfiguration) context.getAttribute("digilib.servlet.configuration");
-		
 		if (dlConfig == null) {
 			// no Configuration
 			throw new ServletException("No Configuration!");
 		}
-
-		
-		
-		
-		temp_directory = dlConfig.getAsString("pdf-temp-dir");
-		cache_directory = dlConfig.getAsString("pdf-cache-dir");
+	
+		String temp_fn = dlConfig.getAsString("pdf-temp-dir");
+		temp_directory = new File(temp_fn);
+		if (!temp_directory.isDirectory()) {
+		    throw new ServletException("Configuration error: problem with pdf-temp-dir="+temp_fn);
+		}
+        // rid the temporary directory of possible incomplete document files
+        emptyDirectory(temp_directory);
+        
+		String cache_fn = dlConfig.getAsString("pdf-cache-dir");
+       	cache_directory = new File(cache_fn);
+        if (!cache_directory.isDirectory()) {
+            throw new ServletException("Configuration error: problem with pdf-cache-dir="+cache_fn);
+        }
 
-
-		// rid the temporary directory of possible incomplete document files
-		emptyTempDirectory();
-		
 		// register this instance globally
-		context.setAttribute(global_instance, this);
+		context.setAttribute(instanceKey, this);
 		
 	}
 	
 	/** 
 	 * clean up any broken and unfinished files from the temporary directory.
 	 */
-	public void emptyTempDirectory(){
-		File temp_dir = new File(temp_directory);
-		String[] cached_files = temp_dir.list();
+	public void emptyDirectory(File temp_dir){
+		File[] temp_files = temp_dir.listFiles();
 		
-		for (String file: cached_files){
-			new File(temp_directory,file).delete();
+		for (File f: temp_files){
+			f.delete();
 		}
 	}
 	
@@ -108,7 +110,6 @@
 		PDFJobInformation pdfji = new PDFJobInformation(dlConfig); 
 		pdfji.setWithRequest(request);
 		
-		
 		String docid = pdfji.getDocumentId();
 		
 		// if some invalid data has been entered ...
@@ -150,24 +151,20 @@
 			// tell the user that the document has to be created before he/she can download it
 			logger.debug("PDFCache: "+documentid+" has STATUS_NONEXISTENT.");
 			jsp = JSP_WIP;
-		}
-		else if(status == STATUS_WIP){
+		} else if(status == STATUS_WIP){
 			logger.debug("PDFCache: "+documentid+" has STATUS_WIP.");
 			jsp = JSP_WIP;
 
 			// estimate remaining work time
 			// tell the user he/she has to wait
-		}
-		else if(status == STATUS_DONE){
+		} else if(status == STATUS_DONE){
 			logger.debug("PDFCache: "+documentid+" has STATUS_DONE.");
-		}
-		else {
+		} else {
 			logger.debug("PDFCache: "+documentid+" has STATUS_ERROR.");
 			jsp = JSP_ERROR;
 		}
 
 		RequestDispatcher dispatch = context.getRequestDispatcher(jsp);
-
 		
 		try {
 			dispatch.forward(request, response);
@@ -185,15 +182,13 @@
 	/** check the status of the document corresponding to the documentid */
 	public Integer getStatus(String documentid){
 		// looks into the cache and temp directory in order to find out the status of the document
-		File cached = new File(cache_directory + documentid);
-		File wip = new File(temp_directory + documentid);
+		File cached = new File(cache_directory, documentid);
+		File wip = new File(temp_directory, documentid);
 		if(cached.exists()){
 			return STATUS_DONE;
-		}
-		else if (wip.exists()){
+		} else if (wip.exists()){
 			return STATUS_WIP;
-		}
-		else {
+		} else {
 			return STATUS_NONEXISTENT;
 		}
 	}
@@ -244,7 +239,7 @@
 
 		try {
 			// get file handle
-			cached_file = new File(cache_directory + cachefile);
+			cached_file = new File(cache_directory, cachefile);
 			// create necessary streams
 			fis = new FileInputStream(cached_file);
 			sos = response.getOutputStream();
@@ -277,11 +272,29 @@
 
 	}
 	
-	public String getCacheDirectory(){
+	public File getCacheDirectory(){
 		return cache_directory;
 	}
 	
-	public String getTempDirectory(){
+	public File getTempDirectory(){
 		return temp_directory;
 	}
+	
+	/** 
+	 * returns a File object based on filename in the temp directory.
+	 * @param filename
+	 * @return
+	 */
+	public File getTempFile(String filename) {
+	    return new File(temp_directory, filename);
+	}
+
+	/** 
+     * returns a File object based on filename in the cache directory.
+     * @param filename
+     * @return
+     */
+    public File getCacheFile(String filename) {
+        return new File(cache_directory, filename);
+    }
 }
--- a/servlet/src/digilib/servlet/PDFJobInformation.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/servlet/PDFJobInformation.java	Fri Apr 09 18:22:05 2010 +0200
@@ -10,16 +10,14 @@
 
 
 /** 
- * A container class for storing a set of instructional parameters 
+ * A container class for storing a set of instruction parameters 
  * used for content generator classes like MakePDF.  
  * 
  * 
  * @author cmielack
  *
  */
-
-
-
+@SuppressWarnings("serial")
 public class PDFJobInformation extends ParameterMap {
 
 	String[] parameter_list = {"pgs"}; // all other parameters get passed into an extra ImageJobInformation  
@@ -117,13 +115,14 @@
 		
 		String intervals[] = pages.split(",");
 		
-		
 		// convert the page-interval-strings into a list containing every single page
 		for(String interval: intervals){
-			if(interval.indexOf("-") > -1){
+			if(interval.contains("-")){
 				String nums[] = interval.split("-");
-				
-				for(int i=Integer.valueOf(nums[0]); i <= Integer.valueOf(nums[1]); i++){
+				int start = Integer.valueOf(nums[0]);
+				int end = Integer.valueOf(nums[1]);
+				for(int i = start; i <= end; i++){
+					// add all numbers to list
 					pgs.add(i);
 				}
 			}
@@ -147,10 +146,10 @@
 	public boolean checkValidity(){
 		String pgs = getAsString("pgs");
 		try{
-				String[] intervals = null;
-				if(pgs.indexOf(",")>0){
-					intervals = pgs.split(",");
-				}
+			String[] intervals = null;
+            if(pgs.indexOf(",")>0){
+                intervals = pgs.split(",");
+			}
 				else{
 					intervals = new String[1];
 					intervals[0]=pgs;
--- a/servlet/src/digilib/servlet/PDFMaker.java	Fri Feb 05 20:59:18 2010 +0100
+++ b/servlet/src/digilib/servlet/PDFMaker.java	Fri Apr 09 18:22:05 2010 +0200
@@ -17,6 +17,7 @@
  * @author cmielack
  *
  */
+@SuppressWarnings("serial")
 public class PDFMaker extends HttpServlet implements Runnable {
 
 	public static String version = "0.1";
@@ -27,7 +28,7 @@
 	private ServletContext context = null;
 
 	/** gengeral logger for this class */
-	protected static Logger logger = Logger.getLogger("digilib.servlet");
+	protected static Logger logger = Logger.getLogger("digilib.PDFMaker");
 
 	
 	
@@ -37,45 +38,37 @@
 		this.dlConfig = pdfji.getDlConfig();
 		this.context = context;
 	}
-	
-
-	
-	
+		
 	public void run() {
 
 		if (! DigilibWorker.canRun()) {
 			// TODO include the logger
-			logger.error("Servlet overloaded!");
-			
+			logger.error("Servlet overloaded!");			
 			return;
 		}
 
-		PDFCache pdfcache = (PDFCache) context.getAttribute(PDFCache.global_instance);
+		PDFCache pdfcache = (PDFCache) context.getAttribute(PDFCache.instanceKey);
 		
+		File tempfile = pdfcache.getTempFile(filename);
 		// create PDFWorker
-		DigilibPDFWorker pdf_worker = new DigilibPDFWorker(dlConfig, job_info, pdfcache.getTempDirectory()+filename);
+		DigilibPDFWorker pdf_worker = new DigilibPDFWorker(dlConfig, job_info, tempfile);
 		
 		// run PDFWorker
 		pdf_worker.run();
 
-		File document = new File(pdfcache.getTempDirectory() + filename);
-
 		if(pdf_worker.hasError()){
 			// raise error, write to logger
 			logger.error(pdf_worker.getError().getMessage());
-			document.delete();
+			tempfile.delete();
 			return;
 		}
 		else{ // move the completed file to the cache directory
-			boolean success = document.renameTo(new File(pdfcache.getCacheDirectory(), filename));
+			boolean success = tempfile.renameTo(pdfcache.getCacheFile(filename));
 			if(!success){
 				// TODO raise error
-				
 			}
 		}
 		
 	}
-
-	
 	
 }