alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

// $Id: AbstractFilePersister.java,v 1.9 2004/09/07 14:09:15 bobtarling Exp $
// Copyright (c) 1996-2004 The Regents of the University of California. All
// Rights Reserved. Permission to use, copy, modify, and distribute this
// software and its documentation without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph appear in all copies.  This software program and
// documentation are copyrighted by The Regents of the University of
// California. The software program and documentation are supplied "AS
// IS", without any accompanying services from The Regents. The Regents
// does not warrant that the operation of the program will be
// uninterrupted or error-free. The end-user understands that the program
// was developed for research purposes and is advised not to rely
// exclusively on the program for any reason.  IN NO EVENT SHALL THE
// UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
// SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
// THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
// PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
// CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

package org.argouml.kernel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.filechooser.FileFilter;

import org.apache.log4j.Logger;

/**
 * To persist to and from zargo (zipped file) storage.
 * 
 * @author Bob Tarling
 */
public abstract class AbstractFilePersister extends FileFilter 
        implements ProjectFilePersister {
    
    private static final Logger LOG = 
        Logger.getLogger(AbstractFilePersister.class);
    
    /**
     * The PERSISTENCE_VERSION is increased every time a new version of 
     * ArgoUML is released, which uses an forwards incompatible file format.
     * This allows conversion of old persistence version files 
     * to be converted to the current one.
     */
    protected static final int PERSISTENCE_VERSION = 2;
    
    /**
     * Copies one file src to another, raising file exceptions
     * if there are some problems.
     * 
     * @param dest The destination file.
     * @param src The source file.
     * @return The destination file after successful copying.
     * @throws IOException if there is some problems with the files.
     * @throws FileNotFoundException if any of the files cannot be found.
     */
    protected File copyFile(File dest, File src)
        throws FileNotFoundException, IOException {
        
        // first delete dest file
        if (dest.exists()) {
            dest.delete();
        }

        FileInputStream fis  = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int i = 0;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fis.close();
        fos.close();
        
        dest.setLastModified(src.lastModified());
        
        return dest;
    }
    
    
    
    ////////////////////////////////////////////////////////////////
    // FileFilter API

    /**
     * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
     */
    public boolean accept(File f) {
        if (f == null) return false;
        if (f.isDirectory()) return true;
        String s = getExtension(f); 
        if (s != null) // this check for files without extension... 
            if (s.equalsIgnoreCase(getExtension())) return true;
        return false;
    }
    
    /**
     * The extension valid for this type of file.
     * (Just the chars, not the dot: e.g. "zargo".)
     *
     * @return the extension valid for this type of file
     */
    public abstract String getExtension();

    /**
     * (Just the description, not the extension between "()".)
     * 
     * @return the description valid for this type of file
     */
    protected abstract String getDesc();
    
    private static String getExtension(File f) {
        if (f == null) return null;
        return getExtension(f.getName());
    }

    private static String getExtension(String filename) {
        int i = filename.lastIndexOf('.');
        if (i > 0 && i < filename.length() - 1) {
            return filename.substring(i + 1).toLowerCase();
        }
        return null;
    }

    /**
     * @see javax.swing.filechooser.FileFilter#getDescription()
     */
    public String getDescription() {
        return getDesc() + " (*." + getExtension() + ")";
    }
    
    /**
     * Save a project to file.

* This first archives the existing file, then calls * doSave(...) to do the actual saving.

* Should doSave(...) throw an exception then it is * caught here and any rollback handled before rethrowing * the exception. * * @param project The project being saved. * @param file The file to which the save is taking place. * @throws SaveException when anything goes wrong * * @see org.argouml.kernel.ProjectFilePersister#save( * org.argouml.kernel.Project, java.io.File) */ public final void save(Project project, File file) throws SaveException { preSave(project, file); doSave(project, file); postSave(project, file); } /** * Handle archiving of previous file or any other common * requirements before saving a model to a file. * * @param project The project being saved. * @param file The file to which the save is taking place. * @throws SaveException when anything goes wrong */ private void preSave(Project project, File file) throws SaveException { } /** * Handle archiving on completion of a save such as renaming * the temporary save file to the real filename. * * @param project The project being saved. * @param file The file to which the save is taking place. * @throws SaveException when anything goes wrong */ private void postSave(Project project, File file) throws SaveException { } /** * Handle any common requirements on detection of a save error. * Such as restoring the archive. This method is called should * the concrete implementation of doSave(...) throw a * SaveException. * * @param project The project being saved. * @param file The file to which the save is taking place. * @param e The original exception that triggered a call to * this method. * @throws SaveException if anything goes wrong in trying to * restore the previous archive. */ private void postSaveFailure(Project project, File file, SaveException e) throws SaveException { } /** * Implement in your concrete class to save a project to a * file.

* There is no need to worry about archiving or restoring * archive on failure, that is handled by the rest of the * framework.

* * @param project the project to save * @param file The file to write. * @throws SaveException when anything goes wrong * * @see org.argouml.kernel.AbstractFilePersister#save( * org.argouml.kernel.Project, java.io.File) */ protected abstract void doSave(Project project, File file) throws SaveException; }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.