JOParser.java 12.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.pdfg.exception.ErrorCode
 *  com.adobe.pdfg.exception.JobOptionsParseException
 *  com.adobe.pdfg.logging.PDFGLogger
 */
package com.adobe.pdfg.joboptions.parser;

import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.exception.JobOptionsParseException;
import com.adobe.pdfg.joboptions.parser.ParamSpecs;
import com.adobe.pdfg.logging.PDFGLogger;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

public class JOParser {
    private static final String DICTIONARY = "dictionary";
    private static final String DICTKEY = "dictionarykey";
    private static final String DICTVALUE = "dictionaryvalue";
    private static final String ARRAY = "array";
    private static final String ARRAYVALUE = "arrayvalue";
    private static final String IMPOSSIBLE = "impossible";
    private static final String LASTCHARACTER = "lastcharacter";
    private static PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(JOParser.class);
    private Stack stateStck = new Stack();
    private Stack dataStack = new Stack();
    private Map jobOptionsMap = null;

    public void parse(String jobStr) throws JobOptionsParseException {
        this.jobOptionsMap = new HashMap();
        StringReader fis = null;
        try {
            fis = new StringReader(jobStr);
            this.readObject(fis);
            this.postParseProcess();
        }
        catch (Exception e) {
            pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
            this.jobOptionsMap = null;
            throw new JobOptionsParseException(e.getMessage());
        }
        finally {
            if (fis != null) {
                fis.close();
                fis = null;
            }
        }
    }

    public Map getJobOptionsMap() {
        return this.jobOptionsMap;
    }

    private void readObject(StringReader fis) throws JobOptionsParseException, IOException {
        block16 : {
            int lastch = 0;
            boolean stringRead = false;
            do {
                char ch;
                if (!stringRead) {
                    lastch = fis.read();
                }
                stringRead = false;
                if (lastch <= 0) break block16;
                if (Character.isWhitespace((char)lastch)) {
                    if (this.peek().equals("dictionary") || !this.peek().equals("array")) continue;
                    continue;
                }
                if (lastch == 60) {
                    ch = '\u0000';
                    ch = (char)fis.read();
                    if (ch == '<') {
                        this.stateStck.push("dictionary");
                        this.dataStack.push(new HashMap());
                        continue;
                    }
                    if (JOParser.isHexLetter(ch)) {
                        HashMap fillMap = new HashMap();
                        stringRead = this.readString(ch, fis, fillMap, true);
                        if (!stringRead) continue;
                        lastch = ((Character)fillMap.get("lastcharacter")).charValue();
                        continue;
                    }
                    throw new JobOptionsParseException(ErrorCode.ExpectedLessThanCharacterInJobOption, (Object)new Character(ch));
                }
                if (lastch == 62) {
                    ch = '\u0000';
                    ch = (char)fis.read();
                    if (ch != '>') {
                        throw new JobOptionsParseException(ErrorCode.ExpectedGreaterThanCharacterInJobOption, (Object)new Character(ch));
                    }
                    if (this.peek().equals("dictionary")) {
                        Map hmap;
                        this.stateStck.pop();
                        if (this.peek().equals("dictionarykey")) {
                            this.stateStck.pop();
                            hmap = (Map)this.dataStack.pop();
                            String key = (String)this.dataStack.pop();
                            ((Map)this.dataStack.peek()).put(key, hmap);
                            continue;
                        }
                        if (this.peek().equals("array")) {
                            hmap = (Map)this.dataStack.pop();
                            ((List)this.dataStack.peek()).add(hmap);
                            continue;
                        }
                        if (this.peek().equals("impossible")) continue;
                        throw new JobOptionsParseException(ErrorCode.IllegalDictionaryLocation);
                    }
                    throw new JobOptionsParseException(ErrorCode.UnexpectedDictionaryEnd);
                }
                if (lastch == 91) {
                    this.stateStck.push("array");
                    this.dataStack.push(new ArrayList());
                    continue;
                }
                if (lastch == 93) {
                    if (this.peek().equals("array")) {
                        this.stateStck.pop();
                        if (this.peek().equals("dictionarykey")) {
                            this.stateStck.pop();
                            List lst = (List)this.dataStack.pop();
                            String key = (String)this.dataStack.pop();
                            ((Map)this.dataStack.peek()).put(key, lst);
                            continue;
                        }
                        if (this.peek().equals("array")) {
                            List lst = (List)this.dataStack.pop();
                            ((List)this.dataStack.peek()).add(lst);
                            continue;
                        }
                        throw new JobOptionsParseException(ErrorCode.IllegalArrayLocation);
                    }
                    throw new JobOptionsParseException(ErrorCode.UnexpectedArrayEnd);
                }
                if (!JOParser.isString((char)lastch)) break;
                HashMap fillMap = new HashMap();
                stringRead = this.readString((char)lastch, fis, fillMap, false);
                if (!stringRead) continue;
                lastch = ((Character)fillMap.get("lastcharacter")).charValue();
            } while (true);
            throw new JobOptionsParseException(ErrorCode.UnknownCharacterInJobOption, (Object)new Integer(lastch));
        }
    }

