use of gate.CorpusExporter in project gate-core by GateNLP.
the class DocumentExportMenu method addExporter.
private void addExporter(final DocumentExporter de) {
if (itemByResource.containsKey(de))
return;
final ResourceData rd = Gate.getCreoleRegister().get(de.getClass().getCanonicalName());
if (DocumentExportMenu.this.getItemCount() == 1) {
DocumentExportMenu.this.addSeparator();
}
JMenuItem item = DocumentExportMenu.this.add(new AbstractAction(de.getFileType() + " (." + de.getDefaultExtension() + ")", MainFrame.getIcon(rd.getIcon(), rd.getResourceClassLoader())) {
@Override
public void actionPerformed(ActionEvent ae) {
List<List<Parameter>> params = rd.getParameterList().getRuntimeParameters();
final FeatureMap options = Factory.newFeatureMap();
final File selectedFile = getSelectedFile(params, de, options);
if (selectedFile == null)
return;
Runnable runnable = new Runnable() {
public void run() {
if (handle.getTarget() instanceof Document) {
long start = System.currentTimeMillis();
listener.statusChanged("Saving as " + de.getFileType() + " to " + selectedFile.toString() + "...");
try {
de.export((Document) handle.getTarget(), selectedFile, options);
} catch (IOException e) {
e.printStackTrace();
}
long time = System.currentTimeMillis() - start;
listener.statusChanged("Finished saving as " + de.getFileType() + " into " + " the file: " + selectedFile.toString() + " in " + ((double) time) / 1000 + "s");
} 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();
}
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;
}
}
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.replaceAll("[\\/:\\*\\?\"<>|]", "_");
if (fileName.toLowerCase().endsWith("." + de.getDefaultExtension())) {
fileName = fileName.substring(0, fileName.length() - de.getDefaultExtension().length() - 1);
}
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) {
handle.processFinished();
return;
}
MainFrame.lockGUI("Saving");
break;
}
case 3:
{
// user gave up; return
handle.processFinished();
return;
}
}
} 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;
}
listener.statusChanged(currentDoc.getName() + " saved");
// 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();
}
}
}
}
};
Thread thread = new Thread(Thread.currentThread().getThreadGroup(), runnable, "Document Exporter Thread");
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
});
itemByResource.put(de, item);
}
use of gate.CorpusExporter in project gate-core by GateNLP.
the class DocumentExportMenu method getSelectedFile.
private File getSelectedFile(List<List<Parameter>> params, DocumentExporter de, FeatureMap options) {
File selectedFile = null;
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 = "";
try {
fileName = document.getSourceUrl().toURI().getPath().trim();
} catch (URISyntaxException e) {
fileName = document.getSourceUrl().getPath().trim();
}
if (fileName.equals("") || fileName.equals("/")) {
if (document.getNamedAnnotationSets().containsKey("Original markups") && !document.getAnnotations("Original markups").get("title").isEmpty()) {
// use the title annotation if any
try {
fileName = document.getContent().getContent(document.getAnnotations("Original markups").get("title").firstNode().getOffset(), document.getAnnotations("Original markups").get("title").lastNode().getOffset()).toString();
} catch (InvalidOffsetException e) {
e.printStackTrace();
}
} else {
fileName = document.getSourceUrl().toString();
}
// cleans the file name
fileName = fileName.replaceAll("/", "_");
} else {
// replaces the extension with the default
fileName = fileName.replaceAll("\\.[a-zA-Z]{1,4}$", "." + de.getDefaultExtension());
}
// cleans the file name
fileName = fileName.replaceAll("[^/a-zA-Z0-9._-]", "_");
fileName = fileName.replaceAll("__+", "_");
// adds the default extension if not present
if (!fileName.endsWith("." + de.getDefaultExtension())) {
fileName += "." + de.getDefaultExtension();
}
selectedFile = new File(fileName);
}
if (params == null || params.isEmpty()) {
XJFileChooser fileChooser = MainFrame.getFileChooser();
fileChooser.resetChoosableFileFilters();
fileChooser.setFileFilter(de.getFileFilter());
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setDialogTitle("Save as " + de.getFileType());
fileChooser.setFileSelectionMode(singleFile ? JFileChooser.FILES_ONLY : JFileChooser.DIRECTORIES_ONLY);
if (selectedFile != null) {
fileChooser.ensureFileIsVisible(selectedFile);
fileChooser.setSelectedFile(selectedFile);
}
if (fileChooser.showSaveDialog(MainFrame.getInstance()) != JFileChooser.APPROVE_OPTION)
return null;
selectedFile = fileChooser.getSelectedFile();
} else {
if (!dialog.show(de, params, singleFile, selectedFile != null ? selectedFile.getAbsolutePath() : ""))
return null;
options.putAll(dialog.getSelectedParameters());
selectedFile = new File(dialog.getSelectedFileName());
}
return selectedFile;
}
Aggregations