Commits

shei...@ithaka.org  committed fe180ff

Resolve issue #116; clean up commented-out code in AbstractSourcefactory, SourceFactory, SourceFactoryUtil

  • Participants
  • Parent commits 37c7b79

Comments (0)

Files changed (8)

File src/main/java/org/jhove2/core/source/AbstractSourceFactory.java

 import java.io.InputStream;
 import java.net.URL;
 import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
 
 import org.jhove2.core.JHOVE2;
 import org.jhove2.core.JHOVE2Exception;
 		return source;
 	}
 
-//	/** Get source unit from a Zip file entry.
-//	 * @param zip Zip file
-//	 * 
-//	 * @param jhove2 JHOVE2 framework object
-//	 * @param entry Zip file entry
-//	 * @see org.jhove2.core.source.SourceFactory#getSource(java.lang.String, java.lang.String, int, java.util.zip.ZipFile, java.util.zip.ZipEntry)
-//	 */
-//	@Override
-//	public Source getSource(JHOVE2 jhove2, ZipFile zip, ZipEntry entry)
-//	throws IOException, JHOVE2Exception
-//	{
-//		Source source = SourceFactoryUtil.getSource(jhove2, zip, entry);
-//		source = source.getSourceAccessor().persistSource(source);
-//		return source;
-//	}
-
-	/** Get source unit from a Zip file entry.
-	 * @param zip Zip file
+	/**
+	 * Get source unit an input stream.  
 	 * 
 	 * @param jhove2 JHOVE2 framework object
-	 * @param entry Zip file entry
-	 * @see org.jhove2.core.source.SourceFactory#getSource(java.lang.String, java.lang.String, int, java.util.zip.ZipFile, java.util.zip.ZipEntry)
+	 * @param inputStream InputStream containing contents of Source
+	 * @param name file name (signature) to be used in identification
+	 * @param otherProperties non-file-system reportable properties to be associated with this source
+	 * @return Source unit
+	 * @throws FileNotFoundException
+	 *             File not found
+	 * @throws IOException
+	 *             I/O exception instantiating source
+	 * @throws JHOVE2Exception
 	 */
 	@Override
 	public Source getSource(JHOVE2 jhove2, InputStream inputStream, String name, Reportable otherProperties)

File src/main/java/org/jhove2/core/source/SourceFactory.java

 	public Source getSource(JHOVE2 jhove2, URL url)
 	    throws IOException, JHOVE2Exception;
 
-//	/**
-//	 * Get source unit from a Zip file entry by creating a temporary file.
-//	 * 
-//     * @param jhove2 JHOVE2 framework object
-//     * @param zip
-//     *            Zip file
-//     * @param entry
-//     *            Zip file entry
-//	 * @return Source unit
-//	 * @throws IOException
-//	 *             I/O exception instantiating source
-//	 * @throws JHOVE2Exception 
-//	 */
-//	public Source getSource(JHOVE2 jhove2, ZipFile zip, ZipEntry entry)
-//		throws IOException, JHOVE2Exception;
 	/**
-	 * 
-	 * @param jhove2
-	 * @param inputStream
-	 * @param name
-	 * @param otherProperties
-	 * @return
+	 * Get a source unit from an inputstream by creating a temporary file
+	 * Used for example to create soure unit from a Zip file entry
+	 * @param jhove2 JHOVE2 framework object
+	 * @param inputStream InputStream containing contents of Source
+	 * @param name file name Stream would have within original container
+	 * @param otherProperties non-file system reportable features associated with source
+	 * @return Source unit
 	 * @throws IOException
 	 * @throws JHOVE2Exception
 	 */

File src/main/java/org/jhove2/core/source/SourceFactoryUtil.java

 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
 
-import org.jhove2.core.Digest;
 import org.jhove2.core.Invocation;
 import org.jhove2.core.JHOVE2;
 import org.jhove2.core.JHOVE2Exception;
 import org.jhove2.core.reportable.Reportable;