    private boolean readString(char lastch, StringReader fis, Map retMap, boolean isHex) throws JobOptionsParseException, IOException {
        char tch = '\u0000';
        StringBuffer sb = new StringBuffer();
        if (lastch != '/' && lastch != '(') {
            sb.append(lastch);
        }
        boolean retVal = false;
        if (this.peek().equals("dictionary")) {
            this.stateStck.push("dictionarykey");
        } else if (this.peek().equals("dictionarykey")) {
            this.stateStck.push("dictionaryvalue");
        } else if (this.peek().equals("array")) {
            this.stateStck.push("arrayvalue");
        } else if (this.peek().equals("impossible") && (this.dataStack.isEmpty() || !Map.class.isAssignableFrom(this.dataStack.peek().getClass()))) {
            throw new JobOptionsParseException(ErrorCode.IllegalDataLocationInJobOption);
        }
        int bracs = 1;
        int fromStream = -1;
        while ((fromStream = fis.read()) != -1) {
            tch = (char)fromStream;
            if (lastch == '(') {
                if (tch == '(') {
                    ++bracs;
                } else if (tch == ')' && --bracs == 0) {
                    break;
                }
            } else if (lastch != '(' && Character.isWhitespace(tch) || JOParser.isReserved(tch)) break;
            sb.append(tch);
        }
        if (fromStream == -1) {
            tch = '\u0000';
        }
        if (this.peek().equals("impossible")) {
            Map hmap = (Map)this.dataStack.pop();
            this.jobOptionsMap.put(sb.toString(), hmap);
        } else {
            Object strObj = this.getStringObject(sb.toString(), lastch, isHex);
            if (this.peek().equals("dictionarykey")) {
                this.dataStack.push(strObj);
            } else if (this.peek().equals("dictionaryvalue")) {
                this.stateStck.pop();
                this.stateStck.pop();
                String key = (String)this.dataStack.pop();
                ((Map)this.dataStack.peek()).put(key, strObj);
            } else if (this.peek().equals("arrayvalue")) {
                this.stateStck.pop();
                ((List)this.dataStack.peek()).add(strObj);
            }
        }
        if (tch != ')' && tch != '>') {
            retMap.put("lastcharacter", new Character(tch));
            retVal = true;
        }
        return retVal;
    }

    public static boolean isReserved(char ch) {
        return ch == '[' || ch == ']' || ch == '<' || ch == '>';
    }

    public static boolean isString(char ch) {
        return ch == '/' || ch == '(' || ch == '-' || Character.isLetterOrDigit(ch);
    }

    public static boolean isHexLetter(char ch) {
        return Character.isDigit(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F';
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private Object getStringObject(String strRead, char lastch, boolean isHex) throws JobOptionsParseException {
        String obj = null;
        if (isHex) {
            return strRead;
        }
        if (lastch == '/') {
            return strRead;
        }
        if (lastch == '(') {
            return strRead.replaceAll("\\\\050", "(").replaceAll("\\\\051", ")");
        }
        if (lastch == '-') return new Double(strRead);
        if (lastch == '.') return new Double(strRead);
        if (Character.isDigit(lastch)) {
            return new Double(strRead);
        }
        if (lastch != 't') {
            if (lastch != 'f') throw new JobOptionsParseException(ErrorCode.UnknownLiteralInJobOption, (Object)strRead);
        }
        if (strRead.equalsIgnoreCase("true")) {
            return Boolean.TRUE;
        }
        if (!strRead.equalsIgnoreCase("false")) throw new JobOptionsParseException(ErrorCode.UnknownLiteralInJobOption, (Object)strRead);
        return Boolean.FALSE;
    }

    private String peek() {
        if (this.stateStck.isEmpty()) {
            return "impossible";
        }
        return (String)this.stateStck.peek();
    }

    private void postParseProcess() {
        Object ispec;
        int i;
        for (i = 0; i < ParamSpecs.INTPARAMS.length; ++i) {
            ispec = ParamSpecs.INTPARAMS[i];
            this.makeInteger(ispec.getKeyName());
        }
        for (i = 0; i < ParamSpecs.INTSETPARAMS.length; ++i) {
            ispec = ParamSpecs.INTSETPARAMS[i];
            this.makeInteger(ispec.getKeyName());
        }
        this.makeInteger("JPEG2000ColorACSImageDict");
        this.makeInteger("JPEG2000ColorImageDict");
        this.makeInteger("JPEG2000GrayACSImageDict");
        this.makeInteger("JPEG2000GrayImageDict");
        this.makeInteger("MonoImageDict");
        Map m = (Map)this.jobOptionsMap.get("setpagedevice");
        List l = (List)m.get("HWResolution");
        l.set(0, this.getInteger(l.get(0)));
        l.set(1, this.getInteger(l.get(1)));
    }

    private void makeInteger(String key) {
        Map m = (Map)this.jobOptionsMap.get("setdistillerparams");
        Object val = m.get(key);
        if (val != null) {
            if (val instanceof Map) {
                m.put(key, this.makeIntegerMap((Map)val));
            } else if (val instanceof List) {
                m.put(key, this.makeIntegerList((List)val));
            } else {
                m.put(key, this.getInteger(val));
            }
        }
    }

    private Map makeIntegerMap(Map map) {
        Map m = (Map)this.jobOptionsMap.get("setdistillerparams");
        HashMap t = new HashMap(map.size());
        for (Map.Entry entry : map.entrySet()) {
            t.put(entry.getKey(), this.getInteger(entry.getValue()));
        }
        return t;
    }

    private List makeIntegerList(List lst) {
        Map m = (Map)this.jobOptionsMap.get("setdistillerparams");
        ArrayList<Integer> t = new ArrayList<Integer>(lst.size());
        Iterator iter = lst.iterator();
        while (iter.hasNext()) {
            t.add(this.getInteger(iter.next()));
        }
        return t;
    }

    private Integer getInteger(Object dbl) {
        double d = (Double)dbl;
        return d >= 0.0 ? new Integer((int)(d + 0.5)) : new Integer((int)(d - 0.5));
    }
}