|
Lines 24-46
Link Here
|
| 24 |
import java.util.List; |
24 |
import java.util.List; |
| 25 |
import java.util.Map; |
25 |
import java.util.Map; |
| 26 |
|
26 |
|
| 27 |
import org.eclipse.core.resources.IResourceStatus; |
|
|
| 28 |
import org.eclipse.core.runtime.IStatus; |
| 29 |
import org.eclipse.core.runtime.Status; |
| 30 |
import org.eclipse.hyades.edit.datapool.IDatapool; |
27 |
import org.eclipse.hyades.edit.datapool.IDatapool; |
| 31 |
import org.eclipse.hyades.edit.datapool.IDatapoolCell; |
28 |
import org.eclipse.hyades.edit.datapool.IDatapoolCell; |
| 32 |
import org.eclipse.hyades.edit.datapool.IDatapoolEquivalenceClass; |
29 |
import org.eclipse.hyades.edit.datapool.IDatapoolEquivalenceClass; |
| 33 |
import org.eclipse.hyades.edit.datapool.IDatapoolRecord; |
30 |
import org.eclipse.hyades.edit.datapool.IDatapoolRecord; |
| 34 |
import org.eclipse.hyades.edit.datapool.IDatapoolSuggestedType; |
31 |
import org.eclipse.hyades.edit.datapool.IDatapoolSuggestedType; |
| 35 |
import org.eclipse.hyades.edit.datapool.IDatapoolVariable; |
32 |
import org.eclipse.hyades.edit.datapool.IDatapoolVariable; |
| 36 |
import org.eclipse.hyades.test.ui.UiPlugin; |
|
|
| 37 |
import org.eclipse.hyades.test.ui.datapool.util.DatapoolUtilities; |
33 |
import org.eclipse.hyades.test.ui.datapool.util.DatapoolUtilities; |
| 38 |
import org.eclipse.hyades.test.ui.internal.resources.UiPluginResourceBundle; |
34 |
import org.eclipse.hyades.test.ui.internal.resources.UiPluginResourceBundle; |
| 39 |
import org.eclipse.jface.dialogs.ErrorDialog; |
|
|
| 40 |
import org.eclipse.osgi.util.NLS; |
35 |
import org.eclipse.osgi.util.NLS; |
| 41 |
import org.eclipse.swt.widgets.Display; |
|
|
| 42 |
|
| 43 |
import com.ibm.icu.text.NumberFormat; |
| 44 |
|
36 |
|
| 45 |
/** |
37 |
/** |
| 46 |
* CSV import and export utilities. |
38 |
* CSV import and export utilities. |
|
Lines 49-56
Link Here
|
| 49 |
* @author Peter Sun |
41 |
* @author Peter Sun |
| 50 |
* @author Bianca Xue Jiang |
42 |
* @author Bianca Xue Jiang |
| 51 |
* @author Paul E. Slauenwhite |
43 |
* @author Paul E. Slauenwhite |
| 52 |
* @author jbozier |
44 |
* @author Jerome Bozier |
| 53 |
* @version October 7, 2008 |
45 |
* @version November 11, 2008 |
| 54 |
* @since January 27, 2005 |
46 |
* @since January 27, 2005 |
| 55 |
*/ |
47 |
*/ |
| 56 |
public class CSVImportExportUtil |
48 |
public class CSVImportExportUtil |
|
Lines 68-343
Link Here
|
| 68 |
|
60 |
|
| 69 |
private static final CSVImportExportUtil instance = new CSVImportExportUtil(); |
61 |
private static final CSVImportExportUtil instance = new CSVImportExportUtil(); |
| 70 |
|
62 |
|
| 71 |
private static long lineCounter = 1; |
|
|
| 72 |
|
| 73 |
public static CSVImportExportUtil getInstance() |
63 |
public static CSVImportExportUtil getInstance() |
| 74 |
{ |
64 |
{ |
| 75 |
return instance; |
65 |
return instance; |
| 76 |
} |
66 |
} |
| 77 |
|
67 |
|
| 78 |
protected CSVImportExportUtil() |
68 |
/** |
| 79 |
{ |
69 |
* Private constructor. |
| 80 |
} |
70 |
*/ |
| 81 |
|
71 |
private CSVImportExportUtil(){ |
| 82 |
public boolean validateCSVFile(String fullyQualifiedCSVName, boolean isFirstRowVariableInfo, boolean isFirstColECInfo, String encoding) throws FileNotFoundException, IOException, CorruptCSVFileException { |
72 |
//No-operation. |
|
|
73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* <p>Validates a Comma Separated Values (CSV) file containing datapool records.</p> |
| 77 |
* |
| 78 |
* <p>Validation is required when importing a CSV file into a new or existing datapool.</p> |
| 79 |
* |
| 80 |
* @param csvFilePath The absolute path of the CSV file. |
| 81 |
* @param isFirstRowVariableInfo If the first row of the CSV file contains the variable information (name/type), otherwise <code>false</code>. |
| 82 |
* @param isFirstColumnEquivalenceClassInfo If the first column of the CSV file contains the equivalence class information (name/row index), otherwise <code>false</code>. |
| 83 |
* @param encoding The encoding of the CSV file, otherwise <code>null</null>. |
| 84 |
* @return <code>true</code> if the CSV file is valid, otherwise <code>false</code>. |
| 85 |
* @throws FileNotFoundException The CSV file could not be found. |
| 86 |
* @throws IOException An error occurs while reading the CSV file. |
| 87 |
* @throws CorruptCSVFileException The CSV is invalid. |
| 88 |
* @see #importCSV(IDatapool, String, boolean, boolean, String) |
| 89 |
* @see #importCSV(IDatapool, String, boolean, boolean, String, int, int) |
| 90 |
* @see #appendFromCSV(IDatapool, String, boolean, boolean, String) |
| 91 |
*/ |
| 92 |
public boolean validateCSVFile(String csvFilePath, |
| 93 |
boolean isFirstRowVariableInfo, |
| 94 |
boolean isFirstColumnEquivalenceClassInfo, |
| 95 |
String encoding) throws FileNotFoundException, IOException, CorruptCSVFileException { |
| 96 |
return (validateCSVFileWithDatapool(csvFilePath, null, isFirstRowVariableInfo, isFirstColumnEquivalenceClassInfo, encoding)); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* <p>Validates a Comma Separated Values (CSV) file containing datapool records against a datapool.</p> |
| 101 |
* |
| 102 |
* <p>Validation is required when importing a CSV file into a new or existing datapool.</p> |
| 103 |
* |
| 104 |
* @param csvFilePath The absolute path of the CSV file. |
| 105 |
* @param datapool The datapool that the CSV file is validated against, otherwise <code>null</null>. |
| 106 |
* @param isFirstRowVariableInfo If the first row of the CSV file contains the variable information (name/type), otherwise <code>false</code>. |
| 107 |
* @param isFirstColumnEquivalenceClassInfo If the first column of the CSV file contains the equivalence class information (name/row index), otherwise <code>false</code>. |
| 108 |
* @param encoding The encoding of the CSV file, otherwise <code>null</null>. |
| 109 |
* @return <code>true</code> if the CSV file is valid, otherwise <code>false</code>. |
| 110 |
* @throws FileNotFoundException The CSV file could not be found. |
| 111 |
* @throws IOException An error occurs while reading the CSV file. |
| 112 |
* @throws CorruptCSVFileException The CSV is invalid. |
| 113 |
* @see #importCSV(IDatapool, String, boolean, boolean, String) |
| 114 |
* @see #importCSV(IDatapool, String, boolean, boolean, String, int, int) |
| 115 |
* @see #appendFromCSV(IDatapool, String, boolean, boolean, String) |
| 116 |
*/ |
| 117 |
public boolean validateCSVFileWithDatapool(String csvFilePath, |
| 118 |
IDatapool datapool, |
| 119 |
boolean isFirstRowVariableInfo, |
| 120 |
boolean isFirstColumnEquivalenceClassInfo, |
| 121 |
String encoding) throws FileNotFoundException, IOException, CorruptCSVFileException { |
| 83 |
|
122 |
|
|
|
123 |
//Determine if the datapool should be validated : |
| 124 |
boolean validateDatapool = ((datapool != null) && (datapool.getVariableCount() > 0) && (!isDefaultEmptyDatapool(datapool))); |
| 125 |
|
| 84 |
//Resolve the input reader: |
126 |
//Resolve the input reader: |
| 85 |
BufferedReader lineReader = null; |
127 |
BufferedReader lineReader = null; |
| 86 |
|
128 |
|
| 87 |
if((encoding == null) || (encoding.trim().length() == 0)) { |
129 |
try { |
| 88 |
lineReader = new BufferedReader(new InputStreamReader(new FileInputStream(fullyQualifiedCSVName))); |
|
|
| 89 |
} |
| 90 |
else { |
| 91 |
lineReader = new BufferedReader(new InputStreamReader(new FileInputStream(fullyQualifiedCSVName), encoding)); |
| 92 |
} |
| 93 |
|
| 94 |
//Read the first line: |
| 95 |
String line = lineReader.readLine(); |
| 96 |
int lineCount = 1; |
| 97 |
int variableCount = -1; |
| 98 |
|
| 99 |
//Check if the first line is empty: |
| 100 |
if((line == null) || (line.trim().length() == 0)){ |
| 101 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount),UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_CVS_LINE})); |
| 102 |
} |
| 103 |
|
| 104 |
//Validate the variable names and optional suggested types in the first row: |
| 105 |
if(isFirstRowVariableInfo){ |
| 106 |
|
| 107 |
//Parse the first row containing the variable names and optional suggested types: |
| 108 |
CSVTokenizer tokenizer = new CSVTokenizer(line); |
| 109 |
|
130 |
|
| 110 |
//Check if the first column is the equivalence class cell (e.g. empty value): |
131 |
if((encoding == null) || (encoding.trim().length() == 0)) { |
| 111 |
if((isFirstColECInfo) && ((!tokenizer.hasMoreTokens()) || (tokenizer.nextToken().trim().length() > 0))){ |
132 |
lineReader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFilePath))); |
| 112 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount),UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_EQUIVALENCE})); |
133 |
} |
| 113 |
} |
134 |
else { |
|
|
135 |
lineReader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFilePath), encoding)); |
| 136 |
} |
| 114 |
|
137 |
|
| 115 |
List variableNames = new ArrayList(); |
138 |
//Read the first line: |
|
|
139 |
String line = lineReader.readLine(); |
| 140 |
int lineCount = 1; |
| 141 |
int variableCount = -1; |
| 116 |
|
142 |
|
| 117 |
//Iterate the columns containing the variable names and optional suggested types: |
143 |
if(validateDatapool){ |
| 118 |
while(tokenizer.hasMoreTokens()){ |
144 |
variableCount = datapool.getVariableCount(); |
| 119 |
|
|
|
| 120 |
//Resolve the variable name: |
| 121 |
String suggestedType = null; |
| 122 |
String variableName = tokenizer.nextToken().trim(); |
| 123 |
int twoColonsIndex = variableName.indexOf(CSVTokenizer.TWO_COLONS); |
| 124 |
|
| 125 |
//Resolve the optional suggested type within the variable name: |
| 126 |
if(twoColonsIndex != -1){ |
| 127 |
|
| 128 |
suggestedType = variableName.substring(twoColonsIndex + CSVTokenizer.TWO_COLONS.length()).trim(); |
| 129 |
variableName = variableName.substring(0, twoColonsIndex).trim(); |
| 130 |
} |
| 131 |
|
| 132 |
//Validate the variable name: |
| 133 |
if(variableName.length() == 0){ |
| 134 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_VARIABLE})); |
| 135 |
} |
| 136 |
else if(!DatapoolUtilities.getInstance().isVariableNameValid(variableName)){ |
| 137 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_NAME_NOT_VALID})); |
| 138 |
} |
| 139 |
else if(variableNames.contains(variableName)){ |
| 140 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_NAME_NOT_UNIQUE})); |
| 141 |
} |
| 142 |
else{ |
| 143 |
variableNames.add(variableName); |
| 144 |
} |
| 145 |
|
| 146 |
//Validate the optional suggested type: |
| 147 |
if((suggestedType != null) && (!DatapoolUtilities.getInstance().isVariableTypeValid(suggestedType))){ |
| 148 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_TYPE_NOT_VALID})); |
| 149 |
} |
| 150 |
} |
145 |
} |
| 151 |
|
146 |
|
| 152 |
variableCount = variableNames.size(); |
147 |
//Check if the first line is empty: |
| 153 |
|
148 |
if((line == null) || (line.trim().length() == 0)){ |
| 154 |
//Check if any variables were validated: |
149 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount),UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_CVS_LINE})); |
| 155 |
if(variableCount == 0){ |
|
|
| 156 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount),UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_VARIABLE})); |
| 157 |
} |
150 |
} |
|
|
151 |
|
| 152 |
//Validate the variable names and optional suggested types in the first row: |
| 153 |
if(isFirstRowVariableInfo){ |
| 158 |
|
154 |
|
| 159 |
line = lineReader.readLine(); |
155 |
//Parse the first row containing the variable names and optional suggested types: |
| 160 |
lineCount++; |
|
|
| 161 |
} |
| 162 |
|
| 163 |
Map equivalenceClassNames = new HashMap(); |
| 164 |
String defaultEquivalenceClassName = null; |
| 165 |
int emptyLineCount = 0; |
| 166 |
|
| 167 |
//Validate the remaining lines: |
| 168 |
while(line != null){ |
| 169 |
|
| 170 |
//Validate the next line that is not empty: |
| 171 |
if(line.trim().length() > 0){ |
| 172 |
|
| 173 |
//Parse the row containing the remaining columns: |
| 174 |
CSVTokenizer tokenizer = new CSVTokenizer(line); |
156 |
CSVTokenizer tokenizer = new CSVTokenizer(line); |
| 175 |
|
157 |
|
| 176 |
//Validate the equivalence class names and optional row numbers in the first column: |
158 |
//Check if the first column is the equivalence class cell (e.g. empty value): |
| 177 |
if(isFirstColECInfo){ |
159 |
if((isFirstColumnEquivalenceClassInfo) && ((!tokenizer.hasMoreTokens()) || (tokenizer.nextToken().trim().length() > 0))){ |
| 178 |
|
160 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount),UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_EQUIVALENCE})); |
| 179 |
//Resolve the equivalence class name: |
161 |
} |
| 180 |
String rowNumber = null; |
162 |
|
| 181 |
String equivalenceClassName = tokenizer.nextToken().trim(); |
163 |
List variableNames = new ArrayList(); |
| 182 |
int twoColonsIndex = equivalenceClassName.indexOf(CSVTokenizer.TWO_COLONS); |
164 |
|
| 183 |
|
165 |
//Iterate the columns containing the variable names and optional suggested types: |
| 184 |
//Resolve the optional row number within the equivalence class name: |
166 |
while(tokenizer.hasMoreTokens()){ |
|
|
167 |
|
| 168 |
//Resolve the variable name: |
| 169 |
String suggestedType = null; |
| 170 |
String variableName = tokenizer.nextToken().trim(); |
| 171 |
int twoColonsIndex = variableName.indexOf(CSVTokenizer.TWO_COLONS); |
| 172 |
|
| 173 |
//Resolve the optional suggested type within the variable name: |
| 185 |
if(twoColonsIndex != -1){ |
174 |
if(twoColonsIndex != -1){ |
| 186 |
|
175 |
|
| 187 |
rowNumber = equivalenceClassName.substring(twoColonsIndex + CSVTokenizer.TWO_COLONS.length()).trim(); |
176 |
suggestedType = variableName.substring(twoColonsIndex + CSVTokenizer.TWO_COLONS.length()).trim(); |
| 188 |
equivalenceClassName = equivalenceClassName.substring(0, twoColonsIndex).trim(); |
177 |
variableName = variableName.substring(0, twoColonsIndex).trim(); |
| 189 |
} |
|
|
| 190 |
|
| 191 |
//Validate the equivalence class name: |
| 192 |
if(equivalenceClassName.length() == 0){ |
| 193 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_EQUIVALENCE})); |
| 194 |
} |
178 |
} |
| 195 |
else if(!DatapoolUtilities.getInstance().isEquivalenceClassNameValid(equivalenceClassName)){ |
179 |
|
| 196 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_ROW_GRP_DLG_ERROR_NAME_NOT_VALID})); |
180 |
//Validate the variable name: |
|
|
181 |
if(variableName.length() == 0){ |
| 182 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_VARIABLE})); |
| 197 |
} |
183 |
} |
| 198 |
else if(equivalenceClassNames.containsKey(equivalenceClassName)){ |
184 |
else if(!DatapoolUtilities.getInstance().isVariableNameValid(variableName)){ |
| 199 |
equivalenceClassNames.put(equivalenceClassName, new Integer(((Integer)(equivalenceClassNames.get(equivalenceClassName))).intValue() + 1)); |
185 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_NAME_NOT_VALID})); |
|
|
186 |
} |
| 187 |
else if(variableNames.contains(variableName)){ |
| 188 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_NAME_NOT_UNIQUE})); |
| 200 |
} |
189 |
} |
| 201 |
else{ |
190 |
else{ |
| 202 |
|
191 |
variableNames.add(variableName); |
| 203 |
equivalenceClassNames.put(equivalenceClassName, new Integer(0)); |
|
|
| 204 |
|
| 205 |
if(defaultEquivalenceClassName == null){ |
| 206 |
defaultEquivalenceClassName = equivalenceClassName; |
| 207 |
} |
| 208 |
} |
192 |
} |
| 209 |
|
193 |
|
| 210 |
//Count the empty lines as empty rows for the default equivalence class: |
194 |
//Validate the optional suggested type: |
| 211 |
if(emptyLineCount > 0){ |
195 |
if((suggestedType != null) && (!DatapoolUtilities.getInstance().isVariableTypeValid(suggestedType))){ |
| 212 |
|
196 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_TYPE_NOT_VALID})); |
| 213 |
equivalenceClassNames.put(defaultEquivalenceClassName, new Integer(((Integer)(equivalenceClassNames.get(defaultEquivalenceClassName))).intValue() + emptyLineCount)); |
|
|
| 214 |
|
| 215 |
emptyLineCount = 0; |
| 216 |
} |
197 |
} |
| 217 |
|
198 |
|
| 218 |
//Validate the optional row number: |
199 |
//Validate the variable name and optional suggested type against the datapool: |
| 219 |
if(rowNumber != null){ |
200 |
if(validateDatapool){ |
| 220 |
|
201 |
|
| 221 |
try { |
202 |
try { |
| 222 |
|
203 |
|
| 223 |
if(Integer.parseInt(rowNumber) != (((Integer)(equivalenceClassNames.get(equivalenceClassName))).intValue())){ |
204 |
org.eclipse.hyades.execution.runtime.datapool.IDatapoolVariable variable = datapool.getVariable(variableNames.size() - 1); |
| 224 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), NLS.bind(UiPluginResourceBundle.DATA_COL_DLG_ERROR_INCONSISTENT_ROW_ORDER, rowNumber)})); |
205 |
|
| 225 |
} |
206 |
if((!variableName.equals(variable.getName())) || ((suggestedType != null) && (!TypeChecker.getInstance().isTypeMatch(suggestedType, variable.getSuggestedType())))){ |
|
|
207 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DatapoolImportWizard_diffVariableInfoWarning})); |
| 208 |
} |
| 226 |
} |
209 |
} |
| 227 |
catch (NumberFormatException n) { |
210 |
catch (Exception e) { |
| 228 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), NLS.bind(UiPluginResourceBundle.DATA_COL_DLG_ERROR_INVALID_ROW_COUNT, rowNumber)})); |
211 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DatapoolImportWizard_diffVariableInfoWarning})); |
| 229 |
} |
212 |
} |
| 230 |
} |
213 |
} |
| 231 |
} |
214 |
} |
| 232 |
|
215 |
|
| 233 |
//Validate the number of remaining columns: |
216 |
int columnCount = variableNames.size(); |
| 234 |
int columnCount = 0; |
217 |
|
| 235 |
|
218 |
//Check if any variables were validated: |
| 236 |
while(tokenizer.hasMoreTokens()){ |
219 |
if(columnCount == 0){ |
| 237 |
|
220 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount),UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_VARIABLE})); |
| 238 |
columnCount++; |
|
|
| 239 |
|
| 240 |
tokenizer.nextToken(); |
| 241 |
} |
221 |
} |
| 242 |
|
222 |
|
| 243 |
if(variableCount == -1){ |
223 |
if(variableCount == -1){ |
| 244 |
variableCount = columnCount; |
224 |
variableCount = columnCount; |
| 245 |
} |
225 |
} |
| 246 |
else if(columnCount != variableCount){ |
226 |
else if(columnCount != variableCount){ |
| 247 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), NLS.bind(UiPluginResourceBundle.DATA_COL_DLG_ERROR_INCONSISTENT_COLUMN_COUNT, String.valueOf(columnCount))})); |
227 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), NLS.bind(UiPluginResourceBundle.DATA_COL_DLG_ERROR_INCONSISTENT_COLUMN_COUNT, String.valueOf(columnCount))})); |
| 248 |
} |
228 |
} |
|
|
229 |
|
| 230 |
line = lineReader.readLine(); |
| 231 |
lineCount++; |
| 249 |
} |
232 |
} |
| 250 |
else{ |
233 |
|
| 251 |
emptyLineCount++; |
234 |
Map equivalenceClassNames = new HashMap(); |
| 252 |
} |
235 |
String defaultEquivalenceClassName = null; |
| 253 |
|
236 |
int emptyLineCount = 0; |
| 254 |
line = lineReader.readLine(); |
237 |
|
| 255 |
lineCount++; |
238 |
//Validate the remaining lines: |
| 256 |
} |
239 |
while(line != null){ |
|
|
240 |
|
| 241 |
//Validate the next line that is not empty: |
| 242 |
if(line.trim().length() > 0){ |
| 243 |
|
| 244 |
//Parse the row containing the remaining columns: |
| 245 |
CSVTokenizer tokenizer = new CSVTokenizer(line); |
| 246 |
|
| 247 |
//Validate the equivalence class names and optional row numbers in the first column: |
| 248 |
if(isFirstColumnEquivalenceClassInfo){ |
| 249 |
|
| 250 |
//Resolve the equivalence class name: |
| 251 |
String rowNumber = null; |
| 252 |
String equivalenceClassName = tokenizer.nextToken().trim(); |
| 253 |
int twoColonsIndex = equivalenceClassName.indexOf(CSVTokenizer.TWO_COLONS); |
| 254 |
|
| 255 |
//Resolve the optional row number within the equivalence class name: |
| 256 |
if(twoColonsIndex != -1){ |
| 257 |
|
| 258 |
rowNumber = equivalenceClassName.substring(twoColonsIndex + CSVTokenizer.TWO_COLONS.length()).trim(); |
| 259 |
equivalenceClassName = equivalenceClassName.substring(0, twoColonsIndex).trim(); |
| 260 |
} |
| 261 |
|
| 262 |
//Validate the equivalence class name: |
| 263 |
if(equivalenceClassName.length() == 0){ |
| 264 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_COL_DLG_ERROR_MISSING_EQUIVALENCE})); |
| 265 |
} |
| 266 |
else if(!DatapoolUtilities.getInstance().isEquivalenceClassNameValid(equivalenceClassName)){ |
| 267 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), UiPluginResourceBundle.DATA_ROW_GRP_DLG_ERROR_NAME_NOT_VALID})); |
| 268 |
} |
| 269 |
else if(equivalenceClassNames.containsKey(equivalenceClassName)){ |
| 270 |
equivalenceClassNames.put(equivalenceClassName, new Integer(((Integer)(equivalenceClassNames.get(equivalenceClassName))).intValue() + 1)); |
| 271 |
} |
| 272 |
else{ |
| 257 |
|
273 |
|
| 258 |
//Close the input reader: |
274 |
int recordCount = 0; |
| 259 |
try { |
275 |
|
| 260 |
lineReader.close(); |
276 |
//Resolve the record count from the equivalence class: |
| 261 |
} |
277 |
if(validateDatapool){ |
| 262 |
catch (IOException i) { |
278 |
|
| 263 |
//Ignore since closing input reader. |
279 |
org.eclipse.hyades.execution.runtime.datapool.IDatapoolEquivalenceClass equivalenceClass = datapool.getEquivalenceClass(datapool.getEquivalenceClassIndex(equivalenceClassName)); |
| 264 |
} |
280 |
|
| 265 |
|
281 |
if(equivalenceClass != null){ |
| 266 |
return true; |
282 |
recordCount = equivalenceClass.getRecordCount(); |
| 267 |
} |
283 |
} |
|
|
284 |
} |
| 285 |
|
| 286 |
equivalenceClassNames.put(equivalenceClassName, new Integer(recordCount)); |
| 287 |
|
| 288 |
if(defaultEquivalenceClassName == null){ |
| 289 |
defaultEquivalenceClassName = equivalenceClassName; |
| 290 |
} |
| 291 |
} |
| 268 |
|
292 |
|
| 269 |
public boolean validateCSVFileWithDatapool(String fullyQualifiedCSVName, IDatapool datapool, boolean isFirstRowVariableInfo, boolean isFirstColECInfo, String encoding) |
293 |
//Count the empty lines as empty rows for the default equivalence class: |
| 270 |
throws FileNotFoundException, IOException, CorruptCSVFileException |
294 |
if(emptyLineCount > 0){ |
| 271 |
{ |
295 |
|
| 272 |
if(!validateCSVFile(fullyQualifiedCSVName, isFirstRowVariableInfo, isFirstColECInfo, encoding)) |
296 |
equivalenceClassNames.put(defaultEquivalenceClassName, new Integer(((Integer)(equivalenceClassNames.get(defaultEquivalenceClassName))).intValue() + emptyLineCount)); |
| 273 |
return false; |
297 |
|
| 274 |
|
298 |
emptyLineCount = 0; |
| 275 |
// datapool without columns or empty is always good. |
299 |
} |
| 276 |
if(datapool.getVariableCount() < 1) |
300 |
|
| 277 |
return true; |
301 |
//Validate the optional row number: |
| 278 |
|
302 |
if(rowNumber != null){ |
| 279 |
// secondly, if there's only one default cell(created by new datapool wizard for easy data entry) |
303 |
|
| 280 |
// and no value in cell, treat as empty datapool |
304 |
try { |
| 281 |
// TODO replace when add function to append to existing columns |
305 |
|
| 282 |
if(isDefaultEmptyDatapool(datapool)) |
306 |
int recordIndex = Integer.parseInt(rowNumber); |
| 283 |
return true; |
307 |
|
| 284 |
|
308 |
if(recordIndex > (((Integer)(equivalenceClassNames.get(equivalenceClassName))).intValue())){ |
| 285 |
InputStreamReader input; |
309 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), NLS.bind(UiPluginResourceBundle.DATA_COL_DLG_ERROR_INCONSISTENT_ROW_ORDER, rowNumber)})); |
| 286 |
if(encoding == null || encoding.length() == 0) { |
310 |
} |
| 287 |
input = new InputStreamReader(new FileInputStream(fullyQualifiedCSVName)); |
311 |
else if(validateDatapool){ |
| 288 |
} else { |
312 |
|
| 289 |
input = new InputStreamReader(new FileInputStream(fullyQualifiedCSVName), encoding); |
313 |
org.eclipse.hyades.execution.runtime.datapool.IDatapoolEquivalenceClass equivalenceClass = datapool.getEquivalenceClass(datapool.getEquivalenceClassIndex(equivalenceClassName)); |
| 290 |
} |
314 |
|
| 291 |
BufferedReader reader = new BufferedReader(input); |
315 |
if((equivalenceClass != null) && (recordIndex < equivalenceClass.getRecordCount()) && (equivalenceClass.getRecord(recordIndex) != null)){ |
| 292 |
String firstLine = reader.readLine(); |
316 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[] {String.valueOf(lineCount), UiPluginResourceBundle.DATA_CSV_ERROR_RECORD_INDEX_DUP})); |
| 293 |
reader.close(); |
317 |
} |
| 294 |
CSVTokenizer tokenizer = new CSVTokenizer(firstLine); |
318 |
} |
| 295 |
List tokens = new ArrayList(); |
319 |
} |
| 296 |
if(isFirstColECInfo && tokenizer.hasMoreTokens()) |
320 |
catch (NumberFormatException n) { |
| 297 |
tokenizer.nextToken(); //ignore the first token |
321 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), NLS.bind(UiPluginResourceBundle.DATA_COL_DLG_ERROR_INVALID_ROW_COUNT, rowNumber)})); |
| 298 |
while(tokenizer.hasMoreTokens()) { |
322 |
} |
| 299 |
String nextToken = tokenizer.nextToken(); |
323 |
} |
| 300 |
//- #138616 the first variable is always empty (due to column arrangment), we need to skip it. |
324 |
} |
| 301 |
if(nextToken.length() != 0) { |
325 |
|
| 302 |
tokens.add(nextToken); |
326 |
//Validate the number of remaining columns: |
| 303 |
} |
327 |
int columnCount = 0; |
| 304 |
} |
328 |
|
| 305 |
|
329 |
while(tokenizer.hasMoreTokens()){ |
| 306 |
// compare number of columns |
330 |
|
| 307 |
if(datapool.getVariableCount() != tokens.size()) |
331 |
columnCount++; |
| 308 |
return false; |
332 |
|
| 309 |
|
333 |
tokenizer.nextToken(); |
| 310 |
if(!isFirstRowVariableInfo) |
334 |
} |
| 311 |
return true; |
335 |
|
| 312 |
|
336 |
if(variableCount == -1){ |
| 313 |
// same number of variables and tokens, compare name and type. |
337 |
variableCount = columnCount; |
| 314 |
for(int i = 0; i < tokens.size(); i++) |
338 |
} |
| 315 |
{ |
339 |
else if(columnCount != variableCount){ |
| 316 |
String nameTypePair = (String)tokens.get(i); |
340 |
throw new CorruptCSVFileException(NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, new String[]{String.valueOf(lineCount), NLS.bind(UiPluginResourceBundle.DATA_COL_DLG_ERROR_INCONSISTENT_COLUMN_COUNT, String.valueOf(columnCount))})); |
| 317 |
int separatorIndex = nameTypePair.indexOf(CSVTokenizer.TWO_COLONS); |
341 |
} |
| 318 |
String name = new String(); |
342 |
} |
| 319 |
String type = new String(); |
343 |
else{ |
| 320 |
if(separatorIndex == -1 ) |
344 |
emptyLineCount++; |
| 321 |
{ |
345 |
} |
| 322 |
name = nameTypePair; |
346 |
|
|
|
347 |
line = lineReader.readLine(); |
| 348 |
lineCount++; |
| 323 |
} |
349 |
} |
| 324 |
else |
350 |
|
| 325 |
{ |
351 |
return true; |
| 326 |
name = nameTypePair.substring(0, separatorIndex); |
352 |
} |
| 327 |
type = nameTypePair.substring(separatorIndex + 2); |
353 |
finally{ |
|
|
354 |
|
| 355 |
//Close the input reader: |
| 356 |
try { |
| 357 |
lineReader.close(); |
| 358 |
} |
| 359 |
catch (IOException i) { |
| 360 |
//Ignore since closing input reader. |
| 328 |
} |
361 |
} |
| 329 |
|
|
|
| 330 |
org.eclipse.hyades.execution.runtime.datapool.IDatapoolVariable variable = datapool.getVariable(i); |
| 331 |
if(!name.equals(variable.getName())) |
| 332 |
return false; |
| 333 |
|
| 334 |
if(type.length() > 0 && !TypeChecker.getInstance().isTypeMatch(type, variable.getSuggestedType())) |
| 335 |
return false; |
| 336 |
} |
362 |
} |
| 337 |
|
|
|
| 338 |
return true; |
| 339 |
} |
363 |
} |
| 340 |
|
364 |
|
|
|
365 |
/** |
| 366 |
* <p>Determines if a datapool is empty.</p> |
| 367 |
* |
| 368 |
* <p>Empty datapools have exactly one of the following:</p> |
| 369 |
* |
| 370 |
* <ul> |
| 371 |
* <li>equivalence class</li> |
| 372 |
* <li>variable</li> |
| 373 |
* <li>record</li> |
| 374 |
* <li>cell</li> |
| 375 |
* </ul> |
| 376 |
* |
| 377 |
* <p>In addition, the cell has no associated value.</p> |
| 378 |
* |
| 379 |
* @param datapool The datapool to be evaluated. |
| 380 |
* @return <code>true</code> if the datapool is empty, otherwise <code>false</code>. |
| 381 |
*/ |
| 341 |
public boolean isDefaultEmptyDatapool(IDatapool datapool) |
382 |
public boolean isDefaultEmptyDatapool(IDatapool datapool) |
| 342 |
{ |
383 |
{ |
| 343 |
if(datapool.getVariableCount() == 1 && datapool.getEquivalenceClassCount() == 1) |
384 |
if(datapool.getVariableCount() == 1 && datapool.getEquivalenceClassCount() == 1) |
|
Lines 353-516
Link Here
|
| 353 |
return false; |
394 |
return false; |
| 354 |
} |
395 |
} |
| 355 |
|
396 |
|
|
|
397 |
/** |
| 398 |
* <p>Imports a Comma Separated Values (CSV) file into a datapool.</p> |
| 399 |
* |
| 400 |
* <p><b>Note</b>: The CSV file is assumed to be valid. To validate the CSV file |
| 401 |
* with the datapool, call the {@link #validateCSVFileWithDatapool(String, IDatapool, boolean, boolean, String)} |
| 402 |
* method.</p> |
| 403 |
* |
| 404 |
* @param datapool The datapool that the CSV file is imported into. |
| 405 |
* @param csvFilePath The absolute path of the CSV file. |
| 406 |
* @param isFirstRowVariableInfo If the first row of the CSV file contains the variable information (name/type), otherwise <code>false</code>. |
| 407 |
* @param isFirstColumnEquivalenceClassInfo If the first column of the CSV file contains the equivalence class information (name/row index), otherwise <code>false</code>. |
| 408 |
* @param encoding The encoding of the CSV file, otherwise <code>null</null>. |
| 409 |
* @throws IOException An error occurs while reading the CSV file. |
| 410 |
* @see #validateCSVFileWithDatapool(String, IDatapool, boolean, boolean, String) |
| 411 |
*/ |
| 356 |
public void importCSV(IDatapool datapool, |
412 |
public void importCSV(IDatapool datapool, |
| 357 |
String csvFileName, |
413 |
String csvFileName, |
| 358 |
boolean isFirstRowVariableNameType, |
414 |
boolean isFirstRowVariableInfo, |
| 359 |
boolean isFirstColEqClsName, |
415 |
boolean isFirstColumnEquivalenceClassInfo, |
| 360 |
String importEncoding) |
416 |
String encoding)throws IOException { |
| 361 |
throws IOException, CorruptCSVFileException |
|
|
| 362 |
{ |
| 363 |
importCSV(datapool, |
417 |
importCSV(datapool, |
| 364 |
csvFileName, |
418 |
csvFileName, |
| 365 |
isFirstRowVariableNameType, |
419 |
isFirstRowVariableInfo, |
| 366 |
isFirstColEqClsName, |
420 |
isFirstColumnEquivalenceClassInfo, |
| 367 |
importEncoding, |
421 |
encoding, |
| 368 |
-1, |
422 |
-1, |
| 369 |
-1); |
423 |
-1); |
| 370 |
} |
424 |
} |
| 371 |
|
425 |
|
|
|
426 |
/** |
| 427 |
* <p>Imports a Comma Separated Values (CSV) file into a datapool.</p> |
| 428 |
* |
| 429 |
* <p><b>Note</b>: The CSV file is assumed to be valid. To validate the CSV file |
| 430 |
* with the datapool, call the {@link #validateCSVFileWithDatapool(String, IDatapool, boolean, boolean, String)} |
| 431 |
* method.</p> |
| 432 |
* |
| 433 |
* @param datapool The datapool that the CSV file is imported into. |
| 434 |
* @param csvFilePath The absolute path of the CSV file. |
| 435 |
* @param isFirstRowVariableInfo If the first row of the CSV file contains the variable information (name/type), otherwise <code>false</code>. |
| 436 |
* @param isFirstColumnEquivalenceClassInfo If the first column of the CSV file contains the equivalence class information (name/row index), otherwise <code>false</code>. |
| 437 |
* @param encoding The encoding of the CSV file, otherwise <code>null</null>. |
| 438 |
* @param numberOfColumns The number of columns of the CSV file to import into the datapool, otherwise <code>-1</code>. |
| 439 |
* @param numberOfRows The number of rows of the CSV file to import into the datapool, otherwise <code>-1</code>. |
| 440 |
* @throws IOException An error occurs while reading the CSV file. |
| 441 |
* @see #validateCSVFileWithDatapool(String, IDatapool, boolean, boolean, String) |
| 442 |
*/ |
| 372 |
public void importCSV(IDatapool datapool, |
443 |
public void importCSV(IDatapool datapool, |
| 373 |
String csvFileName, |
444 |
String csvFilePath, |
| 374 |
boolean isFirstRowVariableNameType, |
445 |
boolean isFirstRowVariableInfo, |
| 375 |
boolean isFirstColEqClsName, |
446 |
boolean isFirstColumnEquivalenceClassInfo, |
| 376 |
String importEncoding, |
447 |
String encoding, |
| 377 |
int numberOfColumns, |
448 |
int numberOfColumns, |
| 378 |
int numberOfRows) |
449 |
int numberOfRows) throws IOException{ |
| 379 |
throws IOException, CorruptCSVFileException |
|
|
| 380 |
{ |
| 381 |
if(datapool == null || csvFileName == null) |
| 382 |
return; |
| 383 |
|
450 |
|
| 384 |
try |
451 |
if((datapool != null) && (csvFilePath != null)){ |
| 385 |
{ |
452 |
|
| 386 |
InputStreamReader inputStreamReader = null; |
453 |
BufferedReader bufferedReader = null; |
| 387 |
if(importEncoding.length() == 0) |
454 |
|
| 388 |
inputStreamReader = new InputStreamReader(new FileInputStream(csvFileName)); //$NON-NLS-1$ |
455 |
try{ |
| 389 |
else |
456 |
|
| 390 |
inputStreamReader = new InputStreamReader(new FileInputStream(csvFileName), importEncoding); //$NON-NLS-1$ |
457 |
if((encoding != null) && (encoding.trim().length() > 0)){ |
|
|
458 |
bufferedReader = new CSVBufferedReader(new InputStreamReader(new FileInputStream(csvFilePath), (encoding.trim()))); |
| 459 |
} |
| 460 |
else{ |
| 461 |
bufferedReader = new CSVBufferedReader(new InputStreamReader(new FileInputStream(csvFilePath))); |
| 462 |
} |
| 463 |
|
| 464 |
String line = bufferedReader.readLine(); |
| 465 |
|
| 466 |
//Initialize to 1 since once line has already been read: |
| 467 |
int lineCounter = 1; |
| 468 |
|
| 469 |
if(isFirstRowVariableInfo){ |
| 470 |
|
| 471 |
createVariablesFromFile(datapool, line, lineCounter, isFirstColumnEquivalenceClassInfo, numberOfColumns); |
| 472 |
|
| 473 |
line = bufferedReader.readLine(); |
| 474 |
|
| 475 |
lineCounter++; |
| 476 |
} |
| 477 |
else{ |
| 478 |
createVariables(datapool, line, isFirstColumnEquivalenceClassInfo, numberOfColumns); |
| 479 |
} |
| 391 |
|
480 |
|
| 392 |
CSVBufferedReader bufferedReader = new CSVBufferedReader(inputStreamReader); |
481 |
int recordCounter = 0; |
| 393 |
String firstLine = bufferedReader.readLine(); |
482 |
|
| 394 |
|
483 |
while((line != null) && ((numberOfRows == -1) || (recordCounter < numberOfRows))){ |
| 395 |
if(isFirstRowVariableNameType) |
484 |
|
| 396 |
{ |
485 |
createRecord(datapool, line, lineCounter, isFirstColumnEquivalenceClassInfo); |
| 397 |
createVariablesFromFile(datapool, firstLine, isFirstColEqClsName, numberOfColumns); |
486 |
|
| 398 |
firstLine = bufferedReader.readLine(); |
487 |
recordCounter++; |
| 399 |
lineCounter++; |
488 |
|
|
|
489 |
line = bufferedReader.readLine(); |
| 490 |
|
| 491 |
lineCounter++; |
| 492 |
} |
| 400 |
} |
493 |
} |
| 401 |
else |
494 |
finally{ |
| 402 |
createVariables(datapool, firstLine, isFirstColEqClsName, numberOfColumns); |
495 |
|
| 403 |
|
496 |
try { |
| 404 |
int recordCounter = 1; |
497 |
|
| 405 |
for(String line = firstLine; |
498 |
if(bufferedReader != null){ |
| 406 |
(line != null && (numberOfRows == -1 || recordCounter <= numberOfRows)); |
499 |
bufferedReader.close(); |
| 407 |
line = bufferedReader.readLine(), lineCounter++) |
500 |
} |
| 408 |
{ |
501 |
} |
| 409 |
createRecord(datapool, line, isFirstColEqClsName); |
502 |
catch (IOException i) { |
| 410 |
recordCounter++; |
503 |
//Ignore since closing input stream reader. |
|
|
504 |
} |
| 411 |
} |
505 |
} |
| 412 |
bufferedReader.close(); |
|
|
| 413 |
} |
| 414 |
catch(IOException e) |
| 415 |
{ |
| 416 |
throw e; |
| 417 |
} |
| 418 |
catch(CorruptCSVFileException e) |
| 419 |
{ |
| 420 |
throw e; |
| 421 |
} |
| 422 |
finally |
| 423 |
{ |
| 424 |
lineCounter = 1; |
| 425 |
} |
506 |
} |
| 426 |
} |
507 |
} |
| 427 |
|
508 |
|
|
|
509 |
/** |
| 510 |
* <p>Appends a Comma Separated Values (CSV) file to a datapool.</p> |
| 511 |
* |
| 512 |
* <p><b>Note</b>: The CSV file is assumed to be valid. To validate the CSV file |
| 513 |
* with the datapool, call the {@link #validateCSVFileWithDatapool(String, IDatapool, boolean, boolean, String)} |
| 514 |
* method.</p> |
| 515 |
* |
| 516 |
* @param datapool The datapool that the CSV file is appended to. |
| 517 |
* @param csvFilePath The absolute path of the CSV file. |
| 518 |
* @param isFirstRowVariableInfo If the first row of the CSV file contains the variable information (name/type), otherwise <code>false</code>. |
| 519 |
* @param isFirstColumnEquivalenceClassInfo If the first column of the CSV file contains the equivalence class information (name/row index), otherwise <code>false</code>. |
| 520 |
* @param encoding The encoding of the CSV file, otherwise <code>null</null>. |
| 521 |
* @throws IOException An error occurs while reading the CSV file. |
| 522 |
* @see #validateCSVFileWithDatapool(String, IDatapool, boolean, boolean, String) |
| 523 |
*/ |
| 428 |
public void appendFromCSV(IDatapool datapool, |
524 |
public void appendFromCSV(IDatapool datapool, |
| 429 |
String csvFileName, |
525 |
String csvFilePath, |
| 430 |
boolean isFirstRowVariableNameType, |
526 |
boolean isFirstRowVariableInfo, |
| 431 |
boolean isFirstColEqClsName, |
527 |
boolean isFirstColumnEquivalenceClassInfo, |
| 432 |
String importEncoding) |
528 |
String encoding) throws IOException { |
| 433 |
throws IOException, CorruptCSVFileException |
|
|
| 434 |
{ |
| 435 |
if(datapool == null || csvFileName == null) |
| 436 |
return; |
| 437 |
|
529 |
|
| 438 |
try |
530 |
if((datapool != null) && (csvFilePath != null)){ |
| 439 |
{ |
531 |
|
| 440 |
InputStreamReader inputStreamReader = null; |
532 |
BufferedReader bufferedReader = null; |
| 441 |
if(importEncoding.length() == 0) |
533 |
|
| 442 |
inputStreamReader = new InputStreamReader(new FileInputStream(csvFileName)); //$NON-NLS-1$ |
534 |
try{ |
| 443 |
else |
535 |
|
| 444 |
inputStreamReader = new InputStreamReader(new FileInputStream(csvFileName), importEncoding); //$NON-NLS-1$ |
536 |
if((encoding != null) && (encoding.trim().length() > 0)){ |
|
|
537 |
bufferedReader = new CSVBufferedReader(new InputStreamReader(new FileInputStream(csvFilePath), (encoding.trim()))); |
| 538 |
} |
| 539 |
else{ |
| 540 |
bufferedReader = new CSVBufferedReader(new InputStreamReader(new FileInputStream(csvFilePath))); |
| 541 |
} |
| 542 |
|
| 543 |
String line = bufferedReader.readLine(); |
| 544 |
|
| 545 |
//Initialize to 1 since once line has already been read: |
| 546 |
int lineCounter = 1; |
| 547 |
|
| 548 |
if(isFirstRowVariableInfo){ |
| 549 |
|
| 550 |
line = bufferedReader.readLine(); |
| 551 |
|
| 552 |
lineCounter++; |
| 553 |
} |
| 445 |
|
554 |
|
| 446 |
CSVBufferedReader bufferedReader = new CSVBufferedReader(inputStreamReader); |
555 |
while(line != null){ |
| 447 |
String firstLine = bufferedReader.readLine(); |
556 |
|
| 448 |
if(isFirstRowVariableNameType) |
557 |
createRecord(datapool, line, lineCounter, isFirstColumnEquivalenceClassInfo); |
| 449 |
{ |
558 |
|
| 450 |
firstLine = bufferedReader.readLine(); |
559 |
line = bufferedReader.readLine(); |
| 451 |
lineCounter++; |
560 |
|
|
|
561 |
lineCounter++; |
| 562 |
} |
| 452 |
} |
563 |
} |
| 453 |
|
564 |
finally{ |
| 454 |
int recordCounter = 1; |
565 |
|
| 455 |
for(String line = firstLine; |
566 |
try { |
| 456 |
line != null; |
567 |
|
| 457 |
line = bufferedReader.readLine(), lineCounter++) |
568 |
if(bufferedReader != null){ |
| 458 |
{ |
569 |
bufferedReader.close(); |
| 459 |
createRecord(datapool, line, isFirstColEqClsName); |
570 |
} |
| 460 |
recordCounter++; |
571 |
} |
|
|
572 |
catch (IOException i) { |
| 573 |
//Ignore since closing input stream reader. |
| 574 |
} |
| 461 |
} |
575 |
} |
| 462 |
bufferedReader.close(); |
|
|
| 463 |
} |
| 464 |
catch(IOException e) |
| 465 |
{ |
| 466 |
throw e; |
| 467 |
} |
| 468 |
catch(CorruptCSVFileException e) |
| 469 |
{ |
| 470 |
throw e; |
| 471 |
} |
| 472 |
finally |
| 473 |
{ |
| 474 |
lineCounter = 1; |
| 475 |
} |
576 |
} |
| 476 |
} |
577 |
} |
| 477 |
|
578 |
|
| 478 |
public boolean exportCSV(IDatapool datapool, |
579 |
/** |
| 479 |
String csvFileName, |
580 |
* <p>Exports a datapool to a Comma Separated Values (CSV) file.</p> |
| 480 |
boolean includeVariables, |
581 |
* |
| 481 |
boolean includeEquivalenceClassNames, |
582 |
* @param datapool The datapool that is exported to the CSV file. |
|
|
583 |
* @param csvFilePath The absolute path of the CSV file. |
| 584 |
* @param includeVariableInfo If the first row of the CSV file contains the variable information (name/type), otherwise <code>false</code>. |
| 585 |
* @param includeEquivalenceClassInfo If the first column of the CSV file contains the equivalence class information (name/row index), otherwise <code>false</code>. |
| 586 |
* @param includeTags If the cell values are enclosed in tags, otherwise <code>false</code>. |
| 587 |
* @param encoding The encoding of the CSV file, otherwise <code>null</null>. |
| 588 |
* @throws IOException An error occurs while writing the CSV file. |
| 589 |
*/ |
| 590 |
public void exportCSV(IDatapool datapool, |
| 591 |
String csvFilePath, |
| 592 |
boolean includeVariableInfo, |
| 593 |
boolean includeEquivalenceClassInfo, |
| 482 |
boolean includeTags, |
594 |
boolean includeTags, |
| 483 |
String exportEncoding) |
595 |
String encoding) throws IOException{ |
| 484 |
{ |
596 |
|
| 485 |
if(datapool == null || csvFileName == null) |
597 |
if((datapool != null) && (csvFilePath != null)){ |
| 486 |
return false; |
598 |
|
| 487 |
try |
599 |
BufferedWriter bufferedWriter = null; |
| 488 |
{ |
600 |
|
| 489 |
OutputStreamWriter outputStreamWriter = null; |
601 |
try{ |
| 490 |
if(exportEncoding.length() == 0) |
602 |
|
| 491 |
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(csvFileName)); //$NON-NLS-1$ |
603 |
if((encoding != null) && (encoding.trim().length() > 0)){ |
| 492 |
else |
604 |
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFilePath), (encoding.trim()))); |
| 493 |
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(csvFileName), exportEncoding); //$NON-NLS-1$ |
605 |
} |
| 494 |
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); |
606 |
else{ |
| 495 |
if(includeVariables) |
607 |
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFilePath))); |
| 496 |
writeVariables(datapool, bufferedWriter, true, includeEquivalenceClassNames); |
608 |
} |
| 497 |
writeRecords(datapool, bufferedWriter, includeEquivalenceClassNames, includeTags); |
609 |
|
| 498 |
bufferedWriter.close(); |
610 |
if(includeVariableInfo){ |
| 499 |
} |
611 |
writeVariables(datapool, bufferedWriter, true, includeEquivalenceClassInfo); |
| 500 |
catch(IOException e) |
612 |
} |
| 501 |
{ |
613 |
|
| 502 |
Status status = new Status(IStatus.ERROR, UiPlugin.getID(), IResourceStatus.ERROR,e.getMessage(), e); |
614 |
writeRecords(datapool, bufferedWriter, includeEquivalenceClassInfo, includeTags); |
| 503 |
ErrorDialog.openError(Display.getCurrent().getActiveShell(), UiPluginResourceBundle.W_ERROR, null, status); |
615 |
} |
| 504 |
return false; |
616 |
finally{ |
|
|
617 |
|
| 618 |
try { |
| 619 |
|
| 620 |
if(bufferedWriter != null){ |
| 621 |
bufferedWriter.close(); |
| 622 |
} |
| 623 |
} |
| 624 |
catch (IOException i) { |
| 625 |
//Ignore since closing input stream reader. |
| 626 |
} |
| 627 |
} |
| 505 |
} |
628 |
} |
| 506 |
finally |
|
|
| 507 |
{ |
| 508 |
lineCounter = 1; |
| 509 |
} |
| 510 |
return true; |
| 511 |
} |
629 |
} |
| 512 |
|
630 |
|
| 513 |
private void createVariablesFromFile(IDatapool datapool, String variableString, boolean ignoreFirst, int numberOfColumns) throws CorruptCSVFileException |
631 |
private void createVariablesFromFile(IDatapool datapool, String variableString, int lineNumber, boolean ignoreFirst, int numberOfColumns) |
| 514 |
{ |
632 |
{ |
| 515 |
boolean first = true; |
633 |
boolean first = true; |
| 516 |
int counter = 1; |
634 |
int counter = 1; |
|
Lines 537-564
Link Here
|
| 537 |
type = nameTypePair.substring(separatorIndex + 2); |
655 |
type = nameTypePair.substring(separatorIndex + 2); |
| 538 |
} |
656 |
} |
| 539 |
|
657 |
|
| 540 |
if(!DatapoolUtilities.getInstance().isVariableNameUnique(datapool, name, null)) |
658 |
|
| 541 |
{ |
|
|
| 542 |
Object[] messageElements = {String.valueOf(lineCounter), |
| 543 |
UiPluginResourceBundle.DATA_COL_DLG_ERROR_NAME_NOT_UNIQUE}; |
| 544 |
String error = NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, messageElements); |
| 545 |
throw new CorruptCSVFileException(error); |
| 546 |
} |
| 547 |
if(!DatapoolUtilities.getInstance().isVariableNameValid(name)) |
| 548 |
{ |
| 549 |
Object[] messageElements = {String.valueOf(lineCounter), |
| 550 |
UiPluginResourceBundle.DATA_COL_DLG_ERROR_NAME_NOT_VALID}; |
| 551 |
String error = NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, messageElements); |
| 552 |
throw new CorruptCSVFileException(error); |
| 553 |
|
| 554 |
} |
| 555 |
if(!DatapoolUtilities.getInstance().isVariableTypeValid(type)) |
| 556 |
{ |
| 557 |
Object[] messageElements = {String.valueOf(lineCounter), |
| 558 |
UiPluginResourceBundle.DATA_COL_DLG_ERROR_TYPE_NOT_VALID}; |
| 559 |
String error = NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, messageElements); |
| 560 |
throw new CorruptCSVFileException(error); |
| 561 |
} |
| 562 |
IDatapoolVariable variable = datapool.constructVariable(); |
659 |
IDatapoolVariable variable = datapool.constructVariable(); |
| 563 |
variable.setName(name); |
660 |
variable.setName(name); |
| 564 |
IDatapoolSuggestedType suggestedType = (IDatapoolSuggestedType)variable.getSuggestedType(); |
661 |
IDatapoolSuggestedType suggestedType = (IDatapoolSuggestedType)variable.getSuggestedType(); |
|
Lines 585-691
Link Here
|
| 585 |
} |
682 |
} |
| 586 |
} |
683 |
} |
| 587 |
|
684 |
|
| 588 |
private void createRecord(IDatapool datapool, String recordString, boolean useSpecifiedEC) throws CorruptCSVFileException |
685 |
private void createRecord(IDatapool datapool, String recordString, int lineNumber, boolean useSpecifiedEquivalenceClass) { |
| 589 |
{ |
686 |
|
| 590 |
CSVTokenizer tokenizer = new CSVTokenizer(recordString); |
687 |
CSVTokenizer tokenizer = new CSVTokenizer(recordString); |
| 591 |
IDatapoolEquivalenceClass equivalenceClass = null; |
688 |
IDatapoolEquivalenceClass equivalenceClass = null; |
| 592 |
String recordIndexStr = null; |
|
|
| 593 |
int recordIndex = -1; |
689 |
int recordIndex = -1; |
| 594 |
|
690 |
|
| 595 |
//Note: Check if the current token is not empty since the tokenizer returns empty tokens. |
691 |
//Note: Check if the current token is not empty since the tokenizer returns empty tokens. |
| 596 |
if(useSpecifiedEC && tokenizer.hasMoreTokens() && tokenizer.currentToken().trim().length() > 0) |
692 |
if((useSpecifiedEquivalenceClass) && (tokenizer.hasMoreTokens()) && (tokenizer.currentToken().trim().length() > 0)){ |
| 597 |
{ |
693 |
|
| 598 |
String ecName = tokenizer.nextToken(); |
694 |
String equivalenceClassName = tokenizer.nextToken(); |
| 599 |
int separatorIndex = ecName.indexOf(CSVTokenizer.TWO_COLONS); |
695 |
int separatorIndex = equivalenceClassName.indexOf(CSVTokenizer.TWO_COLONS); |
| 600 |
|
696 |
|
| 601 |
if(separatorIndex >= 0) |
697 |
if(separatorIndex != -1){ |
| 602 |
{ |
698 |
|
| 603 |
try |
699 |
try{ |
| 604 |
{ |
700 |
recordIndex = Integer.parseInt(equivalenceClassName.substring(separatorIndex + CSVTokenizer.TWO_COLONS.length())); |
| 605 |
recordIndexStr = ecName.substring(separatorIndex + 2); |
|
|
| 606 |
Number number = NumberFormat.getInstance().parse(recordIndexStr); |
| 607 |
recordIndex = number.intValue(); |
| 608 |
} |
701 |
} |
| 609 |
catch(Exception e) |
702 |
catch(NumberFormatException n){ |
| 610 |
{ |
703 |
//Ignore and append the record. |
| 611 |
} |
704 |
} |
| 612 |
ecName = ecName.substring(0, separatorIndex); |
705 |
|
|
|
706 |
equivalenceClassName = equivalenceClassName.substring(0, separatorIndex); |
| 613 |
} |
707 |
} |
|
|
708 |
|
| 709 |
int equivalenceClassIndex = datapool.getEquivalenceClassIndex(equivalenceClassName); |
| 614 |
|
710 |
|
| 615 |
if(!DatapoolUtilities.getInstance().isEquivalenceClassNameValid(ecName)) |
711 |
if(equivalenceClassIndex == -1){ |
| 616 |
{ |
712 |
|
| 617 |
Object[] messageElements = {String.valueOf(lineCounter), |
|
|
| 618 |
UiPluginResourceBundle.DATA_ROW_GRP_DLG_ERROR_NAME_NOT_VALID}; |
| 619 |
String error = NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, messageElements); |
| 620 |
throw new CorruptCSVFileException(error); |
| 621 |
} |
| 622 |
int ecIndex = datapool.getEquivalenceClassIndex(ecName); |
| 623 |
if(ecIndex >= 0) |
| 624 |
equivalenceClass = (IDatapoolEquivalenceClass)datapool.getEquivalenceClass(ecIndex); |
| 625 |
else |
| 626 |
{ |
| 627 |
equivalenceClass = datapool.constructEquivalenceClass(); |
713 |
equivalenceClass = datapool.constructEquivalenceClass(); |
| 628 |
equivalenceClass.setName(ecName); |
714 |
equivalenceClass.setName(equivalenceClassName); |
|
|
715 |
|
| 629 |
datapool.appendEquivalenceClass(equivalenceClass); |
716 |
datapool.appendEquivalenceClass(equivalenceClass); |
|
|
717 |
} |
| 718 |
else{ |
| 719 |
equivalenceClass = ((IDatapoolEquivalenceClass)(datapool.getEquivalenceClass(equivalenceClassIndex))); |
| 630 |
} |
720 |
} |
| 631 |
} |
721 |
} |
| 632 |
else |
722 |
else{ |
| 633 |
{ |
723 |
|
| 634 |
// by default, import all records into the default equivalence class. |
724 |
//By default, import all records into the default equivalence class: |
| 635 |
if(datapool.getEquivalenceClassCount() == 0) |
725 |
if(datapool.getEquivalenceClassCount() == 0){ |
| 636 |
{ |
726 |
|
| 637 |
equivalenceClass = datapool.constructEquivalenceClass(); |
727 |
equivalenceClass = datapool.constructEquivalenceClass(); |
|
|
728 |
|
| 638 |
datapool.appendEquivalenceClass(equivalenceClass); |
729 |
datapool.appendEquivalenceClass(equivalenceClass); |
| 639 |
} |
730 |
} |
| 640 |
else |
731 |
else{ |
| 641 |
{ |
732 |
equivalenceClass = (IDatapoolEquivalenceClass)datapool.getEquivalenceClass(Math.max(0, datapool.getDefaultEquivalenceClassIndex())); |
| 642 |
int defaultIndex = datapool.getDefaultEquivalenceClassIndex(); |
|
|
| 643 |
defaultIndex = defaultIndex < 0 ? 0 : defaultIndex; |
| 644 |
equivalenceClass = (IDatapoolEquivalenceClass)datapool.getEquivalenceClass(defaultIndex); |
| 645 |
} |
733 |
} |
| 646 |
} |
734 |
} |
| 647 |
List cellValues = new ArrayList(); |
|
|
| 648 |
while(tokenizer.hasMoreTokens()) |
| 649 |
{ |
| 650 |
String cellValue = tokenizer.nextToken(); |
| 651 |
cellValues.add(cellValue); |
| 652 |
} |
| 653 |
|
735 |
|
| 654 |
String[] recordValues = new String[cellValues.size()]; |
736 |
//Resolve the remaining cells from the record: |
| 655 |
for(int i = 0; i < cellValues.size(); i++) |
737 |
List cells = new ArrayList(); |
| 656 |
{ |
738 |
|
| 657 |
recordValues[i] = (String)cellValues.get(i); |
739 |
while(tokenizer.hasMoreTokens()){ |
|
|
740 |
cells.add(tokenizer.nextToken()); |
| 658 |
} |
741 |
} |
| 659 |
IDatapoolRecord record = equivalenceClass.constructRecord(recordValues); |
742 |
|
| 660 |
if(recordIndex != -1) |
743 |
IDatapoolRecord record = equivalenceClass.constructRecord(((String[])(cells.toArray(new String[cells.size()])))); |
| 661 |
{ |
744 |
int recordCount = equivalenceClass.getRecordCount(); |
| 662 |
IDatapoolRecord existingRecord = null; |
745 |
|
| 663 |
try |
746 |
if((recordIndex > -1) && (recordIndex < recordCount) && (equivalenceClass.getRecord(recordIndex) == null)){ |
| 664 |
{ |
747 |
equivalenceClass.insertRecord(record, recordIndex); |
| 665 |
existingRecord = (IDatapoolRecord)equivalenceClass.getRecord(recordIndex); |
|
|
| 666 |
} |
| 667 |
catch(Exception e) |
| 668 |
{ |
| 669 |
} |
| 670 |
if(existingRecord != null) |
| 671 |
{ |
| 672 |
Object[] messageElements = {String.valueOf(lineCounter), |
| 673 |
UiPluginResourceBundle.DATA_CSV_ERROR_RECORD_INDEX_DUP}; |
| 674 |
String error = NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, messageElements); |
| 675 |
throw new CorruptCSVFileException(error); |
| 676 |
} |
| 677 |
if(recordIndex > equivalenceClass.getRecordCount()) |
| 678 |
{ |
| 679 |
Object[] messageElements = {String.valueOf(lineCounter), |
| 680 |
UiPluginResourceBundle.DATA_CSV_ERROR_RECORD_INDEX_ORDER}; |
| 681 |
String error = NLS.bind(UiPluginResourceBundle.DATA_CSV_LINE, messageElements); |
| 682 |
throw new CorruptCSVFileException(error); |
| 683 |
} |
| 684 |
else |
| 685 |
equivalenceClass.insertRecord(record, recordIndex); |
| 686 |
} |
748 |
} |
| 687 |
else |
749 |
else{ |
| 688 |
equivalenceClass.appendRecord(record); |
750 |
equivalenceClass.appendRecord(record); |
|
|
751 |
} |
| 689 |
} |
752 |
} |
| 690 |
|
753 |
|
| 691 |
private void writeVariables(IDatapool datapool, BufferedWriter writer, boolean includeTypes, boolean includeEquivalenceClasses) |
754 |
private void writeVariables(IDatapool datapool, BufferedWriter writer, boolean includeTypes, boolean includeEquivalenceClasses) |