Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 249588 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/hyades/test/ui/internal/wizard/DatapoolCSVImportPage.java (-20 / +33 lines)
Lines 27-32 Link Here
27
import org.eclipse.swt.events.ModifyListener;
27
import org.eclipse.swt.events.ModifyListener;
28
import org.eclipse.swt.events.SelectionEvent;
28
import org.eclipse.swt.events.SelectionEvent;
29
import org.eclipse.swt.events.SelectionListener;
29
import org.eclipse.swt.events.SelectionListener;
30
import org.eclipse.swt.graphics.Cursor;
30
import org.eclipse.swt.layout.GridData;
31
import org.eclipse.swt.layout.GridData;
31
import org.eclipse.swt.layout.GridLayout;
32
import org.eclipse.swt.layout.GridLayout;
32
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Button;
Lines 35-40 Link Here
35
import org.eclipse.swt.widgets.FileDialog;
36
import org.eclipse.swt.widgets.FileDialog;
36
import org.eclipse.swt.widgets.Group;
37
import org.eclipse.swt.widgets.Group;
37
import org.eclipse.swt.widgets.Label;
38
import org.eclipse.swt.widgets.Label;
39
import org.eclipse.swt.widgets.Shell;
38
import org.eclipse.swt.widgets.Text;
40
import org.eclipse.swt.widgets.Text;
39
41
40
/**
42
/**
Lines 43-49 Link Here
43
 * 
45
 * 
44
 * @author  Peter Sun
46
 * @author  Peter Sun
45
 * @author  Paul E. Slauenwhite
47
 * @author  Paul E. Slauenwhite
46
 * @version March 4, 2008
48
 * @version November 11, 2008
47
 * @since   February 1, 2005
49
 * @since   February 1, 2005
48
 */
50
 */
49
public class DatapoolCSVImportPage extends WizardPage implements ModifyListener, SelectionListener {
51
public class DatapoolCSVImportPage extends WizardPage implements ModifyListener, SelectionListener {
Lines 286-311 Link Here
286
		return;
288
		return;
287
	}
289
	}
288
290
289
	protected boolean validateSelection()
291
	protected void validateSelection(){
290
	{
292
		
291
		setErrorMessage(null);
293
		//Note: The error message is not initially reset to reduce flicker when setting the same error message.		
292
		boolean isCSVValid = true;
294
		boolean isValid = false;
293
		String path = csvFileText.getText().trim();
295
		String csvFileName = getCSVFileName();		
294
		try
296
295
		{
297
		if((csvFileName != null) && (csvFileName.trim().length() > 0)){			
296
            if (path.length() > 0) { //- Bugzilla 109208
298
297
                isCSVValid = CSVImportExportUtil.getInstance().validateCSVFile(path, firstRowContainsVariableNameType(), firstColumnContainsEquivalenceClassName(),
299
			Shell shell = getShell();
298
                        getImportEncoding());
300
			
299
            }
301
			//Set the wait cursor:
300
		}
302
			shell.setCursor(new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT));
301
		catch(Exception e)
303
302
		{
304
			try{
303
			isCSVValid = false;
305
				isValid = CSVImportExportUtil.getInstance().validateCSVFile(csvFileName, firstRowContainsVariableNameType(), firstColumnContainsEquivalenceClassName(), getImportEncoding());				
304
			setErrorMessage(e.getMessage());
306
			}
307
			catch(Exception e){
308
				setErrorMessage(e.getMessage());
309
			}		
310
			finally{
311
					
312
				//Set the default cursor:
313
				shell.setCursor(null);
314
			}
315
			
316
			if(isValid){
317
				setErrorMessage(null);	
318
			}
305
		}
319
		}
306
		
320
		
307
		setPageComplete(isCSVValid);
321
		setPageComplete(isValid);
308
		getContainer().updateButtons();
322
		getContainer().updateButtons();		
309
		return isCSVValid;
310
	}
323
	}
311
}
324
}
(-)src/org/eclipse/hyades/test/ui/internal/wizard/DatapoolImportPage.java (-26 / +1 lines)
Lines 11-17 Link Here
11
 *******************************************************************************/
11
 *******************************************************************************/
12
package org.eclipse.hyades.test.ui.internal.wizard;
12
package org.eclipse.hyades.test.ui.internal.wizard;
13
13
14
import org.eclipse.hyades.test.ui.datapool.internal.util.CSVImportExportUtil;
15
import org.eclipse.swt.widgets.Composite;
14
import org.eclipse.swt.widgets.Composite;
16
15
17
/**
16
/**
Lines 19-25 Link Here
19
 * 
18
 * 
20
 * @author  Bianca Xue Jiang
19
 * @author  Bianca Xue Jiang
21
 * @author  Paul E. Slauenwhite
20
 * @author  Paul E. Slauenwhite
22
 * @version March 4, 2008
21
 * @version November 11, 2008
23
 * @since   March 23, 2005
22
 * @since   March 23, 2005
24
 */
23
 */
25
public class DatapoolImportPage extends DatapoolCSVImportPage {
24
public class DatapoolImportPage extends DatapoolCSVImportPage {
Lines 42-69 Link Here
42
		setControl(parentComposite);
41
		setControl(parentComposite);
43
		validateSelection();
42
		validateSelection();
44
	}
43
	}
45
	
46
	protected boolean validateSelection()
47
	{
48
		boolean valid = false;
49
		setMessage(null);
50
		setErrorMessage(null);
51
		
52
		String csvFile = getCSVFileName();
53
		
54
		try{
55
			if((csvFile != null) && (csvFile.length() > 0)){			
56
				valid = CSVImportExportUtil.getInstance().validateCSVFile(csvFile, firstRowContainsVariableNameType(), firstColumnContainsEquivalenceClassName(), getImportEncoding());             
57
			}
58
		}
59
		catch(Exception e){
60
			valid = false;
61
			setErrorMessage(e.getMessage());
62
		}		
63
		
64
		setPageComplete(valid);
65
		getContainer().updateButtons();
66
		
67
		return valid;
68
	}
