001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.flatfile;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.commons.flatfile.util.FilterFactory;
023import org.apache.commons.lang3.Validate;
024
025/**
026 * Dynamic field with filtered input. This allows a user to set up a means by which an Entity containing a nested
027 * DynamicField can have a input read in without the DynamicField consuming everything. Instead its filterFactory helps
028 * to filter the incoming data for release at the proper point in the stream.
029 * TODO test
030 * @version $Revision: 1301248 $ $Date: 2012-03-15 17:20:49 -0500 (Thu, 15 Mar 2012) $
031 */
032public class InputFilteringDynamicField extends DynamicField {
033    private static final long serialVersionUID = 5179298156801023038L;
034
035    private FilterFactory filterFactory;
036
037    /**
038     * Create a new InputFilteringDynamicField.
039     */
040    public InputFilteringDynamicField() {
041        super();
042    }
043
044    /**
045     * Create a new InputFilteringDynamicField.
046     * @param filterFactory to filter input
047     */
048    public InputFilteringDynamicField(FilterFactory filterFactory) {
049        this();
050        setFilterFactory(filterFactory);
051    }
052
053    /**
054     * Create a new InputFilteringDynamicField.
055     * @param bounds field bounds
056     */
057    public InputFilteringDynamicField(Bounds bounds) {
058        super(bounds);
059    }
060
061    /**
062     * Create a new InputFilteringDynamicField.
063     * @param bounds field bounds
064     * @param filterFactory to filter input
065     */
066    public InputFilteringDynamicField(Bounds bounds, FilterFactory filterFactory) {
067        this(bounds);
068        setFilterFactory(filterFactory);
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    public synchronized void readFrom(InputStream is) throws IOException {
075        Validate.validState(filterFactory != null, "filterFactory has not been set");
076        final InputStream filter = filterFactory.getFilter(is);
077
078        Validate.validState(filter != null,
079            "Filter factory %s cannot filter %s", filterFactory, is);
080        super.readFrom(filter);
081    }
082
083    /**
084     * Get the filterFactory.
085     * @return FilterFactory
086     */
087    public FilterFactory getFilterFactory() {
088        return filterFactory;
089    }
090
091    /**
092     * Set the filterFactory.
093     * @param filterFactory the FilterFactory filterFactory to set
094     */
095    public void setFilterFactory(FilterFactory filterFactory) {
096        this.filterFactory = filterFactory;
097    }
098}