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 SampleMain2 {
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-sample2.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.parse(new InputStreamLinesReader(
55: getInputStream("/sample2.txt")));
56: List beansTag1 = parser.getBeans("tag1");
57: for (Iterator it = beansTag1.iterator(); it.hasNext();) {
58: System.out.println(it.next());
59: }
60: List beansTag2 = parser.getBeans("tag2");
61: for (Iterator it = beansTag2.iterator(); it.hasNext();) {
62: System.out.println(it.next());
63: }
64: }
65:
66: /**
67: * Return an input stream of a file in the classpath.
68: */
69: private static InputStream getInputStream(String name)
70: throws IOException {
71: return SampleMain2.class.getResource(name).openStream();
72: }
73: }
|