69
}
44
}
(-)src/org/eclipse/hyades/test/ui/internal/wizard/DatapoolImportPageTwo.java (-1 / +13 lines)
Lines 19-36 Link Here
19
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.SelectionAdapter;
20
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.graphics.Cursor;
22
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.layout.GridLayout;
24
import org.eclipse.swt.layout.GridLayout;
24
import org.eclipse.swt.widgets.Button;
25
import org.eclipse.swt.widgets.Button;
25
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Group;
27
import org.eclipse.swt.widgets.Group;
28
import org.eclipse.swt.widgets.Shell;
27
29
28
/**
30
/**
29
 * Datapool CSV import page two.
31
 * Datapool CSV import page two.
30
 * 
32
 * 
31
 * 
33
 * 
32
 * @author  Paul E. Slauenwhite
34
 * @author  Paul E. Slauenwhite
33
 * @version October 31, 2008
35
 * @version November 11, 2008
34
 * @since   October 31, 2008
36
 * @since   October 31, 2008
35
 */
37
 */
36
public class DatapoolImportPageTwo extends DatapoolSelectionPage {
38
public class DatapoolImportPageTwo extends DatapoolSelectionPage {
Lines 116-121 Link Here
116
				//Validate the selected datapool:
118
				//Validate the selected datapool:
117
				if(selectedDatapool != null){
119
				if(selectedDatapool != null){
118
120
121
					Shell shell = getShell();
122
					
123
					//Set the wait cursor:
124
					shell.setCursor(new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT));
125
119
					try {
126
					try {
120
127
121
						DatapoolImportPage datapoolImportPage = ((DatapoolImportWizard)(getWizard())).getDatapoolImportPage();
128
						DatapoolImportPage datapoolImportPage = ((DatapoolImportWizard)(getWizard())).getDatapoolImportPage();
Lines 134-139 Link Here
134
					catch(Exception e) {
141
					catch(Exception e) {
135
						setErrorMessage(e.getMessage());
142
						setErrorMessage(e.getMessage());
136
					}	
143
					}	
144
					finally{
145
						
146
						//Set the default cursor:
147
						shell.setCursor(null);
148
					}
137
				}
149
				}
138
			}
150
			}
139
		}
151
		}
(-)src/org/eclipse/hyades/test/ui/internal/wizard/DatapoolImportWizard.java (-121 / +108 lines)
Lines 46-52 Link Here
46
 * 
46
 * 
47
 * @author  Bianca Xue Jiang
47
 * @author  Bianca Xue Jiang
48
 * @author  Paul E. Slauenwhite
48
 * @author  Paul E. Slauenwhite
49
 * @version October 31, 2008
49
 * @version November 11, 2008
50
 * @since   March 18, 2005
50
 * @since   March 18, 2005
51
 */
51
 */