-import org.jhove2.module.digest.AbstractArrayDigester;
-import org.jhove2.module.digest.CRC32Digester;
-import org.jhove2.module.format.zip.ZipEntryProperties;
 
 /**
  * Factory for JHOVE2 file and directory source units.
 		return source;
 	}
 
-//	/**
-//	 * Get source unit from a Zip file entry by creating a temporary file.
-//	 * @param jhove2 JHOVE2 framework object
-//	 * @param zip
-//	 *            Zip file
-//	 * @param entry
-//	 *            Zip file entry
-//	 * @return Source unit
-//	 * @throws IOException
-//	 *             I/O exception instantiating source
-//	 * @throws JHOVE2Exception 
-//	 */
-//	public static synchronized Source getSource(JHOVE2 jhove2, ZipFile zip,
-//			ZipEntry entry)
-//	throws IOException, JHOVE2Exception
-//	{
-//		Source source = null;
-//		String name = entry.getName();
-//		//		if (entry.isDirectory()) {
-//		//		    /* Delete trailing slash from path name, if necessary. Although this
-//		//		     * always should be a forward slash (/), in practice a backward slash
-//		//		     * \) may be found.
-//		//		     */
-//		//		    int in = name.lastIndexOf('/');
-//		//		    if (in < 0) {
-//		//		        in = name.lastIndexOf('\\');
-//		//		    }
-//		//		    if (in == name.length() - 1) {
-//		//		        name = name.substring(0, in);
-//		//		    }
-//		//		    source = new DirectorySource(jhove2, name, false);        
-//		//		}
-//		//		else {
-//		/* Recover the filename from the pathname. Although the path
-//		 * separator always should be a forward slash (/), in practice a
-//		 * backward slash (\) may be found.
-//		 */
-//		int in = name.lastIndexOf('/');
-//		if (in < 0) {
-//			in = name.lastIndexOf('\\');
-//		}
-//		if (in > -1) {
-//			name = name.substring(in+1);
-//		}
-//		/* Create a temporary Java {@link java.io.File} to represent the
-//		 * file entry.
-//		 */
-//		InputStream stream = zip.getInputStream(entry);
-//		Invocation inv = jhove2.getInvocation();
-//		File file = AbstractSource.createTempFile(stream, name,
-//				inv.getTempDirectoryFile(),
-//				inv.getTempPrefix(),
-//				inv.getTempSuffix(),
-//				inv.getBufferSize());
-//		stream.close();
-//
-//		source = new FileSource(jhove2, file, false);
-//		source.setIsTemp(true);
-//
-//		/* This is a temporary fix.  We need to keep the temporary backing
-//		 * files for Zip components in case we need to later get an
-//		 * {@link java.io.InputStream} on the component
-//		 * (Source.getInputStream()) to pass to a third-party package that
-//		 * doesn't support {@link org.jhove2.core.io.Input}s.
-//		 * 
-//		 * Note that the temporary files will accumulate in the temporary
-//		 * directory after termination.
-//		 * 
-//		 * TODO: Find a better mechanism for dealing with this problem
-//		 * in the recursive processing model.
-//		 */
-//		source.setDeleteTempFileOnClose(false);
-//		//		}
-//
-//		/* Get the entry-specific properties. */
-//		long crc = entry.getCrc();
-//		Digest crc32 = new Digest(AbstractArrayDigester.toHexString(crc),
-//				CRC32Digester.ALGORITHM);
-//		ZipEntryProperties properties =
-//			new ZipEntryProperties(name, entry.getCompressedSize(), crc32,
-//					entry.getComment(),
-//					new Date(entry.getTime()));
-//		source.addExtraProperties(properties);
-//
-//		return source;
-//	}	
-
+	/**
+	 * Get a source unit from an InputStream.  Used for example to create a Source
+	 * unit from a Zip file entry. Cteates backing temporary file
+	 * @param jhove2 JHOVE2 framework object
+	 * @param stream InputStream containing content of source
+	 * @param name file name Source would have in its container
+	 * @param otherProperties non-file-system reportable properties associated with Source
+	 * @return Source unit
+	 * @throws IOException
+	 * @throws JHOVE2Exception
+	 */
 	public static synchronized Source getSource(JHOVE2 jhove2, 
 			InputStream stream, 
 			String name,

File src/main/java/org/jhove2/module/format/sgml/OpenSpWrapper.java

 							esisFileParser.parseEsisFile(esisFilePath, jhove2, source, sgm);
 					} catch (IOException e) {
 						continue; // message already attached to module
-					} 								
+					} 
 				}
 
 				/** now parse the Message (.err) file created by OpenSP */
 						messageParser.parseMessageFile(esisErrFilePath, jhove2, source, sgm);
 					} catch (IOException e) {
 						continue; // message already attached to module
-					} 					
+					}
 				}
 			} while (false);
 		}	
 							doctypeParser.parseNormFile(normOutPath, jhove2, source, sgm);
 					} catch (IOException e) {
 						continue; // message already attached to module
-					} 						
+					} 
 				}
 			}while (false);
 		}
 			}
 			if (jhove2.getInvocation().getDeleteTempFilesOnClose()){
 				try {
-					this.tempFilePaths.add(tempOutFile.getCanonicalPath());
+					this.tempFilePaths.add(tempErrFile.getCanonicalPath());
 				}
 				catch (IOException e){}
 			}
 			}
 			if (jhove2.getInvocation().getDeleteTempFilesOnClose()){
 				try {
-					this.tempFilePaths.add(tempOutFile.getCanonicalPath());
+					this.tempFilePaths.add(tempStdErrFile.getCanonicalPath());
 				}
 				catch (IOException e){}
 			}

