[MOBY-guts] biomoby commit
Paul Gordon
gordonp at dev.open-bio.org
Thu Dec 6 18:50:16 UTC 2007
gordonp
Thu Dec 6 13:50:15 EST 2007
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui
In directory dev.open-bio.org:/tmp/cvs-serv26798/src/main/ca/ucalgary/seahawk/gui
Modified Files:
MobySaveDialog.java
Log Message:
Added support for office document conversiomn for display in Seahawk via HTML conversion in a OpenOffice Web service
moby-live/Java/src/main/ca/ucalgary/seahawk/gui MobySaveDialog.java,1.4,1.5
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/MobySaveDialog.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/MobySaveDialog.java 2007/06/16 00:31:33 1.4
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/MobySaveDialog.java 2007/12/06 18:50:15 1.5
@@ -1,9 +1,14 @@
package ca.ucalgary.seahawk.gui;
import java.io.*;
+import java.net.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
-import ca.ucalgary.seahawk.util.DescriptiveFileFilter;
+import com.artofsolving.jodconverter.*;
+import com.artofsolving.jodconverter.openoffice.connection.*;
+import com.artofsolving.jodconverter.openoffice.converter.*;
+
+import ca.ucalgary.seahawk.util.*;
/**
* A utility to save the current document as XML or HTML, or the tab's history as a workflow (SCUFL format).
@@ -16,15 +21,53 @@
public static final String XML_DESC = "XML (MOBY data envelope format)";
public static final String TXT_DESC = "TXT (Plain Text Format)";
public static final String RTF_DESC = "RTF (Rich Text Format)";
+ public static final String CSV_DESC = "CSV (Comma Separated Values)";
+ public static final String TSV_DESC = "TSV (Tab Separated Values)";
+ public static final String PPT_DESC = "PPT (Microsoft PowerPoint Format)";
+ public static final String ODT_DESC = "ODT (Open Document Text)";
+ public static final String WPD_DESC = "WPD (WordPerfect Format)";
+ public static final String ODS_DESC = "ODS (Open Document Spreadsheet)";
+ public static final String ODP_DESC = "ODP (Open Document Presentation)";
+ public static final String DOC_DESC = "DOC (Microsoft Word Format)";
+ public static final String XLS_DESC = "XLS (Microsoft Excel Format)";
+ public static final String TEX_DESC = "TEX (TeX Typesetting File)";
+ public static final String PDF_DESC = "PDF (Portable Document Format)";
public static final String HTML_DESC = "HTML (Web Page)";
public static final String SCUFL_DESC = "Workflow Macro (Taverna SCUFL format)";
public static final String SCUFL_EXT = ".scufl";
public static final String XML_EXT = ".xml";
public static final String TXT_EXT = ".txt";
+ public static final String CSV_EXT = ".csv";
public static final String RTF_EXT = ".rtf";
+ public static final String PPT_EXT = ".ppt";
+ public static final String DOC_EXT = ".doc";
+ public static final String XLS_EXT = ".xls";
+ public static final String PDF_EXT = ".pdf";
+ public static final String ODP_EXT = ".odp";
+ public static final String ODS_EXT = ".ods";
+ public static final String ODT_EXT = ".odt";
+ public static final String TSV_EXT = ".tsv";
+ public static final String TEX_EXT = ".tex";
+ public static final String WPD_EXT = ".wpd";
public static final String HTML_EXT = ".html";
+ public static final String XLS_MIME = "application/msexcel";
+ public static final String DOC_MIME = "application/msword";
+ public static final String PPT_MIME = "application/mspowerpoint";
+ public static final String XLS_MIME2 = "application/vnd.ms-excel";
+ public static final String DOC_MIME2 = "application/vnd.ms-word";
+ public static final String PPT_MIME2 = "application/vnd.ms-powerpoint";
+ public static final String ODT_MIME = "application/vnd.oasis.opendocument.text";
+ public static final String ODS_MIME = "application/vnd.oasis.opendocument.spreadsheet";
+ public static final String ODP_MIME = "application/vnd.oasis.opendocument.presentation";
+ public static final String CSV_MIME = "text/csv"; //RFC4180
+ public static final String CSV_MIME2 = "text/comma-separated-values";//RFC4180
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(MobySaveDialog.class);
+
+ private static javax.swing.filechooser.FileFilter lastFilterUsed = null; //not java.io.FileFilter
+ private static File lastDirOpened = null;
+
public static void exportDocument(final MobyContentPane document) {
+
if (document != null) {
new Thread() {
@@ -141,4 +184,218 @@
}
}.start();
}
+
+ public static void showFileOpenDialog(MobyContentGUI mcg, boolean useDefaultHandler){
+ JFileChooser fileChooser = new JFileChooser();
+ fileChooser.setDialogTitle(mcg.FILE_CHOOSER_OPEN_TITLE);
+ fileChooser.setName(mcg.FILE_CHOOSER_OPEN_TITLE);
+
+ // Remember the last dir explored (if we have one)
+ if(lastDirOpened != null){
+ fileChooser.setCurrentDirectory(lastDirOpened);
+ }
+
+ fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
+
+ // Loadable only if a document converter is available
+ if(SeahawkOptions.getDocConverterHost() != null){
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(CSV_EXT,
+ CSV_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(DOC_EXT,
+ DOC_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(ODP_EXT,
+ ODP_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(ODS_EXT,
+ ODS_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(ODT_EXT,
+ ODT_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(PPT_EXT,
+ PPT_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(TSV_EXT,
+ TSV_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(WPD_EXT,
+ WPD_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(XLS_EXT,
+ XLS_DESC));
+ }
+
+ // Handled natively by Java or Seahawk
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(HTML_EXT,
+ HTML_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(RTF_EXT,
+ RTF_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(TEX_EXT,
+ TEX_DESC));
+ fileChooser.addChoosableFileFilter(new DescriptiveFileFilter(TXT_EXT,
+ TXT_DESC));
+ DescriptiveFileFilter xmlFilter = new DescriptiveFileFilter(XML_EXT,
+ XML_DESC);
+ fileChooser.addChoosableFileFilter(xmlFilter);
+ fileChooser.setAcceptAllFileFilterUsed(true);
+ if(lastFilterUsed != null){
+ fileChooser.setFileFilter(lastFilterUsed); // remember last thing the user did
+ }
+ else{
+ fileChooser.setFileFilter(xmlFilter); // show xml only by default
+ }
+
+ for(;;){
+ int choice = fileChooser.showOpenDialog(mcg);
+ if (choice == JFileChooser.APPROVE_OPTION) {
+ if(!fileChooser.getSelectedFile().exists()){
+ Object[] options = {"Try again", "Cancel"};
+ int response = JOptionPane.showOptionDialog(mcg,
+ "Sorry, the specified file does not exist",
+ "Invalid File Name",
+ JOptionPane.OK_CANCEL_OPTION,
+ JOptionPane.QUESTION_MESSAGE,
+ null,
+ options,
+ options[0]);
+ if(response == 1){ //Cancel
+ return;
+ }
+ else{
+ continue; // Try again
+ }
+ }
+
+ try{
+ // Remember the directory and filter for future reference
+ lastDirOpened = fileChooser.getCurrentDirectory();
+ lastFilterUsed = fileChooser.getFileFilter();
+
+ // If it's a microsoft format, convert it to RTF for display
+ // via OpenOffice, as Java can't handle displaying it natively
+ File fileToOpen = fileChooser.getSelectedFile();
+
+ mcg.loadPaneFromURL(fileToOpen.toURI().toURL(), useDefaultHandler);
+ return;
+ }
+ catch(Exception e){
+ e.printStackTrace();
+ JOptionPane.showMessageDialog(mcg,
+ "Could not load the data:\n"+e,
+ "Error Loading Data From File",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+ else{
+ return; // Cancel
+ }
+ } //end infinite for loop
+ }
+
+ public static URL convertToDisplayable(URL u) throws Exception{
+ URLConnection uc = convertToDisplayable(u.openConnection());
+ return uc == null ? null : uc.getURL();
+ }
+
+ /**
+ * A method for converting Microsoft Office documents (and perhaps other
+ * formats in the future) into text that Seahawk can load. Uses a remote
+ * OpenOffice server to do the conversion. The converted file will be
+ * deleted on exit.
+ */
+ public static URLConnection convertToDisplayable(URLConnection uc) throws Exception{
+ String conversionHost = SeahawkOptions.getDocConverterHost();
+ if(conversionHost == null){
+ return null; // do not do conversions if there is no host to perform them
+ }
+
+ URL u = uc.getURL();
+ String filePrefix = u.getPath();
+ filePrefix = filePrefix.substring(0, filePrefix.length()-4); // take off .doc or .xls
+ // take off any leading directory structure
+ if(filePrefix.indexOf("/") != -1 && filePrefix.indexOf("/") < filePrefix.length()-1){
+ filePrefix = filePrefix.substring(filePrefix.lastIndexOf("/")+1);
+ }
+ File outputFile = File.createTempFile(filePrefix,
+ getPreferredExtension(uc),
+ SeahawkOptions.getTempDir());
+ outputFile.delete(); // OpenOffice won't overwrite the file
+ outputFile.deleteOnExit();
+
+ // Create a local file for conversion if it's not one
+ // already (this is what JODConverter wants)
+ File fileToOpen = null;
+ if(u.getProtocol().equals("file")){
+ try{
+ fileToOpen = new File(u.toURI());
+ } catch(Exception e){
+ JOptionPane.showMessageDialog(null,
+ "Could not convert the file URL into a system file path:\n"+e,
+ "Error Converting URL to File",
+ JOptionPane.ERROR_MESSAGE);
+ throw e;
+ }
+ } else{
+ // Download the URL's data to a local temporary file for conversion
+ fileToOpen = File.createTempFile(filePrefix,
+ u.getFile().substring(u.getFile().length()-4),
+ SeahawkOptions.getTempDir());
+ fileToOpen.deleteOnExit();
+ OutputStream os = new FileOutputStream(fileToOpen);
+ os.write(HTMLUtils.getURLContentBytes(u));
+ os.close();
+ }
+
+
+ //System.err.println("Converting " + fileToOpen + " to " + outputFile);
+ // Connect to an OpenOffice.org instance running on some machine
+ OpenOfficeConnection connection = new SocketOpenOfficeConnection(conversionHost,
+ SeahawkOptions.getDocConverterPort());
+ connection.connect();
+
+ // convert, and if it's the localhost, use the more efficient converter
+ DocumentConverter converter = conversionHost.equals("localhost") ?
+ new OpenOfficeDocumentConverter(connection) :
+ new StreamOpenOfficeDocumentConverter(connection);
+ converter.convert(fileToOpen, outputFile);
+
+ // close the connection
+ connection.disconnect();
+ return outputFile.toURI().toURL().openConnection();
+
+ // TODO, when the conversion is to HTML for a presentation, many
+ // HTML files are created, and need to be downloaded
+ }
+
+ /**
+ * Given a file name to import, returns the file extension that Seahawk would
+ * prefer to convert the document to.
+ */
+ public static String getPreferredExtension(URLConnection uc){
+ String fileName = uc.getURL().getPath().toLowerCase();
+ String mimeType = uc.getContentType();
+
+ if(fileName.endsWith(XLS_EXT) || mimeType.equals(XLS_MIME) || mimeType.equals(XLS_MIME2) ||
+ fileName.endsWith(ODS_EXT) || mimeType.equals(ODS_MIME) ||
+ fileName.endsWith(TSV_EXT) || mimeType.equals(CSV_MIME) ||
+ fileName.endsWith(CSV_EXT) || mimeType.equals(CSV_MIME2)){
+ return HTML_EXT;
+ }
+ else if(fileName.endsWith(PPT_EXT) || mimeType.equals(PPT_MIME) || mimeType.equals(PPT_MIME2) ||
+ fileName.endsWith(ODP_EXT) || mimeType.equals(ODP_MIME)){
+ return HTML_EXT;
+ }
+ else{
+ return HTML_EXT;
+ }
+ }
+
+ public static boolean isConvertible(URLConnection uc){
+ if(SeahawkOptions.getDocConverterHost() == null){
+ return false; // do not do conversions if there is no host to perform them
+ }
+ String fileName = uc.getURL().getPath().toLowerCase();
+ String mimeType = uc.getContentType();
+ return fileName.endsWith(XLS_EXT) || mimeType.equals(XLS_MIME) || mimeType.equals(XLS_MIME2) ||
+ fileName.endsWith(ODS_EXT) || fileName.endsWith(CSV_EXT) ||
+ fileName.endsWith(PPT_EXT) || mimeType.equals(PPT_MIME) || mimeType.equals(PPT_MIME2) ||
+ fileName.endsWith(ODP_EXT) || mimeType.equals(ODT_MIME) || mimeType.equals(ODP_MIME) ||
+ mimeType.equals(ODS_MIME) || fileName.endsWith(CSV_EXT) || mimeType.equals(CSV_MIME) || mimeType.equals(CSV_MIME2) ||
+ fileName.endsWith(DOC_EXT) || mimeType.equals(DOC_MIME) || mimeType.equals(DOC_MIME2) ||
+ fileName.endsWith(WPD_EXT) || fileName.endsWith(ODT_EXT) || fileName.endsWith(CSV_EXT);
+ }
}
More information about the MOBY-guts
mailing list