    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.ftp.parser;
    019
    020import java.util.regex.Pattern;
    021
    022import org.apache.commons.net.ftp.Configurable;
    023import org.apache.commons.net.ftp.FTPClientConfig;
    024import org.apache.commons.net.ftp.FTPFileEntryParser;
    025
    026
    027/**
    028 * This is the default implementation of the
    029 * FTPFileEntryParserFactory interface.  This is the
    030 * implementation that will be used by
    031 * org.apache.commons.net.ftp.FTPClient.listFiles()
    032 * if no other implementation has been specified.
    033 *
    034 * @see org.apache.commons.net.ftp.FTPClient#listFiles
    035 * @see org.apache.commons.net.ftp.FTPClient#setParserFactory
    036 */
    037public class DefaultFTPFileEntryParserFactory
    038    implements FTPFileEntryParserFactory
    039{
    040
    041    // Match a plain Java Identifier
    042    private static final String JAVA_IDENTIFIER = "\\p{javaJavaIdentifierStart}(\\p{javaJavaIdentifierPart})*";
    043    // Match a qualified name, e.g. a.b.c.Name - but don't allow the default package as that would allow "VMS"/"UNIX" etc.
    044    private static final String JAVA_QUALIFIED_NAME  = "("+JAVA_IDENTIFIER+"\\.)+"+JAVA_IDENTIFIER;
    045    // Create the pattern, as it will be reused many times
    046    private static final Pattern JAVA_QUALIFIED_NAME_PATTERN = Pattern.compile(JAVA_QUALIFIED_NAME);
    047
    048    /**
    049     * This default implementation of the FTPFileEntryParserFactory
    050     * interface works according to the following logic:
    051     * First it attempts to interpret the supplied key as a fully
    052     * qualified classname (default package is not allowed) of a class implementing the
    053     * FTPFileEntryParser interface.  If that succeeds, a parser
    054     * object of this class is instantiated and is returned;
    055     * otherwise it attempts to interpret the key as an identirier
    056     * commonly used by the FTP SYST command to identify systems.
    057     * <p/>
    058     * If <code>key</code> is not recognized as a fully qualified
    059     * classname known to the system, this method will then attempt
    060     * to see whether it <b>contains</b> a string identifying one of
    061     * the known parsers.  This comparison is <b>case-insensitive</b>.
    062     * The intent here is where possible, to select as keys strings
    063     * which are returned by the SYST command on the systems which
    064     * the corresponding parser successfully parses.  This enables
    065     * this factory to be used in the auto-detection system.
    066     * <p/>
    067     *
    068     * @param key    should be a fully qualified classname corresponding to
    069     *               a class implementing the FTPFileEntryParser interface<br/>
    070     *               OR<br/>
    071     *               a string containing (case-insensitively) one of the
    072     *               following keywords:
    073     *               <ul>
    074     *               <li>{@link FTPClientConfig#SYST_UNIX UNIX}</li>
    075     *               <li>{@link FTPClientConfig#SYST_NT WINDOWS}</li>
    076     *               <li>{@link FTPClientConfig#SYST_OS2 OS/2}</li>
    077     *               <li>{@link FTPClientConfig#SYST_OS400 OS/400}</li>
    078     *               <li>{@link FTPClientConfig#SYST_AS400 AS/400}</li>
    079     *               <li>{@link FTPClientConfig#SYST_VMS VMS}</li>
    080     *               <li>{@link FTPClientConfig#SYST_MVS MVS}</li>
    081     *               <li>{@link FTPClientConfig#SYST_NETWARE NETWARE}</li>
    082     *               <li>{@link FTPClientConfig#SYST_L8 TYPE:L8}</li>
    083     *               </ul>
    084     * @return the FTPFileEntryParser corresponding to the supplied key.
    085     * @throws ParserInitializationException thrown if for any reason the factory cannot resolve
    086     *                   the supplied key into an FTPFileEntryParser.
    087     * @see FTPFileEntryParser
    088     */
    089//    @Override
    090    public FTPFileEntryParser createFileEntryParser(String key)
    091    {
    092        if (key == null) {
    093            throw new ParserInitializationException("Parser key cannot be null");
    094        }
    095        return createFileEntryParser(key, null);
    096    }
    097
    098    // Common method to process both key and config parameters.
    099    private FTPFileEntryParser createFileEntryParser(String key, FTPClientConfig config) {
    100        FTPFileEntryParser parser = null;
    101
    102        // Is the key a possible class name?
    103        if (JAVA_QUALIFIED_NAME_PATTERN.matcher(key).matches()) {
    104            try
    105            {
    106                Class<?> parserClass = Class.forName(key);
    107                try {
    108                    parser = (FTPFileEntryParser) parserClass.newInstance();
    109                } catch (ClassCastException e) {
    110                    throw new ParserInitializationException(parserClass.getName()
    111                        + " does not implement the interface "
    112                        + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
    113                } catch (Exception e) {
    114                    throw new ParserInitializationException("Error initializing parser", e);
    115                } catch (ExceptionInInitializerError e) {
    116                    throw new ParserInitializationException("Error initializing parser", e);
    117                }
    118            } catch (ClassNotFoundException e) {
    119                // OK, assume it is an alias
    120            }
    121        }
    122
    123        if (parser == null) { // Now try for aliases
    124            String ukey = key.toUpperCase(java.util.Locale.ENGLISH);
    125            if (ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0)
    126            {
    127                parser = new UnixFTPEntryParser(config);
    128            }
    129            else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0)
    130            {
    131                parser = new VMSVersioningFTPEntryParser(config);
    132            }
    133            else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0)
    134            {
    135                parser = createNTFTPEntryParser(config);
    136            }
    137            else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0)
    138            {
    139                parser = new OS2FTPEntryParser(config);
    140            }
    141            else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0 ||
    142                    ukey.indexOf(FTPClientConfig.SYST_AS400) >= 0)
    143            {
    144                parser = createOS400FTPEntryParser(config);
    145            }
    146            else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0)
    147            {
    148                parser = new MVSFTPEntryParser(); // Does not currently support config parameter
    149            }
    150            else if (ukey.indexOf(FTPClientConfig.SYST_NETWARE) >= 0)
    151            {
    152                parser = new NetwareFTPEntryParser(config);
    153            }
    154            else if (ukey.indexOf(FTPClientConfig.SYST_MACOS_PETER) >= 0)
    155            {
    156                parser = new MacOsPeterFTPEntryParser(config);
    157            }
    158            else if (ukey.indexOf(FTPClientConfig.SYST_L8) >= 0)
    159            {
    160                // L8 normally means Unix, but move it to the end for some L8 systems that aren't.
    161                // This check should be last!
    162                parser = new UnixFTPEntryParser(config);
    163            }
    164            else
    165            {
    166                throw new ParserInitializationException("Unknown parser type: " + key);
    167            }
    168        }
    169
    170        if (parser instanceof Configurable) {
    171            ((Configurable)parser).configure(config);
    172        }
    173        return parser;
    174    }
    175
    176    /**
    177     * <p>Implementation extracts a key from the supplied
    178     * {@link  FTPClientConfig FTPClientConfig}
    179     * parameter and creates an object implementing the
    180     * interface FTPFileEntryParser and uses the supplied configuration
    181     * to configure it.
    182     * </p><p>
    183     * Note that this method will generally not be called in scenarios
    184     * that call for autodetection of parser type but rather, for situations
    185     * where the user knows that the server uses a non-default configuration
    186     * and knows what that configuration is.
    187     * </p>
    188     * @param config  A {@link  FTPClientConfig FTPClientConfig}
    189     * used to configure the parser created
    190     *
    191     * @return the @link  FTPFileEntryParser FTPFileEntryParser} so created.
    192     * @exception ParserInitializationException
    193     *                   Thrown on any exception in instantiation
    194     * @throws NullPointerException if {@code config} is {@code null}
    195     * @since 1.4
    196     */
    197//    @Override
    198    public FTPFileEntryParser createFileEntryParser(FTPClientConfig config)
    199    throws ParserInitializationException
    200    {
    201        String key = config.getServerSystemKey();
    202        return createFileEntryParser(key, config);
    203    }
    204
    205
    206    public FTPFileEntryParser createUnixFTPEntryParser()
    207    {
    208        return new UnixFTPEntryParser();
    209    }
    210
    211    public FTPFileEntryParser createVMSVersioningFTPEntryParser()
    212    {
    213        return new VMSVersioningFTPEntryParser();
    214    }
    215
    216    public FTPFileEntryParser createNetwareFTPEntryParser() {
    217        return new NetwareFTPEntryParser();
    218    }
    219
    220    public FTPFileEntryParser createNTFTPEntryParser()
    221    {
    222        return createNTFTPEntryParser(null);
    223    }
    224
    225    /**
    226     * Creates an NT FTP parser: if the config exists, and the system key equals
    227     * {@link FTPClientConfig.SYST_NT} then a plain {@link NTFTPEntryParser} is used,
    228     * otherwise a composite of {@link NTFTPEntryParser} and {@link UnixFTPEntryParser} is used.
    229     * @param config the config to use, may be {@code null}
    230     * @return the parser
    231     */
    232    private FTPFileEntryParser createNTFTPEntryParser(FTPClientConfig config)
    233    {
    234        if (config != null && FTPClientConfig.SYST_NT.equals(
    235                config.getServerSystemKey()))
    236        {
    237            return new NTFTPEntryParser(config);
    238        } else {
    239            return new CompositeFileEntryParser(new FTPFileEntryParser[]
    240                   {
    241                       new NTFTPEntryParser(config),
    242                       new UnixFTPEntryParser(config)
    243                   });
    244        }
    245    }
    246
    247     public FTPFileEntryParser createOS2FTPEntryParser()
    248    {
    249        return new OS2FTPEntryParser();
    250    }
    251
    252    public FTPFileEntryParser createOS400FTPEntryParser()
    253    {
    254        return createOS400FTPEntryParser(null);
    255    }
    256
    257    /**
    258     * Creates an OS400 FTP parser: if the config exists, and the system key equals
    259     * {@link FTPClientConfig.SYST_OS400} then a plain {@link OS400FTPEntryParser} is used,
    260     * otherwise a composite of {@link OS400FTPEntryParser} and {@link UnixFTPEntryParser} is used.
    261     * @param config the config to use, may be {@code null}
    262     * @return the parser
    263     */
    264    private FTPFileEntryParser createOS400FTPEntryParser(FTPClientConfig config)
    265        {
    266        if (config != null &&
    267                FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey()))
    268        {
    269            return new OS400FTPEntryParser(config);
    270        } else {
    271            return new CompositeFileEntryParser(new FTPFileEntryParser[]
    272                {
    273                    new OS400FTPEntryParser(config),
    274                    new UnixFTPEntryParser(config)
    275                });
    276        }
    277    }
    278
    279    public FTPFileEntryParser createMVSEntryParser()
    280    {
    281        return new MVSFTPEntryParser();
    282    }
    283
    284}
    285