File src/main/java/uk/gov/nationalarchives/droid/binFileReader/FileByteReader.java

  * @author linb
  */
 public class FileByteReader extends AbstractByteReader {
+	// added for jHOVE2
+	boolean isTempFile = false;
 
+	// added for jHOVE2
+    FileByteReader(IdentificationFile theIDFile, boolean readFile, String filePath, boolean isTempFile) {   	
+    	super(theIDFile);
+        this.file = new File(filePath);
+        if (readFile) {
+            this.readFile();
+        }
+        this.isTempFile = isTempFile;
+    }
+    
     /**
      * Creates a new instance of FileByteReader
      * <p/>
      * @param filePath  the backing file (containing the data)
      */
     FileByteReader(IdentificationFile theIDFile, boolean readFile, String filePath) {
-        super(theIDFile);
-        this.file = new File(filePath);
-        if (readFile) {
-            this.readFile();
-        }
-
+//        super(theIDFile);
+//        this.file = new File(filePath);
+//        if (readFile) {
+//            this.readFile();
+//        }
+    	// changed for JHOVE2
+    	this(theIDFile, readFile, filePath, false);
     }
 
     /**
             }
             randomAccessFile = null;
         }
+        // added for JHOVE2
+        if (this.file != null && this.isTempFile){
+        	try{
+        		file.delete();
+        	}
+        	catch(Exception e){
+        		
+        	}
+        }
+        // end added for JHOVE2
     }
 }

File src/main/java/uk/gov/nationalarchives/droid/binFileReader/InputStreamByteReader.java

         if (byteReader.tempFile == null) {
             return byteReader;
         } else {
-            return new FileByteReader(theIDFile, readFile, byteReader.tempFile.getPath());
+            return new FileByteReader(theIDFile, readFile, byteReader.tempFile.getPath(), true);
         }
     }
     
         if (byteReader.tempFile == null) {
             return byteReader;
         } else {
-            return new FileByteReader(theIDFile, true, byteReader.tempFile.getPath());
+            return new FileByteReader(theIDFile, true, byteReader.tempFile.getPath(), true);
         }
     }
   /// **************************** END ADDED FOR JHOVE2 *********************************************   

File src/main/java/uk/gov/nationalarchives/droid/binFileReader/StreamByteReader.java

     public byte[] getbuffer() {
         return buffer.array();
     }
+    
+    /************   Added for JHOVE2 ****/
+    public void close(){
+    	if (this.tempFile != null){
+    		try {
+    			this.tempFile.delete();
+    		}
+    		catch (Exception e){
+    			;
+    		}
+    	}
+    }
 }

File src/main/java/uk/gov/nationalarchives/droid/binFileReader/UrlByteReader.java

         if (byteReader.tempFile == null) {
             return byteReader;
         } else {
-            return new FileByteReader(theIDFile, readFile, byteReader.tempFile.getPath());
+            return new FileByteReader(theIDFile, readFile, byteReader.tempFile.getPath(),true);
         }
     }