52
public class DatapoolImportWizard extends Wizard implements IImportWizard {
52
public class DatapoolImportWizard extends Wizard implements IImportWizard {
Lines 110-237 Link Here
110
	 */
110
	 */
111
	public boolean performFinish() {
111
	public boolean performFinish() {
112
112
113
        IRunnableWithProgress operation = new WorkspaceModifyOperation() {
113
		IRunnableWithProgress operation = new WorkspaceModifyOperation() {
114
            
114
115
        	public void execute(IProgressMonitor monitor) throws InvocationTargetException {
115
			public void execute(IProgressMonitor monitor) throws InvocationTargetException {
116
116
117
    			monitor.beginTask(UiPluginResourceBundle.DatapoolImportWizard_progressMonitor, 4);
117
				monitor.beginTask(UiPluginResourceBundle.DatapoolImportWizard_progressMonitor, 4);
118
118
119
    			try {
119
				//Resolve the selected datapool:
120
120
				IDatapool datapool = datapoolImportPageTwo.getSelectedDatapool();
121
    				IDatapool datapool = datapoolImportPageTwo.getSelectedDatapool();
121
				boolean isEncrypted = datapoolImportPageTwo.isSelectedDatapoolEncrypted();
122
    				
122
				String password = datapoolImportPageTwo.getEncryptedDatapoolPassword();
123
    				monitor.worked(1);
123
124
124
				try {
125
    				if(datapool == null){
125
126
    					
126
					monitor.worked(1);
127
    					//Assumption:  The file name will include the file extension (see org.eclipse.tptp.platform.common.ui.wizard.LocationPage.getFileName()).
127
128
    					String datapoolFileName = locationPage.getFileName();
128
					if(datapool != null){
129
    					
129
130
    					datapool = ((DPLDatapool)(DatapoolUtil.createNewDatapool(datapoolFileName.substring(0, datapoolFileName.indexOf('.')), null)));
130
						if(isEncrypted){
131
    					
131
							DatapoolEncryptManager.decryptDatapool(datapool, password);
132
    					Resource datapoolResource = EMFUtil.getResourceFactory(locationPage.getFileExtension()).createResource(URI.createPlatformResourceURI(locationPage.getContainerFullPath().append(datapoolFileName).toOSString(), false));
132
						}
133
    					datapoolResource.getContents().add(datapool);
133
134
    				}
134
						//Empty the datapool if it contains only an empty default cell:
135
    				
135
						if(CSVImportExportUtil.getInstance().isDefaultEmptyDatapool(datapool)){
136
    				//If the existing datapool only contains only an empty default cell, empty the datapool:
136
137
    				else if(CSVImportExportUtil.getInstance().isDefaultEmptyDatapool(datapool)){
137
							datapool.removeVariable(0);
138
    					
138
							datapool.removeEquivalenceClass(0);
139
    					datapool.removeVariable(0);
139
						}
140
    					datapool.removeEquivalenceClass(0);
140
					}    				
141
    				}
141
					else {
142
    				
142
143
    				String key = datapoolImportPageTwo.getEncryptedDatapoolPasswordText().getText();
143
						//Assumption:  The file name will include the file extension (see org.eclipse.tptp.platform.common.ui.wizard.LocationPage.getFileName()).
144
    				
144
						String datapoolFileName = locationPage.getFileName();
145
    				if(datapoolImportPageTwo.isEncrypted()){
145
146
    					if(!DatapoolEncryptManager.isKeyCorrect(datapool, key)){
146
						//Create a new datapool:
147
    						datapoolImportPageTwo.setMessage(UiPluginResourceBundle.DatapoolExportWizard_wrongpass,3);
147
						datapool = ((DPLDatapool)(DatapoolUtil.createNewDatapool(datapoolFileName.substring(0, datapoolFileName.indexOf('.')), null)));
148
    						datapoolImportPageTwo.getEncryptedDatapoolPasswordText().setText("");
148
149
    						datapoolImportPageTwo.getEncryptedDatapoolPasswordText().setFocus();
149
						//Create the resource and add the datapool:
150
    						throw new Exception("Password is not correct!");
150
						EMFUtil.getResourceFactory(locationPage.getFileExtension()).createResource(URI.createPlatformResourceURI(locationPage.getContainerFullPath().append(datapoolFileName).toOSString(), false)).getContents().add(datapool);
151
    					}
151
					}
152
    				}
152
153
    				
153
					monitor.worked(1);
154
    				if( DatapoolEncryptManager.isDatapoolEncrypted(datapool))
154
155
    				{
155
					if(datapool.getVariableCount() == 0){
156
    					DatapoolEncryptManager.decryptDatapool(datapool, key);
156
157
    				}
157
						//Import the CSV file to the datapool:
158
    				
158
						CSVImportExportUtil.getInstance().importCSV(datapool, 
159
    				monitor.worked(1);
159
								datapoolImportPage.getCSVFileName(), 
160
    				
160
								datapoolImportPage.firstRowContainsVariableNameType(),
161
    				if(datapool.getVariableCount() == 0){
161
								datapoolImportPage.firstColumnContainsEquivalenceClassName(),
162
    					
162
								datapoolImportPage.getImportEncoding());
163
    					CSVImportExportUtil.getInstance().importCSV(datapool, 
163
					}
164
    																datapoolImportPage.getCSVFileName(), 
164
					else{
165
    																datapoolImportPage.firstRowContainsVariableNameType(),
165
166
    																datapoolImportPage.firstColumnContainsEquivalenceClassName(),
166
						//Append the CSV file to the datapool:
167
    																datapoolImportPage.getImportEncoding());
167
						CSVImportExportUtil.getInstance().appendFromCSV(datapool, 
168
    				}
168
								datapoolImportPage.getCSVFileName(), 
169
    				else{
169
								datapoolImportPage.firstRowContainsVariableNameType(),
170
    					
170
								datapoolImportPage.firstColumnContainsEquivalenceClassName(),
171
    					CSVImportExportUtil.getInstance().appendFromCSV(datapool, 
171
								datapoolImportPage.getImportEncoding());
172
    																	datapoolImportPage.getCSVFileName(), 
172
					}
173
    																	datapoolImportPage.firstRowContainsVariableNameType(),
173
174
    																	datapoolImportPage.firstColumnContainsEquivalenceClassName(),
174
					monitor.worked(1);
175
    																	datapoolImportPage.getImportEncoding());
175
176
    				}
176
					Resource datapoolResource = ((EObject)(datapool)).eResource();
177
177
178
    				monitor.worked(1);
178
					//Save (or serialize) the datapool resource to the workspace:
179
    				
179
					EMFUtil.save(datapoolResource);
180
    				if( DatapoolEncryptManager.isDatapoolEncrypted(datapool))
180
181
    				{
181
					//Attempt to open the editor:
182
    					DatapoolEncryptManager.encryptDatapool(datapool, key);
182
					TestUIUtilities.openEditor(datapoolResource, TestUIExtension.DATAPOOL_EDITOR_PART_ID);
183
    				}
183
184
    				
184
					monitor.worked(1);
185
    				Resource datapoolResource = ((EObject)(datapool)).eResource();
185
				}
186
    				
186
				catch(Exception e){
187
    				//Save (or serialize) the datapool resource to the workspace:
187
188
    				EMFUtil.save(datapoolResource);
188
					UiPlugin.logError(e);			
189
    				    				
189
190
    				//Attempt to open the editor:
190
					new ErrorDialog(getShell(), 
191
    				TestUIUtilities.openEditor(datapoolResource, TestUIExtension.DATAPOOL_EDITOR_PART_ID);
191
							UiPluginResourceBundle.WIZ_DATAPOOL_CSV_ERRDLG_TITLE,
192
    				
192
							UiPluginResourceBundle._ERROR_WIZ_DATAPOOL_CSV_ERRDLG_IMPFILE,
193
    				monitor.worked(1);
193
							new Status(IStatus.ERROR,
194
    			}
194
									UiPlugin.PLUGIN_ID,
195
    			catch(Exception e){
195
									IStatus.OK,
196
    				
196
									e.toString(),
197
    				UiPlugin.logError(e);			
197
									e),
198
    				
198
									IStatus.ERROR).open();
199
    				new ErrorDialog(getShell(), 
199
				}
200
    								UiPluginResourceBundle.WIZ_DATAPOOL_CSV_ERRDLG_TITLE,
200
				finally{
201
    								UiPluginResourceBundle._ERROR_WIZ_DATAPOOL_CSV_ERRDLG_IMPFILE,
201
202
    								new Status(IStatus.ERROR,
202
					//Encrypted the datapool if decrypted: 
203
    										UiPlugin.PLUGIN_ID,
203
					if(isEncrypted){		
204
    										IStatus.OK,
204
						DatapoolEncryptManager.encryptDatapool(datapool, password);
205
    										e.toString(),
205
					}
206
    										e),
206
207
    								IStatus.ERROR).open();
207
					monitor.done();		
208
    			}
209
    			finally{
210
                    monitor.done();		
211
    			}
212
    		}
213
        };
214
        
215
        try {
216
        	String key = datapoolImportPageTwo.getEncryptedDatapoolPasswordText().getText();
217
        	IDatapool datapool = datapoolImportPageTwo.getSelectedDatapool();
218
			if(datapoolImportPageTwo.isEncrypted()){
219
				if(!DatapoolEncryptManager.isKeyCorrect(datapool, key)){
220
					datapoolImportPageTwo.setMessage(UiPluginResourceBundle.DatapoolExportWizard_wrongpass,3);
221
					datapoolImportPageTwo.getEncryptedDatapoolPasswordText().setText("");
222
					datapoolImportPageTwo.getEncryptedDatapoolPasswordText().setFocus();
223
					return false;
224
				}
208
				}
225
			}
209
			}
226
            getContainer().run(false, false, operation);
210
		};
227
        } 
211
228
        catch (InvocationTargetException e) {
212
		try {        	
229
        	UiPlugin.logError(e);
213
			getContainer().run(false, false, operation);
230
        } 
214
		} 
231
        catch (InterruptedException e) {
215
		catch (InvocationTargetException e) {
232
            //Ignore since the operation cannot be cancelled.
216
			UiPlugin.logError(e);
233
        }
217
		} 
234
        
218
		catch (InterruptedException e) {
219
			//Ignore since the operation cannot be canceled.
220
		}
221
235
		return true;
222
		return true;
236
	}
223
	}
237
}
224
}
(-)src/org/eclipse/hyades/test/ui/internal/wizard/DatapoolExportWizard.java (-55 / +70 lines)
Lines 12-34 Link Here
12
package org.eclipse.hyades.test.ui.internal.wizard;
12
package org.eclipse.hyades.test.ui.internal.wizard;
13
13
14
import java.io.File;
14
import java.io.File;
15
import java.io.IOException;
15
16
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IFile;
18
import org.eclipse.core.resources.IResourceStatus;
17
import org.eclipse.core.resources.ResourcesPlugin;
19
import org.eclipse.core.resources.ResourcesPlugin;
18
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Path;
22
import org.eclipse.core.runtime.Path;
23
import org.eclipse.core.runtime.Status;
20
import org.eclipse.hyades.edit.datapool.IDatapool;
24
import org.eclipse.hyades.edit.datapool.IDatapool;
21
import org.eclipse.hyades.models.common.datapool.util.DatapoolEncryptManager;
25
import org.eclipse.hyades.models.common.datapool.util.DatapoolEncryptManager;
26
import org.eclipse.hyades.test.ui.TestUI;
22
import org.eclipse.hyades.test.ui.TestUIImages;
27
import org.eclipse.hyades.test.ui.TestUIImages;
23
import org.eclipse.hyades.test.ui.UiPlugin;
28
import org.eclipse.hyades.test.ui.UiPlugin;
24
import org.eclipse.hyades.test.ui.datapool.internal.util.CSVImportExportUtil;
29
import org.eclipse.hyades.test.ui.datapool.internal.util.CSVImportExportUtil;
25
import org.eclipse.hyades.test.ui.internal.resources.UiPluginResourceBundle;
30
import org.eclipse.hyades.test.ui.internal.resources.UiPluginResourceBundle;
31
import org.eclipse.hyades.test.ui.util.TestUIUtil;
32
import org.eclipse.jface.dialogs.ErrorDialog;
26
import org.eclipse.jface.dialogs.IDialogConstants;
33
import org.eclipse.jface.dialogs.IDialogConstants;
27
import org.eclipse.jface.dialogs.MessageDialog;
34
import org.eclipse.jface.dialogs.MessageDialog;
28
import org.eclipse.jface.viewers.IStructuredSelection;
35
import org.eclipse.jface.viewers.IStructuredSelection;
29
import org.eclipse.jface.wizard.Wizard;
36
import org.eclipse.jface.wizard.Wizard;
30
import org.eclipse.osgi.util.NLS;
37
import org.eclipse.osgi.util.NLS;
31
import org.eclipse.swt.widgets.Composite;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.swt.widgets.Display;
32
import org.eclipse.ui.IExportWizard;
40
import org.eclipse.ui.IExportWizard;
33
import org.eclipse.ui.IWorkbench;
41
import org.eclipse.ui.IWorkbench;
34
42
Lines 38-44 Link Here
38
46
39
 * @author  Peter Sun
