From: <pat...@us...> - 2011-01-07 18:41:55
|
Revision: 1189 http://cishell.svn.sourceforge.net/cishell/?rev=1189&view=rev Author: pataphil Date: 2011-01-07 18:41:48 +0000 (Fri, 07 Jan 2011) Log Message: ----------- * http://cns-jira.slis.indiana.edu/browse/SCISQUARED-360 * Also cleaned up code a little, as I read through it. * Reviewed by Joseph. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java 2011-01-06 16:37:08 UTC (rev 1188) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java 2011-01-07 18:41:48 UTC (rev 1189) @@ -22,11 +22,6 @@ import org.eclipse.swt.widgets.Shell; import org.osgi.service.log.LogService; -/** - * Persist the file to disk for the user - * - * @author Team - */ public class FileSaver { public static final String FILE_EXTENSION_PREFIX = "file-ext:"; @@ -37,17 +32,11 @@ private GUIBuilderService guiBuilder; private LogService log; - - /** - * Initializes services to output messages - * - * @param parent - * @param ciShellContext - */ - public FileSaver(Shell parent, CIShellContext context){ + public FileSaver(Shell parent, CIShellContext ciShellContext) { this.parent = parent; - this.guiBuilder = (GUIBuilderService)context.getService(GUIBuilderService.class.getName()); - this.log = (LogService) context.getService(LogService.class.getName()); + this.guiBuilder = + (GUIBuilderService) ciShellContext.getService(GUIBuilderService.class.getName()); + this.log = (LogService) ciShellContext.getService(LogService.class.getName()); } /** @@ -69,15 +58,17 @@ */ private boolean isSaveFileValid(File file) { boolean valid = false; + if (file.isDirectory()) { String message = "Destination cannot be a directory. Please choose a file"; guiBuilder.showError("Invalid Destination", message, ""); valid = false; } else if (file.exists()) { valid = confirmFileOverwrite(file); + } else { + valid = true; } - else - valid = true ; + return valid; } @@ -89,10 +80,10 @@ * @return Whether or not the save was successful */ public boolean save(Converter converter, Data data) { - String outDataStr = - (String) converter.getProperties().get(AlgorithmProperty.OUT_DATA); + String outDataStr = (String) converter.getProperties().get(AlgorithmProperty.OUT_DATA); String ext = ""; + if (outDataStr.startsWith(FILE_EXTENSION_PREFIX)) { ext = outDataStr.substring(FILE_EXTENSION_PREFIX.length()); } @@ -103,14 +94,13 @@ } FileDialog dialog = new FileDialog(parent, SWT.SAVE); - + if (currentDir == null) { - currentDir = new File(System.getProperty("user.home") + File.separator - + "anything"); + currentDir = new File(System.getProperty("user.home") + File.separator + "anything"); } + dialog.setFilterPath(currentDir.getPath()); - - + if (ext != null && !ext.equals("*")) { dialog.setFilterExtensions(new String[]{"*." + ext}); } Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java 2011-01-06 16:37:08 UTC (rev 1188) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java 2011-01-07 18:41:48 UTC (rev 1189) @@ -1,7 +1,5 @@ package org.cishell.reference.gui.persistence.save; -import java.util.Dictionary; - import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; import org.cishell.framework.algorithm.AlgorithmExecutionException; @@ -11,60 +9,43 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; -/** - * Save algorithm for persisting a data object - * - * @author bmarkine - */ + public class Save implements Algorithm { public static final String ANY_FILE_EXTENSION = "file-ext:*"; public static final String SAVE_DIALOG_TITLE = "Save"; - private Data[] data; - private CIShellContext context; + private Data data; + private CIShellContext ciShellContext; + private Shell parentShell; private DataConversionService conversionManager; - - /** - * Sets up default services for the algorithm - * - * @param data The data array to persist - * @param parameters Parameters for the algorithm - * @param ciShellContext Provides services to CIShell services - */ - public Save(Data[] data, Dictionary parameters, CIShellContext context) { + + public Save( + Data data, CIShellContext ciShellContext, DataConversionService conversionManager) { this.data = data; - this.context = context; + this.ciShellContext = ciShellContext; - this.conversionManager = (DataConversionService) - context.getService(DataConversionService.class.getName()); + this.conversionManager = conversionManager; } - /** - * @return Null when successful - */ public Data[] execute() throws AlgorithmExecutionException { - Data outData = data[0]; - - tryToSave(outData, ANY_FILE_EXTENSION); + tryToSave(this.data, ANY_FILE_EXTENSION); return null; } private void tryToSave(final Data outData, String outFormat) throws AlgorithmExecutionException { - final Converter[] converters = - conversionManager.findConverters(outData, outFormat); + final Converter[] converters = conversionManager.findConverters(outData, outFormat); + if (converters.length == 0) { - throw new AlgorithmExecutionException( - "Error: Calculated an empty converter chain."); + throw new AlgorithmExecutionException("Error: Calculated an empty converter chain."); } - - parentShell = - PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell(); - if (parentShell.isDisposed()) { - throw new AlgorithmExecutionException( - "Attempted to use disposed parent shell."); + + this.parentShell = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell(); + + if (this.parentShell.isDisposed()) { + throw new AlgorithmExecutionException("Attempted to use disposed parent shell."); } try { @@ -73,18 +54,17 @@ if (converters.length == 1) { // Only one possible choice in how to save data. Do it. Converter onlyConverter = converters[0]; - final FileSaver saver = - new FileSaver(parentShell, context); + FileSaver saver = new FileSaver(Save.this.parentShell, ciShellContext); saver.save(onlyConverter, outData); } else { // Multiple ways to save the data. Let user choose. - SaveDataChooser saveChooser = - new SaveDataChooser(outData, - parentShell, - converters, - SAVE_DIALOG_TITLE, - context); - saveChooser.createContent(new Shell(parentShell)); + SaveDataChooser saveChooser = new SaveDataChooser( + outData, + Save.this.parentShell, + converters, + SAVE_DIALOG_TITLE, + Save.this.ciShellContext); + saveChooser.createContent(new Shell(Save.this.parentShell)); saveChooser.open(); } } @@ -98,7 +78,7 @@ if (Thread.currentThread() == Display.getDefault().getThread()) { run.run(); } else { - parentShell.getDisplay().syncExec(run); + this.parentShell.getDisplay().syncExec(run); } } } \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java 2011-01-06 16:37:08 UTC (rev 1188) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java 2011-01-07 18:41:48 UTC (rev 1189) @@ -3,13 +3,10 @@ import java.util.Dictionary; import org.cishell.framework.CIShellContext; -import org.cishell.framework.LocalCIShellContext; import org.cishell.framework.algorithm.Algorithm; import org.cishell.framework.algorithm.AlgorithmFactory; import org.cishell.framework.data.Data; -import org.osgi.service.cm.ConfigurationException; -import org.osgi.service.cm.ManagedService; -import org.osgi.service.component.ComponentContext; +import org.cishell.service.conversion.DataConversionService; /** * Create a Save object @@ -18,32 +15,14 @@ * no final file:X->file-ext:* converter. * */ -public class SaveFactory implements AlgorithmFactory, ManagedService { - private CIShellContext ciShellContext; +public class SaveFactory implements AlgorithmFactory { + public Algorithm createAlgorithm( + Data[] data, Dictionary<String, Object> parameters, CIShellContext ciShellContext) { + Data inputData = data[0]; + DataConversionService conversionManager = + (DataConversionService) ciShellContext.getService( + DataConversionService.class.getName()); - /** - * Create a local CIShell ciShellContext - * @param componentContext The current CIShell ciShellContext - */ - protected void activate(ComponentContext componentContext) { - ciShellContext = - new LocalCIShellContext(componentContext.getBundleContext()); + return new Save(inputData, ciShellContext, conversionManager); } - - public void updated(Dictionary properties) throws ConfigurationException { - } - - /** - * Create a Save algorithm - * @param data The data objects to save - * @param parameters The parameters for the algorithm - * @param ciShellContext Reference to services provided by CIShell - * @return An instance of the Save algorithm - */ - public Algorithm createAlgorithm(Data[] data, - Dictionary parameters, - CIShellContext ciShellContext) { - this.ciShellContext = ciShellContext; - return new Save(data, parameters, ciShellContext); - } } \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java 2011-01-06 16:37:08 UTC (rev 1188) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java 2011-01-07 18:41:48 UTC (rev 1189) @@ -1,7 +1,5 @@ package org.cishell.reference.gui.persistence.view; -import java.util.Dictionary; - import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; import org.cishell.framework.algorithm.AlgorithmExecutionException; @@ -19,27 +17,25 @@ private LogService logger; public FileView( - Data[] data, Dictionary parameters, CIShellContext context) { + Data[] data, + CIShellContext ciShellContext, + DataConversionService conversionManager, + LogService logger) { this.dataToView = data; - this.ciShellContext = context; - - this.conversionManager = (DataConversionService)context.getService( - DataConversionService.class.getName()); - this.logger = (LogService)context.getService(LogService.class.getName()); + this.ciShellContext = ciShellContext; + this.conversionManager = conversionManager; + this.logger = logger; } public Data[] execute() throws AlgorithmExecutionException { - for (int ii = 0; ii < this.dataToView.length; ii++) { + for (Data data : this.dataToView) { try { - FileViewer.viewDataFile(this.dataToView[ii], - this.ciShellContext, - this.conversionManager, - this.logger); + FileViewer.viewDataFile( + data, this.ciShellContext, this.conversionManager, this.logger); } catch (FileViewingException fileViewingException) { - String logMessage = - "Error: Unable to view data \"" + - this.dataToView[ii].getMetadata().get(DataProperty.LABEL) + - "\"."; + String logMessage = String.format( + "Error: Unable to view data \"%s\".", + data.getMetadata().get(DataProperty.LABEL)); this.logger.log(LogService.LOG_ERROR, logMessage); } Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java 2011-01-06 16:37:08 UTC (rev 1188) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java 2011-01-07 18:41:48 UTC (rev 1189) @@ -6,12 +6,18 @@ import org.cishell.framework.algorithm.Algorithm; import org.cishell.framework.algorithm.AlgorithmFactory; import org.cishell.framework.data.Data; +import org.cishell.service.conversion.DataConversionService; +import org.osgi.service.log.LogService; public class FileViewFactory implements AlgorithmFactory { - public Algorithm createAlgorithm(Data[] data, - Dictionary parameters, - CIShellContext ciShellContext) { - return new FileView(data, parameters, ciShellContext); + public Algorithm createAlgorithm( + Data[] data, Dictionary<String, Object> parameters, CIShellContext ciShellContext) { + DataConversionService conversionManager = + (DataConversionService) ciShellContext.getService( + DataConversionService.class.getName()); + LogService logger = (LogService) ciShellContext.getService(LogService.class.getName()); + + return new FileView(data, ciShellContext, conversionManager, logger); } } \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java 2011-01-06 16:37:08 UTC (rev 1188) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java 2011-01-07 18:41:48 UTC (rev 1189) @@ -2,8 +2,11 @@ import java.io.File; import java.io.IOException; +import java.util.Dictionary; +import java.util.Enumeration; import org.cishell.framework.CIShellContext; +import org.cishell.framework.data.BasicData; import org.cishell.framework.data.Data; import org.cishell.framework.data.DataProperty; import org.cishell.reference.gui.persistence.view.core.exceptiontypes.ConvertDataForViewingException; @@ -36,11 +39,11 @@ public static final String ANY_FILE_FORMAT_PATTERN = "(file:.*)|(file-ext:.*)"; - public static void viewDataFile(Data data, - CIShellContext ciShellContext, - DataConversionService conversionManager, - LogService logger) - throws FileViewingException { + public static void viewDataFile( + Data data, + CIShellContext ciShellContext, + DataConversionService conversionManager, + LogService logger) throws FileViewingException { viewDataFileWithProgram(data, "", ciShellContext, conversionManager, logger); } @@ -49,11 +52,9 @@ String customFileExtension, CIShellContext ciShellContext, DataConversionService converterManager, - LogService logger) - throws FileViewingException { - FileWithExtension fileWithExtension = - convertDataForViewing(data, ciShellContext, converterManager, - logger); + LogService logger) throws FileViewingException { + FileWithExtension fileWithExtension = convertDataForViewing( + data, ciShellContext, converterManager, logger); viewFileWithExtension(fileWithExtension, customFileExtension); } @@ -64,16 +65,18 @@ LogService logger) throws FileViewingException { try { String dataFormat = data.getFormat(); - //TODO: Add image viewing support here (shouldn't be too hard) + // TODO: Add image viewing support here (shouldn't be too hard). if (dataIsDB(data, converterManager)) { try { - Data genericDBData = converterManager.convert(data, Database.GENERIC_DB_MIME_TYPE); - Database genericDatabase = (Database) genericDBData.getData(); + Data genericDBData = + converterManager.convert(data, Database.GENERIC_DB_MIME_TYPE); + Database genericDatabase = (Database) genericDBData.getData(); + + File dbSchemaOverview = + DatabaseSchemaOverviewGenerator.generateDatabaseSchemaOverview( + genericDatabase); - File dbSchemaOverview = - DatabaseSchemaOverviewGenerator.generateDatabaseSchemaOverview(genericDatabase); - - return new FileWithExtension(dbSchemaOverview, TXT_FILE_EXTENSION); + return new FileWithExtension(dbSchemaOverview, TXT_FILE_EXTENSION); } catch (ConversionException e) { //continue attempts to view for other formats } catch (Exception e) { @@ -85,13 +88,12 @@ } if (isCSVFormat(data)) { /* - * The data is already a CSV file, so it just needs to - * be copied. + * The data is already a CSV file, so it just needs to be copied. */ try { File csvFileForViewing = FileUtilities.createTemporaryFileCopy( - (File)data.getData(), + (File) data.getData(), TEMPORARY_CSV_FILE_NAME, CSV_FILE_EXTENSION); @@ -110,16 +112,14 @@ File preparedFileForViewing = prepareFileForViewing( data, CSV_FILE_EXTENSION, converterManager); - return new FileWithExtension( - preparedFileForViewing, CSV_FILE_EXTENSION); + return new FileWithExtension(preparedFileForViewing, CSV_FILE_EXTENSION); } else if (dataIsFile(data, dataFormat)) { /* * The data is already a text-based file, so it just needs to * be copied to a temporary file for viewing in the default * text-viewing program. */ - return new FileWithExtension( - prepareTextFileForViewing(data), TXT_FILE_EXTENSION); + return new FileWithExtension(prepareTextFileForViewing(data), TXT_FILE_EXTENSION); } else if (convertersExist( data, ANY_FILE_EXTENSION_FILTER, converterManager)) { /* @@ -128,29 +128,24 @@ * text-viewing program. */ return new FileWithExtension( - convertDataToTextFile( - data, converterManager, ciShellContext), + convertDataToTextFile(data, converterManager, ciShellContext), "txt"); } else { - String exceptionMessage = - "No converters exist for the data \"" + - data.getMetadata().get(DataProperty.LABEL) + - "\"."; - + String exceptionMessage = String.format( + "No converters exist for the data \"%s\".", + data.getMetadata().get(DataProperty.LABEL)); + throw new ConvertDataForViewingException(exceptionMessage); } - } catch (ConvertDataForViewingException - convertDataForViewingException) { - String exceptionMessage = - "There was a problem when preparing the data \"" + - data.getMetadata().get(DataProperty.LABEL) + - "\" for viewing."; + } catch (ConvertDataForViewingException e) { + String exceptionMessage = String.format( + "There was a problem when preparing the data \"%s\" for viewing.", + data.getMetadata().get(DataProperty.LABEL)); - throw new FileViewingException( - exceptionMessage, convertDataForViewingException); + throw new FileViewingException(exceptionMessage, e); } } - + private static void viewFileWithExtension( FileWithExtension fileWithExtension, String customFileExtension) throws FileViewingException { @@ -160,47 +155,33 @@ executeProgramWithFile(program, fileWithExtension.file); } catch (NoProgramFoundException noProgramFoundException) { - String exceptionMessage = - "Could not view the file \"" + - fileWithExtension.file.getAbsolutePath() + - "\" because no viewing program could be found for it."; + String exceptionMessage = String.format( + "Could not view the file \"%s\" because no viewing program could be found for it.", + fileWithExtension.file.getAbsolutePath()); - throw new FileViewingException( - exceptionMessage, noProgramFoundException); + throw new FileViewingException(exceptionMessage, noProgramFoundException); } } - + private static boolean isCSVFormat(Data data) { String dataFormat = data.getFormat(); - if (dataFormat.startsWith(CSV_MIME_TYPE) || - dataFormat.startsWith(CSV_FILE_EXT)) { + if (dataFormat.startsWith(CSV_MIME_TYPE) || dataFormat.startsWith(CSV_FILE_EXT)) { return true; } else { return false; } } - - /*private static boolean dataIsCSVCompatibleFile( - Data data, - DataConversionService converterManager) { - return convertersExist(data, CSV_FILE_EXT, converterManager); - }*/ - - private static boolean dataIsCSVCompatible( - Data data, - DataConversionService converterManager) { - if (isCSVFormat(data) || - convertersExist(data, CSV_FILE_EXT, converterManager)) { + + private static boolean dataIsCSVCompatible(Data data, DataConversionService converterManager) { + if (isCSVFormat(data) || convertersExist(data, CSV_FILE_EXT, converterManager)) { return true; } else { return false; } } - - private static boolean dataIsDB ( - Data data, - DataConversionService converterManager) { + + private static boolean dataIsDB(Data data, DataConversionService converterManager) { if (has_DB_MimeType_Prefix(data) || convertersExist(data, Database.GENERIC_DB_MIME_TYPE, converterManager)) { return true; @@ -208,11 +189,11 @@ return false; } } - + private static boolean has_DB_MimeType_Prefix(Data data) { return data.getFormat().startsWith(Database.DB_MIME_TYPE_PREFIX); } - + private static boolean dataIsFile(Data data, String dataFormat) { if (data.getData() instanceof File || dataFormat.startsWith(ANY_MIME_TYPE) || @@ -222,51 +203,45 @@ return false; } } - + private static boolean convertersExist( - Data data, - String targetFormat, - DataConversionService conversionManager) { - final Converter[] converters = - conversionManager.findConverters(data, targetFormat); - + Data data, String targetFormat, DataConversionService conversionManager) { + final Converter[] converters = conversionManager.findConverters(data, targetFormat); + if (converters.length > 0) { return true; } else { return false; } } - + private static File prepareFileForViewing( Data originalData, String fileExtension, - DataConversionService converterManager) - throws ConvertDataForViewingException { - String dataLabel = - (String)originalData.getMetadata().get(DataProperty.LABEL); - + DataConversionService converterManager) throws ConvertDataForViewingException { + String dataLabel = (String) originalData.getMetadata().get(DataProperty.LABEL); + try { - String fileExtensionMimeType = - FILE_EXTENSION_MIME_TYPE_PREFIX + fileExtension; + String fileExtensionMimeType = FILE_EXTENSION_MIME_TYPE_PREFIX + fileExtension; File convertedFile = convertToFile( originalData, fileExtensionMimeType, converterManager); String fileName = FileUtilities.extractFileName(dataLabel); + String cleanedFileName = FileUtilities.replaceInvalidFilenameCharacters(fileName); return FileUtilities.createTemporaryFileCopy( - convertedFile, fileName, fileExtension); - } catch (ConversionException convertingDataToFileException) { - String exceptionMessage = - "A ConversionException occurred when converting the data \"" + - dataLabel + - "\" to " + fileExtension + "."; + convertedFile, cleanedFileName, fileExtension); + } catch (ConversionException e) { + String exceptionMessage = String.format( + "A ConversionException occurred when converting the data \"%s\" to %s.", + dataLabel, + fileExtension); - throw new ConvertDataForViewingException( - exceptionMessage, convertingDataToFileException); + throw new ConvertDataForViewingException(exceptionMessage, e); } catch (FileCopyingException temporaryFileCopyingException) { - String exceptionMessage = - "A FileCopyingException occurred when converting the data \"" + - dataLabel + - "\" to " + fileExtension + "."; + String exceptionMessage = String.format( + "A FileCopyingException occurred when converting the data \"%s\" to %s.", + dataLabel, + fileExtension); throw new ConvertDataForViewingException( exceptionMessage, temporaryFileCopyingException); @@ -275,8 +250,7 @@ private static File prepareTextFileForViewing(Data originalData) throws ConvertDataForViewingException { - String dataLabel = - (String)originalData.getMetadata().get(DataProperty.LABEL); + String dataLabel = (String)originalData.getMetadata().get(DataProperty.LABEL); String dataFormat = originalData.getFormat(); String suggestedFileName = FileUtilities.extractFileName(dataLabel); String cleanedSuggestedFileName = @@ -284,9 +258,8 @@ String fileExtension = FileUtilities.extractExtension(dataFormat); try { - File fileToView = FileUtilities. - createTemporaryFileInDefaultTemporaryDirectory( - cleanedSuggestedFileName, fileExtension); + File fileToView = FileUtilities.createTemporaryFileInDefaultTemporaryDirectory( + cleanedSuggestedFileName, fileExtension); FileUtilities.copyFile((File)originalData.getData(), fileToView); return fileToView; @@ -306,8 +279,7 @@ private static File convertDataToTextFile( Data originalData, DataConversionService converterManager, - CIShellContext ciShellContext) - throws ConvertDataForViewingException { + CIShellContext ciShellContext) throws ConvertDataForViewingException { final Converter[] converters = converterManager.findConverters( originalData, ANY_FILE_EXTENSION_FILTER); @@ -317,20 +289,15 @@ * the conversion. */ try { - return convertToFile( - originalData, converters[0]); - } - catch (ConversionException - convertDataToFileAndPrepareForViewingException) { - String exceptionMessage = - "A ConversionException occurred when converting the " + - "data \"" + - originalData.getMetadata().get(DataProperty.LABEL) + - "\" to a file format."; + return convertToFile(originalData, converters[0]); + } catch (ConversionException e) { + String format = + "A ConversionException occurred when converting the data \"%s\" " + + "to a file format."; + String exceptionMessage = String.format( + format, originalData.getMetadata().get(DataProperty.LABEL)); - throw new ConvertDataForViewingException( - exceptionMessage, - convertDataToFileAndPrepareForViewingException); + throw new ConvertDataForViewingException(exceptionMessage, e); } } else { /* @@ -341,25 +308,20 @@ return convertDataBasedOffUserChosenConverter( originalData, converters, ciShellContext); } catch (ConversionException conversionException) { - String exceptionMessage = - "A ConversionException occurred when converting the " + - "data \"" + - originalData.getMetadata().get(DataProperty.LABEL) + - "\"."; + String exceptionMessage = String.format( + "A ConversionException occurred when converting the data \"%s\".", + originalData.getMetadata().get(DataProperty.LABEL)); - throw new ConvertDataForViewingException( - exceptionMessage, conversionException); - } catch (UserCanceledDataViewSelectionException - userCanceledDataViewSelectionException) { - String exceptionMessage = + throw new ConvertDataForViewingException(exceptionMessage, conversionException); + } catch (UserCanceledDataViewSelectionException e) { + String format = "A UserCanceledDataViewSelectionException occurred " + "when the user did not choose a converter for the " + - "data \"" + - originalData.getMetadata().get(DataProperty.LABEL) + - "\"."; + "data \"%s\"."; + String exceptionMessage = String.format( + format, originalData.getMetadata().get(DataProperty.LABEL)); - throw new ConvertDataForViewingException( - exceptionMessage, userCanceledDataViewSelectionException); + throw new ConvertDataForViewingException(exceptionMessage, e); } } } @@ -432,10 +394,11 @@ } - private static File convertToFile(Data data, Converter converter) - throws ConversionException { - Data newData = converter.convert(data); - return (File)newData.getData(); + private static File convertToFile(Data data, Converter converter) throws ConversionException { + Data dataWithCleanedLabelForConversion = cloneDataWithCleanedLabelForConversion(data); + Data newData = converter.convert(dataWithCleanedLabelForConversion); + + return (File) newData.getData(); } private static File convertDataBasedOffUserChosenConverter( @@ -527,6 +490,25 @@ this.selectedConverter = viewDataChooser.getSelectedConverter(); } } + + private static Data cloneDataWithCleanedLabelForConversion(Data originalData) { + Data clonedData = new BasicData(originalData.getData(), originalData.getFormat()); + Dictionary<String, Object> originalMetadata = originalData.getMetadata(); + Dictionary<String, Object> clonedMetadata = clonedData.getMetadata(); + + for (Enumeration<String> keys = originalMetadata.keys(); keys.hasMoreElements();) { + String key = keys.nextElement(); + + if (DataProperty.LABEL.equals(key)) { + clonedMetadata.put(key, FileUtilities.replaceInvalidFilenameCharacters( + (String) originalMetadata.get(key))); + } else { + clonedMetadata.put(key, originalMetadata.get(key)); + } + } + + return clonedData; + } private static class FileWithExtension { public final File file; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |