- Fix mindmap img export support. Now images are embedded in the SVG.

This commit is contained in:
Paulo Gustavo Veiga 2012-07-12 20:59:01 -03:00
parent 99ee5a7da9
commit 6582f073bd
14 changed files with 3028 additions and 155 deletions

View File

@ -42,3 +42,9 @@ SUB_TOPIC=子节点
ISOLATED_TOPIC=独立节点
CENTRAL_TOPIC=中心节点
SHORTCUTS=快捷键
ENTITIES_COULD_NOT_BE_DELETED=不能删除节点或者关系。至少应选择一个对象。
AT_LEAST_ONE_TOPIC_MUST_BE_SELECTED=至少应选择一个节点。
CLIPBOARD_IS_EMPTY=无法拷贝。 粘贴板是空的。
CENTRAL_TOPIC_CAN_NOT_BE_DELETED=不能删除根节点。
RELATIONSHIP_COULD_NOT_BE_CREATED=不能创建关系。 应先选择创建关系的一对上级节点。
SELECTION_COPIED_TO_CLIPBOARD=节点已拷贝到粘贴板。

View File

@ -43,3 +43,10 @@ SUB_TOPIC=子節點
ISOLATED_TOPIC=獨立節點
CENTRAL_TOPIC=中心節點
SHORTCUTS=快捷鍵
ENTITIES_COULD_NOT_BE_DELETED=不能刪除節點或者關係。至少應選擇一個對象。
AT_LEAST_ONE_TOPIC_MUST_BE_SELECTED=至少應選擇一個節點。
CLIPBOARD_IS_EMPTY=無法拷貝。 粘貼板是空的。
CENTRAL_TOPIC_CAN_NOT_BE_DELETED=不能刪除根節點。
RELATIONSHIP_COULD_NOT_BE_CREATED=不能創建關係。 應先選擇創建關係的一對上級節點。
SELECTION_COPIED_TO_CLIPBOARD=節點已拷貝到粘貼板。

View File

@ -1,86 +1,78 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.exporter;
public class ExportProperties {
private ExportFormat format;
private String baseImgPath;
public ExportFormat getFormat() {
return format;
}
private ExportProperties(final ExportFormat format) {
this.format = format;
}
public static ExportProperties create(final ExportFormat format) {
ExportProperties result;
if (format == ExportFormat.JPG || format == ExportFormat.PNG) {
result = new ImageProperties(format);
} else {
result = new GenericProperties(format);
}
return result;
}
public void setBaseImagePath(String baseUrl) {
this.baseImgPath = baseUrl;
}
public String getBaseImgPath() {
return baseImgPath;
}
static public class GenericProperties extends ExportProperties {
private GenericProperties(ExportFormat format) {
super(format);
}
}
static public class ImageProperties extends ExportProperties {
private Size size;
public Size getSize() {
return size;
}
public void setSize(Size size) {
this.size = size;
}
public ImageProperties(ExportFormat format) {
super(format);
}
public enum Size {
SMALL(100), MEDIUM(800), XMEDIUM(1024), LARGE(2048);
private int width;
Size(int width) {
this.width = width;
}
public Float getWidth() {
return (float) width;
}
}
}
}
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.exporter;
public class ExportProperties {
private ExportFormat format;
private String baseImgPath;
public ExportFormat getFormat() {
return format;
}
private ExportProperties(final ExportFormat format) {
this.format = format;
}
public static ExportProperties create(final ExportFormat format) {
ExportProperties result;
if (format == ExportFormat.JPG || format == ExportFormat.PNG) {
result = new ImageProperties(format);
} else {
result = new GenericProperties(format);
}
return result;
}
static public class GenericProperties extends ExportProperties {
private GenericProperties(ExportFormat format) {
super(format);
}
}
static public class ImageProperties extends ExportProperties {
private Size size;
public Size getSize() {
return size;
}
public void setSize(Size size) {
this.size = size;
}
public ImageProperties(ExportFormat format) {
super(format);
}
public enum Size {
SMALL(100), MEDIUM(800), XMEDIUM(1024), LARGE(2048);
private int width;
Size(int width) {
this.width = width;
}
public Float getWidth() {
return (float) width;
}
}
}
}

View File

