From: <fu...@us...> - 2010-03-26 18:08:41
|
Revision: 1063 http://cishell.svn.sourceforge.net/cishell/?rev=1063&view=rev Author: fugu13 Date: 2010-03-26 18:08:35 +0000 (Fri, 26 Mar 2010) Log Message: ----------- Uses the ones in FileUtilities now. 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/view/core/FileViewer.java Removed Paths: ------------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java Deleted: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java 2010-03-26 18:03:14 UTC (rev 1062) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java 2010-03-26 18:08:35 UTC (rev 1063) @@ -1,138 +0,0 @@ -package org.cishell.reference.gui.persistence; - -import java.io.File; -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; - -import org.osgi.service.log.LogService; - -public class FileUtil { - public static final char FILENAME_CHARACTER_REPLACEMENT = '#'; - - /* Attempt to enumerate characters which cannot be used to name a file. - * For our purposes, this should be as aggressive as sensible. - * This includes all such characters for modern Windows systems, plus %. - * Please add any others. - */ - public static final Collection INVALID_FILENAME_CHARACTERS; - static { - Collection s = new HashSet(); - s.add(new Character('\\')); - s.add(new Character('/')); - s.add(new Character(':')); - s.add(new Character('*')); - s.add(new Character('?')); - s.add(new Character('"')); - s.add(new Character('<')); - s.add(new Character('>')); - s.add(new Character('|')); - s.add(new Character('%')); - INVALID_FILENAME_CHARACTERS = Collections.unmodifiableCollection(s); - } - - private static int uniqueIntForTempFile = 1; - - public static File getTempFile(String fileName, String extension, LogService logger) { - File tempFile; - - if (fileName == null || fileName.equals("")) { - fileName = "unknown"; - } - - if (extension == null || extension.equals("")) { - extension = ".txt"; - } - - if (!extension.startsWith(".")) { - extension = extension + "."; - } - - String tempPath = System.getProperty("java.io.tmpdir"); - File tempDir = new File(tempPath+File.separator+"nwb"); - if(!tempDir.exists()) - tempDir.mkdir(); - try{ - tempFile = File.createTempFile(fileName, extension, tempDir); - - }catch (IOException e1){ - //failed with given file name and extension. Let's use a standard one. - logger.log(LogService.LOG_WARNING, "Failed to create temp file with provided name and extension '" + fileName + extension + "'. Trying a generic name and extension instead.", e1); - try { - tempFile = File.createTempFile("unknown", ".txt", tempDir); - } catch (IOException e2) { - //looks like it doesn't even like that. We'll have to just make a file directly. - tempFile = new File (tempPath+File.separator+"nwb"+File.separator+"unknown" + uniqueIntForTempFile + ".txt"); - uniqueIntForTempFile++; - - logger.log(LogService.LOG_ERROR, "Failed to create temp file twice..."); - logger.log(LogService.LOG_ERROR, "First Try... \r\n " + e1.toString()); - logger.log(LogService.LOG_ERROR, "Second Try... \r\n " + e2.toString()); - } - } - return tempFile; - } - - public static String extractExtension(String format) { - String extension = ""; - /* TODO: We should really have explicit piece of metadata that says what - * the extension is, as this method is not guaranteed to yield the - * correct extension. - */ - if (format.startsWith("file:text/")) { - extension = "." + format.substring("file:text/".length()); - } else if (format.startsWith("file-ext:")) { - extension = "." + format.substring("file-ext:".length()); - } - - extension = extension.replace('+', '.'); - - return extension; - } - - public static String replaceInvalidFilenameCharacters(String filename) { - String cleanedFilename = filename; - - for (Iterator invalidCharacters = INVALID_FILENAME_CHARACTERS.iterator(); - invalidCharacters.hasNext();) { - char invalidCharacter = ((Character) invalidCharacters.next()).charValue(); - - cleanedFilename = - cleanedFilename.replace(invalidCharacter, FILENAME_CHARACTER_REPLACEMENT); - } - - return cleanedFilename; - } - - public static String extractFileName(String fileLabel) { - //index variables will be -1 if index is not found. - int descriptionEndIndex = fileLabel.lastIndexOf(":"); - int filePathEndIndex = fileLabel.lastIndexOf(File.separator); - - //doesn't matter if either variable is -1, since startIndex will be - //zero and none of the string will be cut off the front. - int startIndex = Math.max(descriptionEndIndex, filePathEndIndex) + 1; - - String fileNameWithExtension = fileLabel.substring(startIndex); - - //find the first character of the file name extension. - int extensionBeginIndex = fileNameWithExtension.lastIndexOf("."); - - int endIndex; - - if (extensionBeginIndex != -1) { - //we found a period in the file name. - endIndex = extensionBeginIndex; //cut off everything after - //first period. - } else { - //we didn't find an extension on the file name. - endIndex = fileNameWithExtension.length(); // don't cut any off the end. - } - - String fileNameWithoutExtension = fileNameWithExtension.substring(0, endIndex); - - return fileNameWithoutExtension; - } -} 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 2010-03-26 18:03:14 UTC (rev 1062) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java 2010-03-26 18:08:35 UTC (rev 1063) @@ -13,10 +13,10 @@ import org.cishell.framework.algorithm.AlgorithmProperty; import org.cishell.framework.data.Data; import org.cishell.framework.data.DataProperty; -import org.cishell.reference.gui.persistence.FileUtil; import org.cishell.service.conversion.ConversionException; import org.cishell.service.conversion.Converter; import org.cishell.service.guibuilder.GUIBuilderService; +import org.cishell.utilities.FileUtilities; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; @@ -118,9 +118,9 @@ dialog.setText("Choose File"); String fileLabel = (String)data.getMetadata().get(DataProperty.LABEL); - String suggestedFileName = FileUtil.extractFileName(fileLabel); + String suggestedFileName = FileUtilities.extractFileName(fileLabel); String cleanedSuggestedFileName = - FileUtil.replaceInvalidFilenameCharacters(suggestedFileName); + FileUtilities.replaceInvalidFilenameCharacters(suggestedFileName); dialog.setFileName(cleanedSuggestedFileName + "." + ext); // if (fileLabel == null) { 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 2010-03-26 18:03:14 UTC (rev 1062) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java 2010-03-26 18:08:35 UTC (rev 1063) @@ -6,7 +6,6 @@ import org.cishell.framework.CIShellContext; import org.cishell.framework.data.Data; import org.cishell.framework.data.DataProperty; -import org.cishell.reference.gui.persistence.FileUtil; import org.cishell.reference.gui.persistence.view.core.exceptiontypes.ConvertDataForViewingException; import org.cishell.reference.gui.persistence.view.core.exceptiontypes.FileViewingException; import org.cishell.reference.gui.persistence.view.core.exceptiontypes.NoProgramFoundException; @@ -251,7 +250,7 @@ FILE_EXTENSION_MIME_TYPE_PREFIX + fileExtension; File convertedFile = convertToFile( originalData, fileExtensionMimeType, converterManager); - String fileName = FileUtil.extractFileName(dataLabel); + String fileName = FileUtilities.extractFileName(dataLabel); return FileUtilities.createTemporaryFileCopy( convertedFile, fileName, fileExtension); @@ -279,8 +278,8 @@ String dataLabel = (String)originalData.getMetadata().get(DataProperty.LABEL); String dataFormat = originalData.getFormat(); - String fileName = FileUtil.extractFileName(dataLabel); - String fileExtension = FileUtil.extractExtension(dataFormat); + String fileName = FileUtilities.extractFileName(dataLabel); + String fileExtension = FileUtilities.extractExtension(dataFormat); try { File fileToView = FileUtilities. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |