1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.net.ftp.parser;
19
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.ListIterator;
23 import java.util.regex.MatchResult;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import java.util.regex.PatternSyntaxException;
27
28 import org.apache.commons.net.ftp.FTPClientConfig;
29
30 /**
31 * Special implementation VMSFTPEntryParser with versioning turned on.
32 * This parser removes all duplicates and only leaves the version with the highest
33 * version number for each filename.
34 *
35 * This is a sample of VMS LIST output
36 *
37 * "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
38 * "1-JUN.LIS;2 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
39 * "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
40 * <P>
41 *
42 * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
43 * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
44 * @version $Id: VMSVersioningFTPEntryParser.java 1299238 2012-03-10 17:12:28Z sebb $
45 *
46 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
47 */
48 public class VMSVersioningFTPEntryParser extends VMSFTPEntryParser
49 {
50
51 private final Pattern _preparse_pattern_;
52 private static final String PRE_PARSE_REGEX =
53 "(.*);([0-9]+)\\s*.*";
54
55 /**
56 * Constructor for a VMSFTPEntryParser object.
57 *
58 * @exception IllegalArgumentException
59 * Thrown if the regular expression is unparseable. Should not be seen
60 * under normal conditions. It it is seen, this is a sign that
61 * <code>REGEX</code> is not a valid regular expression.
62 */
63 public VMSVersioningFTPEntryParser()
64 {
65 this(null);
66 }
67
68 /**
69 * This constructor allows the creation of a VMSVersioningFTPEntryParser
70 * object with something other than the default configuration.
71 *
72 * @param config The {@link FTPClientConfig configuration} object used to
73 * configure this parser.
74 * @exception IllegalArgumentException
75 * Thrown if the regular expression is unparseable. Should not be seen
76 * under normal conditions. It it is seen, this is a sign that
77 * <code>REGEX</code> is not a valid regular expression.
78 * @since 1.4
79 */
80 public VMSVersioningFTPEntryParser(FTPClientConfig config)
81 {
82 super();
83 configure(config);
84 try
85 {
86 //_preparse_matcher_ = new Perl5Matcher();
87 _preparse_pattern_ = Pattern.compile(PRE_PARSE_REGEX);
88 }
89 catch (PatternSyntaxException pse)
90 {
91 throw new IllegalArgumentException (
92 "Unparseable regex supplied: " + PRE_PARSE_REGEX);
93 }
94
95 }
96
97 /**
98 * Implement hook provided for those implementers (such as
99 * VMSVersioningFTPEntryParser, and possibly others) which return
100 * multiple files with the same name to remove the duplicates ..
101 *
102 * @param original Original list
103 *
104 * @return Original list purged of duplicates
105 */
106 @Override
107 public List<String> preParse(List<String> original) {
108 HashMap<String, Integer> existingEntries = new HashMap<String, Integer>();
109 ListIterator<String> iter = original.listIterator();
110 while (iter.hasNext()) {
111 String entry = iter.next().trim();
112 MatchResult result = null;
113 Matcher _preparse_matcher_ = _preparse_pattern_.matcher(entry);
114 if (_preparse_matcher_.matches()) {
115 result = _preparse_matcher_.toMatchResult();
116 String name = result.group(1);
117 String version = result.group(2);
118 Integer nv = Integer.valueOf(version);
119 Integer existing = existingEntries.get(name);
120 if (null != existing) {
121 if (nv.intValue() < existing.intValue()) {
122 iter.remove(); // removes older version from original list.
123 continue;
124 }
125 }
126 existingEntries.put(name, nv);
127 }
128
129 }
130 // we've now removed all entries less than with less than the largest
131 // version number for each name that were listed after the largest.
132 // we now must remove those with smaller than the largest version number
133 // for each name that were found before the largest
134 while (iter.hasPrevious()) {
135 String entry = iter.previous().trim();
136 MatchResult result = null;
137 Matcher _preparse_matcher_ = _preparse_pattern_.matcher(entry);
138 if (_preparse_matcher_.matches()) {
139 result = _preparse_matcher_.toMatchResult();
140 String name = result.group(1);
141 String version = result.group(2);
142 Integer nv = Integer.valueOf(version);
143 Integer existing = existingEntries.get(name);
144 if (null != existing) {
145 if (nv.intValue() < existing.intValue()) {
146 iter.remove(); // removes older version from original list.
147 }
148 }
149 }
150
151 }
152 return original;
153 }
154
155
156 @Override
157 protected boolean isVersioning() {
158 return true;
159 }
160
161 }
162
163 /* Emacs configuration
164 * Local variables: **
165 * mode: java **
166 * c-basic-offset: 4 **
167 * indent-tabs-mode: nil **
168 * End: **
169 */