001 /* 002 * mesopotamia @mesopotamia.version@ 003 * Multilingual parser and repository. 004 * Copyright (C) 2005 Hammurapi Group 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 019 * 020 * URL: http://http://www.hammurapi.biz 021 * e-Mail: support@hammurapi.biz 022 */ 023 package org.mesopotamia; 024 025 import java.io.File; 026 import java.io.FileInputStream; 027 import java.io.IOException; 028 import java.security.MessageDigest; 029 030 /** 031 * java.io.File based Source for parsing 032 * 033 * @author sourceeater 034 * @revision $Revision: 1.2 $ 035 */ 036 public class FileSource implements Source { 037 038 private final File theFileSource; 039 private String path; 040 041 public FileSource(File aFileSource, String path) { 042 this.theFileSource = aFileSource; 043 this.path=path.replace(File.separatorChar, '/'); 044 } 045 046 public long getSize() { 047 return theFileSource.length(); 048 } 049 050 public String getDigest(MessageDigest digest) throws MesopotamiaException { 051 try { 052 digest.reset(); 053 FileInputStream fis=new FileInputStream(theFileSource); 054 byte[] buf=new byte[4096]; 055 int l; 056 try { 057 while ((l=fis.read(buf))!=-1) { 058 digest.update(buf, 0, l); 059 } 060 } finally { 061 fis.close(); 062 } 063 byte[] dba = digest.digest(); 064 StringBuffer dBuf=new StringBuffer(); 065 for (int i=0; i<dba.length; i++) { 066 String str=Integer.toHexString(dba[i] & 0xFF); 067 if (str.length()==1) { 068 dBuf.append("0"); 069 } 070 dBuf.append(str); 071 } 072 return dBuf.toString(); 073 } catch (IOException e) { 074 throw new MesopotamiaException("Could not calculate checksum for file: "+path, e); 075 } 076 } 077 078 public String getPath() { 079 return path; 080 } 081 082 public String getName() { 083 return theFileSource.getName(); 084 } 085 086 public Object get() { 087 return this.theFileSource; 088 } 089 090 public long getLastModified() { 091 return theFileSource.lastModified(); 092 } 093 094 public String toString() { 095 return getClass().getName()+": "+path; 096 } 097 }