net.sf.uadetector.DeviceCategoryExamplesReader.java Source code

Java tutorial

Introduction

Here is the source code for net.sf.uadetector.DeviceCategoryExamplesReader.java

Source

/*******************************************************************************
 * Copyright 2013 Andr Roul
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.sf.uadetector;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.csv.CSVParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class DeviceCategoryExamplesReader {

    /**
     * Character set of the file
     */
    private static final String CHARSET = "UTF-8";

    /**
     * File to read
     */
    private static final String FILE = "examples/uaDEVICE_example.csv";

    /**
     * Default log
     */
    private static final Logger LOG = LoggerFactory.getLogger(DeviceCategoryExamplesReader.class);

    public static List<DeviceCategoryExample> read() {
        final InputStream stream = UserAgentStringParserIntegrationTest.class.getClassLoader()
                .getResourceAsStream(FILE);

        CSVParser csvParser = null;
        try {
            csvParser = new CSVParser(new InputStreamReader(stream, CHARSET));
        } catch (final UnsupportedEncodingException e) {
            LOG.warn(e.getLocalizedMessage(), e);
        }

        final List<DeviceCategoryExample> examples = new ArrayList<DeviceCategoryExample>(2000);
        if (csvParser != null) {
            String[] line = null;
            int i = 0;
            do {

                try {
                    line = csvParser.getLine();
                } catch (final IOException e) {
                    LOG.warn(e.getLocalizedMessage(), e);
                    line = null;
                }

                if (line != null) {
                    i++;
                    if (line.length == 2) {
                        examples.add(new DeviceCategoryExample(line[0], line[1]));
                    } else {
                        LOG.warn("Can not read device category example " + i + ", there are too few fields: "
                                + line.length);
                    }
                }
            } while (line != null);
        }
        return examples;
    }

}