001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jwat.warc;
019
020import org.jwat.common.TreeOfIntMapper;
021
022/**
023 * WARC validator factory lookup service.
024 *
025 * @author nicl
026 */
027public class WarcValidatorFactories {
028
029    public static abstract class WarcValidatorFactory {
030        /**
031         * Returns a validator. The supplied version is the header it will be used on.
032         * @param arr integer array denoting a version
033         * @return a validator
034         */
035        public abstract WarcValidatorBase getValidatorFor(int[] arr);
036    }
037
038    /**
039     * WARC/1.0 validator factory.
040     */
041    public static class WarcValidator10Factory extends WarcValidatorFactory {
042        @Override
043        public WarcValidator10 getValidatorFor(int[] arr) {
044            return new WarcValidator10(arr);
045        }
046    }
047
048    /**
049     * WARC/1.1 validator factory.
050     */
051    public static class WarcValidator11Factory extends WarcValidatorFactory {
052        @Override
053        public WarcValidator11 getValidatorFor(int[] arr) {
054            return new WarcValidator11(arr);
055        }
056    }
057
058    /** Tree structure with validator factories store by their version denoted as an interger array */
059    protected TreeOfIntMapper<WarcValidatorFactory> treeOfValidatorFactories;
060
061    /**
062     * Construct a WARC validator factory lookup service.
063     */
064    public WarcValidatorFactories() {
065        treeOfValidatorFactories = new TreeOfIntMapper<>();
066        treeOfValidatorFactories.add(new WarcValidator10Factory(), new int[] {0, 17});
067        treeOfValidatorFactories.add(new WarcValidator10Factory(), new int[] {0, 18});
068        treeOfValidatorFactories.add(new WarcValidator10Factory(), new int[] {1, 0});
069        treeOfValidatorFactories.add(new WarcValidator11Factory(), new int[] {1, 1});
070    }
071
072    /**
073     * Returns a WARC validator factory to create a validator for a specific WARC version.
074     * @param arr int array denoting the WARC version to get a validator factory for
075     * @return a WARC validator factory
076     */
077    public WarcValidatorFactory get(int[] arr) {
078        WarcValidatorFactory vfac = treeOfValidatorFactories.lookup(arr);
079        return vfac;
080    }
081
082}