001package com.hfg.util.io;
002
003import java.util.List;
004
005//------------------------------------------------------------------------------
006/**
007 * Factory for getting the appropriate RemoteFileLister for a URL.
008 * FTP, HTTP, and local files are supported.
009 *
010 * @author J. Alex Taylor, hairyfatguy.com
011 */
012//------------------------------------------------------------------------------
013// com.hfg XML/HTML Coding Library
014//
015// This library is free software; you can redistribute it and/or
016// modify it under the terms of the GNU Lesser General Public
017// License as published by the Free Software Foundation; either
018// version 2.1 of the License, or (at your option) any later version.
019//
020// This library is distributed in the hope that it will be useful,
021// but WITHOUT ANY WARRANTY; without even the implied warranty of
022// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
023// Lesser General Public License for more details.
024//
025// You should have received a copy of the GNU Lesser General Public
026// License along with this library; if not, write to the Free Software
027// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
028//
029// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
030// jataylor@hairyfatguy.com
031//------------------------------------------------------------------------------
032
033public class RemoteFileListerFactory
034{
035
036   //###########################################################################
037   // PRIVATE FIELDS
038   //###########################################################################
039
040   private static final String PROTOCOL_FTP   = "ftp";
041   private static final String PROTOCOL_HTTP  = "http";
042   private static final String PROTOCOL_HTTPS = "https";
043   private static final String PROTOCOL_FILE_SYSTEM = "file system";
044
045
046   //###########################################################################
047   // PUBLIC METHODS
048   //###########################################################################
049
050   //---------------------------------------------------------------------------
051   public static RemoteFileLister getRemoteFileLister(String inFilePath)
052   {
053      return getRemoteFileLister(inFilePath, null);
054   }
055
056   //---------------------------------------------------------------------------
057   public static RemoteFileLister getRemoteFileLister(String inFilePath,
058                                                       List<RemoteFileFilter> inFilterList)
059   {
060      RemoteFileLister remoteFileLister = null;
061
062      String protocol = determineProtocol(inFilePath);
063
064      if (protocol.equalsIgnoreCase(PROTOCOL_FTP))
065      {
066         remoteFileLister = new FTPRemoteFileLister(inFilePath, inFilterList);
067      }
068      else if (protocol.equalsIgnoreCase(PROTOCOL_HTTP)
069               || protocol.equalsIgnoreCase(PROTOCOL_HTTPS))
070      {
071         remoteFileLister = new HTTPRemoteFileLister(inFilePath, inFilterList);
072      }
073      else if (protocol.equalsIgnoreCase(PROTOCOL_FILE_SYSTEM))
074      {
075         remoteFileLister = new FileSystemRemoteFileLister(inFilePath, inFilterList);
076      }
077      else
078      {
079         throw new RuntimeException("'" + protocol + "' is not a protocol "
080                                    + "currently supported by RemoteFileListerFactory.");
081      }
082
083      return remoteFileLister;
084   }
085
086   //###########################################################################
087   // PRIVATE METHODS
088   //###########################################################################
089
090   //---------------------------------------------------------------------------
091   private static String determineProtocol(String inFilePath)
092   {
093      String protocol = null;
094
095      if (inFilePath.startsWith("ftp://"))
096      {
097         protocol = PROTOCOL_FTP;
098      }
099      else if (inFilePath.startsWith("http://"))
100      {
101         protocol = PROTOCOL_HTTP;
102      }
103      else if (inFilePath.startsWith("https://"))
104      {
105         protocol = PROTOCOL_HTTPS;
106      }
107      else if (inFilePath.startsWith("file://"))
108      {
109         protocol = PROTOCOL_FILE_SYSTEM;
110      }
111      else if (inFilePath.indexOf("://") > 0)
112      {
113         protocol = inFilePath.substring(0, inFilePath.indexOf("://"));
114         throw new RuntimeException("'" + protocol + "' is not a protocol "
115                                    + "currently supported by RemoteFileListerFactory.");
116      }
117      else
118      {
119         protocol = PROTOCOL_FILE_SYSTEM;
120      }
121
122      return protocol;
123   }
124
125}