1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package share;
19 
20 import java.io.BufferedReader;
21 import java.io.FileReader;
22 
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.StringTokenizer;
26 
27 /**
28  *
29  * Base Interface to get a description for a given TestJob
30  *
31  */
32 public abstract class DescGetter
33 {
34 
getDescriptionFor(String entry, String DescPath, boolean debug)35     public abstract DescEntry[] getDescriptionFor(String entry,
36             String DescPath,
37             boolean debug);
38 
getDescriptionForSingleJob(String job, String descPath, boolean debug)39     protected abstract DescEntry getDescriptionForSingleJob(String job,
40             String descPath,
41             boolean debug);
42 
createScenario(String descPath, String job, boolean debug)43     protected abstract String[] createScenario(String descPath, String job,
44             boolean debug);
45 
getScenario(String url, String descPath, boolean debug)46     protected DescEntry[] getScenario(String url, String descPath,
47             boolean debug)
48     {
49         ArrayList<DescEntry> entryList = new ArrayList<DescEntry>();
50         String line = "";
51         BufferedReader scenario = null;
52         DescEntry[] entries = null;
53 
54         try
55         {
56             scenario = new BufferedReader(new FileReader(url));
57         }
58         catch (java.io.FileNotFoundException fnfe)
59         {
60             System.out.println("Couldn't find file " + url);
61 
62             return entries;
63         }
64 
65         while (line != null)
66         {
67             try
68             {
69                 if (line.startsWith("-o"))
70                 {
71                     String job = line.substring(3, line.length()).trim();
72                     DescEntry aEntry;
73                     // special in case several Interfaces are given comma separated
74                     if (job.indexOf(',') < 0)
75                     {
76                         aEntry = getDescriptionForSingleJob(job, descPath,
77                                 debug);
78                     }
79                     else
80                     {
81                         ArrayList<String> subs = getSubInterfaces(job);
82                         String partjob = job.substring(0, job.indexOf(',')).trim();
83                         aEntry = getDescriptionForSingleJob(partjob, descPath,
84                                 debug);
85 
86                         if (aEntry != null)
87                         {
88                             for (int i = 0; i < aEntry.SubEntryCount; i++)
89                             {
90                                 String subEntry = aEntry.SubEntries[i].longName;
91                                 int cpLength = aEntry.longName.length();
92                                 subEntry = subEntry.substring(cpLength + 2,
93                                         subEntry.length());
94 
95                                 if (subs.contains(subEntry))
96                                 {
97                                     aEntry.SubEntries[i].isToTest = true;
98                                 }
99                             }
100                         }
101                     }
102                     if (aEntry != null)
103                     {
104                         entryList.add(aEntry);
105                     }
106                 }
107                 else if (line.startsWith("-sce"))
108                 {
109                     DescEntry[] subs = getScenario(line.substring(5,
110                             line.length()).trim(), descPath,
111                             debug);
112 
113                     entryList.addAll(Arrays.asList(subs));
114                 }
115                 else if (line.startsWith("-p"))
116                 {
117                     String[] perModule = createScenario(descPath,
118                             line.substring(3).trim(), debug);
119 
120                     for (int i = 0; i < perModule.length; i++)
121                     {
122                         DescEntry aEntry = getDescriptionForSingleJob(
123                                 perModule[i].substring(3).trim(),
124                                 descPath, debug);
125                         if (aEntry != null)
126                         {
127                             entryList.add(aEntry);
128                         }
129                     }
130                 }
131 
132                 line = scenario.readLine();
133             }
134             catch (java.io.IOException ioe)
135             {
136                 if (debug)
137                 {
138                     System.out.println("Exception while reading scenario");
139                 }
140             }
141         }
142 
143         try
144         {
145             scenario.close();
146         }
147         catch (java.io.IOException ioe)
148         {
149             if (debug)
150             {
151                 System.out.println("Exception while closing scenario");
152             }
153         }
154 
155         if (entryList.isEmpty())
156         {
157             return null;
158         }
159         entries = new DescEntry[entryList.size()];
160         entries = entryList.toArray(entries);
161 
162         return entries;
163     }
164 
getSubInterfaces(String job)165     protected ArrayList<String> getSubInterfaces(String job)
166     {
167         ArrayList<String> namesList = new ArrayList<String>();
168         StringTokenizer st = new StringTokenizer(job, ",");
169 
170         while (st.hasMoreTokens())
171         {
172             String token = st.nextToken();
173 
174             if (token.indexOf('.') < 0)
175             {
176                 namesList.add(token);
177             }
178         }
179 
180         return namesList;
181     }
182 }
183