@ -31,7 +31,9 @@ import org.jetbrains.annotations.Nullable;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import sun.misc.BASE64Encoder;
import javax.servlet.ServletContext;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -48,16 +50,19 @@ public class ExporterFactory {
private static final String GROUP_NODE_NAME = "g";
private static final String RECT_NODE_NAME = "rect";
private static final String IMAGE_NODE_NAME = "image";
private File baseImgDir;
private ExporterFactory() throws ParserConfigurationException {
public ExporterFactory(@NotNull final ServletContext servletContext) throws ParserConfigurationException {
this.baseImgDir = new File(servletContext.getRealPath("/"));
}
public static void export(@NotNull ExportProperties properties, @Nullable String xml, @NotNull OutputStream output, @Nullable String mapSvg) throws TranscoderException, IOException, ParserConfigurationException, SAXException, XMLStreamException, TransformerException, JAXBException, ExportException {
public ExporterFactory(@NotNull final File baseImgDir) throws ParserConfigurationException {
this.baseImgDir = baseImgDir;
}
public void export(@NotNull ExportProperties properties, @Nullable String xml, @NotNull OutputStream output, @Nullable String mapSvg) throws TranscoderException, IOException, ParserConfigurationException, SAXException, XMLStreamException, TransformerException, JAXBException, ExportException {
final ExportFormat format = properties.getFormat();
final String imgPath = properties.getBaseImgPath();
switch (format) {
case PNG: {
// Create a JPEG transcoder
@ -68,7 +73,7 @@ public class ExporterFactory {
transcoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, size.getWidth());
// Create the transcoder input.
final Document document = normalizeSvg(mapSvg, imgPath);
final Document document = normalizeSvg(mapSvg, false);
final String svgString = domToString(document);
final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray()));
@ -89,7 +94,7 @@ public class ExporterFactory {
transcoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, size.getWidth());
// Create the transcoder input.
final Document document = normalizeSvg(mapSvg, imgPath);
final Document document = normalizeSvg(mapSvg, false);
final String svgString = domToString(document);
final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray()));
@ -104,7 +109,7 @@ public class ExporterFactory {
final Transcoder transcoder = new PDFTranscoder();
// Create the transcoder input.
final Document document = normalizeSvg(mapSvg, imgPath);
final Document document = normalizeSvg(mapSvg, false);
final String svgString = domToString(document);
final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray()));
@ -115,7 +120,7 @@ public class ExporterFactory {
break;
}
case SVG: {
final Document dom = normalizeSvg(mapSvg, imgPath);
final Document dom = normalizeSvg(mapSvg, true);
output.write(domToString(dom).getBytes("UTF-8"));
break;
}
@ -129,7 +134,7 @@ public class ExporterFactory {
}
}
private static Document normalizeSvg(@NotNull String svgXml, final String imgBaseUrl) throws XMLStreamException, ParserConfigurationException, IOException, SAXException, TransformerException {
private Document normalizeSvg(@NotNull String svgXml, boolean embedImg) throws XMLStreamException, ParserConfigurationException, IOException, SAXException, TransformerException {
final DocumentBuilder documentBuilder = getDocumentBuilder();
svgXml = svgXml.replaceFirst("<svg ", "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" ");
@ -155,7 +160,8 @@ public class ExporterFactory {
fitSvg(document);
fixImageTagHref(document, imgBaseUrl);
final Node child = document.getFirstChild();
fixImageTagHref(document, (Element) child);
return document;
@ -187,12 +193,7 @@ public class ExporterFactory {
return result.toString();
}
private static void fixImageTagHref(Document document, String imgBaseUrl) {
final Node child = document.getFirstChild();
fixImageTagHref(document, (Element) child, imgBaseUrl);
}
private static void fixImageTagHref(@NotNull Document document, @NotNull Element element, String imgBaseUrl) {
private void fixImageTagHref(@NotNull Document document, @NotNull Element element) {
final NodeList list = element.getChildNodes();
@ -201,7 +202,7 @@ public class ExporterFactory {
// find all groups
if (GROUP_NODE_NAME.equals(node.getNodeName())) {
// Must continue looking ....
fixImageTagHref(document, (Element) node, imgBaseUrl);
fixImageTagHref(document, (Element) node);
} else if (IMAGE_NODE_NAME.equals(node.getNodeName())) {
@ -209,20 +210,63 @@ public class ExporterFactory {
// Cook image href ...
final String imgUrl = elem.getAttribute("href");
int index = imgUrl.lastIndexOf("/");
elem.removeAttribute("href");
if (index != -1) {
final String iconName = imgUrl.substring(index + 1);
// Hack for backward compatibility . This can be removed in 2012. :)
String imgPath;
imgPath = imgBaseUrl + "/" + imgUrl;
elem.setAttribute("xlink:href", imgPath);
FileInputStream fis = null;
// Obtains file name ...
try {
final File iconFile = iconFile(imgUrl);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
fis = new FileInputStream(iconFile);
BASE64Encoder encoder = new BASE64Encoder();
encoder.encode(fis, bos);
elem.setAttribute("xlink:href", "data:image/png;base64," + bos.toString("8859_1"));
elem.appendChild(document.createTextNode(" "));
} catch (IOException e) {
e.printStackTrace();
} finally {
close(fis);
}
}
}
}
private File iconFile(@NotNull final String imgUrl) throws IOException {
int index = imgUrl.lastIndexOf("/");
final String iconName = imgUrl.substring(index + 1);
final File iconsDir = new File(baseImgDir, "icons");
File iconFile = new File(iconsDir, iconName);
if (!iconFile.exists()) {
// It's not a icon, must be a note, attach image ...
final File legacyIconsDir = new File(baseImgDir, "images");
iconFile = new File(legacyIconsDir, iconName);
}
if (!iconFile.exists()) {
final File legacyIconsDir = new File(iconsDir, "legacy");
iconFile = new File(legacyIconsDir, iconName);
}
if (!iconFile.exists()) {
throw new IOException("Icon could not be found:" + imgUrl);
}
return iconFile;
}
private void close(Closeable fis) {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// Ignore ...
}
}
}
private static void fitSvg(Document document) {
// viewBox size
int mapWidth = 1024;

View File

@ -18,30 +18,21 @@
package com.wisemapping.rest.view;
import com.wisemapping.exporter.ExportException;
import com.wisemapping.exporter.ExportFormat;
import com.wisemapping.exporter.ExportProperties;
import com.wisemapping.exporter.ExporterFactory;
import com.wisemapping.mail.NotificationService;
import com.wisemapping.security.Utils;
import org.apache.batik.transcoder.TranscoderException;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.XmlMappingException;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.web.servlet.view.AbstractView;
import org.xml.sax.SAXException;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.util.Map;
public class TransformView extends AbstractView {
@ -53,8 +44,9 @@ public class TransformView extends AbstractView {
@Autowired
private Jaxb2Marshaller jaxbMarshaller;
public TransformView(@NotNull final String contentType) {
public TransformView(@NotNull final String contentType,@NotNull NotificationService notificationService) {
this.contentType = contentType;
this.notificationService = notificationService;
this.exportFormat = ExportFormat.fromContentType(contentType);
}
@ -86,19 +78,19 @@ public class TransformView extends AbstractView {
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// Change image link URL.
setBaseBaseImgUrl(exportFormat, properties);
final ServletContext servletContext = request.getSession().getServletContext();
final ExporterFactory factory = new ExporterFactory(servletContext);
try {
// Write the conversion content ...
final ServletOutputStream outputStream = response.getOutputStream();
if (exportFormat == ExportFormat.FREEMIND) {
ExporterFactory.export(properties, content, outputStream, null);
factory.export(properties, content, outputStream, null);
} else if (exportFormat == ExportFormat.WISEMAPPING) {
final Object mindmap = viewMap.get("mindmap");
final StreamResult result = new StreamResult(outputStream);
jaxbMarshaller.marshal(mindmap, result);
} else {
ExporterFactory.export(properties, null, outputStream, content);
factory.export(properties, null, outputStream, content);
}
} catch (Throwable e) {
notificationService.reportMindmapExportError(content, Utils.getUser(), request.getHeader("User-Agent"),e);
@ -109,19 +101,5 @@ public class TransformView extends AbstractView {
public String getContentType() {
return contentType;
}
private void setBaseBaseImgUrl(@NotNull ExportFormat format, @NotNull ExportProperties properties) {
final String baseUrl;
if (format == ExportFormat.SVG) {
baseUrl = "http://www.wisemapping.com/images";
} else {
final ServletContext servletContext = this.getServletContext();
baseUrl = "file://" + servletContext.getRealPath("/");
}
properties.setBaseImagePath(baseUrl);
}
private static final String IMG_SIZE_PARAMETER = "imgSize";
}

View File

@ -1,4 +1,4 @@
# Chine Traditional
\ufeff# Chine Traditional
NAME=\u540d\u79f0
DESCRIPTION=\u63cf\u8ff0
@ -206,3 +206,5 @@ DESELECT_ALL_TOPIC=\u4e0d\u9009\u4efb\u4f55\u8282\u70b9
SHORTCUTS=\u5feb\u6377\u952e
COLLAPSE_CHILDREN=\u6298\u53e0\u5b50\u8282\u70b9
KEYBOARD_SHORTCUTS_MSG=\u5feb\u6377\u952e\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8282\u7ea6\u65f6\u95f4\uff0c\u4f7f\u4f60\u7684\u624b\u4e0d\u5fc5\u79bb\u5f00\u952e\u76d8\u800c\u53bb\u4f7f\u7528\u9f20\u6807\u3002
COPY_AND_PASTE_TOPICS=\u62f7\u8d1d\u548c\u7c98\u8d34\u8282\u70b9
MULTIPLE_LINES=\u6dfb\u52a0\u591a\u884c\u957f\u6587\u672c

View File

@ -1,4 +1,4 @@
# Chine Simplified
\ufeff# Chine Simplified
NAME=\u540d\u7a31
DESCRIPTION=\u63cf\u8ff0
@ -206,3 +206,5 @@ DESELECT_ALL_TOPIC=\u4e0d\u9078\u4efb\u4f55\u7bc0\u9ede
SHORTCUTS=\u5feb\u6377\u9375
COLLAPSE_CHILDREN=\u6298\u758a\u5b50\u7bc0\u9ede
KEYBOARD_SHORTCUTS_MSG=\u5feb\u6377\u9375\u53ef\u4ee5\u5e6b\u52a9\u4f60\u7bc0\u7d04\u6642\u9593\uff0c\u4f7f\u4f60\u7684\u624b\u4e0d\u5fc5\u96e2\u958b\u9375\u76e4\u800c\u53bb\u4f7f\u7528\u6ed1\u9f20\u3002
COPY_AND_PASTE_TOPICS=\u62f7\u8c9d\u548c\u7c98\u8cbc\u7bc0\u9ede
MULTIPLE_LINES=\u6dfb\u52a0\u591a\u884c\u9577\u6587\u672c

View File

@ -85,29 +85,25 @@
<servlet>
<servlet-name>mvc-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/wisemapping-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>mvc-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/wisemapping-rest.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress SpringModelInspection -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
@ -69,26 +70,32 @@
<bean id="transformViewPdf" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="application/pdf"/>
<constructor-arg ref="notificationService"/>
</bean>
<bean id="transformViewPng" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="image/png"/>
<constructor-arg ref="notificationService"/>
</bean>
<bean id="transformViewJpeg" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="image/jpeg"/>
<constructor-arg ref="notificationService"/>
</bean>
<bean id="transformViewFreemind" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="application/freemind"/>
<constructor-arg ref="notificationService"/>
</bean>
<bean id="transformViewSvg" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="image/svg+xml"/>
<constructor-arg ref="notificationService"/>
</bean>
<bean id="transformViewWise" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="application/wisemapping+xml"/>
<constructor-arg ref="notificationService"/>
</bean>

View File

@ -6,7 +6,6 @@ import com.wisemapping.exporter.ExportProperties;
import com.wisemapping.exporter.ExporterFactory;
import com.wisemapping.importer.ImporterException;
import com.wisemapping.model.MindMap;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;
@ -33,20 +32,17 @@ public class ExportTest {
final ExportProperties properties = ExportProperties.create(format);
final ExportProperties.ImageProperties imageProperties = (ExportProperties.ImageProperties) properties;
imageProperties.setSize(ExportProperties.ImageProperties.Size.LARGE);
String baseUrl = "file://" + svgFile.getParentFile().getAbsolutePath() + "/../../../../../../wise-editor/src/main/webapp/icons";
properties.setBaseImagePath(baseUrl);
String baseUrl = svgFile.getParentFile().getAbsolutePath() + "/../../../../../../wise-editor/src/main/webapp";
ExporterFactory factory = new ExporterFactory(new File(baseUrl));
// Write content ...
if (pngFile.exists()) {
// Export mile content ...
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
ExporterFactory.export(imageProperties, null, bos, svgXml);
// Assert.assertEquals(expContent.toString().trim(), expContent.toString().trim());
factory.export(imageProperties, null, bos, svgXml);
} else {
OutputStream outputStream = new FileOutputStream(pngFile, false);
ExporterFactory.export(imageProperties, null, outputStream, svgXml);
factory.export(imageProperties, null, outputStream, svgXml);
outputStream.close();
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 0 B

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 0 B

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 215 KiB