47
 * @author  Peter Sun
40
 * @author  Paul E. Slauenwhite
48
 * @author  Paul E. Slauenwhite
41
 * @version October 31, 2008
49
 * @version November 11, 2008
42
 * @since   February 1, 2005
50
 * @since   February 1, 2005
43
 */
51
 */
44
public class DatapoolExportWizard extends Wizard implements IExportWizard {
52
public class DatapoolExportWizard extends Wizard implements IExportWizard {
Lines 90-155 Link Here
90
	/* (non-Javadoc)
98
	/* (non-Javadoc)
91
	 * @see org.eclipse.jface.wizard.IWizard#performFinish()
99
	 * @see org.eclipse.jface.wizard.IWizard#performFinish()
92
	 */	
100
	 */	
93
	public boolean performFinish() 
101
	public boolean performFinish() {
94
	{
102
103
		//Resolve the selected datapool:
104
		//Assumption: The datapool is not null.
95
		IDatapool datapool = datapoolSelectionPage.getSelectedDatapool();
105
		IDatapool datapool = datapoolSelectionPage.getSelectedDatapool();
96
		String key = datapoolSelectionPage.getEncryptedDatapoolPasswordText().getText();
106
		boolean isEncrypted = datapoolSelectionPage.isSelectedDatapoolEncrypted();
97
		
107
		String password = datapoolSelectionPage.getEncryptedDatapoolPassword();
98
		if(datapoolSelectionPage.isEncrypted()){
108
99
			if(!DatapoolEncryptManager.isKeyCorrect(datapool, key)){
109
		try {
100
				datapoolSelectionPage.setMessage(UiPluginResourceBundle.DatapoolExportWizard_wrongpass,3);
110
101
				datapoolSelectionPage.getEncryptedDatapoolPasswordText().setText("");
111
			//Decrypt the datapool if encrypted: 
102
				datapoolSelectionPage.getEncryptedDatapoolPasswordText().setFocus();
112
			if(isEncrypted){		
103
				return false;
113
				DatapoolEncryptManager.decryptDatapool(datapool, password);
114
			}
115
116
			//Resolve the CSV file:
117
			String csvFileName = TestUIUtil.validateExtension(csvFileLocationPage.getCSVFileName().trim(), TestUI.CSV_FILE_EXTENSION);
118
119
			if (new File(csvFileName).exists()){
120
121
				MessageDialog overwriteDialog = new MessageDialog(getShell(),
122
						UiPluginResourceBundle.WIZ_EXP_DATAPOOL_TTL, 
123
						null,
124
						NLS.bind(UiPluginResourceBundle.WIZ_EXP_DATAPOOL_MSG_EXISTS, csvFileName), 
125
						MessageDialog.WARNING, 
126
						new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}, 
127
						0);
128
129
				//Return if cannot replace existing CSV file:
130
				if (overwriteDialog.open() != IDialogConstants.OK_ID){
131
					return false;
132
				}
133
			}
134
135
			//Export the datapool to the CSV file:
136
			CSVImportExportUtil.getInstance().exportCSV(datapool, csvFileName, 
137
					csvFileLocationPage.getExportVariables(), 
138
					csvFileLocationPage.getExportEquivalenceClassNames(), 
139
					csvFileLocationPage.getExportTags(), 
140
					csvFileLocationPage.getExportEncoding());
141
142
			//Refresh the CSV file if exported to the workspace:
143
			IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(csvFileName));
144
145
			for (int counter = 0; counter < files.length; counter++) {
146
147
				try {
148
					files[counter].refreshLocal(0, null);
149
				} 
150
				catch (CoreException e) {
151
					UiPlugin.logError(e);
152
				}
104
			}
153
			}
154
155
			return true;
105
		}
156
		}
106
		
157
		catch (IOException i) {
107
		//decrypt datapool before export it into csv file 
158
			ErrorDialog.openError(Display.getCurrent().getActiveShell(), UiPluginResourceBundle.W_ERROR, null, new Status(IStatus.ERROR, UiPlugin.getID(), IResourceStatus.ERROR, i.getMessage(), i));
108
		if( DatapoolEncryptManager.isDatapoolEncrypted(datapool))
109
		{
110
			DatapoolEncryptManager.decryptDatapool(datapool, key);
111
		}
159
		}
112
		String csvFileName = csvFileLocationPage.getCSVFileName();
160
		finally{
113
		if(!csvFileName.endsWith(".csv") && !csvFileName.endsWith(".CSV"))
114
			csvFileName = csvFileName + ".csv";
115
161
116
		File file = new File(csvFileName);
162
			//Encrypted the datapool if decrypted: 
117
		
163
			if(isEncrypted){		
118
		if (file.exists())
164
				DatapoolEncryptManager.encryptDatapool(datapool, password);
119
		{
165
			}
120
			String message = NLS.bind(UiPluginResourceBundle.WIZ_EXP_DATAPOOL_MSG_EXISTS, csvFileName);
121
			MessageDialog overwriteDialog = new MessageDialog(getShell(),
122
															  UiPluginResourceBundle.WIZ_EXP_DATAPOOL_TTL, 
123
															  null,
124
															  message, 
125
															  MessageDialog.WARNING, 
126
															  new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}, 
127
															  0);
128
			
129
			// user selected - "don't replace", return false
130
			if (overwriteDialog.open() != 0)
131
				return false;
132
		}
166
		}
133
		
167
		
134
		boolean exportVariables = csvFileLocationPage.getExportVariables();
168
		return false;
135
		boolean exportEqClsNames = csvFileLocationPage.getExportEquivalenceClassNames();
136
		boolean exportTags = csvFileLocationPage.getExportTags();
137
		String exportEncoding = csvFileLocationPage.getExportEncoding();
138
		boolean close = CSVImportExportUtil.getInstance().exportCSV(datapool, csvFileName, exportVariables, exportEqClsNames, exportTags, exportEncoding);
139
        IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(csvFileName));
