001package com.hfg.util.io; 002 003 004import java.util.ArrayList; 005import java.util.Collections; 006import java.util.List; 007 008import com.hfg.security.LoginCredentials; 009import com.hfg.util.collection.CollectionUtil; 010import com.hfg.datetime.DateSortDirection; 011import com.hfg.util.StringUtil; 012 013//------------------------------------------------------------------------------ 014/** 015 * Abstract RemoteFileLister base class. 016 * 017 * @author J. Alex Taylor, hairyfatguy.com 018 */ 019//------------------------------------------------------------------------------ 020// com.hfg XML/HTML Coding Library 021// 022// This library is free software; you can redistribute it and/or 023// modify it under the terms of the GNU Lesser General Public 024// License as published by the Free Software Foundation; either 025// version 2.1 of the License, or (at your option) any later version. 026// 027// This library is distributed in the hope that it will be useful, 028// but WITHOUT ANY WARRANTY; without even the implied warranty of 029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 030// Lesser General Public License for more details. 031// 032// You should have received a copy of the GNU Lesser General Public 033// License along with this library; if not, write to the Free Software 034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 035// 036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 037// jataylor@hairyfatguy.com 038//------------------------------------------------------------------------------ 039 040public abstract class AbstractRemoteFileLister<T extends RemoteFile> implements RemoteFileLister 041{ 042 043 //########################################################################### 044 // PRIVATE FIELDS 045 //########################################################################### 046 047 private String mFilePath; 048 private List<RemoteFileFilter> mFilterList; 049 private LoginCredentials mCredentials; 050 private boolean mReturnOnlyMostRecent; 051 052 //########################################################################### 053 // CONSTRUCTORS 054 //########################################################################### 055 056 //--------------------------------------------------------------------------- 057 public AbstractRemoteFileLister() 058 { 059 060 } 061 062 //--------------------------------------------------------------------------- 063 public AbstractRemoteFileLister(String inFilePath) 064 { 065 setFilePath(inFilePath); 066 } 067 068 //--------------------------------------------------------------------------- 069 public AbstractRemoteFileLister(String inFilePath, List<RemoteFileFilter> inFilterList) 070 { 071 this(inFilePath); 072 mFilterList = inFilterList; 073 } 074 075 076 //########################################################################### 077 // PUBLIC METHODS 078 //########################################################################### 079 080 //--------------------------------------------------------------------------- 081 public abstract String getProtocol(); 082 083 //--------------------------------------------------------------------------- 084 public RemoteFileLister setReturnOnlyMostRecentFile(boolean inValue) 085 { 086 mReturnOnlyMostRecent = inValue; 087 return this; 088 } 089 090 //--------------------------------------------------------------------------- 091 public boolean getReturnOnlyMostRecentFile() 092 { 093 return mReturnOnlyMostRecent; 094 } 095 096 //--------------------------------------------------------------------------- 097 public void setFilePath(String inFilePath) 098 { 099 mFilePath = inFilePath; 100 } 101 102 //--------------------------------------------------------------------------- 103 public String getFilePath() 104 { 105 return mFilePath; 106 } 107 108 //--------------------------------------------------------------------------- 109 public RemoteFileLister setCredentials(LoginCredentials inCredentials) 110 { 111 mCredentials = inCredentials; 112 return this; 113 } 114 115 //--------------------------------------------------------------------------- 116 public abstract void clearRemoteFileList(); 117 118 //--------------------------------------------------------------------------- 119 public List<T> getUnfilteredRemoteFileList() 120 { 121 List<T> remoteFiles = getUnfilteredRemoteFileListImpl(); 122 if (getReturnOnlyMostRecentFile() 123 && remoteFiles != null 124 && remoteFiles.size() > 1) 125 { 126 Collections.sort(remoteFiles, new RemoteFileTimestampComparator(DateSortDirection.NEWER_TO_OLDER)); 127 remoteFiles = remoteFiles.subList(0, 1); 128 } 129 130 return remoteFiles; 131 } 132 133 //--------------------------------------------------------------------------- 134 public List<T> getFilteredRemoteFileList(List<RemoteFileFilter> inRemoteFileFilters) 135 { 136 setFilterList(inRemoteFileFilters); 137 return getFilteredRemoteFileList(); 138 } 139 140 //--------------------------------------------------------------------------- 141 public List<T> getFilteredRemoteFileList() 142 { 143 List<T> filteredList = new ArrayList<T>(); 144 145 if (CollectionUtil.hasValues(mFilterList)) 146 { 147 for (T file : getUnfilteredRemoteFileList()) 148 { 149 if (accept(file)) 150 { 151 filteredList.add(file); 152 } 153 } 154 } 155 else 156 { 157 filteredList.addAll(getUnfilteredRemoteFileList()); 158 } 159 160 return filteredList; 161 } 162 163 //--------------------------------------------------------------------------- 164 public void setFilterList(List<RemoteFileFilter> inRemoteFileFilters) 165 { 166 mFilterList = inRemoteFileFilters; 167 } 168 169 //--------------------------------------------------------------------------- 170 public void addFilter(RemoteFileFilter inRemoteFileFilter) 171 { 172 if (null == mFilterList) 173 { 174 mFilterList = new ArrayList<RemoteFileFilter>(5); 175 } 176 177 mFilterList.add(inRemoteFileFilter); 178 } 179 180 //--------------------------------------------------------------------------- 181 public List<RemoteFileFilter> getFilterList() 182 { 183 return mFilterList; 184 } 185 186 //########################################################################### 187 // PROTECTED METHODS 188 //########################################################################### 189 190 //--------------------------------------------------------------------------- 191 protected abstract List<T> getUnfilteredRemoteFileListImpl(); 192 193 //########################################################################### 194 // PACKAGE-LEVEL METHODS 195 //########################################################################### 196 197 //--------------------------------------------------------------------------- 198 LoginCredentials getCredentials() 199 { 200 return mCredentials; 201 } 202 203 //--------------------------------------------------------------------------- 204 boolean wildcardMatch(String inWildcardString, String inFilename) 205 { 206 String regexp = StringUtil.replaceAll(inWildcardString, ".", "\\."); 207 regexp = StringUtil.replaceAll(regexp, "*", ".*"); 208 return inFilename.matches(regexp); 209 } 210 211 //########################################################################### 212 // PRIVATE METHODS 213 //########################################################################### 214 215 //--------------------------------------------------------------------------- 216 private boolean accept(T inRemoteFile) 217 { 218 if (CollectionUtil.hasValues(mFilterList)) 219 { 220 for (RemoteFileFilter filter : mFilterList) 221 { 222 if (!filter.accept(inRemoteFile)) return false; 223 } 224 } 225 226 return true; 227 } 228 229}