| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory |
|
|
| 1 | /* |
|
| 2 | * Copyright 2004-2005 The Apache Software Foundation |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | package org.apache.commons.net.ftp.parser; |
|
| 17 | ||
| 18 | import org.apache.commons.net.ftp.Configurable; |
|
| 19 | import org.apache.commons.net.ftp.FTPClientConfig; |
|
| 20 | import org.apache.commons.net.ftp.FTPFileEntryParser; |
|
| 21 | ||
| 22 | ||
| 23 | /** |
|
| 24 | * This is the default implementation of the |
|
| 25 | * FTPFileEntryParserFactory interface. This is the |
|
| 26 | * implementation that will be used by |
|
| 27 | * org.apache.commons.net.ftp.FTPClient.listFiles() |
|
| 28 | * if no other implementation has been specified. |
|
| 29 | * |
|
| 30 | * @see org.apache.commons.net.ftp.FTPClient#listFiles |
|
| 31 | * @see org.apache.commons.net.ftp.FTPClient#setParserFactory |
|
| 32 | */ |
|
| 33 | 4 | public class DefaultFTPFileEntryParserFactory |
| 34 | implements FTPFileEntryParserFactory |
|
| 35 | { |
|
| 36 | 4 | private FTPClientConfig config = null; |
| 37 | ||
| 38 | /** |
|
| 39 | * This default implementation of the FTPFileEntryParserFactory |
|
| 40 | * interface works according to the following logic: |
|
| 41 | * First it attempts to interpret the supplied key as a fully |
|
| 42 | * qualified classname of a class implementing the |
|
| 43 | * FTPFileEntryParser interface. If that succeeds, a parser |
|
| 44 | * object of this class is instantiated and is returned; |
|
| 45 | * otherwise it attempts to interpret the key as an identirier |
|
| 46 | * commonly used by the FTP SYST command to identify systems. |
|
| 47 | * <p/> |
|
| 48 | * If <code>key</code> is not recognized as a fully qualified |
|
| 49 | * classname known to the system, this method will then attempt |
|
| 50 | * to see whether it <b>contains</b> a string identifying one of |
|
| 51 | * the known parsers. This comparison is <b>case-insensitive</b>. |
|
| 52 | * The intent here is where possible, to select as keys strings |
|
| 53 | * which are returned by the SYST command on the systems which |
|
| 54 | * the corresponding parser successfully parses. This enables |
|
| 55 | * this factory to be used in the auto-detection system. |
|
| 56 | * <p/> |
|
| 57 | * |
|
| 58 | * @param key should be a fully qualified classname corresponding to |
|
| 59 | * a class implementing the FTPFileEntryParser interface<br/> |
|
| 60 | * OR<br/> |
|
| 61 | * a string containing (case-insensitively) one of the |
|
| 62 | * following keywords: |
|
| 63 | * <ul> |
|
| 64 | * <li>{@link FTPClientConfig#SYST_UNIX UNIX}</li> |
|
| 65 | * <li>{@link FTPClientConfig#SYST_NT WINDOWS}</li> |
|
| 66 | * <li>{@link FTPClientConfig#SYST_OS2 OS/2}</li> |
|
| 67 | * <li>{@link FTPClientConfig#SYST_OS400 OS/400}</li> |
|
| 68 | * <li>{@link FTPClientConfig#SYST_VMS VMS}</li> |
|
| 69 | * <li>{@link FTPClientConfig#SYST_MVS MVS}</li> |
|
| 70 | * </ul> |
|
| 71 | * @return the FTPFileEntryParser corresponding to the supplied key. |
|
| 72 | * @throws ParserInitializationException thrown if for any reason the factory cannot resolve |
|
| 73 | * the supplied key into an FTPFileEntryParser. |
|
| 74 | * @see FTPFileEntryParser |
|
| 75 | */ |
|
| 76 | public FTPFileEntryParser createFileEntryParser(String key) |
|
| 77 | { |
|
| 78 | 26 | Class parserClass = null; |
| 79 | 26 | FTPFileEntryParser parser = null; |
| 80 | try |
|
| 81 | { |
|
| 82 | 26 | parserClass = Class.forName(key); |
| 83 | 4 | parser = (FTPFileEntryParser) parserClass.newInstance(); |
| 84 | } |
|
| 85 | 22 | catch (ClassNotFoundException e) |
| 86 | { |
|
| 87 | 22 | String ukey = null; |
| 88 | 22 | if (null != key) |
| 89 | { |
|
| 90 | 22 | ukey = key.toUpperCase(); |
| 91 | } |
|
| 92 | 22 | if (ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0) |
| 93 | { |
|
| 94 | 10 | parser = createUnixFTPEntryParser(); |
| 95 | } |
|
| 96 | 12 | else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0) |
| 97 | { |
|
| 98 | 2 | parser = createVMSVersioningFTPEntryParser(); |
| 99 | } |
|
| 100 | 10 | else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0) |
| 101 | { |
|
| 102 | 2 | parser = createNTFTPEntryParser(); |
| 103 | } |
|
| 104 | 8 | else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0) |
| 105 | { |
|
| 106 | 2 | parser = createOS2FTPEntryParser(); |
| 107 | } |
|
| 108 | 6 | else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0) |
| 109 | { |
|
| 110 | 2 | parser = createOS400FTPEntryParser(); |
| 111 | } |
|
| 112 | 4 | else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0) |
| 113 | { |
|
| 114 | 0 | parser = createMVSEntryParser(); |
| 115 | } |
|
| 116 | else |
|
| 117 | { |
|
| 118 | 4 | throw new ParserInitializationException("Unknown parser type: " + key); |
| 119 | } |
|
| 120 | } |
|
| 121 | 2 | catch (ClassCastException e) |
| 122 | { |
|
| 123 | 2 | throw new ParserInitializationException(parserClass.getName() |
| 124 | + " does not implement the interface " |
|
| 125 | + "org.apache.commons.net.ftp.FTPFileEntryParser.", e); |
|
| 126 | } |
|
| 127 | 0 | catch (Throwable e) |
| 128 | { |
|
| 129 | 0 | throw new ParserInitializationException("Error initializing parser", e); |
| 130 | 20 | } |
| 131 | ||
| 132 | 20 | if (parser instanceof Configurable) { |
| 133 | 16 | ((Configurable)parser).configure(this.config); |
| 134 | } |
|
| 135 | 20 | return parser; |
| 136 | } |
|
| 137 | ||
| 138 | /** |
|
| 139 | * <p>Implementation extracts a key from the supplied |
|
| 140 | * {@link FTPClientConfig FTPClientConfig} |
|
| 141 | * parameter and creates an object implementing the |
|
| 142 | * interface FTPFileEntryParser and uses the supplied configuration |
|
| 143 | * to configure it. |
|
| 144 | * </p><p> |
|
| 145 | * Note that this method will generally not be called in scenarios |
|
| 146 | * that call for autodetection of parser type but rather, for situations |
|
| 147 | * where the user knows that the server uses a non-default configuration |
|
| 148 | * and knows what that configuration is. |
|
| 149 | * </p> |
|
| 150 | * @param config A {@link FTPClientConfig FTPClientConfig} |
|
| 151 | * used to configure the parser created |
|
| 152 | * |
|
| 153 | * @return the @link FTPFileEntryParser FTPFileEntryParser} so created. |
|
| 154 | * @exception ParserInitializationException |
|
| 155 | * Thrown on any exception in instantiation |
|
| 156 | * @since 1.4 |
|
| 157 | */ |
|
| 158 | public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) |
|
| 159 | throws ParserInitializationException |
|
| 160 | { |
|
| 161 | 0 | this.config = config; |
| 162 | 0 | String key = config.getServerSystemKey(); |
| 163 | 0 | return createFileEntryParser(key); |
| 164 | } |
|
| 165 | ||
| 166 | ||
| 167 | public FTPFileEntryParser createUnixFTPEntryParser() |
|
| 168 | { |
|
| 169 | 10 | return (FTPFileEntryParser) new UnixFTPEntryParser(); |
| 170 | } |
|
| 171 | ||
| 172 | public FTPFileEntryParser createVMSVersioningFTPEntryParser() |
|
| 173 | { |
|
| 174 | 2 | return (FTPFileEntryParser) new VMSVersioningFTPEntryParser(); |
| 175 | } |
|
| 176 | ||
| 177 | public FTPFileEntryParser createNTFTPEntryParser() |
|
| 178 | { |
|
| 179 | 2 | if (config != null && FTPClientConfig.SYST_NT.equals( |
| 180 | config.getServerSystemKey())) |
|
| 181 | { |
|
| 182 | 0 | return new NTFTPEntryParser(); |
| 183 | } else { |
|
| 184 | 2 | return new CompositeFileEntryParser(class="keyword">new FTPFileEntryParser[] |
| 185 | { |
|
| 186 | new NTFTPEntryParser(), |
|
| 187 | new UnixFTPEntryParser() |
|
| 188 | }); |
|
| 189 | } |
|
| 190 | } |
|
| 191 | ||
| 192 | public FTPFileEntryParser createOS2FTPEntryParser() |
|
| 193 | { |
|
| 194 | 2 | return (FTPFileEntryParser) new OS2FTPEntryParser(); |
| 195 | } |
|
| 196 | ||
| 197 | public FTPFileEntryParser createOS400FTPEntryParser() |
|
| 198 | { |
|
| 199 | 2 | if (config != null && |
| 200 | FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey())) |
|
| 201 | { |
|
| 202 | 0 | return new OS400FTPEntryParser(); |
| 203 | } else { |
|
| 204 | 2 | return new CompositeFileEntryParser(class="keyword">new FTPFileEntryParser[] |
| 205 | { |
|
| 206 | new OS400FTPEntryParser(), |
|
| 207 | new UnixFTPEntryParser() |
|
| 208 | }); |
|
| 209 | } |
|
| 210 | } |
|
| 211 | ||
| 212 | public FTPFileEntryParser createMVSEntryParser() |
|
| 213 | { |
|
| 214 | 0 | return new MVSFTPEntryParser(); |
| 215 | } |
|
| 216 | ||
| 217 | ||
| 218 | ||
| 219 | } |
|
| 220 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |