changeset 233:e40d8b2e3978

Servlet version 1.19b1 -- with context metadata - reads and caches context tags from index meta file - for use in dlContext-xml.jsp
author robcast
date Sat, 17 Jul 2004 19:25:26 +0200
parents 69b616b50431
children aa1920322b1a
files servlet/src/digilib/io/DocuDirectory.java servlet/src/digilib/io/ImageFileset.java servlet/src/digilib/io/XMLMetaLoader.java servlet/src/digilib/servlet/Scaler.java
diffstat 4 files changed, 137 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/io/DocuDirectory.java	Sat Jul 17 16:57:45 2004 +0200
+++ b/servlet/src/digilib/io/DocuDirectory.java	Sat Jul 17 19:25:26 2004 +0200
@@ -46,6 +46,8 @@
 	private String dirName = null;
 	// directory metadata
 	private HashMap dirMeta = null;
+	// state of metadata is valid
+	private boolean metaChecked = false;
 	// unresolved file metadata
 	private HashMap unresolvedFileMeta = null;
 	// time of last access of this object (not the filesystem)
@@ -257,6 +259,7 @@
 			}
 		}
 		readParentMeta();
+		metaChecked = true;
 	}
 
 	/** Read metadata from all known parent directories.
@@ -451,6 +454,18 @@
 	public HashMap getDirMeta() {
 		return dirMeta;
 	}
+	
+	/**
+	 * Checks metadata
+	 *  
+	 */
+	public void checkMeta() {
+		if (metaChecked) {
+			return;
+		} else {
+			readMeta();
+		}
+	}
 
 	/**
 	 * @return long
--- a/servlet/src/digilib/io/ImageFileset.java	Sat Jul 17 16:57:45 2004 +0200
+++ b/servlet/src/digilib/io/ImageFileset.java	Sat Jul 17 19:25:26 2004 +0200
@@ -36,10 +36,13 @@
 
 	/** list of files (ImageFile) */
 	private ArrayList list = null;
+
 	/** aspect ratio (width/height) */
 	private double aspect = 0;
+
 	/** resolution of the biggest image (DPI) */
 	private double resX = 0;
+
 	/** resolution of the biggest image (DPI) */
 	private double resY = 0;
 
@@ -62,13 +65,13 @@
 	 * 
 	 * 
 	 * @see fill
-	 *      
+	 * 
 	 * @param dirs
-	 *           array of base directories
+	 *            array of base directories
 	 * @param file
-	 *           first file to read
+	 *            first file to read
 	 * @param scalext
