001    package calhoun.analysis.crf.io;
002    
003    import java.io.File;
004    
005    import calhoun.util.Assert;
006    import calhoun.util.FileUtil;
007    
008    /** an implementation of {@link FilenameMapper} that changes file extensions or add a new file extension onto the source file.
009     * If the <code>append<code> property is set, the new extension will be added to the end of the existing filename.  Otherwise,
010     * the existing file extension is replaced with the new extension.
011     */
012    public class ExtensionMapper implements FilenameMapper {
013    
014            boolean append;
015            String extension;
016    
017            /** returns the value of the append property.  If true, the <code>extension<code> will be appended to the source filename.  
018             * Otherwise, the existing extension will replaced with the new one.  Default is false.
019             * @return the value of the append property.
020             */
021            public boolean isAppend() {
022                    return append;
023            }
024    
025            /** set the value of the append property.
026             * @param append set to true if the extension should be appended to create the new filename.
027             */
028            public void setAppend(boolean append) {
029                    this.append = append;
030            }
031            
032            /** the new extension to use in the mapped file.  Must be set before {link #mapFilename} is called.
033             * @return the extension to use in the mapped file.
034             */
035            public String getExtension() {
036                    return extension;
037            }
038            
039            /** sets the extension.
040             * @param extension the extension to use in the mapped file.
041             */
042            public void setExtension(String extension) {
043                    this.extension = extension;
044            }
045    
046            /** Maps the source file name to a new name by altering the file extension.  The new <code>extension</code>
047             * will either be added to the end of the existing filename or will replace the existing extension based on the
048             * value of the <code>append</code> property.
049             * @see calhoun.analysis.crf.io.FilenameMapper#mapFilename(java.io.File)
050             */
051            public File mapFilename(File source) {
052                    Assert.a(extension != null, "Extension must be set befure mapFilename can be called.");
053                    String dest;
054                    if(append) {
055                            dest = source.getPath()+extension;
056                    }
057                    else {
058                            String[] baseAndExtension = FileUtil.getBaseAndExtension(source);
059                            String directory = "";
060                            if(source.getParent() != null) {
061                                    directory = source.getParent()+File.separator;
062                            }
063                            dest = directory+baseAndExtension[0]+extension;
064                    }
065                    return new File(dest);
066            }
067    }