SlengCompiler.java 5.32 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.sleng.CacheEnum
 *  com.scene7.is.sleng.Engine
 *  com.scene7.is.sleng.FXGServer
 *  com.scene7.is.sleng.ImageAccessException
 *  com.scene7.is.sleng.ImageServer
 *  com.scene7.is.sleng.LayerFactory
 *  com.scene7.is.sleng.LayerFactoryTypeEnum
 *  com.scene7.is.sleng.Sleng
 *  com.scene7.is.sleng.SlengCodeGenerator
 *  com.scene7.is.sleng.SlengCodeInterpreter
 *  com.scene7.is.sleng.SlengOptimizerFilter
 *  com.scene7.is.sleng.SlengPreprocessingFilter
 *  com.scene7.is.util.text.ParameterException
 *  org.jetbrains.annotations.NotNull
 */
package com.scene7.is.ps.provider;

import com.scene7.is.ps.provider.IZoomException;
import com.scene7.is.ps.provider.Request;
import com.scene7.is.ps.provider.RequestProcessor;
import com.scene7.is.sleng.CacheEnum;
import com.scene7.is.sleng.Engine;
import com.scene7.is.sleng.FXGServer;
import com.scene7.is.sleng.ImageAccessException;
import com.scene7.is.sleng.ImageServer;
import com.scene7.is.sleng.LayerFactory;
import com.scene7.is.sleng.LayerFactoryTypeEnum;
import com.scene7.is.sleng.Sleng;
import com.scene7.is.sleng.SlengCodeGenerator;
import com.scene7.is.sleng.SlengCodeInterpreter;
import com.scene7.is.sleng.SlengOptimizerFilter;
import com.scene7.is.sleng.SlengPreprocessingFilter;
import com.scene7.is.util.text.ParameterException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;

public class SlengCompiler {
    @NotNull
    private final ImageServer imageServer;
    private static final Logger LOGGER = Logger.getLogger(SlengCompiler.class.getName());
    private static final String ERROR_MESSAGE = "error during sleng optimization, reverting to unoptimized code";
    private final boolean doPostprocess;
    private final boolean doComments;
    private final boolean doHotSpots;

    public SlengCompiler(@NotNull ImageServer imageServer, boolean doPostprocess, boolean doComments, boolean doHotSpots) {
        this.imageServer = imageServer;
        this.doPostprocess = doPostprocess;
        this.doComments = doComments;
        this.doHotSpots = doHotSpots;
    }

    public byte[] compileRequest(@NotNull Request request) throws ParameterException, IZoomException {
        try {
            RequestProcessor proc = new RequestProcessor(request, this.doComments, this.doHotSpots);
            proc.preprocess();
            proc.processLayers();
            if (this.doPostprocess) {
                proc.postprocess();
            }
            return proc.getCode();
        }
        catch (ImageAccessException e) {
            throw new IZoomException(IZoomException.IMAGE_NOT_FOUND, e.getMessage(), (Throwable)e);
        }
    }

    public byte[] compileOptimizedRequest(@NotNull FXGServer fxgServer, @NotNull Request request) throws ParameterException, IZoomException {
        byte[] unoptimizedCode = this.compileRequest(request);
        return this.optimizeSleng(fxgServer, unoptimizedCode);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @NotNull
    public byte[] optimizeSleng(@NotNull FXGServer fxgServer, @NotNull byte[] slengCode) {
        Engine engine = new Engine(this.imageServer.getLayerFactory(LayerFactoryTypeEnum.LAYER, true), fxgServer, CacheEnum.ON);
        try {
            SlengPreprocessingFilter slengPreprocessingFilter = new SlengPreprocessingFilter(engine, new SlengCodeGenerator(this.doComments, this.doHotSpots));
            SlengCodeInterpreter interpreterForward = new SlengCodeInterpreter((Sleng)slengPreprocessingFilter);
            interpreterForward.run((InputStream)new ByteArrayInputStream(slengCode));
            byte[] preprocessedSleng = slengPreprocessingFilter.getCode();
            byte[] reversedSleng = SlengOptimizerFilter.reverseSleng((byte[])preprocessedSleng, (boolean)this.doComments, (boolean)this.doHotSpots);
            SlengOptimizerFilter slengOptimizerFilter = new SlengOptimizerFilter(new SlengCodeGenerator(this.doComments, this.doHotSpots), this.imageServer, 1.0, 1.0);
            SlengCodeInterpreter interpreter = new SlengCodeInterpreter((Sleng)slengOptimizerFilter);
            interpreter.run((InputStream)new ByteArrayInputStream(reversedSleng));
            byte[] arrby = SlengOptimizerFilter.reverseSleng((byte[])slengOptimizerFilter.getCode(), (boolean)this.doComments, (boolean)this.doHotSpots);
            return arrby;
        }
        catch (IOException e) {
            LOGGER.log(Level.WARNING, "error during sleng optimization, reverting to unoptimized code", e);
            byte[] interpreterForward = slengCode;
            return interpreterForward;
        }
        catch (ImageAccessException e) {
            LOGGER.log(Level.WARNING, "error during sleng optimization, reverting to unoptimized code", (Throwable)e);
            byte[] interpreterForward = slengCode;
            return interpreterForward;
        }
        catch (UnsupportedOperationException e) {
            LOGGER.log(Level.WARNING, "error during sleng optimization, reverting to unoptimized code - " + e.getMessage());
            byte[] interpreterForward = slengCode;
            return interpreterForward;
        }
        finally {
            engine.dispose();
        }
    }
}