Fix i18n encodding issue.

This commit is contained in:
Paulo Gustavo Veiga 2012-11-19 22:47:20 -03:00
parent b717a5f910
commit c3dcd8f3a9
3 changed files with 21 additions and 44 deletions

View File

@ -82,14 +82,14 @@ public class Mindmap {
throws IOException { throws IOException {
byte[] result = this.xml; byte[] result = this.xml;
if (result != null) { if (result != null) {
result = ZipUtils.stringToZip(new String(result, UTF_8)); result = ZipUtils.bytesToZip(result);
} }
return result; return result;
} }
public void setZippedXml(byte[] xml) public void setZippedXml(byte[] xml)
throws IOException { throws IOException {
this.xml = ZipUtils.zipToString(xml).getBytes(UTF_8); this.xml = ZipUtils.zipToBytes(xml);
} }
public Set<Collaboration> getCollaborations() { public Set<Collaboration> getCollaborations() {

View File

@ -18,67 +18,46 @@
package com.wisemapping.util; package com.wisemapping.util;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset; import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
public class ZipUtils { public class ZipUtils {
public static String zipToString(byte[] zip) throws IOException { public static byte[] zipToBytes(byte[] zip) throws IOException {
String result = null; byte[] result = null;
if (zip != null) if (zip != null) {
{
final ByteArrayInputStream in = new ByteArrayInputStream(zip); final ByteArrayInputStream in = new ByteArrayInputStream(zip);
final ZipInputStream zipIn = new ZipInputStream(in); final ZipInputStream zipIn = new ZipInputStream(in);
zipIn.getNextEntry(); zipIn.getNextEntry();
result = IOUtils.toByteArray(zipIn);
byte[] buffer = new byte[512];
int len;
StringBuilder sb_result = new StringBuilder();
while ((len = zipIn.read(buffer)) > 0) {
sb_result.append(new String(buffer, 0, len, Charset.forName("UTF-8")));
}
zipIn.closeEntry(); zipIn.closeEntry();
zipIn.close(); zipIn.close();
result = sb_result.toString();
} }
return result; return result;
} }
public static byte[] stringToZip(String content) throws IOException { public static byte[] bytesToZip(@NotNull final byte[] content) throws IOException {
ZipOutputStream zip = null; ZipOutputStream zip = null;
ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"));
try { try {
zip = new ZipOutputStream(byteArray); zip = new ZipOutputStream(byteArray);
ZipEntry zEntry = new ZipEntry("content"); ZipEntry zEntry = new ZipEntry("content");
zip.putNextEntry(zEntry); zip.putNextEntry(zEntry);
int bytesRead; IOUtils.write(content, zip);
byte[] buffer = new byte[8192];
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
zip.write(buffer, 0, bytesRead);
}
zip.closeEntry(); zip.closeEntry();
} } finally {
finally if (zip != null) {
{
if (zip != null)
{
zip.flush(); zip.flush();
zip.close(); zip.close();
} }

View File

@ -26,8 +26,9 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.*; import java.util.ArrayList;
import java.util.regex.Pattern; import java.util.List;
import java.util.Set;
public class MindMapBean { public class MindMapBean {
private Mindmap mindmap; private Mindmap mindmap;
@ -124,10 +125,7 @@ public class MindMapBean {
} }
public String getXmlAsJsLiteral() throws IOException { public String getXmlAsJsLiteral() throws IOException {
final String xmlAsJsLiteral = this.mindmap.getXmlAsJsLiteral(); return this.mindmap.getXmlAsJsLiteral();
// Firefox is failing for this. Need to be reviewed ...
return xmlAsJsLiteral.replace("\\u0000","");
} }
public String getProperties() throws WiseMappingException { public String getProperties() throws WiseMappingException {