/*
* Copyright 2004-2006 Fouad HAMDI with the idea
* of SameLAN, S.L. Soluciones Tecnolgicas.
*
* 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 org.csvbeans.samples.validators;
import java.io.IOException;
import java.io.InputStream;
import org.csvbeans.CSVBeansException;
import org.csvbeans.parsers.InputStreamLinesReader;
import org.csvbeans.parsers.ParsingStrategy;
import org.csvbeans.specs.SpecificationsFile;
import org.csvbeans.specs.SpecificationsFileParser;
/**
* A sample for the validators.
*
* @author Fouad Hamdi
* @since 0.7
*/
public class ZipCodeSample {
public static void main(String[] args) throws Exception {
System.out.println("Starting sample...");
SpecificationsFileParser parser = new SpecificationsFileParser();
SpecificationsFile specs = parser.parse(getInputStream("/mappingZipCodes.xml"));
parseSample(specs);
System.out.println("Sample ended...");
}
/**
* Return an input stream of a file in the classpath.
*/
private static InputStream getInputStream(String name) throws IOException {
return ZipCodeSample.class.getResource(name).openStream();
}
/**
* Parse the fixed length file.
*/
private static void parseSample(SpecificationsFile specs) throws Exception {
System.out.println("Beginning parsing...");
ParsingStrategy parser = specs.getParsingStrategy();
try {
parser.parse(new InputStreamLinesReader(getInputStream("/fileToParseZipCodes.txt")));
} catch (CSVBeansException e) {
System.out.println("An exception has been thrown: " + e.getMessage());
}
System.out.println("End of parsing...");
}
}
|