From: <mwl...@us...> - 2008-03-27 21:25:40
|
Revision: 735 http://cishell.svn.sourceforge.net/cishell/?rev=735&view=rev Author: mwlinnem Date: 2008-03-27 14:25:36 -0700 (Thu, 27 Mar 2008) Log Message: ----------- If there is no way to save something such that it goes through a validator as it is saved, we now try to save it anyway. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/save.xml 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/SaveDataChooser.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/save.xml =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/save.xml 2008-03-27 21:06:14 UTC (rev 734) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/save.xml 2008-03-27 21:25:36 UTC (rev 735) @@ -8,7 +8,5 @@ <service> <provide interface= "org.cishell.framework.algorithm.AlgorithmFactory"/> - <provide interface= - "org.cishell.framework.algorithm.DataValidator"/> </service> </component> \ No newline at end of file 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 2008-03-27 21:06:14 UTC (rev 734) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java 2008-03-27 21:25:36 UTC (rev 735) @@ -88,8 +88,11 @@ public boolean save(Converter converter, Data data) { String outDataStr = (String)converter.getProperties().get(AlgorithmProperty.OUT_DATA); - String ext = outDataStr.substring(outDataStr.indexOf(':')+1); - + String ext = ""; + if (outDataStr.startsWith("file-ext:")) { + ext = outDataStr.substring(outDataStr.indexOf(':')+1); + } + if ((""+ext).startsWith(".")) { ext = ext.substring(1); } 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 2008-03-27 21:06:14 UTC (rev 734) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java 2008-03-27 21:25:36 UTC (rev 735) @@ -1,6 +1,5 @@ package org.cishell.reference.gui.persistence.save; -import java.io.File; import java.util.Dictionary; import java.util.Hashtable; @@ -9,6 +8,7 @@ import org.cishell.framework.algorithm.AlgorithmExecutionException; import org.cishell.framework.algorithm.AlgorithmFactory; import org.cishell.framework.algorithm.AlgorithmProperty; +import org.cishell.framework.data.BasicData; import org.cishell.framework.data.Data; import org.cishell.service.conversion.Converter; import org.cishell.service.conversion.DataConversionService; @@ -17,6 +17,7 @@ import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; import org.osgi.framework.ServiceReference; +import org.osgi.service.log.LogService; /** * Save algorithm for persisting a data object * @@ -30,6 +31,7 @@ private GUIBuilderService guiBuilder; private DataConversionService conversionManager; + private LogService log; /** * Sets up default services for the algorithm @@ -46,6 +48,7 @@ this.conversionManager = (DataConversionService) context.getService( DataConversionService.class.getName()); + this.log = (LogService) context.getService(LogService.class.getName()); this.guiBuilder = (GUIBuilderService)context.getService(GUIBuilderService.class.getName()); } @@ -55,45 +58,87 @@ * @return Null for this algorithm */ public Data[] execute() throws AlgorithmExecutionException { - try { - //This only checks the first Data in the array - final Converter[] converters = conversionManager.findConverters(data[0], "file-ext:*"); + //NOTE: A "converter" here is actually a converter path + //starting with the format for the data we want to save + //and ending in a output format + Data dataToSave = data[0]; - if (converters.length < 1 && !(data[0].getData() instanceof File)) { - guiBuilder.showError("No Converters", - "No valid converters for data type: " + - data[0].getFormat(), - "Please install a plugin that will save the data type to a file"); - } else { - parentShell = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell(); - - if (!parentShell.isDisposed()) { - guiRun(new Runnable() { - public void run() { - if (converters.length == 0) { - FileSaver saver = new FileSaver(parentShell, context); - saver.save(new NoConversionConverter(), data[0]); - } else if (converters.length == 1) { - final FileSaver saver = new FileSaver(parentShell, context); - saver.save(converters[0], data[0]); - } else { - SaveDataChooser sdc = new SaveDataChooser(data[0], - parentShell, converters, - "Save", - context); - sdc.createContent(new Shell(parentShell)); - sdc.open(); - } - }}); + //first, try to save the normal way, which is using validators to validate the output. + String saveThroughValidators = "file-ext:*"; + Object firstAttemptResult = tryToSave(dataToSave, saveThroughValidators); + if (firstAttemptResult instanceof Boolean) { + boolean succeeded = ((Boolean) firstAttemptResult).booleanValue(); + if (succeeded) { + System.out.println("Success"); + return null; //FILE SAVED SUCCESSFULLY. DONE. + } else { + System.out.println("No converter"); + this.log.log(LogService.LOG_WARNING, "No converters found that can save file through a validator." + + " Attempting to save without validating."); } + } else { //result instanceof Exception + Exception reasonForFailure = (Exception) firstAttemptResult; + this.log.log(LogService.LOG_WARNING, "Exception occurred while attempting to save" + + " file using a validator. Attempting to save without validating."); + System.out.println("Exception"); } - return null; - } catch (Throwable e) { - throw new AlgorithmExecutionException(e); + + System.out.println("Trying without validators"); + + //if saving with validators didn't work, try to save it without using validators + String saveWithoutValidators = "file:*"; + Object secondAttemptResult = tryToSave(dataToSave, saveWithoutValidators); + if (secondAttemptResult instanceof Boolean) { + boolean succeeded = ((Boolean) secondAttemptResult).booleanValue(); + if (succeeded) { + return null; //FILE SAVED SUCCESSFULLY. DONE. + } else { + throw new AlgorithmExecutionException("No converters found that could save file. Save failed"); + } + } else { //result instanceof Exception + Exception reasonForFailure2 = (Exception) secondAttemptResult; + throw new AlgorithmExecutionException("Exception occurred while attempting to save", reasonForFailure2); + } + } + + //returns True if save was successful + //return False if there are no converter chains available to save to the given format + //return an Exception if an exception occurred while attempting to save + private Object tryToSave(final Data dataToSave, String formatToSaveTo) { + final Converter[] converters = conversionManager.findConverters(dataToSave, formatToSaveTo); + if (converters.length == 0) {return new Boolean(false);}; + + parentShell = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell(); + if (parentShell.isDisposed()) {return makeParentShellDisposedException();}; + + try { + guiRun(new Runnable() { + public void run() { + if (converters.length == 1){ + //only one possible choice in how to save data. Just do it. + Converter onlyConverter = converters[0]; + final FileSaver saver = new FileSaver(parentShell, context); + saver.save(onlyConverter, dataToSave); + } else { //converters.length > 1 + //multiple ways to save the data. Let user choose. + SaveDataChooser saveChooser = new SaveDataChooser(dataToSave, + parentShell, converters, "Save", context); + saveChooser.createContent(new Shell(parentShell)); + saveChooser.open(); + } + }}); + } catch (Exception e) { + return e; + } + + return new Boolean(true); } - } - private void guiRun(Runnable run) { + private AlgorithmExecutionException makeParentShellDisposedException() { + return new AlgorithmExecutionException("Attempted to use disposed parent shell"); + } + + private void guiRun(Runnable run) { if (Thread.currentThread() == Display.getDefault().getThread()) { run.run(); } else { @@ -121,4 +166,8 @@ return props; } } + + private Data removeExtension(Data data) { + return new BasicData(data.getMetadata(), data, ""); + } } \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveDataChooser.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveDataChooser.java 2008-03-27 21:06:14 UTC (rev 734) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveDataChooser.java 2008-03-27 21:25:36 UTC (rev 735) @@ -17,7 +17,6 @@ import org.cishell.framework.algorithm.AlgorithmProperty; import org.cishell.framework.data.Data; import org.cishell.reference.gui.common.AbstractDialog; -import org.cishell.service.conversion.ConversionException; import org.cishell.service.conversion.Converter; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyleRange; 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 2008-03-27 21:06:14 UTC (rev 734) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java 2008-03-27 21:25:36 UTC (rev 735) @@ -23,7 +23,7 @@ * @author bmarkine * */ -public class SaveFactory implements AlgorithmFactory, DataValidator { +public class SaveFactory implements AlgorithmFactory { private CIShellContext context; /** @@ -45,26 +45,4 @@ this.context = context; return new Save(data, parameters, context); } - - /** - * Validate the SaveFactory can handle the incoming file type - * @param data The data to save - * @return empty string on success - */ - public String validate(Data[] data) { - DataConversionService conversionManager = (DataConversionService) context.getService( - DataConversionService.class.getName()); - - //Fix me - //Bonnie:why only check data[0]? An user can select multiple objects from data manager. - Converter[] converters = conversionManager.findConverters(data[0], "file-ext:*"); - if (converters.length == 0 && !(data[0].getData() instanceof File)) { - return "No valid converters from " + - data[0].getData().getClass().getName() + " to any file extension"; - } - else { - return ""; - } - } - } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |