changeset 509:ab94692bff0c digilibPDF

*** empty log message ***
author cmielack
date Fri, 13 Mar 2009 15:54:37 +0100
parents 1f3afa8d0d18
children 1d8e95c3eba5
files servlet/src/digilib/servlet/DigilibInfoReader.java servlet/src/digilib/servlet/DigilibPDFWorker.java servlet/src/digilib/servlet/ImageJobInformation.java servlet/src/digilib/servlet/OldDigilibPDFWorker.java servlet/src/digilib/servlet/PDFCache.java servlet/src/digilib/servlet/PDFJobInformation.java servlet/src/digilib/servlet/PDFMaker.java servlet/src/digilib/servlet/PDFTitlePage.java
diffstat 8 files changed, 292 insertions(+), 238 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servlet/src/digilib/servlet/DigilibInfoReader.java	Fri Mar 13 15:54:37 2009 +0100
@@ -0,0 +1,65 @@
+package digilib.servlet;
+
+/** DigilibInfoReader 
+ * A class for reading the information from info.xml files used in digilib image directories.
+ *
+ */
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.jdom.*;
+import org.jdom.input.*;
+import org.jdom.output.*;
+
+
+
+public class DigilibInfoReader {
+
+	/** gengeral logger for this class */
+	protected static Logger logger = Logger.getLogger("digilib.servlet");
+	
+	private String filename = null;
+	private static String base_element = "info";
+	
+	public DigilibInfoReader(String fn){
+		filename = fn;
+	}
+
+	
+	public String getAsString(String attr){
+		try{
+			SAXBuilder builder = new SAXBuilder();
+			Document doc = builder.build(new File(filename));
+			Element root = doc.getRootElement();
+			List mainElements = root.getChildren();
+			logger.debug("XML mainElements:"+mainElements.toString());
+
+			for(int i=0; i<mainElements.size(); i++){
+				Element elem = (Element) mainElements.get(i);
+				if(elem.getName()==attr){
+					logger.debug(attr+" == "+(String)elem.getTextTrim());
+					return (String)elem.getTextTrim();
+				}
+			}
+
+		}
+		catch(Exception e){
+			logger.error(e.getMessage());
+		}
+		return null;
+	}
+	
+	public boolean hasInfo(){
+		try {
+			SAXBuilder builder = new SAXBuilder();
+			Document doc = builder.build(new File(filename));
+			return true;
+		}
+		catch(Exception e){
+			return false;
+		}
+	}
+	
+}
--- a/servlet/src/digilib/servlet/DigilibPDFWorker.java	Fri Mar 13 15:51:47 2009 +0100
+++ b/servlet/src/digilib/servlet/DigilibPDFWorker.java	Fri Mar 13 15:54:37 2009 +0100
@@ -78,7 +78,7 @@
 		// create document object
 		doc = new Document(PageSize.A4, 0,0,0,0);
 		PdfWriter docwriter = null;
-		File output_file = new File(PDFCache.temp_directory + filename);
+		File output_file = new File(filename);
 		FileOutputStream fos;
 		
 		try {
@@ -101,6 +101,9 @@
 
 			doc.open();
 
+			addTitlePage();
+
+			
 			logger.debug("- "+filename+" doc.open()ed ("+(System.currentTimeMillis()-start_time) + "ms)");
 			start_time = System.currentTimeMillis();
 			
@@ -164,6 +167,13 @@
 		doc.addCreator(this.getClass().getName());
 	}
 	
