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.FileOutputStream;
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.util.Arrays;
23:
24: import org.csvbeans.builders.BuildingStrategy;
25: import org.csvbeans.builders.GenerationException;
26: import org.csvbeans.builders.OutputStreamLinesWriter;
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 SampleMain4 {
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: buildCSVFile(specs);
41: System.out
42: .println("Sample ended...Please take a look at the generated sample4.txt file");
43: }
44:
45: /**
46: * @param s
47: * @throws SpecificationsFileException
48: * @throws GenerationException
49: * @throws FileNotFoundException
50: */
51: private static void buildCSVFile(SpecificationsFile s)
52: throws SpecificationsFileException, GenerationException,
53: FileNotFoundException {
54: BuildingStrategy builder = s.getBuildingStrategy();
55: Section1Bean b1 = new Section1Bean();
56: b1.setColumn1(new Integer(10));
57: b1.setColumn2(new Integer(20));
58: SectionBean sb1 = new SectionBean();
59: sb1.setValue1("0");
60: sb1.setValue2("1");
61: b1.setSectionBean(sb1);
62: builder.addBeans("[Section 1]", Arrays
63: .asList(new Object[] { b1 }));
64: builder.build(new OutputStreamLinesWriter(new FileOutputStream(
65: "sample4.txt")));
66: }
67:
68: /**
69: * Return an input stream of a file in the classpath.
70: */
71: private static InputStream getInputStream(String name)
72: throws IOException {
73: return SampleMain4.class.getResource(name).openStream();
74: }
75: }
|