Revision: 18429
http://sourceforge.net/p/gate/code/18429
Author: ian_roberts
Date: 2014-11-05 18:03:55 +0000 (Wed, 05 Nov 2014)
Log Message:
-----------
Added the concept of a "corpus exporter" - a document exporter that is also
capable of exporting a whole corpus in one go to a single file.
Modified Paths:
--------------
gate/trunk/src/main/gate/gui/DocumentExportMenu.java
Added Paths:
-----------
gate/trunk/src/main/gate/CorpusExporter.java
Added: gate/trunk/src/main/gate/CorpusExporter.java
===================================================================
--- gate/trunk/src/main/gate/CorpusExporter.java
(rev 0)
+++ gate/trunk/src/main/gate/CorpusExporter.java 2014-11-05 18:03:55 UTC
(rev 18429)
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 1995-2014, The University of Sheffield. See the file
+ * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ * This file is part of GATE (see http://gate.ac.uk/), and is free
+ * software, licenced under the GNU Library General Public License,
+ * Version 2, June 1991 (in the distribution as file licence.html,
+ * and also available at http://gate.ac.uk/gate/licence.html).
+ *
+ * Ian Roberts, 03/11/2014
+ *
+ */
+package gate;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.io.IOUtils;
+
+/**
+ * A {@link DocumentExporter} that is also capable of exporting
+ * a whole corpus to a single file.
+ */
+public abstract class CorpusExporter extends DocumentExporter {
+
+ public CorpusExporter(String fileType, String defaultExtension,
+ String mimeType) {
+ super(fileType, defaultExtension, mimeType);
+ }
+
+ /**
+ * Equivalent to {@link #export(Corpus,File,FeatureMap)} with an empty map
+ * of options.
+ */
+ public void export(Corpus corpus, File file) throws IOException {
+ export(corpus, file, Factory.newFeatureMap());
+ }
+
+ /**
+ * Equivalent to {@link #export(Corpus,OutputStream,FeatureMap)} using a
+ * FileOutputStream instance constructed from the File param.
+ */
+ public void export(Corpus corpus, File file, FeatureMap options)
+ throws IOException {
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(file);
+ export(corpus, new FileOutputStream(file), options);
+ out.flush();
+ } finally {
+ IOUtils.closeQuietly(out);
+ }
+ }
+
+ /**
+ * Equivalent to {@link #export(Corpus,OutputStream)} with an empty
+ * map of options.
+ */
+ public void export(Corpus corpus, OutputStream out) throws IOException {
+ export(corpus, out, Factory.newFeatureMap());
+ }
+
+ /**
+ * Exports the provided {@link Corpus} instance to the specified
+ * {@link OutputStream} using the specified options.
+ *
+ * @param corpus the corpus to export
+ * @param out the OutputStream to export the document to
+ * @param options DocumentExporter specific options
+ */
+ public abstract void export(Corpus corpus, OutputStream out, FeatureMap
options)
+ throws IOException;
+}
Property changes on: gate/trunk/src/main/gate/CorpusExporter.java
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Modified: gate/trunk/src/main/gate/gui/DocumentExportMenu.java
===================================================================
--- gate/trunk/src/main/gate/gui/DocumentExportMenu.java 2014-11-05
18:01:54 UTC (rev 18428)
+++ gate/trunk/src/main/gate/gui/DocumentExportMenu.java 2014-11-05
18:03:55 UTC (rev 18429)
@@ -14,6 +14,7 @@
package gate.gui;
import gate.Corpus;
+import gate.CorpusExporter;
import gate.Document;
import gate.DocumentExporter;
import gate.Factory;
@@ -117,6 +118,8 @@
Document document =
(handle.getTarget() instanceof Document ? (Document)handle
.getTarget() : null);
+ // are we looking for a file or a directory?
+ boolean singleFile = (document != null) || (de instanceof CorpusExporter);
if(document != null && document.getSourceUrl() != null) {
String fileName = "";
@@ -171,7 +174,7 @@
fileChooser.setFileFilter(de.getFileFilter());
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setDialogTitle("Save as " + de.getFileType());
- fileChooser.setFileSelectionMode(document != null
+ fileChooser.setFileSelectionMode(singleFile
? JFileChooser.FILES_ONLY
: JFileChooser.DIRECTORIES_ONLY);
@@ -184,7 +187,7 @@
return null;
selectedFile = fileChooser.getSelectedFile();
} else {
- if(!dialog.show(de, params, document != null, selectedFile != null
+ if(!dialog.show(de, params, singleFile, selectedFile != null
? selectedFile.getAbsolutePath()
: "")) return null;
@@ -243,164 +246,182 @@
+ de.getFileType() + " into " + " the file: "
+ selectedFile.toString() + " in "
+ ((double)time) / 1000 + "s");
- } else {
- try {
- File dir = selectedFile;
- // create the top directory if needed
- if(!dir.exists()) {
- if(!dir.mkdirs()) {
- JOptionPane.showMessageDialog(
- MainFrame.getInstance(),
- "Could not create top directory!", "GATE",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
+ } else { // corpus
+ if(de instanceof CorpusExporter) {
+
+ long start = System.currentTimeMillis();
+ listener.statusChanged("Saving as " + de.getFileType()
+ + " to " + selectedFile.toString() + "...");
+ try {
+
((CorpusExporter)de).export((Corpus)handle.getTarget(), selectedFile,
+ options);
+ } catch(IOException e) {
+ e.printStackTrace();
}
- MainFrame.lockGUI("Saving...");
-
- Corpus corpus = (Corpus)handle.getTarget();
-
- // iterate through all the docs and save
- // each of
- // them
- Iterator<Document> docIter = corpus.iterator();
- boolean overwriteAll = false;
- int docCnt = corpus.size();
- int currentDocIndex = 0;
- Set<String> usedFileNames = new HashSet<String>();
- while(docIter.hasNext()) {
- boolean docWasLoaded =
- corpus.isDocumentLoaded(currentDocIndex);
- Document currentDoc = docIter.next();
-
- URL sourceURL = currentDoc.getSourceUrl();
- String fileName = null;
- if(sourceURL != null) {
- fileName = sourceURL.getPath();
- fileName = Files.getLastPathComponent(fileName);
+ long time = System.currentTimeMillis() - start;
+ listener.statusChanged("Finished saving as "
+ + de.getFileType() + " into " + " the file: "
+ + selectedFile.toString() + " in "
+ + ((double)time) / 1000 + "s");
+ } else { // not a CorpusExporter
+ try {
+ File dir = selectedFile;
+ // create the top directory if needed
+ if(!dir.exists()) {
+ if(!dir.mkdirs()) {
+ JOptionPane.showMessageDialog(
+ MainFrame.getInstance(),
+ "Could not create top directory!",
"GATE",
+ JOptionPane.ERROR_MESSAGE);
+ return;
+ }
}
- if(fileName == null || fileName.length() == 0) {
- fileName = currentDoc.getName();
- }
- // makes sure that the filename does not
- // contain
- // any
- // forbidden character
- fileName =
- fileName.replaceAll("[\\/:\\*\\?\"<>|]",
"_");
- if(fileName.toLowerCase().endsWith(
- "." + de.getDefaultExtension())) {
+
+ MainFrame.lockGUI("Saving...");
+
+ Corpus corpus = (Corpus)handle.getTarget();
+
+ // iterate through all the docs and save
+ // each of
+ // them
+ Iterator<Document> docIter = corpus.iterator();
+ boolean overwriteAll = false;
+ int docCnt = corpus.size();
+ int currentDocIndex = 0;
+ Set<String> usedFileNames = new HashSet<String>();
+ while(docIter.hasNext()) {
+ boolean docWasLoaded =
+ corpus.isDocumentLoaded(currentDocIndex);
+ Document currentDoc = docIter.next();
+
+ URL sourceURL = currentDoc.getSourceUrl();
+ String fileName = null;
+ if(sourceURL != null) {
+ fileName = sourceURL.getPath();
+ fileName = Files.getLastPathComponent(fileName);
+ }
+ if(fileName == null || fileName.length() == 0) {
+ fileName = currentDoc.getName();
+ }
+ // makes sure that the filename does not
+ // contain
+ // any
+ // forbidden character
fileName =
- fileName.substring(0, fileName.length() -
5);
- }
- if(usedFileNames.contains(fileName)) {
- // name clash -> add unique ID
- String fileNameBase = fileName;
- int uniqId = 0;
- fileName = fileNameBase + "-" + uniqId++;
- while(usedFileNames.contains(fileName)) {
+ fileName.replaceAll("[\\/:\\*\\?\"<>|]",
"_");
+ if(fileName.toLowerCase().endsWith(
+ "." + de.getDefaultExtension())) {
+ fileName =
+ fileName.substring(0, fileName.length()
- 5);
+ }
+ if(usedFileNames.contains(fileName)) {
+ // name clash -> add unique ID
+ String fileNameBase = fileName;
+ int uniqId = 0;
fileName = fileNameBase + "-" + uniqId++;
+ while(usedFileNames.contains(fileName)) {
+ fileName = fileNameBase + "-" + uniqId++;
+ }
}
- }
- usedFileNames.add(fileName);
- if(!fileName.toLowerCase().endsWith(
- "." + de.getDefaultExtension()))
- fileName += "." + de.getDefaultExtension();
- File docFile = null;
- boolean nameOK = false;
- do {
- docFile = new File(dir, fileName);
- if(docFile.exists() && !overwriteAll) {
- // ask the user if we can overwrite
- // the file
- Object[] opts =
- new Object[] {"Yes", "All", "No",
- "Cancel"};
- MainFrame.unlockGUI();
- int answer =
- JOptionPane.showOptionDialog(
- MainFrame.getInstance(), "File "
- + docFile.getName()
- + " already exists!\n"
- + "Overwrite?", "GATE",
- JOptionPane.DEFAULT_OPTION,
- JOptionPane.WARNING_MESSAGE,
- null, opts, opts[2]);
- MainFrame.lockGUI("Saving...");
- switch(answer) {
- case 0: {
- nameOK = true;
- break;
- }
- case 1: {
- nameOK = true;
- overwriteAll = true;
- break;
- }
- case 2: {
- // user said NO, allow them to
- // provide
- // an
- // alternative name;
- MainFrame.unlockGUI();
- fileName =
- (String)JOptionPane.showInputDialog(
- MainFrame.getInstance(),
- "Please provide an
alternative file name",
- "GATE",
- JOptionPane.QUESTION_MESSAGE,
- null, null, fileName);
- if(fileName == null) {
+ usedFileNames.add(fileName);
+ if(!fileName.toLowerCase().endsWith(
+ "." + de.getDefaultExtension()))
+ fileName += "." + de.getDefaultExtension();
+ File docFile = null;
+ boolean nameOK = false;
+ do {
+ docFile = new File(dir, fileName);
+ if(docFile.exists() && !overwriteAll) {
+ // ask the user if we can overwrite
+ // the file
+ Object[] opts =
+ new Object[] {"Yes", "All", "No",
+ "Cancel"};
+ MainFrame.unlockGUI();
+ int answer =
+ JOptionPane.showOptionDialog(
+ MainFrame.getInstance(), "File
"
+ + docFile.getName()
+ + " already exists!\n"
+ + "Overwrite?", "GATE",
+ JOptionPane.DEFAULT_OPTION,
+ JOptionPane.WARNING_MESSAGE,
+ null, opts, opts[2]);
+ MainFrame.lockGUI("Saving...");
+ switch(answer) {
+ case 0: {
+ nameOK = true;
+ break;
+ }
+ case 1: {
+ nameOK = true;
+ overwriteAll = true;
+ break;
+ }
+ case 2: {
+ // user said NO, allow them to
+ // provide
+ // an
+ // alternative name;
+ MainFrame.unlockGUI();
+ fileName =
+
(String)JOptionPane.showInputDialog(
+ MainFrame.getInstance(),
+ "Please provide an
alternative file name",
+ "GATE",
+
JOptionPane.QUESTION_MESSAGE,
+ null, null, fileName);
+ if(fileName == null) {
+ handle.processFinished();
+ return;
+ }
+ MainFrame.lockGUI("Saving");
+ break;
+ }
+ case 3: {
+ // user gave up; return
handle.processFinished();
return;
}
- MainFrame.lockGUI("Saving");
- break;
}
- case 3: {
- // user gave up; return
- handle.processFinished();
- return;
- }
+
+ } else {
+ nameOK = true;
}
-
- } else {
- nameOK = true;
+ } while(!nameOK);
+ // save the file
+ try {
+ // do the actual exporting
+ de.export(currentDoc, docFile, options);
+ } catch(Exception ioe) {
+ MainFrame.unlockGUI();
+ JOptionPane.showMessageDialog(
+ MainFrame.getInstance(),
+ "Could not create write file:"
+ + ioe.toString(), "GATE",
+ JOptionPane.ERROR_MESSAGE);
+ ioe.printStackTrace(Err.getPrintWriter());
+ return;
}
- } while(!nameOK);
- // save the file
- try {
- // do the actual exporting
- de.export(currentDoc, docFile, options);
- } catch(Exception ioe) {
- MainFrame.unlockGUI();
- JOptionPane.showMessageDialog(
- MainFrame.getInstance(),
- "Could not create write file:"
- + ioe.toString(), "GATE",
- JOptionPane.ERROR_MESSAGE);
- ioe.printStackTrace(Err.getPrintWriter());
- return;
- }
-
- listener.statusChanged(currentDoc.getName()
- + " saved");
- // close the doc if it wasn't already
- // loaded
- if(!docWasLoaded) {
- corpus.unloadDocument(currentDoc);
- Factory.deleteResource(currentDoc);
- }
-
- handle.progressChanged(100 * currentDocIndex++
- / docCnt);
- }// while(docIter.hasNext())
- listener.statusChanged("Corpus Saved");
- handle.processFinished();
-
- } finally {
- MainFrame.unlockGUI();
+
+ listener.statusChanged(currentDoc.getName()
+ + " saved");
+ // close the doc if it wasn't already
+ // loaded
+ if(!docWasLoaded) {
+ corpus.unloadDocument(currentDoc);
+ Factory.deleteResource(currentDoc);
+ }
+
+ handle.progressChanged(100 * currentDocIndex++
+ / docCnt);
+ }// while(docIter.hasNext())
+ listener.statusChanged("Corpus Saved");
+ handle.processFinished();
+ } finally {
+ MainFrame.unlockGUI();
+ }
}
}
}
@@ -492,7 +513,7 @@
private ResourceParametersEditor parametersEditor;
- private boolean isDocument, userCanceled;
+ private boolean singleFile, userCanceled;
public DocumentExportDialog() {
super(MainFrame.getInstance(), "Save As...", true);
@@ -554,7 +575,7 @@
fileChooser.setFileFilter(de.getFileFilter());
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setDialogTitle("Save as " + de.getFileType());
- fileChooser.setFileSelectionMode(isDocument
+ fileChooser.setFileSelectionMode(singleFile
? JFileChooser.FILES_ONLY
: JFileChooser.DIRECTORIES_ONLY);
@@ -620,9 +641,9 @@
}
public synchronized boolean show(DocumentExporter de,
- List<List<Parameter>> params, boolean isDocument, String filePath)
{
+ List<List<Parameter>> params, boolean singleFile, String filePath)
{
- this.isDocument = isDocument;
+ this.singleFile = singleFile;
this.de = de;
setTitle("Save as " + de.getFileType());
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs