LayerSourceConverter.java 7.59 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.provider.LayerSourceEnum
 *  com.scene7.is.util.Converter
 *  com.scene7.is.util.collections.CollectionUtil
 *  com.scene7.is.util.text.CharSetBuilder
 *  com.scene7.is.util.text.Parser
 *  com.scene7.is.util.text.ParserUtil
 *  com.scene7.is.util.text.ParsingException
 *  com.scene7.is.util.text.Scanner
 *  com.scene7.is.util.text.coders.SelectiveURLCoder
 *  com.scene7.is.util.text.parsers.URLDecodeParser
 *  org.apache.commons.beanutils.ConversionException
 *  org.jetbrains.annotations.NotNull
 */
package com.scene7.is.ps.provider.parsers.query;

import com.scene7.is.provider.LayerSourceEnum;
import com.scene7.is.ps.provider.EmbeddedLayerSource;
import com.scene7.is.ps.provider.ImageLayerSource;
import com.scene7.is.ps.provider.LayerSource;
import com.scene7.is.ps.provider.Util;
import com.scene7.is.util.Converter;
import com.scene7.is.util.collections.CollectionUtil;
import com.scene7.is.util.text.CharSetBuilder;
import com.scene7.is.util.text.Parser;
import com.scene7.is.util.text.ParserUtil;
import com.scene7.is.util.text.ParsingException;
import com.scene7.is.util.text.Scanner;
import com.scene7.is.util.text.coders.SelectiveURLCoder;
import com.scene7.is.util.text.parsers.URLDecodeParser;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.BitSet;
import java.util.Map;
import org.apache.commons.beanutils.ConversionException;
import org.jetbrains.annotations.NotNull;

public class LayerSourceConverter
extends Converter<String, LayerSource> {
    private static final Map<String, LayerSourceEnum> PREFIX_TO_TYPE = CollectionUtil.lookupTable(LayerSourceEnum.class);
    private static final BitSet CLOSING_BRACES = CollectionUtil.bitSet((String)"})");
    private static final boolean[] ALPHA = CharSetBuilder.charSetBuilder().setRange(true, 'A', 'Z').setRange(true, 'a', 'z').getProduct();
    private static final boolean[] OPENING_BRACES = CharSetBuilder.charSetBuilder().set(true, new char[]{'{', '('}).getProduct();
    private static final Parser<LayerSourceEnum> TYPE_PARSER = ParserUtil.enumParser(LayerSourceEnum.class, (boolean)false);
    private static final Converter<String, LayerSource> INSTANCE = new LayerSourceConverter();
    private static final SelectiveURLCoder PATH_CODER = SelectiveURLCoder.selectiveUrlCoder((String)"/");

    @NotNull
    public static Converter<String, LayerSource> layerSourceConverter() {
        return INSTANCE;
    }

    @NotNull
    private static ImageLayerSource imageLayerSource(@NotNull String decoded) throws ParsingException {
        try {
            return ImageLayerSource.imageLayerSource(decoded);
        }
        catch (IllegalArgumentException e) {
            String message = "Illegal image path: " + e.getMessage();
            throw new ParsingException(4, message, (Throwable)e);
        }
    }

    private static boolean isEmbeddedSource(String value, int code) {
        return LayerSourceConverter.contains(OPENING_BRACES, code) && LayerSourceConverter.endsWith(value, LayerSourceConverter.closingBrace(code));
    }

    private static char openingBrace(char lastChar) {
        if (lastChar == '}') {
            return '{';
        }
        if (lastChar == ')') {
            return '(';
        }
        throw new AssertionError(lastChar);
    }

    private static void collectAll(StringReader in, StringBuilder buffer) {
        try {
            Scanner.collectAll((Reader)in, (StringBuilder)buffer);
        }
        catch (IOException e) {
            throw new AssertionError(e);
        }
    }

    private static int collectWhile(StringReader in, StringBuilder buffer, boolean[] charSet, boolean collectOutSideRange) {
        try {
            return Scanner.collectWhile((Reader)in, (StringBuilder)buffer, (boolean[])charSet, (boolean)collectOutSideRange);
        }
        catch (IOException e) {
            throw new AssertionError(e);
        }
    }

    private static boolean contains(boolean[] charset, int code) {
        return code != -1 && code <= charset.length && charset[code];
    }

    private static boolean endsWith(@NotNull CharSequence value, char code) {
        return code == value.charAt(value.length() - 1);
    }

    @NotNull
    private static String decode(@NotNull String value) throws ParsingException {
        return (String)URLDecodeParser.urlDecodeParser().parse(value);
    }

    private static char closingBrace(int openingBrace) {
        switch (openingBrace) {
            case 123: {
                return '}';
            }
            case 40: {
                return ')';
            }
        }
        throw new AssertionError(openingBrace);
    }

    @NotNull
    public LayerSource convert(@NotNull String value) throws ConversionException {
        try {
            if (value.isEmpty()) {
                throw new ParsingException(7, value, null);
            }
            if (Util.isInvalidPath(value)) {
                throw new ParsingException(1, value, null);
            }
            StringReader in = new StringReader(value);
            StringBuilder buffer = new StringBuilder();
            int code = LayerSourceConverter.collectWhile(in, buffer, ALPHA, false);
            if (LayerSourceConverter.isEmbeddedSource(value, code)) {
                String embeddedSource = buffer.toString();
                buffer.setLength(0);
                LayerSourceEnum type = embeddedSource.isEmpty() ? LayerSourceEnum.URL : (LayerSourceEnum)TYPE_PARSER.parse(embeddedSource);
                LayerSourceConverter.collectAll(in, buffer);
                String embeddedRequest = buffer.substring(1, buffer.length() - 1);
                return EmbeddedLayerSource.embeddedLayerSource(type, embeddedRequest);
            }
            String decoded = LayerSourceConverter.decode(value);
            return LayerSourceConverter.decodedLayerSource(decoded);
        }
        catch (ParsingException e) {
            throw new ConversionException((Throwable)e);
        }
    }

    @NotNull
    public String revert(@NotNull LayerSource value) throws ConversionException {
        if (value instanceof ImageLayerSource) {
            ImageLayerSource src = (ImageLayerSource)value;
            return PATH_CODER.encode(src.getValue());
        }
        if (value instanceof EmbeddedLayerSource) {
            EmbeddedLayerSource src = (EmbeddedLayerSource)value;
            StringBuilder result = new StringBuilder();
            if (src.getType() != LayerSourceEnum.URL) {
                result.append(src.getType().toString().toLowerCase());
            }
            result.append('(').append(src.getValue()).append(')');
            return result.toString();
        }
        throw new AssertionError((Object)("Layer Source String Converter - Invalid Layer Source Value: " + value.toString()));
    }

    @NotNull
    private static LayerSource decodedLayerSource(@NotNull String value) throws ParsingException {
        String prefix;
        char lastChar;
        int index;
        LayerSourceEnum type;
        char openingBrace;
        if (value.length() > 2 && CLOSING_BRACES.get(lastChar = value.charAt(value.length() - 1)) && (index = value.indexOf(openingBrace = LayerSourceConverter.openingBrace(lastChar))) != -1 && (type = PREFIX_TO_TYPE.get(prefix = value.substring(0, index).toUpperCase())) != null) {
            String request = value.substring(index + 1, value.length() - 1);
            return EmbeddedLayerSource.embeddedLayerSource(type, request);
        }
        return LayerSourceConverter.imageLayerSource(value);
    }

    private LayerSourceConverter() {
        super(String.class, LayerSource.class);
    }
}