140
        for (int i = 0; i < files.length; i++) {
141
            try {
142
                files[i].refreshLocal(0, null);
143
            } catch (CoreException e) {
144
                UiPlugin.logError(e);
145
            }
146
        }
147
        
148
        //encrypted datapool again after finish export it  
149
        if( DatapoolEncryptManager.isDatapoolEncrypted(datapool))
150
		{
151
			DatapoolEncryptManager.encryptDatapool(datapool, key);
152
		}
153
        return close;
154
	}
169
	}
155
}
170
}
(-)src/org/eclipse/hyades/test/ui/internal/wizard/DatapoolSelectionPage.java (-34 / +38 lines)
Lines 38-44 Link Here
38
 * 
38
 * 
39
 * 
39
 * 
40
 * @author  Paul E. Slauenwhite
40
 * @author  Paul E. Slauenwhite
41
 * @version October 31, 2008
41
 * @version November 11, 2008
42
 * @since   October 31, 2008
42
 * @since   October 31, 2008
43
 */
43
 */
44
public class DatapoolSelectionPage extends WizardPage {
44
public class DatapoolSelectionPage extends WizardPage {
Lines 48-54 Link Here
48
	private Text encryptedDatapoolPasswordText = null;	
48
	private Text encryptedDatapoolPasswordText = null;	
49
	private IStructuredSelection selection = null;
49
	private IStructuredSelection selection = null;
50
	private IDatapool selectedDatapool = null;
50
	private IDatapool selectedDatapool = null;
51
	private boolean isEncrypted = false;
51
	private boolean isSelectedDatapoolEncrypted = false;
52
52
53
	public DatapoolSelectionPage(String name){
53
	public DatapoolSelectionPage(String name){
54
		this(name, null);
54
		this(name, null);
Lines 80-86 Link Here
80
				super.setSelection();
80
				super.setSelection();
81
				
81
				
82
				selectedDatapool = null;
82
				selectedDatapool = null;
83
				isEncrypted = false;
83
				isSelectedDatapoolEncrypted = false;
84
				
84
				
85
				IProxyNode[] selectedProxyNodes = datapoolSelectionViewer.getSelectedProxyNodes();
85
				IProxyNode[] selectedProxyNodes = datapoolSelectionViewer.getSelectedProxyNodes();
86
86
Lines 93-104 Link Here
93
						selectedDatapool = ((IDatapool)(eObject));
93
						selectedDatapool = ((IDatapool)(eObject));
94
94
95
						//Determine if the selected datapool is encrypted:
95
						//Determine if the selected datapool is encrypted:
96
						isEncrypted = DatapoolEncryptManager.isDatapoolEncrypted(selectedDatapool);
96
						isSelectedDatapoolEncrypted = DatapoolEncryptManager.isDatapoolEncrypted(selectedDatapool);
97
97
98
						//Show or hide the encrypted datapool password group:
98
						if((isSelectedDatapoolEncrypted) && (!encryptedDatapoolPasswordGroup.isVisible())){
99
						encryptedDatapoolPasswordGroup.setVisible(isEncrypted);						
100
101
						if(isEncrypted){
102
						
99
						
103
							//Set the title of the encrypted datapool password group:
100
							//Set the title of the encrypted datapool password group:
104
							encryptedDatapoolPasswordGroup.setText(NLS.bind(UiPluginResourceBundle.DatapoolExportWizard_password, selectedDatapool.getName()));
101
							encryptedDatapoolPasswordGroup.setText(NLS.bind(UiPluginResourceBundle.DatapoolExportWizard_password, selectedDatapool.getName()));
Lines 107-112 Link Here
107
							encryptedDatapoolPasswordText.setText("");
104
							encryptedDatapoolPasswordText.setText("");
108
							encryptedDatapoolPasswordText.setFocus();				
105
							encryptedDatapoolPasswordText.setFocus();				
109
						}
106
						}
107
						
108
						//Show or hide the encrypted datapool password group:
109
						encryptedDatapoolPasswordGroup.setVisible(isSelectedDatapoolEncrypted);						
110
					}
110
					}
111
				}
111
				}
112
				
112
				
