Bug WISE-422 fixed: Add Safari hack for importing images.

This commit is contained in:
Paulo Gustavo Veiga
2015-03-24 21:33:13 -03:00
parent 298efc4d20
commit db9c90c0a2
8 changed files with 341 additions and 37 deletions

View File

@@ -20,7 +20,6 @@ package com.wisemapping.exporter;
public class ExportProperties {
private ExportFormat format;
private String baseImgPath;
private String version;
public ExportFormat getFormat() {

View File

@@ -54,7 +54,7 @@ import java.util.regex.Pattern;
public class ExporterFactory {
private static final String GROUP_NODE_NAME = "g";
private static final String IMAGE_NODE_NAME = "image";
public static final int MARGING = 50;
public static final int MANGING = 50;
public static final String UTF_8_CHARSET_NAME = "UTF-8";
private File baseImgDir;
@@ -127,17 +127,17 @@ public class ExporterFactory {
break;
}
case TEXT: {
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.TEXT);
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.TEXT);
exporter.export(xml.getBytes(UTF_8_CHARSET_NAME), output);
break;
}
case OPEN_OFFICE_WRITER: {
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.OPEN_OFFICE);
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.OPEN_OFFICE);
exporter.export(xml.getBytes(UTF_8_CHARSET_NAME), output);
break;
}
case MICROSOFT_EXCEL: {
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.MICROSOFT_EXCEL);
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.MICROSOFT_EXCEL);
exporter.export(xml.getBytes(UTF_8_CHARSET_NAME), output);
break;
}
@@ -148,7 +148,7 @@ public class ExporterFactory {
break;
}
case MINDJET: {
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.MINDJET);
final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.MINDJET);
exporter.export(xml.getBytes(UTF_8_CHARSET_NAME), output);
break;
}
@@ -243,27 +243,22 @@ public class ExporterFactory {
Element elem = (Element) node;
// If the image is a external URL, embeed it...
String imgUrl = elem.getAttribute("href");
if (!imgUrl.startsWith("image/png;base64") ||!imgUrl.startsWith("data:image/png;base64") ) {
final String imgUrl = fixHref(elem);
if (!imgUrl.isEmpty() && (!imgUrl.startsWith("image/png;base64") || !imgUrl.startsWith("data:image/png;base64"))) {
elem.removeAttribute("href");
if (imgUrl == null || imgUrl.isEmpty()) {
imgUrl = elem.getAttribute("xlink:href"); // Do not support namespaces ...
elem.removeAttribute("xlink:href");
}
FileInputStream fis = null;
InputStream fis = null;
// Obtains file name ...
try {
final File iconFile = iconFile(imgUrl);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
final 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 {
@@ -274,28 +269,58 @@ public class ExporterFactory {
}
}
@NotNull
private String fixHref(@NotNull Element elem) {
// Fix href attribute ...
// Hack for IE: If the image is a external URL, embeed it...
String result = elem.getAttribute("href");
if (result.isEmpty()) {
result = elem.getAttribute("xlink:href");
if (!result.isEmpty()) {
elem.removeAttribute("xlink:href");
}
// Bug WISE-422: This seems to be a bug in Safari. For some reason, img add prefixed with NS1
// <image NS1:href="icons/sign_help.png"
final NamedNodeMap attributes = elem.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
final Node node = attributes.item(i);
String nodeName = node.getNodeName();
if(nodeName.contains(":href")){
elem.removeAttribute(nodeName);
result = node.getNodeValue();
}
}
elem.setAttribute("href", result);
}
return result;
}
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()) {
File result = new File(iconsDir, iconName);
if (!result.exists()) {
// It's not a icon, must be a note, attach image ...
final File legacyIconsDir = new File(baseImgDir, "images");
iconFile = new File(legacyIconsDir, iconName);
result = new File(legacyIconsDir, iconName);
}
if (!iconFile.exists()) {
if (!result.exists()) {
final File legacyIconsDir = new File(iconsDir, "legacy");
iconFile = new File(legacyIconsDir, iconName);
result = new File(legacyIconsDir, iconName);
}
if (!iconFile.exists()) {
if (!result.exists() || result.isDirectory()) {
throw new IOException("Icon could not be found:" + imgUrl);
}
return iconFile;
return result;
}
@@ -349,11 +374,11 @@ public class ExporterFactory {
}
// Add some extra margin ...
maxX += MARGING;
minX += -MARGING;
maxX += MANGING;
minX += -MANGING;
maxY += MARGING;
minY += -MARGING;
maxY += MANGING;
minY += -MANGING;
// Calculate dimentions ...
final double width = maxX + Math.abs(minX);
@@ -366,13 +391,7 @@ public class ExporterFactory {
svgNode.setAttribute("width", Double.toString(width));
svgNode.setAttribute("height", Double.toString(height));
svgNode.setAttribute("preserveAspectRatio", "xMinYMin");
} catch (XPathExpressionException e) {
throw new ExportException(e);
} catch (ParseException e) {
throw new ExportException(e);
} catch (NumberFormatException e) {
throw new ExportException(e);
} catch (DOMException e) {
} catch (XPathExpressionException | ParseException | NumberFormatException | DOMException e) {
throw new ExportException(e);
}
}

View File

@@ -23,6 +23,7 @@ import com.wisemapping.exporter.ExportProperties;
import com.wisemapping.exporter.ExporterFactory;
import com.wisemapping.mail.NotificationService;
import com.wisemapping.security.Utils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@@ -38,6 +39,8 @@ import java.util.Map;
public class TransformView extends AbstractView {
@NonNls
private static final String DEFAULT_ENCODING = "UTF-8";
private String contentType;
private ExportFormat exportFormat;
private NotificationService notificationService;
@@ -74,7 +77,7 @@ public class TransformView extends AbstractView {
// Set file name...:http://stackoverflow.com/questions/5325322/java-servlet-download-filename-special-characters/13359949#13359949
final String fileName = (filename != null ? filename : "map") + "." + exportFormat.getFileExtension();
setContentDisposition(request, response, fileName);
this.setContentDisposition(request, response, fileName);
// Change image link URL.
final ServletContext servletContext = request.getSession().getServletContext();
@@ -86,14 +89,15 @@ public class TransformView extends AbstractView {
response.setCharacterEncoding("ASCII");
factory.export(properties, content, outputStream, null);
} else if (exportFormat == ExportFormat.WISEMAPPING) {
response.setCharacterEncoding("UTF-8");
response.setCharacterEncoding(DEFAULT_ENCODING);
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 || exportFormat == ExportFormat.MINDJET) {
response.setCharacterEncoding("UTF-8");
response.setCharacterEncoding(DEFAULT_ENCODING);
factory.export(properties, content, outputStream, null);
} else {
// Image export ...
factory.export(properties, null, outputStream, content);
}
} catch (Throwable e) {