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                    if (aFileSource==null) {
043                            throw new NullPointerException();
044                    }
045                    if (!aFileSource.exists()) {
046                            throw new IllegalArgumentException("File does not exist: "+aFileSource.getAbsolutePath());
047                    }
048                    if (!aFileSource.isFile()) {
049                            throw new IllegalArgumentException("Not a file: "+aFileSource.getAbsolutePath());
050                    }
051                    this.theFileSource = aFileSource;
052                    this.path=path.replace(File.separatorChar, '/');
053            }
054    
055            public long getSize() {
056                    return theFileSource.length();
057            }
058    
059            public String getDigest(MessageDigest digest) throws MesopotamiaException {
060                    try {
061                            digest.reset();
062                            FileInputStream fis=new FileInputStream(theFileSource);         
063                            byte[] buf=new byte[4096];
064                            int l;
065                            try {
066                                    while ((l=fis.read(buf))!=-1) {
067                                            digest.update(buf, 0, l);
068                                    }
069                            } finally {
070                                    fis.close();
071                            }
072                            byte[] dba = digest.digest();
073                    StringBuffer dBuf=new StringBuffer();           
074                        for (int i=0; i<dba.length; i++) {
075                            String str=Integer.toHexString(dba[i] & 0xFF);
076                                    if (str.length()==1) {
077                                    dBuf.append("0");
078                            }
079                            dBuf.append(str);
080                        }                   
081                        return dBuf.toString();
082                    } catch (IOException e) {
083                            throw new MesopotamiaException("Could not calculate checksum for file: "+path, e);
084                    }
085            }
086    
087            public String getPath() {
088                    return path;
089            }
090    
091            public String getName() {
092                    return theFileSource.getName();
093            }
094    
095            public Object get() {
096                    return this.theFileSource;
097            }
098    
099            public long getLastModified() {
100                    return theFileSource.lastModified();
101            }
102    
103            public String toString() {
104                    return getClass().getName()+": "+path;
105            }
106    }