-	 *           extension for scaled images
+	 *            extension for scaled images
 	 */
 	public ImageFileset(Directory[] dirs, File file, String scalext) {
 		int nb = dirs.length;
@@ -85,7 +88,7 @@
 	 * 
 	 * 
 	 * @param f
-	 *           file to add
+	 *            file to add
 	 * @return true (always)
 	 */
 	public boolean add(ImageFile f) {
@@ -125,8 +128,8 @@
 	/**
 	 * Get the next smaller ImageFile than the given size.
 	 * 
-	 * Returns the ImageFile from the set that has a width and height smaller
-	 * or equal the given size. Returns null if there isn't any smaller image.
+	 * Returns the ImageFile from the set that has a width and height smaller or
+	 * equal the given size. Returns null if there isn't any smaller image.
 	 * Needs DocuInfo instance to checkFile().
 	 * 
 	 * 
@@ -154,8 +157,8 @@
 	 * Get the next bigger ImageFile than the given size.
 	 * 
 	 * Returns the ImageFile from the set that has a width or height bigger or
-	 * equal the given size. Returns null if there isn't any bigger image.
-	 * Needs DocuInfo instance to checkFile().
+	 * equal the given size. Returns null if there isn't any bigger image. Needs
+	 * DocuInfo instance to checkFile().
 	 * 
 	 * 
 	 * @param size
@@ -228,11 +231,11 @@
 	 * 
 	 * 
 	 * @param dirs
-	 *           list of base directories
+	 *            list of base directories
 	 * @param fl
-	 *           file (from first base dir)
+	 *            file (from first base dir)
 	 * @param scalext
-	 *           first extension to try in other base dirs
+	 *            first extension to try in other base dirs
 	 */
 	void fill(Directory[] dirs, File fl, String scalext) {
 		int nb = dirs.length;
@@ -286,12 +289,22 @@
 			readMeta();
 			if (fileMeta == null) {
 				// try directory metadata
+				((DocuDirectory) parent).checkMeta();
 				if (((DocuDirectory) parent).getDirMeta() != null) {
 					fileMeta = ((DocuDirectory) parent).getDirMeta();
 				} else {
-					// no metadata available
-					metaChecked = true;
-					return;
+					// try parent directory metadata
+					DocuDirectory gp = (DocuDirectory) parent.getParent();
+					if (gp != null) {
+						gp.checkMeta();
+						if (gp.getDirMeta() != null) {
+							fileMeta = gp.getDirMeta();
+						} else {
+							// no metadata available
+							metaChecked = true;
+							return;
+						}
+					}
 				}
 			}
 		}
@@ -318,12 +331,12 @@
 		}
 		// DPI-X and DPI-Y
 		if (fileMeta.containsKey("original-dpi-x")
-			&& fileMeta.containsKey("original-dpi-y")) {
+				&& fileMeta.containsKey("original-dpi-y")) {
 			try {
-				dpix =
-					Double.parseDouble((String) fileMeta.get("original-dpi-x"));
-				dpiy =
-					Double.parseDouble((String) fileMeta.get("original-dpi-y"));
+				dpix = Double.parseDouble((String) fileMeta
+						.get("original-dpi-x"));
+				dpiy = Double.parseDouble((String) fileMeta
+						.get("original-dpi-y"));
 			} catch (NumberFormatException e) {
 			}
 			if ((dpix != 0) && (dpiy != 0)) {
@@ -334,22 +347,18 @@
 		}
 		// SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y
 		if (fileMeta.containsKey("original-size-x")
-			&& fileMeta.containsKey("original-size-y")
-			&& fileMeta.containsKey("original-pixel-x")
-			&& fileMeta.containsKey("original-pixel-y")) {
+				&& fileMeta.containsKey("original-size-y")
+				&& fileMeta.containsKey("original-pixel-x")
+				&& fileMeta.containsKey("original-pixel-y")) {
 			try {
-				sizex =
-					Double.parseDouble(
-						(String) fileMeta.get("original-size-x"));
-				sizey =
-					Double.parseDouble(
-						(String) fileMeta.get("original-size-y"));
-				pixx =
-					Double.parseDouble(
-						(String) fileMeta.get("original-pixel-x"));
-				pixy =
-					Double.parseDouble(
-						(String) fileMeta.get("original-pixel-y"));
+				sizex = Double.parseDouble((String) fileMeta
+						.get("original-size-x"));
+				sizey = Double.parseDouble((String) fileMeta
+						.get("original-size-y"));
+				pixx = Double.parseDouble((String) fileMeta
+						.get("original-pixel-x"));
+				pixy = Double.parseDouble((String) fileMeta
+						.get("original-pixel-y"));
 			} catch (NumberFormatException e) {
 			}
 			if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) {
@@ -387,7 +396,7 @@
 	/**
 	 * Returns the aspect ratio.
 	 * 
-	 * Aspect ratio is (width/height). So it's <1 for portrait and  >1 for
+	 * Aspect ratio is (width/height). So it's <1 for portrait and >1 for
 	 * landscape.
 	 * 
 	 * 
@@ -397,4 +406,4 @@
 		return aspect;
 	}
 
-}
+}
\ No newline at end of file
--- a/servlet/src/digilib/io/XMLMetaLoader.java	Sat Jul 17 16:57:45 2004 +0200
+++ b/servlet/src/digilib/io/XMLMetaLoader.java	Sat Jul 17 19:25:26 2004 +0200
@@ -41,7 +41,8 @@
 	private String fileTag = "file";
 	private String fileNameTag = "name";
 	private String filePathTag = "path";
-	private String infoTag = "img";
+	private String imgTag = "img";
+	private String collectTag = "context";
 
 	public XMLMetaLoader() {
 	}
@@ -55,6 +56,8 @@
 		private HashMap files;
 		private HashMap meta;
 		private StringBuffer content;
+		private boolean collecting;
+		private StringBuffer collectedContent;
 		private String fileName;
 		private String filePath;
 
@@ -75,10 +78,28 @@
 			return qn;
 		}
 
+		/**
+		 * returns all attributes as a String
+		 * 
+		 * @param attrs
+		 * @return 
+		 */
+		private String getAttrString(Attributes attrs) {
+			StringBuffer s = new StringBuffer();
+			for (int i = 0; i < attrs.getLength(); i++) {
+				String key = getName(attrs.getLocalName(i), attrs.getQName(i));
+				s.append(" "+key+"=\""+attrs.getValue(i));
+			}
+			return s.toString();
+		}
+
+			
 		// Parser calls this once at the beginning of a document
 		public void startDocument() throws SAXException {
 			tags = new LinkedList();
 			files = new HashMap();
+			collecting = false;
+			collectedContent = null;
 		}
 
 		// Parser calls this for each element in a document
@@ -98,11 +119,26 @@
 			if (name.equals(metaTag)) {
 				// new meta tag
 				meta = new HashMap();
+				collectedContent = new StringBuffer();
 			} else if (name.equals(fileTag)) {
 				// new file tag
 				fileName = null;
 				filePath = null;
 				meta = new HashMap();
+				collectedContent = new StringBuffer();
+			} else if (name.equals(collectTag)) {
+				// start collecting
+				collecting = true;
+				if (collectedContent == null) {
+					collectedContent = new StringBuffer();
+				}
+			}
+			
+			// record mode
+			if (collecting) {
+				collectedContent.append("<"+name);
+				collectedContent.append(getAttrString(atts));
+				collectedContent.append(">");
 			}
 		}
 
@@ -110,6 +146,9 @@
 		public void characters(char[] ch, int start, int length)
 			throws SAXException {
 			// append data to current string buffer
+			if (content == null) {
+				content = new StringBuffer();
+			}
 			content.append(ch, start, length);
 		}
 
@@ -123,22 +162,25 @@
 			String name = getName(localName, qName);
 			// exit the tag
 			tags.removeLast();
+			String lastTag = (tags.isEmpty()) ? "" : (String) tags.getLast();
 
 			// was it a file/name tag?
-			if (name.equals(fileNameTag) && tags.contains(fileTag)) {
+			if (name.equals(fileNameTag) && lastTag.equals(fileTag)) {
 				// save name as filename
 				if ((content != null) && (content.length() > 0)) {
 					fileName = content.toString().trim();
 				}
+				content = null;
 				return;
 			}
 
 			// was it a file/path tag?
-			if (name.equals(filePathTag) && tags.contains(fileTag)) {
+			if (name.equals(filePathTag) && lastTag.equals(fileTag)) {
 				// save path as filepath 
 				if ((content != null) && (content.length() > 0)) {
 					filePath = content.toString().trim();
 				}
+				content = null;
 				return;
 			}
 
@@ -157,11 +199,13 @@
 						}
 					} else {
 						// no file name, no file
+						content = null;
 						return;
 					}
 					// save meta in file list 
 					files.put(fn, meta);
 				}
+				content = null;
 				return;
 			}
 
