001package com.hfg.util.io; 002 003 004import java.io.File; 005import java.util.ArrayList; 006import java.util.List; 007import java.util.StringTokenizer; 008 009 010//------------------------------------------------------------------------------ 011/** 012 * RemoteFileLister for local file system files. 013 * 014 * @author J. Alex Taylor, hairyfatguy.com 015 */ 016//------------------------------------------------------------------------------ 017// com.hfg XML/HTML Coding Library 018// 019// This library is free software; you can redistribute it and/or 020// modify it under the terms of the GNU Lesser General Public 021// License as published by the Free Software Foundation; either 022// version 2.1 of the License, or (at your option) any later version. 023// 024// This library is distributed in the hope that it will be useful, 025// but WITHOUT ANY WARRANTY; without even the implied warranty of 026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 027// Lesser General Public License for more details. 028// 029// You should have received a copy of the GNU Lesser General Public 030// License along with this library; if not, write to the Free Software 031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 032// 033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 034// jataylor@hairyfatguy.com 035//------------------------------------------------------------------------------ 036 037public class FileSystemRemoteFileLister extends AbstractRemoteFileLister<FileSystemRemoteFile> 038{ 039 040 //########################################################################### 041 // PRIVATE FIELDS 042 //########################################################################### 043 044 private List<FileSystemRemoteFile> mRemoteFiles; 045 046 private List<String> mFilePathPieces; 047 048 //########################################################################### 049 // CONSTRUCTORS 050 //########################################################################### 051 052 //--------------------------------------------------------------------------- 053 public FileSystemRemoteFileLister() 054 { 055 super(); 056 } 057 058 //--------------------------------------------------------------------------- 059 public FileSystemRemoteFileLister(String inFilePath) 060 { 061 super(inFilePath); 062 } 063 064 //--------------------------------------------------------------------------- 065 public FileSystemRemoteFileLister(String inFilePath, List<RemoteFileFilter> inFilterList) 066 { 067 super(inFilePath, inFilterList); 068 } 069 070 071 //########################################################################### 072 // PUBLIC METHODS 073 //########################################################################### 074 075 //--------------------------------------------------------------------------- 076 @Override 077 public String getProtocol() 078 { 079 return null; 080 } 081 082 //--------------------------------------------------------------------------- 083 public void clearRemoteFileList() 084 { 085 mRemoteFiles = null; 086 } 087 088 //--------------------------------------------------------------------------- 089 public String getFilePath() 090 { 091 String filePath = super.getFilePath(); 092 093 // Remove the 'file://' if present. 094 if (filePath.startsWith("file://")) 095 { 096 filePath = filePath.substring(7); 097 } 098 099 return filePath; 100 } 101 102 //########################################################################### 103 // PROTECTED METHODS 104 //########################################################################### 105 106 //--------------------------------------------------------------------------- 107 protected List<FileSystemRemoteFile> getUnfilteredRemoteFileListImpl() 108 { 109 if (null == mRemoteFiles) 110 { 111 checkPathValidity(); 112 113 mFilePathPieces = splitFilePath(); 114 115 mRemoteFiles = recursiveFileLister(0, null); 116 } 117 118 return mRemoteFiles; 119 } 120 121 //########################################################################### 122 // PRIVATE METHODS 123 //########################################################################### 124 125 //--------------------------------------------------------------------------- 126 private void checkPathValidity() 127 { 128 String filePath = getFilePath(); 129 130 // Get the last directory before any wildcarding. 131 int index = filePath.indexOf("*"); 132 if (index >= 0) 133 { 134 filePath = filePath.substring(0, index); 135 } 136 137 File deepestDir = new File(filePath).getParentFile(); 138 if (null == deepestDir) deepestDir = new File("."); 139 140 if (! deepestDir.exists()) 141 { 142 throw new RuntimeException("'" + deepestDir + "' doesn't exist!"); 143 } 144 } 145 146 //--------------------------------------------------------------------------- 147 private List<String> splitFilePath() 148 { 149 String fileSeparator = System.getProperty("file.separator"); 150 StringTokenizer st = new StringTokenizer(getFilePath(), fileSeparator); 151 152 List<String> pieces = new ArrayList<String>(); 153 if (getFilePath().startsWith(fileSeparator)) pieces.add(fileSeparator); 154 155 while (st.hasMoreTokens()) 156 { 157 pieces.add(st.nextToken()); 158 } 159 160 return pieces; 161 } 162 163 //--------------------------------------------------------------------------- 164 private List<FileSystemRemoteFile> recursiveFileLister(int inLevel, String inFilePath) 165 { 166 List<FileSystemRemoteFile> remoteFiles = new ArrayList<FileSystemRemoteFile>(); 167 168 if (inLevel < mFilePathPieces.size()) 169 { 170 String levelString = mFilePathPieces.get(inLevel); 171 172 // Wildcard? 173 if (levelString.contains("*")) 174 { 175 // Get a list of the current dir. 176 File dir = new File(inFilePath); 177 if (dir.isDirectory()) 178 { 179 String[] filenames = dir.list(); 180 for (String filename : filenames) 181 { 182 if (wildcardMatch(levelString, filename)) 183 { 184 File file = new File(inFilePath, filename); 185 186 if (inLevel == mFilePathPieces.size() - 1) 187 { 188 remoteFiles.add(new FileSystemRemoteFile(file)); 189 } 190 else 191 { 192 remoteFiles.addAll(recursiveFileLister(inLevel + 1, file.getPath())); 193 } 194 } 195 } 196 } 197 } 198 else 199 { 200 File file = new File(inFilePath, levelString); 201 if (file.exists()) 202 { 203 if (inLevel == mFilePathPieces.size() - 1) 204 { 205 remoteFiles.add(new FileSystemRemoteFile(file)); 206 } 207 else 208 { 209 remoteFiles.addAll(recursiveFileLister(inLevel + 1, file.getPath())); 210 } 211 } 212 } 213 214 } 215 216 return remoteFiles; 217 } 218 219}