Lines 127-145 Link Here
127
		encryptedDatapoolPasswordText.addModifyListener(new ModifyListener(){
127
		encryptedDatapoolPasswordText.addModifyListener(new ModifyListener(){
128
			
128
			
129
			public void modifyText(ModifyEvent e){
129
			public void modifyText(ModifyEvent e){
130
130
				setPageComplete(validatePage());
131
				if(encryptedDatapoolPasswordText.getText().trim().length() > 0){
132
133
					setErrorMessage(null);
134
135
					setPageComplete(true);
136
				}
137
				else {
138
139
					setErrorMessage(NLS.bind(UiPluginResourceBundle.DatapoolExportWizard_encrypted, selectedDatapool.getName()));
140
141
					setPageComplete(false);
142
				}
143
			}
131
			}
144
		});
132
		});
145
133
Lines 153-167 Link Here
153
	public IDatapool getSelectedDatapool(){
141
	public IDatapool getSelectedDatapool(){
154
		return selectedDatapool;
142
		return selectedDatapool;
155
	}
143
	}
156
157
	public Text getEncryptedDatapoolPasswordText(){
158
		return encryptedDatapoolPasswordText;
159
	}
160
	
144
	
161
	public boolean isEncrypted(){
145
	public boolean isSelectedDatapoolEncrypted(){
162
		return isEncrypted;
146
		return isSelectedDatapoolEncrypted;
163
	}	
147
	}	
164
148
149
	public String getEncryptedDatapoolPassword(){
150
		return (encryptedDatapoolPasswordText.getText().trim());
151
	}
152
165
	protected boolean validatePage() {
153
	protected boolean validatePage() {
166
		
154
		
167
		//Note: The error message is not initially reset to reduce flicker when setting the same error message.		
155
		//Note: The error message is not initially reset to reduce flicker when setting the same error message.		
Lines 171-191 Link Here
171
		if(selectedDatapool != null){
159
		if(selectedDatapool != null){
172
160
173
			//Validate the selected encrypted datapool and its password:
161
			//Validate the selected encrypted datapool and its password:
174
			if((isEncrypted) && (encryptedDatapoolPasswordText.getText().trim().length() == 0)){
162
			if(isSelectedDatapoolEncrypted){
163
164
				String password = getEncryptedDatapoolPassword();
165
166
				if(password.length() == 0){
167
168
					setErrorMessage(NLS.bind(UiPluginResourceBundle.DatapoolExportWizard_encrypted, selectedDatapool.getName()));
169
170
					//Set focus to the encrypted datapool password:				
171
					encryptedDatapoolPasswordText.setFocus();
172
				}
173
				else if(!DatapoolEncryptManager.isKeyCorrect(selectedDatapool, password)){
175
174
176
				setErrorMessage(NLS.bind(UiPluginResourceBundle.DatapoolExportWizard_encrypted, selectedDatapool.getName()));
175
					setErrorMessage(UiPluginResourceBundle.DatapoolExportWizard_wrongpass);
177
176
178
				//Set focus to the encrypted datapool password:				
177
					//Set focus to the encrypted datapool password:				
179
				encryptedDatapoolPasswordText.setFocus();				
178
					encryptedDatapoolPasswordText.setFocus();
179
				}
180
				else{					
181
					isValid = true;
182
				}
180
			}
183
			}
181
			else{
184
			else{
182
183
				isValid = true;
185
				isValid = true;
184
185
				setErrorMessage(null);
186
			}
186
			}
187
		}
187
		}
188
188
189
		if(isValid){
190
			setErrorMessage(null);			
191
		}
192
189
		return isValid;
193
		return isValid;
190
	}	
194
	}	
191
}
195
}
(-)src/org/eclipse/hyades/test/ui/datapool/internal/util/CSVImportExportUtil.java (-468 / +531 lines)
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)
(-)src/org/eclipse/hyades/test/ui/internal/resources/UiPluginResourceBundle.java (-2 / +1 lines)
Lines 19-25 Link Here
19
 * 
19
 * 
20
 * 
20
 * 
21
 * @author      Paul E. Slauenwhite
21
 * @author      Paul E. Slauenwhite
22
 * @version     August 7, 2008
22
 * @version     November 11, 2008
23
 * @since       March 15, 2007
23
 * @since       March 15, 2007
24
 */
24
 */
