Add text/plan, open office and xls transformation support.
This commit is contained in:
@@ -26,9 +26,12 @@ public enum ExportFormat {
|
||||
PNG("image/png", "png"),
|
||||
PDF("application/pdf", "pdf"),
|
||||
FREEMIND("application/freemind", "mm"),
|
||||
TEXT("text/plain", "txt"),
|
||||
MICROSOFT_EXCEL("application/vnd.ms-excel", "xls"),
|
||||
MICROSOFT_WORD("application/msword", "doc"),
|
||||
OPEN_OFFICE_WRITER("application/vnd.oasis.opendocument.text", "odt"),
|
||||
WISEMAPPING("application/wisemapping+xml", "wxml");
|
||||
|
||||
|
||||
private String contentType;
|
||||
private String fileExtension;
|
||||
|
||||
|
@@ -19,10 +19,11 @@
|
||||
package com.wisemapping.exporter;
|
||||
|
||||
import com.wisemapping.model.Mindmap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
public interface Exporter {
|
||||
public void export(byte[] xml, OutputStream outputStream) throws ExportException;
|
||||
public void export(@NotNull byte[] xml, @NotNull OutputStream outputStream) throws ExportException;
|
||||
public void export(Mindmap map, OutputStream outputStream) throws ExportException;
|
||||
}
|
||||
|
@@ -124,6 +124,21 @@ public class ExporterFactory {
|
||||
output.write(svgString.getBytes("UTF-8"));
|
||||
break;
|
||||
}
|
||||
case TEXT: {
|
||||
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.TEXT);
|
||||
exporter.export(xml.getBytes("UTF-8"), output);
|
||||
break;
|
||||
}
|
||||
case OPEN_OFFICE_WRITER: {
|
||||
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.OPEN_OFFICE);
|
||||
exporter.export(xml.getBytes("UTF-8"), output);
|
||||
break;
|
||||
}
|
||||
case MICROSOFT_EXCEL: {
|
||||
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.MICROSOFT_EXCEL);
|
||||
exporter.export(xml.getBytes("UTF-8"), output);
|
||||
break;
|
||||
}
|
||||
case FREEMIND: {
|
||||
final FreemindExporter exporter = new FreemindExporter();
|
||||
exporter.export(xml.getBytes("UTF-8"), output);
|
||||
|
@@ -0,0 +1,88 @@
|
||||
package com.wisemapping.exporter;
|
||||
|
||||
import com.wisemapping.model.Mindmap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.*;
|
||||
|
||||
public class XSLTExporter implements Exporter {
|
||||
|
||||
private Type type;
|
||||
|
||||
public XSLTExporter(@NotNull Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void export(@NotNull byte[] xml, @NotNull OutputStream outputStream) throws ExportException {
|
||||
final ByteArrayOutputStream mmos = new ByteArrayOutputStream();
|
||||
|
||||
// Convert to freemind ...
|
||||
final FreemindExporter exporter = new FreemindExporter();
|
||||
exporter.export(xml, mmos);
|
||||
|
||||
// Convert to xslt transform ...
|
||||
final InputStream xsltis = this.getClass().getResourceAsStream("/com/wisemapping/export/xslt/" + type.getXsltName());
|
||||
if (xsltis == null) {
|
||||
throw new IllegalStateException("XSLT could not be resolved.");
|
||||
}
|
||||
|
||||
try {
|
||||
final TransformerFactory factory = TransformerFactory.newInstance();
|
||||
final Source xslt = new StreamSource(xsltis);
|
||||
Transformer transformer = factory.newTransformer(xslt);
|
||||
|
||||
final CharArrayReader reader = new CharArrayReader(mmos.toString("iso-8859-1").toCharArray());
|
||||
final Source mmSource = new StreamSource(reader);
|
||||
transformer.transform(mmSource, new StreamResult(outputStream));
|
||||
} catch (TransformerException e) {
|
||||
throw new ExportException(e);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new ExportException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void export(@NotNull Mindmap map, OutputStream outputStream) throws ExportException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Exporter createTextExporter() {
|
||||
return create(Type.TEXT);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Exporter create(@NotNull Type type) {
|
||||
return new XSLTExporter(type);
|
||||
}
|
||||
|
||||
public static enum Type {
|
||||
TEXT("mm2text.xsl"),
|
||||
WORD("mm2wordml_utf8.xsl"),
|
||||
CSV("mm2csv.xsl"),
|
||||
LATEX("mm2latex.xsl"),
|
||||
MICROSOFT_EXCEL("mm2xls_utf8.xsl"),
|
||||
OPEN_OFFICE("mm2oowriter.xsl");
|
||||
|
||||
public String getXsltName() {
|
||||
return xsltName;
|
||||
}
|
||||
|
||||
private String xsltName;
|
||||
|
||||
Type(@NotNull String xstFile) {
|
||||
this.xsltName = xstFile;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -88,6 +88,38 @@ public class MindmapController extends BaseController {
|
||||
return new ModelAndView("transformViewFreemind", values);
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"text/plain"}, params = {"download=txt"})
|
||||
@ResponseBody
|
||||
public ModelAndView retrieveDocumentAsText(@PathVariable int id) throws IOException {
|
||||
final Mindmap mindMap = mindmapService.findMindmapById(id);
|
||||
final Map<String, Object> values = new HashMap<String, Object>();
|
||||
values.put("content", mindMap.getXmlStr());
|
||||
values.put("filename", mindMap.getTitle());
|
||||
return new ModelAndView("transformViewTxt", values);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/vnd.ms-excel"}, params = {"download=xls"})
|
||||
@ResponseBody
|
||||
public ModelAndView retrieveDocumentAsExcel(@PathVariable int id) throws IOException {
|
||||
final Mindmap mindMap = mindmapService.findMindmapById(id);
|
||||
final Map<String, Object> values = new HashMap<String, Object>();
|
||||
values.put("content", mindMap.getXmlStr());
|
||||
values.put("filename", mindMap.getTitle());
|
||||
return new ModelAndView("transformViewXls", values);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/vnd.oasis.opendocument.text"}, params = {"download=odt"})
|
||||
@ResponseBody
|
||||
public ModelAndView retrieveDocumentAsOdt(@PathVariable int id) throws IOException {
|
||||
final Mindmap mindMap = mindmapService.findMindmapById(id);
|
||||
final Map<String, Object> values = new HashMap<String, Object>();
|
||||
values.put("content", mindMap.getXmlStr());
|
||||
values.put("filename", mindMap.getTitle());
|
||||
return new ModelAndView("transformViewOdt", values);
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/maps/", produces = {"application/json", "text/html", "application/xml"})
|
||||
public ModelAndView retrieveList(@RequestParam(required = false) String q) throws IOException {
|
||||
final User user = Utils.getUser();
|
||||
|
@@ -86,6 +86,9 @@ public class TransformView extends AbstractView {
|
||||
final Object mindmap = viewMap.get("mindmap");
|
||||
final StreamResult result = new StreamResult(outputStream);
|
||||
jaxbMarshaller.marshal(mindmap, result);
|
||||
} else if (exportFormat == ExportFormat.MICROSOFT_EXCEL || exportFormat == ExportFormat.TEXT || exportFormat == ExportFormat.OPEN_OFFICE_WRITER) {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
factory.export(properties, content, outputStream, null);
|
||||
} else {
|
||||
factory.export(properties, null, outputStream, content);
|
||||
}
|
||||
|
Reference in New Issue
Block a user