@@ -171,17 +215,43 @@
 				if ((meta != null) && (meta.size() > 0)) {
 					files.put("", meta);
 				}
+				content = null;
 				return;
 			}
 
-			// is this inside an info (=img) tag?
-			if (tags.contains(infoTag)) {
+			// is this inside an digilib info (=img) tag?
+			if (lastTag.equals(imgTag)) {
 				// then add whatever this is
 				if ((content != null) && (content.length() > 0)) {
 					meta.put(name, content.toString().trim());
 				}
+				content = null;
+				return;
 			}
 
+			// is this the end of collectTag?
+			if (name.equals(collectTag)) {
+				collecting = false;
+				collectedContent.append("</"+collectTag+">\n");
+				// store collected stuff
+				meta.put(collectTag, collectedContent.toString());
+				//logger.debug("collected: '"+collectedContent+"'");
+				content = null;
+				return;
+			}
+
+			// write collected content
+			if (collecting) {
+				String s = "";
+				if ((content != null) && (content.length() > 0)) {
+					s = content.toString().trim();
+				}
+				//logger.debug("collect:"+name+" = "+s);
+				collectedContent.append(s);
+				collectedContent.append("</"+name+">\n");
+				content = null;
+				return;
+			}
 		}
 
 	}
--- a/servlet/src/digilib/servlet/Scaler.java	Sat Jul 17 16:57:45 2004 +0200
+++ b/servlet/src/digilib/servlet/Scaler.java	Sat Jul 17 19:25:26 2004 +0200
@@ -59,7 +59,7 @@
 public class Scaler extends HttpServlet {
 
 	// digilib servlet version (for all components)
-	public static final String dlVersion = "1.18b12";
+	public static final String dlVersion = "1.19b1";
 
 	// logger for accounting requests
 	Logger accountlog = Logger.getLogger("account.request");