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  package org.apache.commons.flatfile;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.apache.commons.flatfile.util.FilterFactory;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Dynamic field with filtered input. This allows a user to set up a means by which an Entity containing a nested
27   * DynamicField can have a input read in without the DynamicField consuming everything. Instead its filterFactory helps
28   * to filter the incoming data for release at the proper point in the stream.
29   * TODO test
30   * @version $Revision: 1301248 $ $Date: 2012-03-15 17:20:49 -0500 (Thu, 15 Mar 2012) $
31   */
32  public class InputFilteringDynamicField extends DynamicField {
33      private static final long serialVersionUID = 5179298156801023038L;
34  
35      private FilterFactory filterFactory;
36  
37      /**
38       * Create a new InputFilteringDynamicField.
39       */
40      public InputFilteringDynamicField() {
41          super();
42      }
43  
44      /**
45       * Create a new InputFilteringDynamicField.
46       * @param filterFactory to filter input
47       */
48      public InputFilteringDynamicField(FilterFactory filterFactory) {
49          this();
50          setFilterFactory(filterFactory);
51      }
52  
53      /**
54       * Create a new InputFilteringDynamicField.
55       * @param bounds field bounds
56       */
57      public InputFilteringDynamicField(Bounds bounds) {
58          super(bounds);
59      }
60  
61      /**
62       * Create a new InputFilteringDynamicField.
63       * @param bounds field bounds
64       * @param filterFactory to filter input
65       */
66      public InputFilteringDynamicField(Bounds bounds, FilterFactory filterFactory) {
67          this(bounds);
68          setFilterFactory(filterFactory);
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public synchronized void readFrom(InputStream is) throws IOException {
75          Validate.validState(filterFactory != null, "filterFactory has not been set");
76          final InputStream filter = filterFactory.getFilter(is);
77  
78          Validate.validState(filter != null,
79              "Filter factory %s cannot filter %s", filterFactory, is);
80          super.readFrom(filter);
81      }
82  
83      /**
84       * Get the filterFactory.
85       * @return FilterFactory
86       */
87      public FilterFactory getFilterFactory() {
88          return filterFactory;
89      }
90  
91      /**
92       * Set the filterFactory.
93       * @param filterFactory the FilterFactory filterFactory to set
94       */
95      public void setFilterFactory(FilterFactory filterFactory) {
96          this.filterFactory = filterFactory;
97      }
98  }