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