01: /*
02: * Copyright 2004-2005 Fouad HAMDI.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.csvbeans.samples.strategy;
17:
18: import java.io.FileNotFoundException;
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: import org.csvbeans.parsers.InputStreamLinesReader;
25: import org.csvbeans.parsers.ParsingException;
26: import org.csvbeans.parsers.ParsingStrategy;
27: import org.csvbeans.specs.SpecificationsFile;
28: import org.csvbeans.specs.SpecificationsFileException;
29: import org.csvbeans.specs.SpecificationsFileParser;
30:
31: /**
32: * @author Fouad Hamdi
33: */
34: public class SampleMain1 {
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-sample1.xml"));
40: parseCSVFile(specs);
41: System.out.println("Sample ended...");
42: }
43:
44: /**
45: * @param s
46: * @throws SpecificationsFileException
47: * @throws ParsingException
48: * @throws FileNotFoundException
49: */
50: private static void parseCSVFile(SpecificationsFile s)
51: throws SpecificationsFileException, ParsingException,
52: IOException {
53: ParsingStrategy parser = s.getParsingStrategy();
54: parser.addProperty("continueParsingIfErrors", "false");
55: parser.parse(new InputStreamLinesReader(
56: getInputStream("/sample1.txt")));
57: List beansSection1 = parser.getBeans("[Section 1]");
58: for (Iterator it = beansSection1.iterator(); it.hasNext();) {
59: System.out.println(it.next());
60: }
61: List beansSection2 = parser.getBeans("[Section 2]");
62: for (Iterator it = beansSection2.iterator(); it.hasNext();) {
63: System.out.println(it.next());
64: }
65: List beansSection3 = parser.getBeans("[Section 3]");
66: for (Iterator it = beansSection3.iterator(); it.hasNext();) {
67: System.out.println(it.next());
68: }
69: }
70:
71: /**
72: * Return an input stream of a file in the classpath.
73: */
74: private static InputStream getInputStream(String name)
75: throws IOException {
76: return SampleMain1.class.getResource(name).openStream();
77: }
78: }
|