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 package share;
28 
29 import java.io.BufferedReader;
30 import java.io.FileReader;
31 import java.util.ArrayList;
32 import java.util.StringTokenizer;
33 
34 import java.util.Vector;
35 
36 /**
37  *
38  * Base Interface to get a description for a given TestJob
39  *
40  */
41 public abstract class DescGetter
42 {
43 
44     public abstract DescEntry[] getDescriptionFor(String entry,
45             String DescPath,
46             boolean debug);
47 
48     protected abstract DescEntry getDescriptionForSingleJob(String job,
49             String descPath,
50             boolean debug);
51 
52     protected abstract String[] createScenario(String descPath, String job,
53             boolean debug);
54 
55     protected DescEntry[] getScenario(String url, String descPath,
56             boolean debug)
57     {
58         Vector entryList = new Vector();
59         String line = "";
60         BufferedReader scenario = null;
61         DescEntry[] entries = null;
62 
63         try
64         {
65             scenario = new BufferedReader(new FileReader(url));
66         }
67         catch (java.io.FileNotFoundException fnfe)
68         {
69             System.out.println("Couldn't find file " + url);
70 
71             return entries;
72         }
73 
74         while (line != null)
75         {
76             try
77             {
78                 if (line.startsWith("-o"))
79                 {
80                     String job = line.substring(3, line.length()).trim();
81                     DescEntry aEntry;
82                     // special in case several Interfaces are given comma separated
83                     if (job.indexOf(",") < 0)
84                     {
85                         aEntry = getDescriptionForSingleJob(job, descPath,
86                                 debug);
87                     }
88                     else
89                     {
90                         ArrayList subs = getSubInterfaces(job);
91                         String partjob = job.substring(0, job.indexOf(",")).trim();
92                         aEntry = getDescriptionForSingleJob(partjob, descPath,
93                                 debug);
94 
95                         if (aEntry != null)
96                         {
97                             for (int i = 0; i < aEntry.SubEntryCount; i++)
98                             {
99                                 String subEntry = aEntry.SubEntries[i].longName;
100                                 int cpLength = aEntry.longName.length();
101                                 subEntry = subEntry.substring(cpLength + 2,
102                                         subEntry.length());
103 
104                                 if (subs.contains(subEntry))
105                                 {
106                                     aEntry.SubEntries[i].isToTest = true;
107                                 }
108                             }
109                         }
110                     }
111 //                    DescEntry aEntry = getDescriptionForSingleJob(
112 //                                          line.substring(3).trim(), descPath,
113 //                                          debug);
114                     if (aEntry != null)
115                     {
116                         entryList.add(aEntry);
117                     }
118                 }
119                 else if (line.startsWith("-sce"))
120                 {
121                     DescEntry[] subs = getScenario(line.substring(5,
122                             line.length()).trim(), descPath,
123                             debug);
124 
125                     for (int i = 0; i < subs.length; i++)
126                     {
127                         entryList.add(subs[i]);
128                     }
129                 }
130                 else if (line.startsWith("-p"))
131                 {
132                     String[] perModule = createScenario(descPath,
133                             line.substring(3).trim(), debug);
134 
135                     for (int i = 0; i < perModule.length; i++)
136                     {
137                         DescEntry aEntry = getDescriptionForSingleJob(
138                                 perModule[i].substring(3).trim(),
139                                 descPath, debug);
140                         if (aEntry != null)
141                         {
142                             entryList.add(aEntry);
143                         }
144                     }
145                 }
146 
147                 line = scenario.readLine();
148             }
149             catch (java.io.IOException ioe)
150             {
151                 if (debug)
152                 {
153                     System.out.println("Exception while reading scenario");
154                 }
155             }
156         }
157 
158         try
159         {
160             scenario.close();
161         }
162         catch (java.io.IOException ioe)
163         {
164             if (debug)
165             {
166                 System.out.println("Exception while closeing scenario");
167             }
168         }
169 
170         if (entryList.size() == 0)
171         {
172             return null;
173         }
174         entries = new DescEntry[entryList.size()];
175         entries = (DescEntry[]) entryList.toArray(entries);
176 
177         return entries;
178     }
179 
180     protected ArrayList getSubInterfaces(String job)
181     {
182         ArrayList namesList = new ArrayList();
183         StringTokenizer st = new StringTokenizer(job, ",");
184 
185         for (int i = 0; st.hasMoreTokens(); i++)
186         {
187             String token = st.nextToken();
188 
189             if (token.indexOf(".") < 0)
190             {
191                 namesList.add(token);
192             }
193         }
194 
195         return namesList;
196     }
197 }