1 16 17 package org.apache.commons.net.ftp.parser; 18 import org.apache.commons.net.ftp.FTPFileEntryParserImpl; 19 import org.apache.oro.text.regex.MalformedPatternException; 20 import org.apache.oro.text.regex.MatchResult; 21 import org.apache.oro.text.regex.Pattern; 22 import org.apache.oro.text.regex.PatternMatcher; 23 import org.apache.oro.text.regex.Perl5Compiler; 24 import org.apache.oro.text.regex.Perl5Matcher; 25 26 27 28 37 public abstract class RegexFTPFileEntryParserImpl extends FTPFileEntryParserImpl 38 { 39 43 private Pattern pattern = null; 44 45 48 private MatchResult result = null; 49 50 54 protected PatternMatcher _matcher_ = null; 55 56 69 70 public RegexFTPFileEntryParserImpl(String regex) 71 { 72 super(); 73 try 74 { 75 _matcher_ = new Perl5Matcher(); 76 pattern = new Perl5Compiler().compile(regex); 77 } 78 catch (MalformedPatternException e) 79 { 80 throw new IllegalArgumentException ( 81 "Unparseable regex supplied: " + regex); 82 } 83 } 84 85 92 93 public boolean matches(String s) 94 { 95 this.result = null; 96 if (_matcher_.matches(s.trim(), this.pattern)) 97 { 98 this.result = _matcher_.getMatch(); 99 } 100 return null != this.result; 101 } 102 103 104 105 111 112 public int getGroupCnt() 113 { 114 if (this.result == null) 115 { 116 return 0; 117 } 118 return this.result.groups(); 119 } 120 121 122 123 133 public String group(int matchnum) 134 { 135 if (this.result == null) 136 { 137 return null; 138 } 139 return this.result.group(matchnum); 140 } 141 142 148 149 public String getGroupsAsString() 150 { 151 StringBuffer b = new StringBuffer (); 152 for (int i = 1; i <= this.result.groups(); i++) 153 { 154 b.append(i).append(") ").append(this.result.group(i)) 155 .append(System.getProperty("line.separator")); 156 } 157 return b.toString(); 158 } 159 160 } 161 162 163 | Popular Tags |