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
018 package org.apache.commons.net.ftp.parser;
019
020 import java.util.HashMap;
021 import java.util.List;
022 import java.util.ListIterator;
023 import java.util.regex.MatchResult;
024 import java.util.regex.Matcher;
025 import java.util.regex.Pattern;
026 import java.util.regex.PatternSyntaxException;
027
028 import 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 filename.
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 * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
043 * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
044 * @version $Id: VMSVersioningFTPEntryParser.java 636854 2008-03-13 19:55:01Z sebb $
045 *
046 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
047 */
048 public class VMSVersioningFTPEntryParser extends VMSFTPEntryParser
049 {
050
051 private Matcher _preparse_matcher_;
052 private Pattern _preparse_pattern_;
053 private static final String PRE_PARSE_REGEX =
054 "(.*);([0-9]+)\\s*.*";
055
056 /**
057 * Constructor for a VMSFTPEntryParser object. Sets the versioning member
058 * to the supplied value.
059 *
060 * @exception IllegalArgumentException
061 * Thrown if the regular expression is unparseable. Should not be seen
062 * under normal conditions. It it is seen, this is a sign that
063 * <code>REGEX</code> is not a valid regular expression.
064 */
065 public VMSVersioningFTPEntryParser()
066 {
067 this(null);
068 }
069
070 /**
071 * This constructor allows the creation of a VMSVersioningFTPEntryParser
072 * object with something other than the default configuration.
073 *
074 * @param config The {@link FTPClientConfig configuration} object used to
075 * configure this parser.
076 * @exception IllegalArgumentException
077 * Thrown if the regular expression is unparseable. Should not be seen
078 * under normal conditions. It it is seen, this is a sign that
079 * <code>REGEX</code> is not a valid regular expression.
080 * @since 1.4
081 */
082 public VMSVersioningFTPEntryParser(FTPClientConfig config)
083 {
084 super();
085 configure(config);
086 try
087 {
088 //_preparse_matcher_ = new Perl5Matcher();
089 _preparse_pattern_ = Pattern.compile(PRE_PARSE_REGEX);
090 }
091 catch (PatternSyntaxException pse)
092 {
093 throw new IllegalArgumentException (
094 "Unparseable regex supplied: " + PRE_PARSE_REGEX);
095 }
096
097 }
098
099
100
101 private static class NameVersion {
102 String name;
103 int versionNumber;
104 NameVersion(String name, String vers) {
105 this.name = name;
106 this.versionNumber = Integer.parseInt(vers);
107 }
108 }
109
110 /**
111 * Implement hook provided for those implementers (such as
112 * VMSVersioningFTPEntryParser, and possibly others) which return
113 * multiple files with the same name to remove the duplicates ..
114 *
115 * @param original Original list
116 *
117 * @return Original list purged of duplicates
118 */
119 @Override
120 public List<String> preParse(List<String> original) {
121 original = super.preParse(original);
122 HashMap<String, NameVersion> existingEntries = new HashMap<String, NameVersion>();
123 ListIterator<String> iter = original.listIterator();
124 while (iter.hasNext()) {
125 String entry = iter.next().trim();
126 MatchResult result = null;
127 _preparse_matcher_ = _preparse_pattern_.matcher(entry);
128 if (_preparse_matcher_.matches()) {
129 result = _preparse_matcher_.toMatchResult();
130 String name = result.group(1);
131 String version = result.group(2);
132 NameVersion nv = new NameVersion(name, version);
133 NameVersion existing = existingEntries.get(name);
134 if (null != existing) {
135 if (nv.versionNumber < existing.versionNumber) {
136 iter.remove(); // removal removes from original list.
137 continue;
138 }
139 }
140 existingEntries.put(name, nv);
141 }
142
143 }
144 // we've now removed all entries less than with less than the largest
145 // version number for each name that were listed after the largest.
146 // we now must remove those with smaller than the largest version number
147 // for each name that were found before the largest
148 while (iter.hasPrevious()) {
149 String entry = iter.previous().trim();
150 MatchResult result = null;
151 _preparse_matcher_ = _preparse_pattern_.matcher(entry);
152 if (_preparse_matcher_.matches()) {
153 result = _preparse_matcher_.toMatchResult();
154 String name = result.group(1);
155 String version = result.group(2);
156 NameVersion nv = new NameVersion(name, version);
157 NameVersion existing = existingEntries.get(name);
158 if (null != existing) {
159 if (nv.versionNumber < existing.versionNumber) {
160 iter.remove(); // removal removes from original list.
161 }
162 }
163 }
164
165 }
166 return original;
167 }
168
169
170 @Override
171 protected boolean isVersioning() {
172 return true;
173 }
174
175 }
176
177 /* Emacs configuration
178 * Local variables: **
179 * mode: java **
180 * c-basic-offset: 4 **
181 * indent-tabs-mode: nil **
182 * End: **
183 */