    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.HashMap;
    021import java.util.List;
    022import java.util.ListIterator;
    023import java.util.regex.MatchResult;
    024import java.util.regex.Matcher;
    025import java.util.regex.Pattern;
    026import java.util.regex.PatternSyntaxException;
    027
    028import org.apache.commons.net.ftp.FTPClientConfig;
    029
    030/**
    031 * Special implementation VMSFTPEntryParser with versioning turned on.
    032 * This parser removes all duplicates and only leaves the version with the highest
    033 * version number for each file name.
    034 *
    035 * This is a sample of VMS LIST output
    036 *
    037 *  "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
    038 *  "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
    039 *  "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
    040 * <P>
    041 *
    042 *
    043 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
    044 */
    045public class VMSVersioningFTPEntryParser extends VMSFTPEntryParser
    046{
    047
    048    private final Pattern preparsePattern;
    049    private static final String PRE_PARSE_REGEX =
    050        "(.*?);([0-9]+)\\s*.*";
    051
    052    /**
    053     * Constructor for a VMSFTPEntryParser object.
    054     *
    055     * @throws IllegalArgumentException
    056     * Thrown if the regular expression is unparseable.  Should not be seen
    057     * under normal conditions.  It it is seen, this is a sign that
    058     * <code>REGEX</code> is  not a valid regular expression.
    059     */
    060    public VMSVersioningFTPEntryParser()
    061    {
    062        this(null);
    063    }
    064
    065    /**
    066     * This constructor allows the creation of a VMSVersioningFTPEntryParser
    067     * object with something other than the default configuration.
    068     *
    069     * @param config The {@link FTPClientConfig configuration} object used to
    070     * configure this parser.
    071     * @throws IllegalArgumentException
    072     * Thrown if the regular expression is unparseable.  Should not be seen
    073     * under normal conditions.  It it is seen, this is a sign that
    074     * <code>REGEX</code> is  not a valid regular expression.
    075     * @since 1.4
    076     */
    077    public VMSVersioningFTPEntryParser(final FTPClientConfig config)
    078    {
    079        super();
    080        configure(config);
    081        try
    082        {
    083            //_preparse_matcher_ = new Perl5Matcher();
    084            preparsePattern = Pattern.compile(PRE_PARSE_REGEX);
    085        }
    086        catch (final PatternSyntaxException pse)
    087        {
    088            throw new IllegalArgumentException (
    089                "Unparseable regex supplied:  " + PRE_PARSE_REGEX);
    090        }
    091
    092   }
    093
    094    /**
    095     * Implement hook provided for those implementers (such as
    096     * VMSVersioningFTPEntryParser, and possibly others) which return
    097     * multiple files with the same name to remove the duplicates ..
    098     *
    099     * @param original Original list
    100     *
    101     * @return Original list purged of duplicates
    102     */
    103    @Override
    104    public List<String> preParse(final List<String> original) {
    105        final HashMap<String, Integer> existingEntries = new HashMap<>();
    106        final ListIterator<String> iter = original.listIterator();
    107        while (iter.hasNext()) {
    108            final String entry = iter.next().trim();
    109            MatchResult result = null;
    110            final Matcher _preparse_matcher_ = preparsePattern.matcher(entry);
    111            if (_preparse_matcher_.matches()) {
    112                result = _preparse_matcher_.toMatchResult();
    113                final String name = result.group(1);
    114                final String version = result.group(2);
    115                final Integer nv = Integer.valueOf(version);
    116                final Integer existing = existingEntries.get(name);
    117                if (null != existing) {
    118                    if (nv.intValue() < existing.intValue()) {
    119                        iter.remove();  // removes older version from original list.
    120                        continue;
    121                    }
    122                }
    123                existingEntries.put(name, nv);
    124            }
    125
    126        }
    127        // we've now removed all entries less than with less than the largest
    128        // version number for each name that were listed after the largest.
    129        // we now must remove those with smaller than the largest version number
    130        // for each name that were found before the largest
    131        while (iter.hasPrevious()) {
    132            final String entry = iter.previous().trim();
    133            MatchResult result = null;
    134            final Matcher _preparse_matcher_ = preparsePattern.matcher(entry);
    135            if (_preparse_matcher_.matches()) {
    136                result = _preparse_matcher_.toMatchResult();
    137                final String name = result.group(1);
    138                final String version = result.group(2);
    139                final Integer nv = Integer.valueOf(version);
    140                final Integer existing = existingEntries.get(name);
    141                if (null != existing) {
    142                    if (nv.intValue() < existing.intValue()) {
    143                        iter.remove(); // removes older version from original list.
    144                    }
    145                }
    146            }
    147
    148        }
    149        return original;
    150    }
    151
    152
    153    @Override
    154    protected boolean isVersioning() {
    155        return true;
    156    }
    157
    158}
    159
    160/* Emacs configuration
    161 * Local variables:        **
    162 * mode:             java  **
    163 * c-basic-offset:   4     **
    164 * indent-tabs-mode: nil   **
    165 * End:                    **
    166 */