25
public final class UiPluginResourceBundle extends NLS {
25
public final class UiPluginResourceBundle extends NLS {
Lines 216-222 Link Here
216
	public static String DATA_DLG_FIRST_ELEMENT;
216
	public static String DATA_DLG_FIRST_ELEMENT;
217
	public static String DATA_CSV_LINE;
217
	public static String DATA_CSV_LINE;
218
	public static String DATA_CSV_ERROR_RECORD_INDEX_DUP;
218
	public static String DATA_CSV_ERROR_RECORD_INDEX_DUP;
219
	public static String DATA_CSV_ERROR_RECORD_INDEX_ORDER;
220
	public static String DATA_DEL_VAR_DLG_TITLE;
219
	public static String DATA_DEL_VAR_DLG_TITLE;
221
	public static String DATA_DEL_VAR_DLG_SEL;
220
	public static String DATA_DEL_VAR_DLG_SEL;
222
	public static String DATA_ADD_ROW_OPTION_BEFORE;
221
	public static String DATA_ADD_ROW_OPTION_BEFORE;
(-)src/org/eclipse/hyades/test/ui/internal/resources/messages.properties (-2 lines)
Lines 270-277 Link Here
270
DATA_CSV_LINE							= CSV file line {0}: {1}
270
DATA_CSV_LINE							= CSV file line {0}: {1}
271
#IWAT0221E
271
#IWAT0221E
272
DATA_CSV_ERROR_RECORD_INDEX_DUP			= Duplicate record.
272
DATA_CSV_ERROR_RECORD_INDEX_DUP			= Duplicate record.
273
#IWAT0222E
274
DATA_CSV_ERROR_RECORD_INDEX_ORDER		= Record not in proper order.
275
273
276
# Delete Variable Dialog
274
# Delete Variable Dialog
277
DATA_DEL_VAR_DLG_TITLE					= Delete Variable
275
DATA_DEL_VAR_DLG_TITLE					= Delete Variable
(-)src-test/org/eclipse/hyades/models/common/datapool/impl/messages.properties (-1 lines)
Lines 16-22 Link Here
16
Common_DatapoolFactoryImpl.23=IWAT3058E Error while accessing iterator 
16
Common_DatapoolFactoryImpl.23=IWAT3058E Error while accessing iterator 
17
Common_DatapoolFactoryImpl.25=IWAT3059E Datapool Iterator could not be opened 
17
Common_DatapoolFactoryImpl.25=IWAT3059E Datapool Iterator could not be opened 
18
Common_DatapoolFactoryImpl.26=IWAT3060E Datapool Iterator could not be opened 
18
Common_DatapoolFactoryImpl.26=IWAT3060E Datapool Iterator could not be opened 
19
DPLCellImpl.18               =IWAT3061E No extensions found for tag  
20
DPLEquivalenceClassImpl.1    =IWAT3062E Attempt to enter duplicate records into an equivalence class
19
DPLEquivalenceClassImpl.1    =IWAT3062E Attempt to enter duplicate records into an equivalence class
21
DPLEquivalenceClassImpl.2    =IWAT3063E Record reordering attempt with wrong number of records specified
20
DPLEquivalenceClassImpl.2    =IWAT3063E Record reordering attempt with wrong number of records specified
22
DPLRecordImpl.1              =IWAT3064E Nonexistent Variable Index Used 
21
DPLRecordImpl.1              =IWAT3064E Nonexistent Variable Index Used 
(-)src-test/org/eclipse/hyades/models/common/datapool/impl/DPLCellImpl.java (-102 / +91 lines)
Lines 17-22 Link Here
17
package org.eclipse.hyades.models.common.datapool.impl;
17
package org.eclipse.hyades.models.common.datapool.impl;
18
18
19
import java.util.Map;
19
import java.util.Map;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
20
22
21
import org.eclipse.emf.common.notify.Notification;
23
import org.eclipse.emf.common.notify.Notification;
22
import org.eclipse.emf.ecore.EClass;
24
import org.eclipse.emf.ecore.EClass;
Lines 25-31 Link Here
25
import org.eclipse.emf.ecore.impl.EObjectImpl;
27
import org.eclipse.emf.ecore.impl.EObjectImpl;
26
import org.eclipse.hyades.edit.datapool.IDatapoolRecord;
28
import org.eclipse.hyades.edit.datapool.IDatapoolRecord;
27
import org.eclipse.hyades.edit.datapool.IDatapoolVariable;
29
import org.eclipse.hyades.edit.datapool.IDatapoolVariable;
28
import org.eclipse.hyades.execution.runtime.datapool.DatapoolException;
29
import org.eclipse.hyades.execution.runtime.datapool.IDatapoolEvaluatorExt;
30
import org.eclipse.hyades.execution.runtime.datapool.IDatapoolEvaluatorExt;
30
import org.eclipse.hyades.models.common.datapool.Common_DatapoolPackage;
31
import org.eclipse.hyades.models.common.datapool.Common_DatapoolPackage;
31
import org.eclipse.hyades.models.common.datapool.DPLCell;
32
import org.eclipse.hyades.models.common.datapool.DPLCell;
Lines 33-40 Link Here
33
import org.eclipse.hyades.models.common.datapool.util.DPLPasswordCollection;
34
import org.eclipse.hyades.models.common.datapool.util.DPLPasswordCollection;
34
import org.eclipse.hyades.models.common.util.DatapoolUtil;
35
import org.eclipse.hyades.models.common.util.DatapoolUtil;
35
import org.eclipse.hyades.models.common.util.EncryptionManager;
36
import org.eclipse.hyades.models.common.util.EncryptionManager;
36
import org.eclipse.hyades.models.common.util.XMLParserUtil;
37
import org.w3c.dom.Document;
38
37
39
/**
38
/**
40
 * <!-- begin-user-doc -->
39
 * <!-- begin-user-doc -->
Lines 301-358 Link Here
301
		return (org.eclipse.hyades.execution.runtime.datapool.IDatapoolRecord)this.eContainer();
300
		return (org.eclipse.hyades.execution.runtime.datapool.IDatapoolRecord)this.eContainer();
302
	}
301
	}
303
302
304
	private static final String XML_AMPERSAND = "&amp;"; //$NON-NLS-1$
305
	private static final String AMPERSAND = "&"; //$NON-NLS-1$
306
	
307
	/**
303
	/**
308
	 * The value associated with the cell is returned.
304
	 * <p>Pre-compiled pattern for matching the open/close tag names and enclosed 
305
	 * character data of an XML fragment.  For example:</p>
306
	 * 
307
	 * <p><code>&lt;[white space]OPEN_TAG_NAME[white space]&gt;CHARACTER_DATA&lt;/[white space]CLOSE_TAG_NAME[white space]&gt;</code></p>
309
	 * 
308
	 * 
310
	 * @return	The value associated with the cell is returned.
309
	 * <p>The open tag name is in the first group, the character data is in the second group, and
310
	 * the close tag name is in the third group.</p>
311
	 * 
312
	 * <p>Note, the input string is assumed to be trimmed.</p>
311
	 */
313
	 */
312
	public Object getCellValue()
314
	private static final Pattern XML_FRAGMENT_PATTERN = Pattern.compile("<\\s*(\\w+)\\s*>(.*)</\\s*(\\w+)\\s*>"); //$NON-NLS-1$	
313
	{	
315
	private static final String START_LITERAL = "<Literal>"; //$NON-NLS-1$
314
		if (this.getValue() == null)
316
	private static final String END_LITERAL = "</Literal>"; //$NON-NLS-1$
315
			return null;
316
		
317
		String internalValue;
318
317
319
		internalValue = this.getValue().trim();
318
	/**
319
	 * The value associated with the cell is returned.
320
	 * 
321
	 * @return The value associated with the cell is returned.
322
	 */
323
	public Object getCellValue(){	
320
324
321
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=117932
325
		String cellValue = getValue();
322
		// performance optimization. Try unwrapping the <Literal> tags first for literal cell values to avoid xml parsing
323
		// All cell values are treated literal when no evaluator is present.
324
		if(internalValue.startsWith(START_LITERAL) && internalValue.endsWith(END_LITERAL))
325
		{
326
			return decryptValue(internalValue.substring(START_LITERAL.length(), (internalValue.length() - END_LITERAL.length())));
327
		}
328
						
329
		internalValue = internalValue.replaceAll(AMPERSAND, XML_AMPERSAND);
330
		String tag = null, val = null;
331
		
326
		
332
        //get the tag and value from the XML fragment
327
		if(cellValue == null){
333
		try {		
328
			return null;
334
			Document doc = XMLParserUtil.parse(internalValue);		
335
			tag = doc.getFirstChild().getNodeName();
336
			DPLLogImpl.log("Tag: " + tag); //$NON-NLS-1$
337
			val = doc.getFirstChild().getFirstChild().getNodeValue();
338
			DPLLogImpl.log("Val: " + val); //$NON-NLS-1$
339
	    }
340
	    catch (Throwable t) {
341
			DPLLogImpl.log(t.getMessage());
342
	    }
343
		
344
	    //try to find extension for evaluating tag
345
	    try 
346
		{
347
	    	IDatapoolEvaluatorExt evaluator = DatapoolUtil.findDatapoolEvaluator(tag);
348
	    	if(evaluator != null)
349
	    		return decryptValue(evaluator.evaluate(val));
350
		}
329
		}
351
	    catch (Exception e) {
330
352
			DPLLogImpl.log(e.getMessage());
331
		String trimmedCellValue = cellValue.trim();
332
333
		//Performance optimization to determine if an XML fragment cell value (e.g. <[tag]>[value]</[tag]>):
334
		if(trimmedCellValue.startsWith("<")) {
335
					
336
			//Performance optimization for unwrapping literal cell values (e.g. <Literal>[value]</Literal>):
337
			if((trimmedCellValue.startsWith(START_LITERAL)) && (trimmedCellValue.endsWith(END_LITERAL))){
338
				return (decryptValue(trimmedCellValue.substring((START_LITERAL.length()), (trimmedCellValue.length() - END_LITERAL.length()))));
339
			}
340
											
341
			try {		
342
				
343
				//Parse the cell value:
344
				Matcher matcher = XML_FRAGMENT_PATTERN.matcher(trimmedCellValue);
345
346
			    //Determine if an XML fragment cell value:
347
				if(matcher.matches()){
348
349
					//Resolve the datapool evaluator for the XML tag:
350
			    	IDatapoolEvaluatorExt evaluator = DatapoolUtil.findDatapoolEvaluator(matcher.group(1));
351
			    	
352
			    	//Determine if the datapool evaluator exists for the XML tag: 
353
			    	if(evaluator != null){
354
			    		return (decryptValue(evaluator.evaluate(matcher.group(2))));
355
			    	}
356
				}				
357
		    }
358
			catch (Throwable t) {
359
		    	//Ignore since an invalid/unsupported tag.
360
		    }	
353
		}
361
		}
354
		
362
355
	    return decryptValue(this.getValue());
363
		return (decryptValue(cellValue));
356
	}  
364
	}  
357
	
365
	
358
	/**
366
	/**
Lines 491-552 Link Here
491
	/**
499
	/**
492
	 * Replace the value associated with the cell.
500
	 * Replace the value associated with the cell.
493
	 * 
501
	 * 
494
	 * @param	value		The value to be associated with this cell.
502
	 * @param value The value to be associated with this cell.
495
	 * 
496
	 * @see #getCellValue()
503
	 * @see #getCellValue()
497
	 */	
504
	 */	
498
	public void setCellValue(Object value)
505
	public void setCellValue(Object cellObject){
499
	{
506
		
500
		if(value == null)
507
		if(cellObject == null){
501
		{
508
			setValue("");
502
			this.setValue("");
503
			return;
504
		}
509
		}
510
		else{
511
			
512
			String cellValue = cellObject.toString();
513
			String trimmedCellValue = cellValue.trim();
505
514
506
		String tag = null, val = null;
515
			//Performance optimization to determine if an XML fragment cell value (e.g. <[tag]>[value]</[tag]>):
507
516
			if(trimmedCellValue.startsWith("<")) {
508
		if(value.toString().indexOf("<") != -1) //$NON-NLS-1$
517
		        
509
		{
518
				try {	
510
	        //get the tag and value from the XML fragment
519
511
			try {	
520
					//Parse the cell value:
512
				Document doc = XMLParserUtil.parse(value.toString());					
521
					Matcher matcher = XML_FRAGMENT_PATTERN.matcher(trimmedCellValue);
513
				tag = doc.getFirstChild().getNodeName();
522
514
				val = doc.getFirstChild().getFirstChild().getNodeValue();
523
				    //Determine if an XML fragment cell value and a datapool evaluator exists for the tag:
515
		    }
524
					if((matcher.matches()) && (DatapoolUtil.findDatapoolEvaluator(matcher.group(1)) != null)){
516
		    catch (Throwable t) 
525
517
			{
526
			    		setValue(cellValue);
518
		    }
527
			    			
528
			    		return;
529
			    	}
530
			    }
531
			    catch (Throwable t) {
532
			    	//Ignore since an invalid/unsupported tag.
533
			    }
534
			}
535
			
536
			if((trimmedCellValue.startsWith(START_LITERAL)) && (trimmedCellValue.endsWith(END_LITERAL))){			
537
				setValue(cellValue);
538
			}
539
			else{
540
				setValue(wrapAsLiteral(cellValue));
541
			}
519
		}
542
		}
520
		
521
	    //try to find extension for evaluating tag
522
	    try 
523
		{
524
	    	if(tag != null && val != null)
525
	    	{
526
	    		IDatapoolEvaluatorExt evaluator = DatapoolUtil.findDatapoolEvaluator(tag);
527
	    		if(evaluator != null)
528
	    		{
529
	    			this.setValue(value.toString());
530
	    			return;
531
	    		}
532
	    	}
533
	    	
534
			//no extension point found which would match tag - throw exception
535
		    throw new DatapoolException(Messages.getString("DPLCellImpl.18") + tag); //$NON-NLS-1$
536
		}   
537
		catch (Throwable t) 
538
		{   
539
			String stringValue = value.toString();
540
			int index = stringValue.indexOf(START_LITERAL);
541
			if(index == -1)
542
			{			
543
				String wrappedValue = wrapAsLiteral(value.toString());
544
				this.setValue(wrappedValue);
545
				return;
546
			}				
547
			this.setValue(stringValue);
548
			return;
549
		}				
550
	}
543
	}
551
544
552
	/**
545
	/**
Lines 559-568 Link Here
559
		return this.getCellValue();
552
		return this.getCellValue();
560
	}
553
	}
561
554
562
	
563
	private static final String START_LITERAL = "<Literal>"; //$NON-NLS-1$
564
	private static final String END_LITERAL = "</Literal>"; //$NON-NLS-1$
565
566
	/*
555
	/*
567
	 * Wraps the text in tags that indicate the text is a literal.  All cell 
556
	 * Wraps the text in tags that indicate the text is a literal.  All cell 
568
	 * values in this release are literals.  Returns the wrapped string.
557
	 * values in this release are literals.  Returns the wrapped string.

Return to bug 249588