01: /*
02: * Copyright 2004-2006 Fouad HAMDI with the idea
03: * of SameLAN, S.L. Soluciones Tecnológicas.
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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.csvbeans.samples.validators;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21:
22: import org.csvbeans.CSVBeansException;
23: import org.csvbeans.parsers.InputStreamLinesReader;
24: import org.csvbeans.parsers.ParsingStrategy;
25: import org.csvbeans.specs.SpecificationsFile;
26: import org.csvbeans.specs.SpecificationsFileParser;
27:
28: /**
29: * A sample for the validators.
30: *
31: * @author Fouad Hamdi
32: * @since 0.7
33: */
34: public class SampleMain {
35: public static void main(String[] args) throws Exception {
36: System.out.println("Starting sample...");
37: SpecificationsFileParser parser = new SpecificationsFileParser();
38: SpecificationsFile specs = parser
39: .parse(getInputStream("/mapping.xml"));
40: parseSample(specs);
41: System.out.println("Sample ended...");
42: }
43:
44: /**
45: * Return an input stream of a file in the classpath.
46: */
47: private static InputStream getInputStream(String name)
48: throws IOException {
49: return SampleMain.class.getResource(name).openStream();
50: }
51:
52: /**
53: * Parse the fixed length file.
54: */
55: private static void parseSample(SpecificationsFile specs)
56: throws Exception {
57: System.out.println("Beginning parsing...");
58: ParsingStrategy parser = specs.getParsingStrategy();
59: try {
60: parser.parse(new InputStreamLinesReader(
61: getInputStream("/fileToParse.txt")));
62: } catch (CSVBeansException e) {
63: System.out.println("An exception has been thrown: "
64: + e.getMessage());
65: }
66: System.out.println("End of parsing...");
67: }
68: }
|