changeset 511:e706a777798b digilibPDF

documentation and comments added/cleaned up
author cmielack
date Fri, 27 Mar 2009 15:14:30 +0100
parents 1d8e95c3eba5
children 501ec0802157
files servlet/src/digilib/servlet/DigilibInfoReader.java servlet/src/digilib/servlet/DigilibPDFWorker.java servlet/src/digilib/servlet/ImageJobInformation.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 servlet/src/digilib/servlet/Scaler.java
diffstat 8 files changed, 153 insertions(+), 188 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/servlet/DigilibInfoReader.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/DigilibInfoReader.java	Fri Mar 27 15:14:30 2009 +0100
@@ -27,19 +27,24 @@
 		filename = fn;
 	}
 
-	
+	/**
+	 * Returns the attribute defined by 'attr' as a String.
+	 * 
+	 * @param attr
+	 * @return
+	 */
 	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());
+			// 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());
+					// logger.debug(attr+" == "+(String)elem.getTextTrim());
 					return (String)elem.getTextTrim();
 				}
 			}
@@ -51,6 +56,11 @@
 		return null;
 	}
 	
+	
+	/**
+	 * Find out if the info.xml exists
+	 * @return
+	 */
 	public boolean hasInfo(){
 		try {
 			SAXBuilder builder = new SAXBuilder();
--- a/servlet/src/digilib/servlet/DigilibPDFWorker.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/DigilibPDFWorker.java	Fri Mar 27 15:14:30 2009 +0100
@@ -51,7 +51,7 @@
 import digilib.io.ImageFile;
 
 /**
- * worker for pdf operations.
+ * Worker for pdf generation.
  * 
  * @author cmielack
  * 
@@ -108,7 +108,7 @@
 			start_time = System.currentTimeMillis();
 			
 
-			Integer[] pgs = get_pgs();
+			Integer[] pgs = job_info.getPageNrs();//get_pgs();
 
 			for(Integer p: pgs){
 				logger.debug(" - adding Image "+p+" to " + filename);
@@ -158,6 +158,9 @@
 		}
 	}
 
+	/**
+	 * Set PDF-Meta-Attributes.
+	 */
 	public void setPDFProperties(){
 		// TODO get proper Information from dlConfig
 		doc.addAuthor(this.getClass().getName());
@@ -167,40 +170,23 @@
 		doc.addCreator(this.getClass().getName());
 	}
 	
-	
+	/**
+	 * Create a title page and append it to the document (should, of course, be called first)
+	 * @throws DocumentException
+	 */
 	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>();
-		Integer[] out = null;
-		
-		String intervals[] = pages.split(",");
-		
-		
-		// convert the page-interval-strings into a list containing every single page
-		for(String interval: intervals){
-			if(interval.indexOf("-") > -1){
-				String nums[] = interval.split("-");
-				
-				for(int i=Integer.valueOf(nums[0]); i <= Integer.valueOf(nums[1]); i++){
-					pgs.add(i);
-				}
-			}
-			else{
-				pgs.add(Integer.valueOf(interval));
-			}
-		}
-		out = new Integer[pgs.size()];
-
-		pgs.toArray(out);
-		return out;
-	}
+	
 	
+	/**
+	 * add the image with page number 'pn' to the document.
+	 * 
+	 * @param pn
+	 */
 	public void addImage(int pn) {
 		// create ImageJobInformation
 		ImageJobInformation iji = job_info.getImageJobInformation();
@@ -243,7 +229,6 @@
 	
 	
 	
-	// unnecessary 
 	@Override
 	public DocuImage render() throws Exception {
 		return null;
--- a/servlet/src/digilib/servlet/ImageJobInformation.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/ImageJobInformation.java	Fri Mar 27 15:14:30 2009 +0100
@@ -26,7 +26,9 @@
  * A container class for storing a set of instructional parameters 
  * used for content generating classes like MakePDF.  
  * 
- * TODO aufraeumen, zwischenwerte in der ParameterMap cachen
+ * This contains the functionality formerly found in Scaler, processRequest, only factorized.
+ * 
+ * TODO clean up...
  * 
  * @author cmielack
  *
@@ -130,15 +132,10 @@
 	}
 
 
-	/*public void setPageNumber(int pn){
-		put("pn",pn);
-	}*/
 	
 	public void setWithRequest(HttpServletRequest request) {
 		for (String param : parameter_list){
 			if (request.getParameterMap().containsKey(param)){
-				//request.get
-//				put(param, request.getParameter(param));
 				this.setValueFromString(param, request.getParameter(param));
 			}
 		}
@@ -170,13 +167,6 @@
 	}
 	
 	
-	/**
-	 * evaluate request data in order to gain the parameters for the image worker
-	 * 
-	 * @throws ImageOpException 
-	 * @throws IOException 
-	 * */
-
 	public String get_mimeType() {
 		String mimeType = "image/png";
 		
@@ -206,7 +196,6 @@
 	
 	public ImageFile get_fileToLoad() throws IOException, ImageOpException{
 		
-		//logger.debug("get_fileToLoad()");
 		if(fileToLoad == null){
 			ImageFileset fileset = get_fileset();
 			
@@ -237,7 +226,6 @@
 	}
 	
 	public ImageFileset get_fileset() throws FileOpException{
-		//logger.debug("get_fileset()");
 		if(fileset==null){
 			DocuDirCache dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache");
 	
@@ -251,7 +239,6 @@
 	}
 	
 	public String getFilePath() {
-		//logger.debug("getFilePath()");
 		if(FilePath == null){
 			String s = this.getAsString("request.path");
 			s += this.getAsString("fn");
@@ -261,31 +248,26 @@
 	}
 
 	public boolean get_hiresOnly(){
-		//logger.debug("get_hiresOnly()");
 		return hasOption("mo","clip") || hasOption("mo","osize") || hasOption("mo","hires");
 	}
 	
 	public boolean get_loresOnly(){
-		//logger.debug("get_loresOnly()");
 
 		return hasOption("mo","lores");
 	}
 
 	public boolean get_scaleToFit() {
-		//logger.debug("get_scaleToFit()");
 
 		return !(hasOption("mo","clip") || hasOption("mo","osize") || hasOption("mo","ascale"));
 	}
 
 	public boolean get_absoluteScale(){
-		//logger.debug("get_absoluteScale()");
 
 		return hasOption("mo","osize") || hasOption("mo","ascale");
 	}
 	
 	
 	public ImageSize get_expectedSourceSize() throws IOException, ImageOpException{
-		//logger.debug("get_expectedSourceSize()");
 
 		if (expectedSourceSize == null){
 			expectedSourceSize = new ImageSize();
--- a/servlet/src/digilib/servlet/PDFCache.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/PDFCache.java	Fri Mar 27 15:14:30 2009 +0100
@@ -18,6 +18,9 @@
 /**
  * A class for handling user requests for pdf documents from digilib images.  
  * 
+ * If a document does not already exist, it will be enqueued for generation; if it does exist, it is sent
+ * to the user.
+ * 
  * @author cmielack
  *
  */
@@ -49,10 +52,7 @@
 	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 
+	// TODO ? functionality for the pre-generation of complete books/chapters using default values
 	
 	
 	public void init(ServletConfig config) throws ServletException{
@@ -84,7 +84,9 @@
 		
 	}
 	
-	
+	/** 
+	 * 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();
@@ -134,10 +136,15 @@
 		}
 	}
 
-
+	/**
+	 * depending on the documents status, redirect the user to an appropriate waiting- or download-site
+	 * 
+	 * @param status
+	 * @param documentid
+	 * @param request
+	 * @param response
+	 */
 	public void notifyUser(int status, String documentid, HttpServletRequest request, HttpServletResponse response){
-		// depending on the documents status, redirect the user to an appropriate waiting- or download-site
-		// TODO
 		
 		String jsp=null;
 		
@@ -193,7 +200,12 @@
 		}
 	}
 
-
+	/** 
+	 * create new thread for pdf generation.
+	 * 
+	 * @param pdfji
+	 * @param filename
+	 */
 	public void createNewPdfDocument(PDFJobInformation pdfji, String filename){
 		// start new worker
 		PDFMaker pdf_maker = new PDFMaker(context, pdfji,filename);
@@ -201,7 +213,14 @@
 	}
 	
 	
+	/**
+	 * generate the filename the user is going to receive the pdf as
+	 * 
+	 * @param pdfji
+	 * @return
+	 */
 	public String downloadFilename(PDFJobInformation pdfji){
+		// filename example: digilib_example_pgs1-3.pdf
 		String filename;
 		filename =  "digilib_";
 		filename += pdfji.getImageJobInformation().getAsString("fn");
@@ -226,17 +245,21 @@
 		BufferedInputStream bis = null;
 
 		try {
+			// get file handle
 			cached_file = new File(cache_directory + cachefile);
+			// create necessary streams
 			fis = new FileInputStream(cached_file);
 			sos = response.getOutputStream();
 			bis = new BufferedInputStream(fis);
 
 			int bytes = 0;
 
+			// set http headers
 			response.setContentType("application/pdf");
 			response.addHeader("Content-Disposition", "attachment; filename="+filename);
 			response.setContentLength( (int) cached_file.length());
-			
+
+			// send the bytes
 			while ((bytes = bis.read()) != -1){ 
 				sos.write(bytes);
 			}
--- a/servlet/src/digilib/servlet/PDFJobInformation.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/PDFJobInformation.java	Fri Mar 27 15:14:30 2009 +0100
@@ -11,7 +11,7 @@
 
 /** 
  * A container class for storing a set of instructional parameters 
- * used for content generating classes like MakePDF.  
+ * used for content generator classes like MakePDF.  
  * 
  * 
  * @author cmielack
@@ -22,11 +22,8 @@
 
 public class PDFJobInformation extends ParameterMap {
 
-	String[] parameter_list = {"pgs"};//{"fn","pgs","dw","dh"};
-	/*,
-			"wx", "wy", "ww", "wh", "ws", 
-			"mo", "rot", "cont", "brgt", "rgbm", "rbgm", 
-			"ddpi", "ddpix", "ddpiy", "scale"};*/
+	String[] parameter_list = {"pgs"}; // all other parameters get passed into an extra ImageJobInformation  
+									   // (this should be redesigned later...)
 	
 	
 	ImageJobInformation image_info = null;
@@ -35,86 +32,27 @@
 	protected static Logger logger = Logger.getLogger("digilib.servlet");
 
 	
+	/**
+	 * Initialize the PDFJobInformation
+	 * 
+	 * @param dlcfg			
+	 * 						The DigilibConfiguration. 
+	 */
 	public PDFJobInformation(DigilibConfiguration dlcfg) {
 		super(30);
-		
-		// url of the page/document (second part)
-//		newParameter("fn", "", null, 's');
-		// page number
+
+		// page numbers
 		newParameter("pgs", "", null, 's');
-		// width of client in pixels
-//		newParameter("dw", new Integer(0), null, 's');
-		// height of client in pixels
-//		newParameter("dh", new Integer(0), null, 's');
-		// left edge of image (float from 0 to 1)
-/*		newParameter("wx", new Float(0), null, 's');
-		// top edge in image (float from 0 to 1)
-		newParameter("wy", new Float(0), null, 's');
-		// width of image (float from 0 to 1)
-		newParameter("ww", new Float(1), null, 's');
-		// height of image (float from 0 to 1)
-		newParameter("wh", new Float(1), null, 's');
-		// scale factor
-		newParameter("ws", new Float(1), null, 's');
-		// special options like 'fit' for gifs
-		newParameter("mo", "", null, 's');
-		// rotation angle (degree)
-		newParameter("rot", new Float(0), null, 's');
-		// contrast enhancement factor
-		newParameter("cont", new Float(0), null, 's');
-		// brightness enhancement factor
-		newParameter("brgt", new Float(0), null, 's');
-		// color multiplicative factors
-		newParameter("rgbm", "0/0/0", null, 's');
-		// color additive factors
-		newParameter("rgba", "0/0/0", null, 's');
-		// display dpi resolution (total)
-		newParameter("ddpi", new Float(0), null, 's');
-		// display dpi X resolution
-		newParameter("ddpix", new Float(0), null, 's');
-		// display dpi Y resolution
-		newParameter("ddpiy", new Float(0), null, 's');
-		// scale factor for mo=ascale
-		newParameter("scale", new Float(1), null, 's');
-*/
-		/*
-		 * Parameters of type 'i' are not exchanged between client and server,
-		 * but are for the servlets or JSPs internal use.
-		 */
-
-/*		// 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');
-		// DocuImage instance for this request
-*/
-		/*
-		 * Parameters of type 'c' are for the clients use
-		 */
-
-/*		// "real" filename
-		newParameter("img.fn", "", null, 'c');
-		// image dpi x
-		newParameter("img.dpix", new Integer(0), null, 'c');
-		// image dpi y
-		newParameter("img.dpiy", new Integer(0), null, 'c');
-		// hires image size x
-		newParameter("img.pix_x", new Integer(0), null, 'c');
-		// hires image size y
-		newParameter("img.pix_y", new Integer(0), null, 'c');
-		// total number of pages
-		newParameter("pt", new Integer(0), null, 'c');
-		// display level of digilib (0 = just image, 1 = one HTML page
-		// 2 = in frameset, 3 = XUL-'frameset'
-		// 4 = XUL-Sidebar )
-		newParameter("lv", new Integer(2), null, 'c');
-		// marks
-		newParameter("mk", "", null, 'c');
-*/	
 		dlConfig = dlcfg;
 		
 	}
 
+	
+	/**
+	 * Read in the request object.
+	 * 
+	 * @param request
+	 */
 	public void setWithRequest(HttpServletRequest request) {
 		image_info = new ImageJobInformation(dlConfig);
 		image_info.setWithRequest(request);
@@ -126,6 +64,12 @@
 		}
 	}
 	
+	
+	/**
+	 * Generate the filename of the pdf to be created.
+	 * 
+	 * @return
+	 */
 	public String getDocumentId(){
 		String id;
 
@@ -145,43 +89,54 @@
 		return id;
 	}
 
+	
 	public ImageJobInformation getImageJobInformation(){
 		ImageJobInformation new_image_info = (ImageJobInformation) image_info.clone();
 		return new_image_info;
 	}
 	
+	
+	/**
+	 *	Convert the "pgs"-Parameter to an Array of Integers.	
+	 *
+	 * @return
+	 * @throws Exception
+	 */
 	public Integer[] getPageNrs() throws Exception{
-		ArrayList<Integer> pgs=new ArrayList<Integer>(); 
-		Integer[] numarray = null;
-
-		String intervals[] = getAsString("pgs").split(",");
+		
+		String pages =	getAsString("pgs");
+		ArrayList<Integer> pgs = new ArrayList<Integer>();
+		Integer[] out = null;
+		
+		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.indexOf("-") > -1){
 				String nums[] = interval.split("-");
 				
-//				if(nums.length!=2){
-//					throw new Exception("Malformed pageset expression: "+getAsString("pgs"));
-//				}
-				
 				for(int i=Integer.valueOf(nums[0]); i <= Integer.valueOf(nums[1]); i++){
 					pgs.add(i);
 				}
 			}
-			else if (interval.indexOf("-") < 0){
+			else{
 				pgs.add(Integer.valueOf(interval));
 			}
-//			else{
-//				throw new Exception("Malformed pageset expression: "+getAsString("pgs"));
-//			}
 		}
+		out = new Integer[pgs.size()];
 
-		pgs.toArray(numarray);
-		return numarray;
+		pgs.toArray(out);
+		return out;
 	}
 	
 	
+	/**
+	 * Check parameters for validity.
+	 * Returns true if no errors are found.
+	 * 
+	 * @return
+	 */
 	public boolean checkValidity(){
 		String pgs = getAsString("pgs");
 		try{
--- a/servlet/src/digilib/servlet/PDFMaker.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/PDFMaker.java	Fri Mar 27 15:14:30 2009 +0100
@@ -11,6 +11,16 @@
 
 import org.apache.log4j.Logger;
 
+
+
+
+/**
+ * A Runnable that creates the PDFWorker and moves completed files from a temporary location
+ *  (defined in PDFCache) to the cache directory.
+ * 
+ * @author cmielack
+ *
+ */
 public class PDFMaker extends HttpServlet implements Runnable {
 
 	public static String version = "0.1";
@@ -56,7 +66,7 @@
 
 		if(pdf_worker.hasError()){
 			// raise error, write to logger
-			logger.error("@@@@ "+pdf_worker.getError().getMessage());
+			logger.error(pdf_worker.getError().getMessage());
 			document.delete();
 			return;
 		}
--- a/servlet/src/digilib/servlet/PDFTitlePage.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/PDFTitlePage.java	Fri Mar 27 15:14:30 2009 +0100
@@ -23,6 +23,10 @@
 	protected static Logger logger = Logger.getLogger("digilib.servlet");
 
 	
+	/**
+	 * Initialize a TitlePage
+	 * @param pdfji
+	 */
 	public PDFTitlePage(PDFJobInformation pdfji){
 		job_info = pdfji;
 		dirCache = (DocuDirCache) job_info.getDlConfig().getValue("servlet.dir.cache");
@@ -32,7 +36,11 @@
 		info_reader = new DigilibInfoReader(fn);
 	}
 	
-	
+	/**
+	 * generate iText-PDF-Contents for the title page
+	 * 
+	 * @return
+	 */
 	public Element getPageContents(){
 		Paragraph content = new Paragraph();
 		content.setAlignment(Element.ALIGN_CENTER);
@@ -92,8 +100,13 @@
 		return content;
 	}
 	
+	/**
+	 * return base directory of an image directory
+	 * 
+	 * @param path
+	 * @return
+	 */
 	private String getBase(String path){
-		// return base directory of an image directory
 		if(path.contains("/")){
 			String[] x = path.split("/");
 			String newpath = "";
@@ -106,12 +119,15 @@
 			return "";
 	}
 	
+
+	/**
+	 * Methods for the different attributes.
+	 * 
+	 */
+	
 	
 	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);
@@ -130,8 +146,6 @@
 		}
 		return null;
 	}
-	
-	
 	private String getTitle(){
 		if(info_reader.hasInfo())
 			return info_reader.getAsString("title");
--- a/servlet/src/digilib/servlet/Scaler.java	Fri Mar 13 15:55:13 2009 +0100
+++ b/servlet/src/digilib/servlet/Scaler.java	Fri Mar 27 15:14:30 2009 +0100
@@ -28,6 +28,9 @@
 import digilib.io.ImageFile;
 import digilib.io.ImageFileset;
 
+
+// TODO digilibError is not used anymore and may need to get reintegrated
+
 public class Scaler extends RequestHandler {
 
 	/** digilib servlet version (for all components) */
@@ -151,6 +154,9 @@
 		defaultQuality = dlConfig.getAsInt("default-quality");
 	}
 
+	
+	
+	
 	@Override
 	public void processRequest(HttpServletRequest request,
 			HttpServletResponse response) throws ServletException, ImageOpException {
@@ -159,12 +165,9 @@
 		if (dlConfig == null) {
 			throw new ServletException("ERROR: No Configuration!");
 		}
-
-		
 		
 		accountlog.debug("request: " + request.getQueryString());
 		logger.debug("request: " + request.getQueryString());
-
 		
 
 		// define the job information
@@ -225,9 +228,6 @@
 			e1.printStackTrace();
 			logger.error(e1.getMessage());
 		}
-
-		
-		
 		
 		if (! DigilibWorker.canRun()) {
 			logger.error("Servlet overloaded!");
@@ -240,9 +240,6 @@
 		}
 
 		
-		
-		
-		
 		DigilibWorker job=null;
 		try {
 			
@@ -291,36 +288,25 @@
 
 			
 		} catch (IOException e) {
-			// TODO Auto-generated catch block
 			e.printStackTrace();
 			logger.error(e.getClass()+": "+ e.getMessage());
 			//response.sendError(1);
 		} catch (ImageOpException e) {
-			// TODO Auto-generated catch block
 			e.printStackTrace();
 			logger.error(e.getClass()+": "+ e.getMessage());
 			//response.sendError(1);
 		}
 
 		
-		
-
-		
-		/*boolean errorMsgHtml = false;
+	/*	boolean errorMsgHtml = false;
 		
 		if(jobdeclaration.hasOption("mo","errtxt")){
 			errorMsgHtml = true;
 		} else if (jobdeclaration.hasOption("mo","errimg")) {
 			errorMsgHtml = true;
-		}
-		
-		
-		
-		*/
-				
+		} */
 		
 	}
-
 	
 	
 	/**