1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._scripting;
29 
30 import java.io.File;
31 import java.io.FileReader;
32 import java.io.BufferedReader;
33 import java.io.IOException;
34 
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.StringTokenizer;
38 
39 import lib.TestEnvironment;
40 import lib.Parameters;
41 
42 public class TestDataLoader {
43 
44     private TestDataLoader() {
45     }
46 
47     public static void setupData(TestEnvironment tEnv, String name) {
48         String filename =
49             util.utils.getFullTestDocName("testdata");
50         File testdatadir = new File(filename);
51         File f = new File(testdatadir, name + ".csv");
52 
53         if (!f.exists())
54             return;
55 
56         BufferedReader in;
57 
58         try {
59             in = new BufferedReader(new FileReader(f));
60 
61             String s, previous, current;
62             ArrayList list = new ArrayList(11);
63 
64             if ((s = in.readLine()) != null) {
65                 StringTokenizer st = new StringTokenizer(s, ";");
66 
67                 current = previous = st.nextToken();
68                 list.add(getParameters(st));
69             }
70             else {
71                 return;
72             }
73 
74             while ((s = in.readLine()) != null) {
75                 StringTokenizer st = new StringTokenizer(s, ";");
76 
77                 current = st.nextToken();
78 
79                 if (!current.equals(previous)) {
80                     tEnv.addObjRelation(previous, list);
81                     previous = current;
82                     list = new ArrayList(11);
83                 }
84 
85                 list.add(getParameters(st));
86             }
87 
88             tEnv.addObjRelation(previous, list);
89         }
90         catch (IOException ioe) {
91         }
92     }
93 
94     private static Parameters getParameters(StringTokenizer st) {
95         String separator = "=";
96         HashMap map = new HashMap(5);
97 
98         while (st.hasMoreTokens()) {
99             String pair = st.nextToken();
100             StringTokenizer tokens = new StringTokenizer(pair, separator);
101 
102             String name;
103             String value;
104 
105             if (tokens.countTokens() < 2)
106                 continue;
107 
108             name = tokens.nextToken();
109             if (tokens.countTokens() == 1)
110                 value = tokens.nextToken();
111             else {
112                 StringBuffer buf = new StringBuffer(tokens.nextToken());
113                 while (tokens.hasMoreTokens())
114                     buf.append(separator).append(tokens.nextToken());
115                 value = buf.toString();
116             }
117 
118             map.put(name, value);
119         }
120 
121         return new Parameters(map);
122     }
123 }
124