+	
+	public void addTitlePage() throws DocumentException{
+		PDFTitlePage titlepage = new PDFTitlePage(job_info);
+		doc.add(titlepage.getPageContents());
+		doc.newPage();
+	}
+	
 	public Integer[] get_pgs(){
 		String pages = job_info.getAsString("pgs");
 		ArrayList<Integer> pgs = new ArrayList<Integer>();
--- a/servlet/src/digilib/servlet/ImageJobInformation.java	Fri Mar 13 15:51:47 2009 +0100
+++ b/servlet/src/digilib/servlet/ImageJobInformation.java	Fri Mar 13 15:54:37 2009 +0100
@@ -101,7 +101,7 @@
 		// url of the page/document (first part, may be empty)
 		newParameter("request.path", "", null, 'i');
 		// base URL (from http:// to below /servlet)
-		newParameter("base.url", null, null, 'i');
+		newParameter("base.url", "", null, 'i');
 
 		/*
 		 * Parameters of type 'c' are for the clients use
@@ -143,7 +143,12 @@
 			}
 		}
 	setValueFromString("request.path", ((HttpServletRequest) request).getPathInfo());
-
+	String[] baseurl_parts = ((HttpServletRequest) request).getRequestURL().toString().split("/");
+	String baseurl = "";
+	for(int i=0; i<baseurl_parts.length-2; i++){
+		baseurl += baseurl_parts[i]+"/";
+	}
+	setValueFromString("base.url", baseurl);
 	}
 	
 	public String[] getParameterList(){
--- a/servlet/src/digilib/servlet/OldDigilibPDFWorker.java	Fri Mar 13 15:51:47 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,205 +0,0 @@
-/* DigilibImageWorker.java -- worker for image operations
- * 
- * Digital Image Library servlet components
- * 
- * Copyright (C) 2004 Robert Casties (robcast@mail.berlios.de)
- * 
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- * 
- * Please read license.txt for the full details. A copy of the GPL may be found
- * at http://www.gnu.org/copyleft/lgpl.html
- * 
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- *  
- * Created on 19.10.2004
- */
-
-package digilib.servlet;
-
-import java.awt.Rectangle;
-import java.awt.geom.Rectangle2D;
-import java.io.BufferedOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-
-import javax.servlet.http.HttpServletResponse;
-
-import com.lowagie.text.BadElementException;
-import com.lowagie.text.Document;
-import com.lowagie.text.DocumentException;
-import com.lowagie.text.Image;
-import com.lowagie.text.PageSize;
-
-import digilib.image.DocuImage;
-import digilib.image.DocuImageImpl;
-import digilib.image.ImageLoaderDocuImage;
-import digilib.image.ImageOpException;
-import digilib.image.ImageOps;
-import digilib.servlet.DigilibImageWorker;
-import digilib.io.FileOpException;
-import digilib.io.ImageFile;
-
-/**
- * worker for image operations.
- * 
- * @author casties
- * 
- */
-public class OldDigilibPDFWorker extends DigilibImageWorker {
-
-	private DigilibConfiguration dlConfig;
-
-
-	long startTime;
-
-	String mimeType;
-
-	int scaleQual;
-
-	DigilibRequest dlRequest;
-
-	float paramROT;
-
-	float paramCONT;
-
-	float paramBRGT;
-
-	float[] paramRGBM;
-
-	float[] paramRGBA;
-
-	ImageFile fileToLoad;
-
-	float areaXoff;
-
-	float areaYoff;
-
-	float areaWidth;
-
-	float areaHeight;
-
-	float scaleXY;
-
-	Rectangle2D outerUserImgArea;
-
-	Rectangle2D innerUserImgArea;
-
-	float minSubsample;
-
-	boolean wholeRotArea;
-
-	int forceType;
-	
-	Document doc;
-	
-	
-
-	public OldDigilibPDFWorker(DigilibConfiguration dlConfig,
-			BufferedOutputStream outstream, String mimeType, int scaleQual,
-			DigilibRequest dlRequest, float paramCONT,
-			float paramBRGT, float[] paramRGBM, float[] paramRGBA,
-			ImageFile fileToLoad, float scaleXY, Rectangle2D outerUserImgArea,
-			Rectangle2D innerUserImgArea, float minSubsample,
-			boolean wholeRotArea, int forceType, Document doc) {
-		super(dlConfig,
-				outstream, mimeType, scaleQual,
-				//dlRequest, 
-				0.0f , paramCONT,
-				paramBRGT, paramRGBM,paramRGBA,
-				fileToLoad, scaleXY, outerUserImgArea,
-				innerUserImgArea,minSubsample,
-				wholeRotArea, forceType, false, false);
-		
-		this.dlConfig = dlConfig;
-		this.outstream = outstream;
-		this.mimeType = mimeType;
-		this.scaleQual = scaleQual;
-		this.dlRequest = dlRequest;
-		//this.paramROT = paramROT;
-		this.paramCONT = paramCONT;
-		this.paramBRGT = paramBRGT;
-		this.paramRGBM = paramRGBM;
-		this.paramRGBA = paramRGBA;
-		this.fileToLoad = fileToLoad;
-		this.scaleXY = scaleXY;
-		this.outerUserImgArea = outerUserImgArea;
-		this.innerUserImgArea = innerUserImgArea;
-		this.minSubsample = minSubsample;
-		this.wholeRotArea = wholeRotArea;
-		this.forceType = forceType;
-		this.doc = doc;
-	}
-
-	public void run() {
-		//logger.debug((++waitingThreads) + " waiting threads");
-		ImageLoaderDocuImage img = null;
-		try {
-			sem.acquire();
-			//waitingThreads--;
-		} catch (InterruptedException e) {
-			error = e;
-			//waitingThreads--;
-			// should we reinterrupt?
-			return;
-		}
-		//logger.debug((++runningThreads) + " running threads");
-		try {
-			/* 
-			 * do rendering under the semaphore 
-			 */
-			img = (ImageLoaderDocuImage) super.render();
-		} catch (Throwable e) {
-			error = e;
-			logger.error(e);
-		} finally {
-		//	runningThreads--;
-			sem.release();
-		}
-		/* 
-		 * write the result without semaphore
-		 */
-		if (!hasError()) {
-			try{
-				write(img);
-			} catch (Throwable e) {
-				error = e;
-				logger.error(e);
-			}
-		}
-	}
-
-	public void write(ImageLoaderDocuImage img) throws FileOpException, IOException {
-		/* write the resulting image */
-
-
-		try {
-			long timing = System.currentTimeMillis();
-			Image theimg = Image.getInstance(img.getImage(),null);
-			
-			theimg.scaleToFit(PageSize.A4.getWidth(),PageSize.A4.getHeight());
-			
-			logger.debug(" --- loading and scaling took "+(-timing+System.currentTimeMillis())+"ms");
-
-			timing = System.currentTimeMillis();
-			
-			doc.add(theimg);
-			logger.debug(" --- adding took "+(-timing+System.currentTimeMillis())+"ms");
-
-		} catch (BadElementException e) {
-			e.printStackTrace();
-			logger.error("DigilibPDFWorker write BadElementException");
-		} catch (DocumentException e) {
-			e.printStackTrace();
-			logger.error("DigilibPDFWorker write DocumentException");
-		}
-		
-		logger.info("pdf worker " + this.getName() + " done in "
-				+ (System.currentTimeMillis() - startTime));
-
-	}
-}
\ No newline at end of file
--- a/servlet/src/digilib/servlet/PDFCache.java	Fri Mar 13 15:51:47 2009 +0100
+++ b/servlet/src/digilib/servlet/PDFCache.java	Fri Mar 13 15:54:37 2009 +0100
@@ -15,17 +15,22 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+/**
+ * A class for handling user requests for pdf documents from digilib images.  
+ * 
+ * @author cmielack
+ *
+ */
+
 public class PDFCache extends RequestHandler {
 
 	private DigilibConfiguration dlConfig = null;
 	
-	
-	
-	public static String cache_directory = "cache/";  // TODO set using dlConfig
+	public static String global_instance = "digilib.servlet.PDFCache";
 	
-	public static String temp_directory = "pdf_temp/";
+	private String cache_directory = "cache/";  
 	
-	
+	private String temp_directory = "pdf_temp/";
 	
 	private static String JSP_WIP = "/pdf/wip.jsp";
 	
@@ -41,22 +46,20 @@
 	
 	public static String version = "0.2";
 	
+	private ServletContext context = null;
+
+	
 	// TODO functionality for the pre-generation of complete books/chapters using default values
 	// TODO use DLConfig for default values
 	// TODO use JSPs for automatically refreshing waiting-pages and download-pages
 	// TODO register the PDFCache instance globally and implement getters for cache_dir 
 	
-	private ServletContext context = null;
 	
 	public void init(ServletConfig config) throws ServletException{
-		
 		super.init(config);
 		
-		
-		
 		logger.info("initialized PDFCache v."+version);
 		
-		// create and register hashtable
 		context = config.getServletContext();
 		
 		dlConfig = (DigilibConfiguration) context.getAttribute("digilib.servlet.configuration");
@@ -65,29 +68,27 @@
 			// no Configuration
 			throw new ServletException("No Configuration!");
 		}
+
 		
-		//context.setAttribute(cache_hash_id, new HashMap<String,Integer>());
+		
 		
-		//cache_hash = (HashMap<String,Integer>) context.getAttribute(cache_hash_id);
+		temp_directory = dlConfig.getAsString("pdf-temp-dir");
+		cache_directory = dlConfig.getAsString("pdf-cache-dir");
 
-		/*if (cache_hash==null){
-			cache_hash = new HashMap<String,Integer>();
-			context.setAttribute(cache_hash_id, cache_hash);
-		}*/
+
+		// rid the temporary directory of possible incomplete document files
+		emptyTempDirectory();
 		
-		// scan the directory
-		// scanCacheDirectory();
-		emptyTempDirectory();
+		// register this instance globally
+		context.setAttribute(global_instance, this);
+		
 	}
 	
 	
 	public void emptyTempDirectory(){
-		// search the cache-directory for existing files and fill them into the Hashtable as STATUS_DONE
-
 		File temp_dir = new File(temp_directory);
 		String[] cached_files = temp_dir.list();
 		
-		
 		for (String file: cached_files){
 			new File(temp_directory,file).delete();
 		}
@@ -195,7 +196,7 @@
 
 	public void createNewPdfDocument(PDFJobInformation pdfji, String filename){
 		// start new worker
-		PDFMaker pdf_maker = new PDFMaker(dlConfig, pdfji,filename);
+		PDFMaker pdf_maker = new PDFMaker(context, pdfji,filename);
 		new Thread(pdf_maker, "PDFMaker").start();
 	}
 	
@@ -254,4 +255,12 @@
 			}
 
 	}
