Hack for workaround batik OOM

This commit is contained in:
Paulo Veiga 2018-09-16 18:06:47 -07:00
parent 4aaa00fc45
commit 4c66002339
2 changed files with 20 additions and 7 deletions

View File

@ -19,6 +19,7 @@
package com.wisemapping.exporter; package com.wisemapping.exporter;
import com.wisemapping.importer.VersionNumber; import com.wisemapping.importer.VersionNumber;
import org.apache.batik.ext.awt.image.rendered.TileCache;
import org.apache.batik.parser.AWTTransformProducer; import org.apache.batik.parser.AWTTransformProducer;
import org.apache.batik.parser.ParseException; import org.apache.batik.parser.ParseException;
import org.apache.batik.parser.TransformListParser; import org.apache.batik.parser.TransformListParser;
@ -53,6 +54,11 @@ import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ExporterFactory { public class ExporterFactory {
static {
// Try to prevent OOM.
TileCache.setSize(0);
}
private static final String GROUP_NODE_NAME = "g"; private static final String GROUP_NODE_NAME = "g";
private static final String IMAGE_NODE_NAME = "image"; private static final String IMAGE_NODE_NAME = "image";
private static final int MANGING = 50; private static final int MANGING = 50;
@ -70,6 +76,7 @@ public class ExporterFactory {
public void export(@NotNull ExportProperties properties, @Nullable String xml, @NotNull OutputStream output, @Nullable String mapSvg) throws ExportException, IOException, TranscoderException { public void export(@NotNull ExportProperties properties, @Nullable String xml, @NotNull OutputStream output, @Nullable String mapSvg) throws ExportException, IOException, TranscoderException {
final ExportFormat format = properties.getFormat(); final ExportFormat format = properties.getFormat();
switch (format) { switch (format) {
case PNG: { case PNG: {
// Create a JPEG transcoder // Create a JPEG transcoder
@ -81,12 +88,14 @@ public class ExporterFactory {
// Create the transcoder input. // Create the transcoder input.
final String svgString = normalizeSvg(mapSvg); final String svgString = normalizeSvg(mapSvg);
final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray())); final CharArrayReader reader = new CharArrayReader(svgString.toCharArray());
final TranscoderInput input = new TranscoderInput(reader);
TranscoderOutput transcoderOutput = new TranscoderOutput(output); TranscoderOutput transcoderOutput = new TranscoderOutput(output);
// Save the image. // Save the image.
transcoder.transcode(input, transcoderOutput); transcoder.transcode(input, transcoderOutput);
reader.close();
break; break;
} }
case JPG: { case JPG: {
@ -101,7 +110,9 @@ public class ExporterFactory {
// Create the transcoder input. // Create the transcoder input.
final String svgString = normalizeSvg(mapSvg); final String svgString = normalizeSvg(mapSvg);
final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray())); CharArrayReader reader = new CharArrayReader(svgString.toCharArray());
final TranscoderInput input = new TranscoderInput(reader);
reader.close();
TranscoderOutput trascoderOutput = new TranscoderOutput(output); TranscoderOutput trascoderOutput = new TranscoderOutput(output);
@ -115,9 +126,10 @@ public class ExporterFactory {
// Create the transcoder input. // Create the transcoder input.
final String svgString = normalizeSvg(mapSvg); final String svgString = normalizeSvg(mapSvg);
final TranscoderInput input = new TranscoderInput(new CharArrayReader(svgString.toCharArray())); CharArrayReader reader = new CharArrayReader(svgString.toCharArray());
final TranscoderInput input = new TranscoderInput(reader);
TranscoderOutput trascoderOutput = new TranscoderOutput(output); TranscoderOutput trascoderOutput = new TranscoderOutput(output);
reader.close();
// Save the image. // Save the image.
transcoder.transcode(input, trascoderOutput); transcoder.transcode(input, trascoderOutput);
break; break;
@ -156,6 +168,9 @@ public class ExporterFactory {
default: default:
throw new UnsupportedOperationException("Export method not supported."); throw new UnsupportedOperationException("Export method not supported.");
} }
output.flush();
output.close();
} }
private String normalizeSvg(@NotNull String svgXml) throws ExportException { private String normalizeSvg(@NotNull String svgXml) throws ExportException {

View File

@ -41,9 +41,7 @@ public class XSLTExporter implements Exporter {
final CharArrayReader reader = new CharArrayReader(mmos.toString("iso-8859-1").toCharArray()); final CharArrayReader reader = new CharArrayReader(mmos.toString("iso-8859-1").toCharArray());
final Source mmSource = new StreamSource(reader); final Source mmSource = new StreamSource(reader);
transformer.transform(mmSource, new StreamResult(outputStream)); transformer.transform(mmSource, new StreamResult(outputStream));
} catch (TransformerException e) { } catch (TransformerException | UnsupportedEncodingException e) {
throw new ExportException(e);
} catch (UnsupportedEncodingException e) {
throw new ExportException(e); throw new ExportException(e);
} }