001 /**
002 * Copyright (c) 2010, SIB. All rights reserved.
003 *
004 * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005 * https://sourceforge.net/projects/javaprotlib/
006 *
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions are met:
009 * Redistributions of source code must retain the above copyright notice, this
010 * list of conditions and the following disclaimer. Redistributions in binary
011 * form must reproduce the above copyright notice, this list of conditions and
012 * the following disclaimer in the documentation and/or other materials provided
013 * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014 * its contributors may be used to endorse or promote products derived from this
015 * software without specific prior written permission.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020 * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package org.expasy.jpl.commons.base.io;
029
030
031 import java.io.File;
032 import java.io.FileReader;
033 import java.io.FileWriter;
034 import java.io.IOException;
035
036
037 /**
038 * A File that supports localized paths
039 *
040 * Also adds basename/dirname functionality.
041 *
042 * Note: This kind of crosses the line between the core interpreter and the
043 * "util" package. It's here so that we're consistent in the source() feature of
044 * the interpreter.
045 */
046 public final class JPLFile extends File {
047
048 private static final long serialVersionUID = 3173731867857806938L;
049
050 private String dirName = "";
051 private String baseName;
052 private String baseNameNoExt;
053 private String extension;
054
055 public JPLFile(String fileName) {
056 super(fileName);
057
058 // init dirName, baseName
059 int lastSepIndex = fileName.lastIndexOf(File.separator);
060
061 if (lastSepIndex != -1) {
062 dirName = fileName.substring(0, lastSepIndex);
063 baseName = fileName.substring(lastSepIndex + 1);
064 } else {
065 baseName = fileName;
066 }
067
068 int extIndex = fileName.lastIndexOf('.');
069
070 if (extIndex < lastSepIndex) {
071 extIndex = -1;
072 }
073
074 if (extIndex >= 0) {
075 baseNameNoExt = fileName.substring(lastSepIndex + 1, extIndex);
076 extension = fileName.substring(extIndex);
077 } else {
078 baseNameNoExt = baseName;
079 }
080 }
081
082 public String getDirName() {
083 return dirName;
084 }
085
086 public String getBaseName() {
087 return baseName;
088 }
089
090 public String getBaseNameNoExt() {
091 return baseNameNoExt;
092 }
093
094 public String getExtension() {
095 return extension;
096 }
097
098 public boolean hasExtension() {
099 return extension != null && extension.length() > 0;
100 }
101
102 public static String getDirName(String filename) {
103 JPLFile file = new JPLFile(filename);
104 return file.getDirName();
105 }
106
107 public static String getBaseName(String filename) {
108 JPLFile file = new JPLFile(filename);
109 return file.getBaseName();
110 }
111
112 public static String getBaseNameNoExt(String filename) {
113 JPLFile file = new JPLFile(filename);
114 return file.getBaseNameNoExt();
115 }
116
117 public static String getExtension(String filename) {
118 JPLFile file = new JPLFile(filename);
119 return file.getExtension();
120 }
121
122 public static boolean hasExtension(String filename) {
123 JPLFile file = new JPLFile(filename);
124 return file.hasExtension();
125 }
126
127 public static void copy(File file1, File file2) throws IOException {
128 FileReader in = new FileReader(file1);
129 FileWriter out = new FileWriter(file2);
130 int c;
131
132 while ((c = in.read()) != -1)
133 out.write(c);
134
135 in.close();
136 out.close();
137 }
138
139 public String toString() {
140 return super.toString() + ", dirName = " + dirName + ", baseName = "
141 + baseName;
142 }
143
144 }