| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.net.ftp.parser.RegexFTPFileEntryParserImpl |
|
|
| 1 | /* |
|
| 2 | * Copyright 2004-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 | ||
| 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 | /** |
|
| 29 | * This abstract class implements both the older FTPFileListParser and |
|
| 30 | * newer FTPFileEntryParser interfaces with default functionality. |
|
| 31 | * All the classes in the parser subpackage inherit from this. |
|
| 32 | * |
|
| 33 | * This is the base for all regular based FTPFileEntryParser |
|
| 34 | * |
|
| 35 | * @author Steve Cohen <scohen@apache.org> |
|
| 36 | */ |
|
| 37 | public abstract class RegexFTPFileEntryParserImpl extends FTPFileEntryParserImpl |
|
| 38 | { |
|
| 39 | /** |
|
| 40 | * internal pattern the matcher tries to match, representing a file |
|
| 41 | * entry |
|
| 42 | */ |
|
| 43 | 222 | private Pattern pattern = null; |
| 44 | ||
| 45 | /** |
|
| 46 | * internal match result used by the parser |
|
| 47 | */ |
|
| 48 | 222 | private MatchResult result = null; |
| 49 | ||
| 50 | /** |
|
| 51 | * Internal PatternMatcher object used by the parser. It has protected |
|
| 52 | * scope in case subclasses want to make use of it for their own purposes. |
|
| 53 | */ |
|
| 54 | 222 | protected PatternMatcher _matcher_ = null; |
| 55 | ||
| 56 | /** |
|
| 57 | * The constructor for a RegexFTPFileEntryParserImpl object. |
|
| 58 | * |
|
| 59 | * @param regex The regular expression with which this object is |
|
| 60 | * initialized. |
|
| 61 | * |
|
| 62 | * @exception IllegalArgumentException |
|
| 63 | * Thrown if the regular expression is unparseable. Should not be seen in |
|
| 64 | * normal conditions. It it is seen, this is a sign that a subclass has |
|
| 65 | * been created with a bad regular expression. Since the parser must be |
|
| 66 | * created before use, this means that any bad parser subclasses created |
|
| 67 | * from this will bomb very quickly, leading to easy detection. |
|
| 68 | */ |
|
| 69 | ||
| 70 | public RegexFTPFileEntryParserImpl(String regex) |
|
| 71 | { |
|
| 72 | 222 | super(); |
| 73 | try |
|
| 74 | { |
|
| 75 | 222 | _matcher_ = new Perl5Matcher(); |
| 76 | 222 | pattern = new Perl5Compiler().compile(regex); |
| 77 | } |
|
| 78 | 0 | catch (MalformedPatternException e) |
| 79 | { |
|
| 80 | 0 | throw new IllegalArgumentException ( |
| 81 | "Unparseable regex supplied: " + regex); |
|
| 82 | 222 | } |
| 83 | 222 | } |
| 84 | ||
| 85 | /** |
|
| 86 | * Convenience method delegates to the internal MatchResult's matches() |
|
| 87 | * method. |
|
| 88 | * |
|
| 89 | * @param s the String to be matched |
|
| 90 | * @return true if s matches this object's regular expression. |
|
| 91 | */ |
|
| 92 | ||
| 93 | public boolean matches(String s) |
|
| 94 | { |
|
| 95 | 496 | this.result = null; |
| 96 | 496 | if (_matcher_.matches(s.trim(), this.pattern)) |
| 97 | { |
|
| 98 | 348 | this.result = _matcher_.getMatch(); |
| 99 | } |
|
| 100 | 496 | return null != this.result; |
| 101 | } |
|
| 102 | ||
| 103 | ||
| 104 | ||
| 105 | /** |
|
| 106 | * Convenience method delegates to the internal MatchResult's groups() |
|
| 107 | * method. |
|
| 108 | * |
|
| 109 | * @return the number of groups() in the internal MatchResult. |
|
| 110 | */ |
|
| 111 | ||
| 112 | public int getGroupCnt() |
|
| 113 | { |
|
| 114 | 0 | if (this.result == null) |
| 115 | { |
|
| 116 | 0 | return 0; |
| 117 | } |
|
| 118 | 0 | return this.result.groups(); |
| 119 | } |
|
| 120 | ||
| 121 | ||
| 122 | ||
| 123 | /** |
|
| 124 | * Convenience method delegates to the internal MatchResult's group() |
|
| 125 | * method. |
|
| 126 | * |
|
| 127 | * @param matchnum match group number to be retrieved |
|
| 128 | * |
|
| 129 | * @return the content of the <code>matchnum'th<code> group of the internal |
|
| 130 | * match or null if this method is called without a match having |
|
| 131 | * been made. |
|
| 132 | */ |
|
| 133 | public String group(int matchnum) |
|
| 134 | { |
|
| 135 | 2936 | if (this.result == null) |
| 136 | { |
|
| 137 | 0 | return null; |
| 138 | } |
|
| 139 | 2936 | return this.result.group(matchnum); |
| 140 | } |
|
| 141 | ||
| 142 | /** |
|
| 143 | * For debugging purposes - returns a string shows each match group by |
|
| 144 | * number. |
|
| 145 | * |
|
| 146 | * @return a string shows each match group by number. |
|
| 147 | */ |
|
| 148 | ||
| 149 | public String getGroupsAsString() |
|
| 150 | { |
|
| 151 | 0 | StringBuffer b = new StringBuffer(); |
| 152 | 0 | for (int i = 1; i <= this.result.groups(); i++) |
| 153 | { |
|
| 154 | 0 | b.append(i).append(") ").append(this.result.group(i)) |
| 155 | .append(System.getProperty("line.separator")); |
|
| 156 | } |
|
| 157 | 0 | return b.toString(); |
| 158 | } |
|
| 159 | ||
| 160 | } |
|
| 161 | ||
| 162 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |