Revision: 17660
http://sourceforge.net/p/gate/code/17660
Author: markagreenwood
Date: 2014-03-14 14:59:44 +0000 (Fri, 14 Mar 2014)
Log Message:
-----------
added the new export methods to DocumentFormat as well as an example in the
FastInfoset plugin -- it would be nice to add some GUI code so that the export
options become menu items automatically without needing resource helpers
Modified Paths:
--------------
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetDocumentFormat.java
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetExporter.java
gate/trunk/src/main/gate/DocumentFormat.java
Modified:
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetDocumentFormat.java
===================================================================
---
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetDocumentFormat.java
2014-03-14 09:19:11 UTC (rev 17659)
+++
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetDocumentFormat.java
2014-03-14 14:59:44 UTC (rev 17660)
@@ -29,8 +29,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
+import java.util.Map;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -46,11 +48,14 @@
@CreoleResource(name = "Fast Infoset Document Format", isPrivate = true,
autoinstances = {@AutoInstance(hidden = true)})
public class FastInfosetDocumentFormat extends TextualDocumentFormat {
+ private static final long serialVersionUID = -2394353168913311584L;
+
private static StAXManager staxManager;
/** Default construction */
public FastInfosetDocumentFormat() {
super();
+ supportsExport = true;
}
/** We could collect repositioning information during XML parsing */
@@ -330,4 +335,13 @@
return this;
}
+ @Override
+ public void export(Document doc, OutputStream out, Map<String,Object>
options) throws IOException {
+ try {
+ FastInfosetExporter.export(doc, out);
+ }
+ catch (Exception e) {
+ throw new IOException("Error exporting document",e);
+ }
+ }
}
\ No newline at end of file
Modified:
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetExporter.java
===================================================================
---
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetExporter.java
2014-03-14 09:19:11 UTC (rev 17659)
+++
gate/trunk/plugins/Format_FastInfoset/src/gate/corpora/FastInfosetExporter.java
2014-03-14 14:59:44 UTC (rev 17660)
@@ -18,8 +18,6 @@
import gate.Document;
import gate.Factory;
import gate.Gate;
-import gate.LanguageResource;
-import gate.Resource;
import gate.creole.metadata.AutoInstance;
import gate.creole.metadata.CreoleResource;
import gate.gui.MainFrame;
@@ -50,6 +48,7 @@
import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer;
+@SuppressWarnings("serial")
@CreoleResource(name = "Fast Infoset Exporter", tool = true, autoinstances =
@AutoInstance)
public class FastInfosetExporter extends ResourceHelper {
Modified: gate/trunk/src/main/gate/DocumentFormat.java
===================================================================
--- gate/trunk/src/main/gate/DocumentFormat.java 2014-03-14 09:19:11 UTC
(rev 17659)
+++ gate/trunk/src/main/gate/DocumentFormat.java 2014-03-14 14:59:44 UTC
(rev 17660)
@@ -23,8 +23,11 @@
import gate.util.BomStrippingInputStreamReader;
import gate.util.DocumentFormatException;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
@@ -104,6 +107,8 @@
/** Flag for enable/disable collecting of repositioning information */
private Boolean shouldCollectRepositioning = new Boolean(false);
+
+ protected boolean supportsExport = false;
/** If the document format could collect repositioning information
* during the unpack phase this method will return <B>true</B>.
@@ -159,6 +164,60 @@
doc.setFeatures(fm);
unpackMarkup(doc);
}// unpackMarkup();
+
+ /**
+ * Returns true if this DocumentFormat instance supports exporting
+ * GATE documents, false otherwise.
+ *
+ * @return true if this DocumentFormat instance supports exporting
+ * GATE documents, false otherwise.
+ */
+ public boolean isExportSupported() {
+ return supportsExport;
+ }
+
+ /**
+ * Equivalent to {@link #export(Document,File, Map)} with an
+ * empty map of options.
+ */
+ public void export(Document doc, File file) throws IOException {
+ export(doc,file,new HashMap<String,Object>());
+ }
+
+ /**
+ * Equivalent to {@link #export(Document,OutputStream, Map)} using a
+ * FileOutputStream instance constructed from the File param.
+ */
+ public void export(Document doc, File file, Map<String,Object> options)
throws IOException {
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(file);
+ export(doc, new FileOutputStream(file), options);
+ }
+ finally {
+ IOUtils.closeQuietly(out);
+ }
+ }
+
+ /**
+ * Equivalent to {@link #export(Document,OutputStream)} with an
+ * empty map of options.
+ */
+ public void export(Document doc, OutputStream out) throws IOException {
+ export(doc,out,new HashMap<String,Object>());
+ }
+
+ /**
+ * Exports the provided {@link Document} instance to the specified
+ * {@link OutputStream} using the specified options.
+ *
+ * @param doc the document to export
+ * @param out the OutputStream to export the document to
+ * @param options DocumentFormat specific export options
+ */
+ public void export(Document doc, OutputStream out, Map<String, Object>
options) throws IOException {
+ throw new UnsupportedOperationException("Export Not Supported");
+ }
/**
* Returns a MimeType having as input a fileSufix.
@@ -536,6 +595,18 @@
String fileSuffix)
{
return getDocumentFormat(aGateDocument, getMimeType(fileSuffix));
} // getDocumentFormat(String)
+
+ /**
+ * Find the DocumentFormat implementation that deals with the given
+ * MIME type.
+ *
+ * @param mimeType the MIME type you want the DocumentFormat for
+ * @return the DocumentFormat associated with the MIME type or null if
+ * the MIME type does not have a registered DocumentFormat
+ */
+ public static DocumentFormat getDocumentFormat(MimeType mimeType) {
+ return mimeString2ClassHandlerMap.get(mimeType);
+ }
/**
* Find a DocumentFormat implementation that deals with a particular
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs