View Javadoc

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  
19  package org.apache.commons.net.ftp.parser;
20  
21  import java.util.regex.MatchResult;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  import java.util.regex.PatternSyntaxException;
25  
26  import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
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 class for all regular expression based FTPFileEntryParser classes
34   *
35   * @author Steve Cohen <scohen@apache.org>
36   */
37  public abstract class RegexFTPFileEntryParserImpl extends
38          FTPFileEntryParserImpl {
39      /**
40       * internal pattern the matcher tries to match, representing a file
41       * entry
42       */
43      private Pattern pattern = null;
44  
45      /**
46       * internal match result used by the parser
47       */
48      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      protected Matcher _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          super();
72          setRegex(regex);
73      }
74  
75      /**
76       * Convenience method delegates to the internal MatchResult's matches()
77       * method.
78       *
79       * @param s the String to be matched
80       * @return true if s matches this object's regular expression.
81       */
82  
83      public boolean matches(String s) {
84          this.result = null;
85          _matcher_ = pattern.matcher(s);
86          if (_matcher_.matches()) {
87              this.result = _matcher_.toMatchResult();
88          }
89          return null != this.result;
90      }
91  
92      /**
93       * Convenience method
94       *
95       * @return the number of groups() in the internal MatchResult.
96       */
97  
98      public int getGroupCnt() {
99          if (this.result == null) {
100             return 0;
101         }
102         return this.result.groupCount();
103     }
104 
105     /**
106      * Convenience method delegates to the internal MatchResult's group()
107      * method.
108      *
109      * @param matchnum match group number to be retrieved
110      *
111      * @return the content of the <code>matchnum'th</code> group of the internal
112      *         match or null if this method is called without a match having
113      *         been made.
114      */
115     public String group(int matchnum) {
116         if (this.result == null) {
117             return null;
118         }
119         return this.result.group(matchnum);
120     }
121 
122     /**
123      * For debugging purposes - returns a string shows each match group by
124      * number.
125      *
126      * @return a string shows each match group by number.
127      */
128 
129     public String getGroupsAsString() {
130         StringBuilder b = new StringBuilder();
131         for (int i = 1; i <= this.result.groupCount(); i++) {
132             b.append(i).append(") ").append(this.result.group(i)).append(
133                     System.getProperty("line.separator"));
134         }
135         return b.toString();
136     }
137 
138     /**
139      * Alter the current regular expression being utilised for entry parsing
140      * and create a new {@link Pattern} instance.
141      * @param regex The new regular expression
142      * @return  true
143      * @since 2.0
144      * @throws PatternSyntaxException if the regex cannot be compiled
145      */
146     public boolean setRegex(String regex) {
147         try {
148             pattern = Pattern.compile(regex);
149             return true;
150         } catch (PatternSyntaxException pse) {
151             throw new IllegalArgumentException("Unparseable regex supplied: "
152                     + regex);
153         }
154     }
155 
156 }