+	
+	public String getCacheDirectory(){
+		return cache_directory;
+	}
+	
+	public String getTempDirectory(){
+		return temp_directory;
+	}
 }
--- a/servlet/src/digilib/servlet/PDFJobInformation.java	Fri Mar 13 15:51:47 2009 +0100
+++ b/servlet/src/digilib/servlet/PDFJobInformation.java	Fri Mar 13 15:54:37 2009 +0100
@@ -217,4 +217,8 @@
 		return true;
 	}
 	
+	public DigilibConfiguration getDlConfig(){
+		return dlConfig;
+	}
+	
 }
\ No newline at end of file
--- a/servlet/src/digilib/servlet/PDFMaker.java	Fri Mar 13 15:51:47 2009 +0100
+++ b/servlet/src/digilib/servlet/PDFMaker.java	Fri Mar 13 15:54:37 2009 +0100
@@ -13,20 +13,23 @@
 
 public class PDFMaker extends HttpServlet implements Runnable {
 
+	public static String version = "0.1";
+	
 	private PDFJobInformation job_info = null;
 	private String filename = null;
 	private DigilibConfiguration dlConfig = null;
-	
+	private ServletContext context = null;
 
 	/** gengeral logger for this class */
 	protected static Logger logger = Logger.getLogger("digilib.servlet");
 
 	
 	
-	public PDFMaker(DigilibConfiguration dlConfig, PDFJobInformation pdfji, String filename){
+	public PDFMaker(ServletContext context, PDFJobInformation pdfji, String filename){
 		this.job_info = pdfji;
 		this.filename = filename;
-		this.dlConfig = dlConfig;
+		this.dlConfig = pdfji.getDlConfig();
+		this.context = context;
 	}
 	
 
@@ -41,13 +44,15 @@
 			return;
 		}
 
+		PDFCache pdfcache = (PDFCache) context.getAttribute(PDFCache.global_instance);
+		
 		// create PDFWorker
-		DigilibPDFWorker pdf_worker = new DigilibPDFWorker(dlConfig, job_info, filename);
+		DigilibPDFWorker pdf_worker = new DigilibPDFWorker(dlConfig, job_info, pdfcache.getTempDirectory()+filename);
 		
 		// run PDFWorker
 		pdf_worker.run();
 
-		File document = new File(PDFCache.temp_directory + filename);
+		File document = new File(pdfcache.getTempDirectory() + filename);
 
 		if(pdf_worker.hasError()){
 			// raise error, write to logger
@@ -55,8 +60,8 @@
 			document.delete();
 			return;
 		}
-		else{
-			boolean success = document.renameTo(new File(PDFCache.cache_directory, filename));
+		else{ // move the completed file to the cache directory
+			boolean success = document.renameTo(new File(pdfcache.getCacheDirectory(), filename));
 			if(!success){
 				// TODO raise error
 				
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servlet/src/digilib/servlet/PDFTitlePage.java	Fri Mar 13 15:54:37 2009 +0100
@@ -0,0 +1,161 @@
+package digilib.servlet;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.log4j.Logger;
+
+import com.lowagie.text.*;
+import com.lowagie.text.pdf.*;
+
+import digilib.io.DocuDirCache;
+
+/** A class for the generation of title pages for the generated pdf documents.
+ * 
+ * 
+ */
+public class PDFTitlePage {
+	
+	private PDFJobInformation job_info = null;
+	private DigilibInfoReader info_reader= null;
+	private DocuDirCache dirCache = null;
+	protected static Logger logger = Logger.getLogger("digilib.servlet");
+
+	
+	public PDFTitlePage(PDFJobInformation pdfji){
+		job_info = pdfji;
+		dirCache = (DocuDirCache) job_info.getDlConfig().getValue("servlet.dir.cache");
+
+		String fn = getBase(dirCache.getDirectory(pdfji.getImageJobInformation().getAsString("fn")).getDir().getPath()) + "presentation/info.xml";
+		
+		info_reader = new DigilibInfoReader(fn);
+	}
+	
+	
+	public Element getPageContents(){
+		Paragraph content = new Paragraph();
+		content.setAlignment(Element.ALIGN_CENTER);
+
+		// add vertical whitespace
+		for(int i=0; i<8; i++){
+			content.add(Chunk.NEWLINE);
+		}
+		
+		
+		// add logo
+		content.add(getLogo());
+		content.add(Chunk.NEWLINE);
+		content.add(Chunk.NEWLINE);
+
+		// add title
+		Anchor title = new Anchor(new Paragraph(getTitle(),FontFactory.getFont(FontFactory.HELVETICA,16)));
+		String burl = job_info.getImageJobInformation().getAsString("base.url");
+		
+		title.setReference(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"));
+		content.add(title);		
+		content.add(Chunk.NEWLINE);
+
+		// add author
+		if(getDate()!=" ")
+			content.add(new Paragraph(getAuthor()+" ("+getDate()+")",FontFactory.getFont(FontFactory.HELVETICA,14)));
+		else
+			content.add(new Paragraph(getAuthor(),FontFactory.getFont(FontFactory.HELVETICA,14)));
+		
+		content.add(Chunk.NEWLINE);
+		
+		// add page numbers
+		content.add(new Paragraph(getPages(), FontFactory.getFont(FontFactory.HELVETICA, 12)));
+
+
+		content.add(Chunk.NEWLINE);
+		content.add(Chunk.NEWLINE);
+		content.add(Chunk.NEWLINE);
+
+		// add credits
+		content.add(new Paragraph("MPIWG Berlin 2009", FontFactory.getFont(FontFactory.HELVETICA,10)));
+
+		// add digilib version
+		content.add(new Paragraph(getDigilibVersion(),FontFactory.getFont(FontFactory.HELVETICA,10)));
+
+		for(int i=0; i<8; i++){
+			content.add(Chunk.NEWLINE);
+		}
+		Anchor address = new Anchor(
+				new Paragraph(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"), FontFactory.getFont(FontFactory.COURIER, 9))
+									);
+		address.setReference(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"));
+		
+		content.add(address);
+
+		
+		return content;
+	}
+	
+	private String getBase(String path){
+		// return base directory of an image directory
+		if(path.contains("/")){
+			String[] x = path.split("/");
+			String newpath = "";
+			for(int i=0; i<x.length-1; i++){
+				newpath += x[i]+"/";
+			}
+			return newpath;
+		}
+		else
+			return "";
+	}
+	
+	
+	private Image getLogo(){
+		
+		try {
+			// loads a local image. 
+			// In order to use a remote image, getInstance needs to be called using a URL object
+			URL url = new URL(job_info.getDlConfig().getAsString("pdf-logo"));
+			if(url!=null && !url.equals("")){
+				Image logo = Image.getInstance(url);
+				logo.setAlignment(Element.ALIGN_CENTER);
+				return logo;
+			}
+		} catch (BadElementException e) {
+			logger.error(e.getMessage());
+			e.printStackTrace();
+		} catch (MalformedURLException e) {
+			logger.error(e.getMessage());
+			e.printStackTrace();
+		} catch (IOException e) {
+			logger.error(e.getMessage());
+			e.printStackTrace();
+		}
+		return null;
+	}
+	
+	
+	private String getTitle(){
+		if(info_reader.hasInfo())
+			return info_reader.getAsString("title");
+		else
+			return job_info.getImageJobInformation().getAsString("fn");
+	}
+	private String getAuthor(){
+		if(info_reader.hasInfo())
+			return info_reader.getAsString("author");
+		else
+			return " ";
+	}
+	private String getDate(){
+		if(info_reader.hasInfo())
+			return info_reader.getAsString("date");
+		else
+			return " ";
+	}
+	private String getPages(){
+		return "Pages "+job_info.getAsString("pgs") + " (scan page numbers)";
+	}
+
+	private String getDigilibVersion(){
+		return "Digilib PDFMaker v."+PDFMaker.version;
+	}
+	
+}