1 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 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 64 public VMSVersioningFTPEntryParser() 65 { 66 this(null); 67 } 68 69 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 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(); continue; 135 } 136 } 137 existingEntries.put(name, nv); 138 } 139 140 } 141 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(); } 158 } 159 } 160 161 } 162 return original; 163 } 164 165 protected boolean isVersioning() { 166 return true; 167 } 168 169 } 170 171 178 | Popular Tags |