001/* 002 * Version 0.70 01/04/2002 003 * 004 * Copyright 1999-2002 MicroETernity. All Rights Reserved. 005 * 006 * jFtp was developed by Bea Petrovicova <beapetrovicova@yahoo.com>. 007 * The design and implementation of jFtp are available for royalty-free 008 * adoption and use. This software is provided 'as is' without any 009 * guarantees. Copyright is retained by Bea Petrovicova. Redistribution 010 * of any part of jFtp or any derivative works must include this notice. 011 * 012 */ 013 014package cz.dhl.io; 015 016import cz.dhl.io.CoFile; 017import cz.dhl.ui.CoConsole; 018import java.io.File; 019import java.io.FileInputStream; 020import java.io.FileOutputStream; 021import java.io.InputStream; 022import java.io.IOException; 023import java.io.OutputStream; 024import java.text.DateFormat; 025import java.util.Calendar; 026import java.util.Date; 027import java.util.Vector; 028import java.util.StringTokenizer; 029import java.util.NoSuchElementException; 030 031/** 032 * Allows uniform manipulation with local files. 033 * Equivalent for File object. 034 * 035 * <P><B>Only absolute pathnames are supported!</B></P> 036 * 037 * @see CoFile 038 * @see java.io.File 039 * 040 * @Version 0.70 01/04/2002 041 * @author Bea Petrovicova <beapetrovicova@yahoo.com> 042 */ 043public final class LocalFile 044 extends File implements CoFile 045{ 046 /* CoOrder Implementation. */ 047 048 private String name=null, ext=null; 049 050 private void sortSetup(String name) 051 { this.name=name.toUpperCase(); int index = this.name.lastIndexOf("."); 052 if(index != -1 && index < this.name.length()) 053 ext = this.name.substring(index); else ext = " " +this.name; } 054 055 public int compareNameToIgnoreCase(CoOrder file) 056 { if(file instanceof LocalFile) 057 { LocalFile l2 = (LocalFile)file; 058 return name.compareTo(l2.name); } 059 else throw new ClassCastException(); } 060 061 public int compareExtToIgnoreCase(CoOrder file) 062 { if(file instanceof LocalFile) 063 { LocalFile l2 = (LocalFile)file; 064 int result=ext.compareTo(l2.ext); 065 if(result==0) 066 result=name.compareTo(l2.name); 067 return result; } 068 else throw new ClassCastException(); } 069 070 public boolean startsWithIgnoreCase(char ch) 071 { return (name.charAt(0) == Character.toUpperCase(ch)); } 072 073 public boolean equalsExtTo(String filter) 074 { return (ext.compareTo(filter)==0); } 075 076 public boolean equalsExtTo(String filter[]) 077 { boolean done = false; 078 for(int j=0;j<filter.length;j++) 079 if(ext.compareTo(filter[j])==0) 080 { done = true; break; } 081 return done; } 082 083 public boolean equals(Object o) 084 { if(o==null) return false; else return(compareTo(o)==0); } 085 086 public int compareTo(Object o) 087 { String s1 = getHost()+getAbsolutePath(), s2; 088 if(o instanceof CoFile) 089 { CoFile f2 = (CoFile)o; 090 s2 = f2.getHost()+f2.getAbsolutePath(); } 091 else if(o instanceof String) 092 s2 = (String)o; 093 else throw new ClassCastException(); 094 return s1.compareTo(s2); } 095 096 public boolean isConnected() { return true; } 097 098 /* CoOrder Implementation. */ 099 100 public char getDataType() { return 'I'; } 101 102 public InputStream getInputStream() throws IOException 103 { return new FileInputStream(this); } 104 105 public OutputStream getOutputStream() throws IOException 106 { return new FileOutputStream(this); } 107 108 public OutputStream getOutputStream(boolean append) throws IOException 109 { return new FileOutputStream(toString(),append); } 110 111 public CoFile newFileChild(String child) 112 { return new LocalFile(this,child); } 113 114 public CoFile newFileRename(String name) 115 { return new LocalFile(this.getParent(),name); } 116 117 public CoConsole getConsole() { return null; } 118 119 /* CoFile Implementation. */ 120 121 /** Creates a new LocalFile instance by converting the 122 * given pathname string into an abstract pathname. */ 123 public LocalFile(String path) 124 { super(path); sortSetup(getName()); } 125 126 /** Creates a new LocalFile instance from a parent 127 * pathname string and a child pathname string. */ 128 public LocalFile(String path, String name) 129 { super(path,name); sortSetup(name); } 130 131 /** Creates a new LocalFile instance from a parent 132 * abstract pathname and a child pathname string. */ 133 public LocalFile(LocalFile dir, String name) 134 { super(dir,name); sortSetup(name); } 135 136 public String getHost() { return ""; } 137 138 public int getPathDepth() 139 { String path = getAbsolutePath(); 140 int depth = -1; int length = -1; 141 while((length = path.indexOf(separatorChar,length+1)) >= 0) 142 depth++; 143 if(!path.endsWith(separator)) 144 depth++; 145 return depth; 146 } 147 148 public CoFile getPathFragment(int depth) 149 { String path = getAbsolutePath(); 150 if(depth>0) 151 { int length = -1; 152 for(int n=0;n<=depth;n++) 153 if((length = path.indexOf(separatorChar,length+1)) < 0) 154 break; 155 if(length>0) 156 return new LocalFile(path.substring(0,length)); 157 else return this; 158 } else 159 return new LocalFile(path.substring(0,path.indexOf(separatorChar)+1)); 160 } 161 162 public String[] getPathArray() 163 { Vector dv = new Vector(); 164 String path = getAbsolutePath(); 165 if(path != null) 166 { StringTokenizer tokenizer = new StringTokenizer(path,separator); 167 while (true) 168 try 169 { String d = tokenizer.nextToken(); 170 dv.addElement(d); 171 } catch(NoSuchElementException e) 172 { break; } 173 } 174 String[] ds = new String[dv.size()]; 175 dv.copyInto(ds); 176 return ds; 177 } 178 179 public String getName() { return super.getName(); } 180 181 public String getParent() { return super.getParent(); } 182 183 public boolean delete() throws SecurityException { return super.delete(); } 184 public boolean mkdir() throws SecurityException { return super.mkdir(); } 185 public boolean mkdirs() throws SecurityException { return super.mkdirs(); } 186 187 public boolean renameTo(CoFile dest) throws SecurityException 188 { return super.renameTo(((File)dest)); } 189 190 public String lastModifiedString() 191 { return (DateFormat.getDateTimeInstance( 192 DateFormat.SHORT,DateFormat.SHORT) 193 ).format(new Date(lastModified())); } 194 195 public long length() { return super.length(); } 196 public long lastModified() { return super.lastModified(); } 197 198 public boolean isAbsolute() { return super.isAbsolute(); } 199 public boolean isDirectory() { return super.isDirectory(); } 200 public boolean isFile() { return super.isFile(); } 201 202 public boolean isSpecial() { return false; } 203 public boolean isLink() { return false; } 204 205 /* Emulation for Java v1.0 and v1.1 206 public boolean isHidden() { return false; } */ 207 public boolean canRead() { return super.canRead(); } 208 public boolean canWrite() { return super.canWrite(); } 209 public boolean exists() { return super.exists(); } 210 211 public String getAccess() 212 { String access = null; 213 if(isDirectory()) 214 access = "d"; 215 else access = "-"; 216 if(canRead()) 217 access += "r"; 218 else access += "-"; 219 if(canWrite()) 220 access += "w?"; 221 else access += "-?"; 222 return access; 223 } 224 225 public String propertyString() 226 { return (isFile()?""+length()+" ":"") +getAccess(); } 227 228 public CoFile[] listCoRoots() 229 { /* Emulation for Java v1.0 and v1.1 230 CoFile fs[] = new CoFile[1]; 231 fs[0] = getPathFragment(0); */ 232 File[] ls = listRoots(); 233 if (ls == null) return null; 234 CoFile[] fs = new CoFile[ls.length]; 235 for (int i = 0; i < ls.length; i++) 236 fs[i] = new LocalFile(ls[i].getAbsolutePath()); 237 return fs; 238 } 239 240 public CoFile[] listCoFiles() 241 throws SecurityException 242 { String[] ss = list(); 243 if (ss == null) return null; 244 CoFile[] fs = new CoFile[ss.length]; 245 for (int i = 0; i < ss.length; i++) 246 fs[i] = new LocalFile(getAbsolutePath(), ss[i]); 247 return fs; 248 } 249 250 public CoFile[] listCoFiles(CoFilenameFilter filter) 251 throws SecurityException 252 { LocalFile[] fs = (LocalFile[])listCoFiles(); 253 if(filter!=null) 254 { Vector fv = new Vector(); 255 for(int i=0;i<fs.length;i++) 256 if(filter.accept(this,fs[i].getName())) 257 fv.addElement(fs[i]); 258 fs = new LocalFile[fv.size()]; 259 fv.copyInto(fs); } 260 return fs; 261 } 262 263 public String toString() 264 { return getAbsolutePath(); } 265}