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  package org.apache.commons.net.ftp.parser;
19  
20  import java.text.ParseException;
21  import java.util.Calendar;
22  
23  import org.apache.commons.net.ftp.Configurable;
24  import org.apache.commons.net.ftp.FTPClientConfig;
25  
26  
27  /**
28   * <p>
29   * This abstract class implements the common timestamp parsing
30   * algorithm for all the concrete parsers.  Classes derived from
31   * this one will parse file listings via a supplied regular expression
32   * that pulls out the date portion as a separate string which is
33   * passed to the underlying {@link FTPTimestampParser delegate} to
34   * handle parsing of the file timestamp.
35   * <p>
36   * This class also implements the {@link Configurable Configurable}
37   * interface to allow the parser to be configured from the outside.
38   *
39   * @since 1.4
40   */
41  public abstract class ConfigurableFTPFileEntryParserImpl
42  extends RegexFTPFileEntryParserImpl
43  implements Configurable
44  {
45  
46      private final FTPTimestampParser timestampParser;
47  
48      /**
49       * constructor for this abstract class.
50       * @param regex  Regular expression used main parsing of the
51       * file listing.
52       */
53      public ConfigurableFTPFileEntryParserImpl(String regex)
54      {
55          super(regex);
56          this.timestampParser = new FTPTimestampParserImpl();
57      }
58  
59      /**
60       * constructor for this abstract class.
61       * @param regex  Regular expression used main parsing of the
62       * file listing.
63       * @param flags the flags to apply, see
64       * {@link java.util.regex.Pattern#compile(String, int) Pattern#compile(String, int)}. Use 0 for none.
65       * @since 3.4
66       */
67      public ConfigurableFTPFileEntryParserImpl(String regex, int flags)
68      {
69          super(regex, flags);
70          this.timestampParser = new FTPTimestampParserImpl();
71      }
72  
73      /**
74       * This method is called by the concrete parsers to delegate
75       * timestamp parsing to the timestamp parser.
76       *
77       * @param timestampStr the timestamp string pulled from the
78       * file listing by the regular expression parser, to be submitted
79       * to the <code>timestampParser</code> for extracting the timestamp.
80       * @return a <code>java.util.Calendar</code> containing results of the
81       * timestamp parse.
82       * @throws ParseException on parse error
83       */
84      public Calendar parseTimestamp(String timestampStr) throws ParseException {
85          return this.timestampParser.parseTimestamp(timestampStr);
86      }
87  
88  
89      /**
90       * Implementation of the {@link  Configurable  Configurable}
91       * interface. Configures this parser by delegating to the
92       * underlying Configurable FTPTimestampParser implementation, '
93       * passing it the supplied {@link  FTPClientConfig FTPClientConfig}
94       * if that is non-null or a default configuration defined by
95       * each concrete subclass.
96       *
97       * @param config the configuration to be used to configure this parser.
98       * If it is null, a default configuration defined by
99       * each concrete subclass is used instead.
100      */
101 //    @Override
102     public void configure(FTPClientConfig config)
103     {
104         if (this.timestampParser instanceof Configurable) {
105             FTPClientConfig defaultCfg = getDefaultConfiguration();
106             if (config != null) {
107                 if (null == config.getDefaultDateFormatStr()) {
108                     config.setDefaultDateFormatStr(defaultCfg.getDefaultDateFormatStr());
109                 }
110                 if (null == config.getRecentDateFormatStr()) {
111                     config.setRecentDateFormatStr(defaultCfg.getRecentDateFormatStr());
112                 }
113                 ((Configurable)this.timestampParser).configure(config);
114             } else {
115                 ((Configurable)this.timestampParser).configure(defaultCfg);
116             }
117         }
118     }
119 
120     /**
121      * Each concrete subclass must define this member to create
122      * a default configuration to be used when that subclass is
123      * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
124      * parameter being specified.
125      * @return the default configuration for the subclass.
126      */
127     protected abstract FTPClientConfig getDefaultConfiguration();
128 }