|
From: <tan...@us...> - 2009-03-06 23:42:10
|
Revision: 865
http://cishell.svn.sourceforge.net/cishell/?rev=865&view=rev
Author: tankchintan
Date: 2009-03-06 23:41:41 +0000 (Fri, 06 Mar 2009)
Log Message:
-----------
Refactored the code for File > Load mechanism suitably. Made changes in all the input file validators to include detailed comments. Added ambiguous_extension field to all the validators that have ambgiuous_extension. for e.g. a CSV file can be a plain csv, NSF or SCOPUS. modified the codebase to handle the use cases generated by these.
Code Reviewed By Micah
Modified Paths:
--------------
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java 2009-03-04 15:54:38 UTC (rev 864)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java 2009-03-06 23:41:41 UTC (rev 865)
@@ -17,6 +17,7 @@
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
@@ -32,13 +33,17 @@
private CIShellContext ciShellContext;
private static String defaultLoadDirectory;
+ private final static String FILTER_EXTENSION_ALL = "*";
+ private final static String FILTER_AMBIGUOUS = "&(type=validator)(format_name=*)(in_data=file-ext:*)";
+ private final static String FILTER_IN_DATA = "&(type=validator)(format_name=*)";
+
public FileLoad(CIShellContext ciContext, BundleContext bContext,
Dictionary prefProperties) {
this.ciShellContext = ciContext;
this.bundleContext = bContext;
logger = (LogService) ciContext.getService(LogService.class.getName());
guiBuilder = (GUIBuilderService) ciContext
- .getService(GUIBuilderService.class.getName());
+ .getService(GUIBuilderService.class.getName());
// unpack preference properties
if (defaultLoadDirectory == null) {
@@ -90,32 +95,25 @@
// + "sampledata" +File.separator + "anything");
// }
// }
- System.err.println("defaultLoadDirectory is "
- + defaultLoadDirectory);
File currentDir = new File(defaultLoadDirectory); // ? good way to
- // do this?
+ // do this?
String absolutePath = currentDir.getAbsolutePath();
- System.err.println("absolutePath:" + absolutePath);
String name = currentDir.getName();
- System.err.println("name:" + name);
dialog.setFilterPath(absolutePath);
// dialog.setFilterPath(name);
dialog.setText("Select a File");
String fileName = dialog.open();
- System.out.println("Resulting file name!:" + fileName);
if (fileName == null) {
return;
}
File file = new File(fileName);
if (file.isDirectory()) {
- System.out.println("directory");
defaultLoadDirectory = file.getAbsolutePath();
} else {
// File parentFile = file.getParentFile();
// if (parentFile != null) {
- System.out.println("file");
defaultLoadDirectory = file.getParentFile().getAbsolutePath();
// }
}
@@ -123,44 +121,71 @@
String fileExtension = getFileExtension(file).toLowerCase();
/*
- * This filter is used to filter out all the services which are not,
+ * This filter is used to filter out all the services which are NOT,
* 1. validators 2. validators but for output file functionality
* Hence only input file functionality validators are allowed.
*/
- String preLoadValidatorFilter = "(&(type=validator)(in_data=file-ext:"
- + fileExtension + "))";
try {
// get all the service references of validators that can load
// this type of file.
- ServiceReference[] serviceReferences = bundleContext
- .getAllServiceReferences(AlgorithmFactory.class
- .getName(), preLoadValidatorFilter);
+ ServiceReference[] selectedFileServiceReferences = null;
+
+ if(fileExtension != null && fileExtension.length() > 0) {
+ selectedFileServiceReferences = getApplicableServiceReferences(
+ FILTER_IN_DATA, fileExtension);
+ }
+
/*
* This use case is for input files selected that are, 1.
* without any file extensions 2. with file extensions that do
* not match any service "in_data" field.
*/
- if (serviceReferences == null || serviceReferences.length == 0) {
+ if ((selectedFileServiceReferences == null || selectedFileServiceReferences.length == 0)) {
/*
* This filter is used to accept only those services which
* are, 1. type = validators and, 2. which have non-empty
* format_name and, 3. which have non-empty in_data field.
- * this is used so that all the validators for output file
- * functionality are filtered out.
+ * or, 3. which have empty in_data field and non-empty
+ * ambiguous_extension field. This is used so that all the
+ * validators for output file functionality are filtered
+ * out.
*/
- String validatorFilter = "(&(type=validator)(format_name=*)(in_data=file-ext:*))";
+ ServiceReference[] potentialValidators = null;
- ServiceReference[] allValidators = bundleContext
- .getAllServiceReferences(AlgorithmFactory.class
- .getName(), validatorFilter);
+ /*
+ * This is used to find validators that support ambiguous
+ * extensions for the provided file extension. There are
+ * good chances that the file selected does not have any
+ * file extension, this is handled by below case.
+ */
+ if (fileExtension != null && fileExtension.length() > 0) {
+
+ potentialValidators = getApplicableServiceReferences(
+ FILTER_AMBIGUOUS, fileExtension);
+
+ }
+
/*
+ * If no services are found then provide for all the
+ * validators list.
+ */
+
+ if (potentialValidators == null
+ || potentialValidators.length == 0) {
+
+ potentialValidators = getApplicableServiceReferences(
+ FILTER_IN_DATA, FILTER_EXTENSION_ALL);
+
+ }
+
+ /*
* SelectedFileServiceSelector is used to create a GUI for
* selecting a service for the selected file from a list of
* applicable services. On selection of a service it calls
@@ -176,79 +201,93 @@
new SelectedFileServiceSelector("Load", file, window
.getShell(), ciShellContext, bundleContext,
- allValidators, selectedServicesForLoadedFileList)
- .open();
+ potentialValidators,
+ selectedServicesForLoadedFileList).open();
}
/*
* This use case is for input files selected that have only one
- * applicable service. In this case the system goes ahead and
- * loads the file with that service.
+ * applicable service. Special case where the file extension
+ * belongs to ambiguous file extension group like csv is also
+ * handled. In the simple case system goes ahead and loads the
+ * file with that service.
*/
- else if (serviceReferences.length == 1) {
- AlgorithmFactory selectedValidatorExecutor = (AlgorithmFactory) bundleContext
- .getService(serviceReferences[0]);
- Data[] outputDataAfterValidation;
- Data[] inputDataForValidation = new Data[] { new BasicData(
- file.getPath(), String.class.getName()) };
- outputDataAfterValidation = selectedValidatorExecutor
- .createAlgorithm(inputDataForValidation, null,
- ciShellContext).execute();
+ else if (selectedFileServiceReferences.length == 1) {
/*
- * outputDataAfterValidation = null implies that file was
- * not loaded properly.
+ * To check for the files with ambiguous file extension a
+ * seperate list of service references is created.
*/
- if (outputDataAfterValidation != null) {
- loadFileSuccess = true;
- logger.log(LogService.LOG_INFO, "Loaded: "
- + file.getPath());
- for (int i = 0; i < outputDataAfterValidation.length; i++)
- selectedServicesForLoadedFileList
- .add(outputDataAfterValidation[i]);
+ ServiceReference[] selectedFileAmbiguousValidators = getApplicableServiceReferences(
+ FILTER_AMBIGUOUS, fileExtension);
+
+ /*
+ * If allAmbiguousValidators is not empty then the system
+ * provides a dialog box to select from the available
+ * validators. In this case CSV, NSF & SCOPUS
+ */
+
+ if ((selectedFileAmbiguousValidators != null && selectedFileAmbiguousValidators.length > 0)) {
+ new SelectedFileServiceSelector("Load", file, window
+ .getShell(), ciShellContext, bundleContext,
+ selectedFileAmbiguousValidators,
+ selectedServicesForLoadedFileList).open();
}
- }
+ /*
+ * If allAmbiguousValidators is not empty we go forward with
+ * normal work flow of loading the file with selected file
+ * format.
+ */
- /*
- * This use case is for input files selected that have more than
- * one applicable services. For e.g. ".xml" can be xgmml,
- * graphml, treeml. TODO: Right now this is handled by going
- * over all the selected services and loading the file with a
- * service that did not fail the validation test. For e.g. for a
- * ".xml" file which is treeml it will try out first graphml
- * then treeml. This will throw error on graphml which can be
- * resolved. A better approach will be to display
- * SelectedFileServiceSelector with graphml, treeml & xgmml
- * options.
- */
+ else {
- else if (serviceReferences.length > 1) {
- for (int index = 0; index < serviceReferences.length; index++) {
+ AlgorithmFactory selectedValidatorExecutor = (AlgorithmFactory) bundleContext
+ .getService(selectedFileServiceReferences[0]);
Data[] outputDataAfterValidation;
- AlgorithmFactory selectedValidatorExecutor = (AlgorithmFactory) bundleContext
- .getService(serviceReferences[index]);
Data[] inputDataForValidation = new Data[] { new BasicData(
file.getPath(), String.class.getName()) };
outputDataAfterValidation = selectedValidatorExecutor
- .createAlgorithm(inputDataForValidation, null,
- ciShellContext).execute();
+ .createAlgorithm(inputDataForValidation, null,
+ ciShellContext).execute();
+
+ /*
+ * outputDataAfterValidation = null implies that file
+ * was not loaded properly.
+ */
+
if (outputDataAfterValidation != null) {
loadFileSuccess = true;
logger.log(LogService.LOG_INFO, "Loaded: "
+ file.getPath());
- for (int i = 0; i < outputDataAfterValidation.length; i++) {
+ for (int i = 0; i < outputDataAfterValidation.length; i++)
selectedServicesForLoadedFileList
- .add(outputDataAfterValidation[i]);
- }
- break;
+ .add(outputDataAfterValidation[i]);
}
}
}
+
/*
+ * This use case is for input files selected that have more than
+ * one applicable services. For e.g. ".xml" can be xgmml,
+ * graphml, treeml. This now triggers the
+ * SelectedFileServiceSelector dialog & can be used to select
+ * any one of the available services for that particular file
+ * extension.
+ */
+
+ else if (selectedFileServiceReferences.length > 1) {
+
+ new SelectedFileServiceSelector("Load", file, window
+ .getShell(), ciShellContext, bundleContext,
+ selectedFileServiceReferences,
+ selectedServicesForLoadedFileList).open();
+
+ }
+ /*
* Bonnie: I commented out the following functions since when
* the application failed to load an nwb file, etc, the reader
* has report the error. It does not need this second error
@@ -278,6 +317,49 @@
}// end run()
+ /**
+ * @param selectedFileServiceReferencesFilter
+ * @param fileExtensionManipulations
+ * @return
+ * @throws InvalidSyntaxException
+ */
+ private ServiceReference[] getApplicableServiceReferences(
+ String selectedFileServiceReferencesFilter,
+ String fileExtensionManipulations)
+ throws InvalidSyntaxException {
+
+ String appliedValidatorFilter = getValidatorFilter(
+ selectedFileServiceReferencesFilter,
+ fileExtensionManipulations);
+
+ ServiceReference[] selectedFileServiceReferences = bundleContext
+ .getAllServiceReferences(AlgorithmFactory.class.getName(),
+ appliedValidatorFilter);
+
+ return selectedFileServiceReferences;
+
+ }
+
+ private String getValidatorFilter(
+ String selectedFileServiceReferencesFilter,
+ String fileExtensionManipulations) {
+
+ if (selectedFileServiceReferencesFilter.equals(FILTER_IN_DATA)) {
+ return "(" + selectedFileServiceReferencesFilter
+ + "(in_data=file-ext:" + fileExtensionManipulations
+ + ")" +
+ ")";
+
+ } else if (selectedFileServiceReferencesFilter.equals(FILTER_AMBIGUOUS)) {
+ return "(" + selectedFileServiceReferencesFilter
+ + "(ambiguous_extension=" + fileExtensionManipulations
+ + ")" +
+ ")";
+ }
+
+ return null;
+ }
+
public String getFileExtension(File theFile) {
String fileName = theFile.getName();
String extension;
@@ -290,9 +372,9 @@
} // end class
private IWorkbenchWindow getFirstWorkbenchWindow()
- throws AlgorithmExecutionException {
+ throws AlgorithmExecutionException {
final IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
- .getWorkbenchWindows();
+ .getWorkbenchWindows();
if (windows.length == 0) {
throw new AlgorithmExecutionException(
"Cannot obtain workbench window needed to open dialog.");
@@ -302,7 +384,7 @@
}
private Data[] extractLoadedFileData(FileLoadRunnable dataUpdater)
- throws AlgorithmExecutionException {
+ throws AlgorithmExecutionException {
Data[] loadedFileData;
try {
if (!dataUpdater.selectedServicesForLoadedFileList.isEmpty()) {
@@ -310,12 +392,12 @@
loadedFileData = new Data[size];
for (int index = 0; index < size; index++) {
loadedFileData[index] = (Data) dataUpdater.selectedServicesForLoadedFileList
- .get(index);
+ .get(index);
}
return loadedFileData;
} else {
this.logger
- .log(LogService.LOG_WARNING, "File loading canceled");
+ .log(LogService.LOG_WARNING, "File loading canceled");
return new Data[0];
}
} catch (Throwable e2) {
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java 2009-03-04 15:54:38 UTC (rev 864)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java 2009-03-06 23:41:41 UTC (rev 865)
@@ -46,13 +46,20 @@
private BundleContext bundleContext;
private ArrayList returnList;
- private static final String[] details_prop_names =
- {"format_name", "supported_file_extension", "format_description",
- "restorable_model_name", "restorable_model_description" };
- private static final String[] details_prop_name_descriptions =
- {"Format name", "Supported file extension", "Format description",
- "Restorable model name", "Restorable model description" };
+ private static final String[] DETAILS_ITEM_KEY =
+ {"format_name", "supported_file_extension", "format_description" };
+ /*
+ * Other possible keys could be restorable_model_name, restorable_model_description
+ * */
+
+ private static final String[] DETAILS_ITEM_KEY_DISPLAY_VALUE =
+ {"Format name", "Supported file extension", "Format description"};
+
+ /*
+ * Other possible keys display values could be "Restorable model name", "Restorable model description"
+ * */
+
public SelectedFileServiceSelector(String title, File theFile,
Shell parent, CIShellContext ciContext, BundleContext bContext,
ServiceReference[] persisterArray, ArrayList returnList){
@@ -142,13 +149,13 @@
private void updateDetailPane(ServiceReference persister) {
detailPane.setText("");
- for (int i=0; i<details_prop_names.length; i++){
- String val = (String) persister.getProperty(details_prop_names[i]);
+ for (int i=0; i<DETAILS_ITEM_KEY.length; i++){
+ String val = (String) persister.getProperty(DETAILS_ITEM_KEY[i]);
StyleRange styleRange = new StyleRange();
styleRange.start = detailPane.getText().length();
- detailPane.append(details_prop_name_descriptions[i] + ":\n");
- styleRange.length = details_prop_names[i].length() + 1;
+ detailPane.append(DETAILS_ITEM_KEY_DISPLAY_VALUE[i] + ":\n");
+ styleRange.length = DETAILS_ITEM_KEY[i].length() + 1;
styleRange.fontStyle = SWT.BOLD;
detailPane.setStyleRange(styleRange);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|