View Javadoc

    1   /*
    2    * Copyright 2001-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 java.util.HashMap;
    19  import java.util.List;
    20  import java.util.ListIterator;
    21
    22  import org.apache.commons.net.ftp.FTPClientConfig;
    23  import org.apache.oro.text.regex.MalformedPatternException;
    24  import org.apache.oro.text.regex.MatchResult;
    25  import org.apache.oro.text.regex.Pattern;
    26  import org.apache.oro.text.regex.Perl5Compiler;
    27  import org.apache.oro.text.regex.Perl5Matcher;
    28
    29  /***
    30   * Special implementation VMSFTPEntryParser with versioning turned on.
    31   * This parser removes all duplicates and only leaves the version with the highest
    32   * version number for each filename.
    33   *
    34   * This is a sample of VMS LIST output
    35   *
    36   *  "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
    37   *  "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
    38   *  "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
    39   * <P>
    40   *
    41   * @author  <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
    42   * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
    43   * @version $Id: VMSVersioningFTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $
    44   *
    45   * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
    46   */
    47  public class VMSVersioningFTPEntryParser extends VMSFTPEntryParser
    48  {
    49
    50      private Perl5Matcher _preparse_matcher_;
    51      private Pattern _preparse_pattern_;
    52      private static final String PRE_PARSE_REGEX =
    53          "(.*);([0-9]+)//s*.*";
    54
    55      /***
    56       * Constructor for a VMSFTPEntryParser object.  Sets the versioning member
    57       * to the supplied value.
    58       *
    59       * @exception IllegalArgumentException
    60       * Thrown if the regular expression is unparseable.  Should not be seen
    61       * under normal conditions.  It it is seen, this is a sign that
    62       * <code>REGEX</code> is  not a valid regular expression.
    63       */
    64      public VMSVersioningFTPEntryParser()
    65      {
    66          this(null);
    67      }
    68
    69      /***
    70       * This constructor allows the creation of a VMSVersioningFTPEntryParser
    71       * object with something other than the default configuration.
    72       *
    73       * @param config The {@link FTPClientConfig configuration} object used to
    74       * configure this parser.
    75       * @exception IllegalArgumentException
    76       * Thrown if the regular expression is unparseable.  Should not be seen
    77       * under normal conditions.  It it is seen, this is a sign that
    78       * <code>REGEX</code> is  not a valid regular expression.
    79       * @since 1.4
    80       */
    81      public VMSVersioningFTPEntryParser(FTPClientConfig config)
    82      {
    83          super();
    84          configure(config);
    85          try
    86          {
    87              _preparse_matcher_ = new Perl5Matcher();
    88              _preparse_pattern_ = new Perl5Compiler().compile(PRE_PARSE_REGEX);
    89          }
    90          catch (MalformedPatternException e)
    91          {
    92              throw new IllegalArgumentException (
    93                  "Unparseable regex supplied:  " + PRE_PARSE_REGEX);
    94          }
    95
    96     }
    97
    98
    99
    100     private class NameVersion {
    101         String name;
    102         int versionNumber;
    103         NameVersion(String name, String vers) {
    104             this.name = name;
    105             this.versionNumber = Integer.parseInt(vers);
    106         }
    107     }
    108
    109     /***
    110      * Implement hook provided for those implementers (such as
    111      * VMSVersioningFTPEntryParser, and possibly others) which return
    112      * multiple files with the same name to remove the duplicates ..
    113      *
    114      * @param original Original list
    115      *
    116      * @return Original list purged of duplicates
    117      */
    118     public List preParse(List original) {
    119         original = super.preParse(original);
    120         HashMap existingEntries = new HashMap();
    121         ListIterator iter = original.listIterator();
    122         while (iter.hasNext()) {
    123             String entry = ((String)iter.next()).trim();
    124             MatchResult result = null;
    125             if (_preparse_matcher_.matches(entry, _preparse_pattern_)) {
    126                 result = _preparse_matcher_.getMatch();
    127                 String name = result.group(1);
    128                 String version = result.group(2);
    129                 NameVersion nv = new NameVersion(name, version);
    130                 NameVersion existing = (NameVersion) existingEntries.get(name);
    131                 if (null != existing) {
    132                     if (nv.versionNumber < existing.versionNumber) {
    133                         iter.remove();  // removal removes from original list.
    134                         continue;
    135                     }
    136                 }
    137                 existingEntries.put(name, nv);
    138             }
    139
    140         }
    141         // we've now removed all entries less than with less than the largest
    142         // version number for each name that were listed after the largest.
    143         // we now must remove those with smaller than the largest version number
    144         // for each name that were found before the largest
    145         while (iter.hasPrevious()) {
    146             String entry = ((String)iter.previous()).trim();
    147             MatchResult result = null;
    148             if (_preparse_matcher_.matches(entry, _preparse_pattern_)) {
    149                 result = _preparse_matcher_.getMatch();
    150                 String name = result.group(1);
    151                 String version = result.group(2);
    152                 NameVersion nv = new NameVersion(name, version);
    153                 NameVersion existing = (NameVersion) existingEntries.get(name);
    154                 if (null != existing) {
    155                     if (nv.versionNumber < existing.versionNumber) {
    156                         iter.remove(); // removal removes from original list.
    157                     }
    158                 }
    159             }
    160
    161         }
    162         return original;
    163     }
    164
    165     protected boolean isVersioning() {
    166         return true;
    167     }
    168
    169 }
    170
    171 /* Emacs configuration
    172  * Local variables:        **
    173  * mode:             java  **
    174  * c-basic-offset:   4     **
    175  * indent-tabs-mode: nil   **
    176  * End:                    **
    177  */