JOWriter.java 4.94 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.pdfg.exception.ErrorCode
 *  com.adobe.pdfg.exception.JobOptionsWriteException
 */
package com.adobe.pdfg.joboptions.parser;

import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.exception.JobOptionsWriteException;
import com.adobe.pdfg.joboptions.parser.JOParser;
import com.adobe.pdfg.joboptions.parser.ParamSpecs;
import java.io.StringWriter;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

public class JOWriter {
    private final Map jmap;
    private int depth = 0;
    private boolean indent = false;
    private String currentKey = null;
    private static final String ENDLINE = "\r\n";
    private static final String DICTSTART = "<<\r\n";
    private static final String DICTEND = ">>\r\n";
    private static final String LISTSTART = "[\r\n";
    private static final String LISTEND = "]\r\n";
    private static final String SPACESTRINGSTART = "(";
    private static final String SPACESTRINGEND = ")\r\n";
    private static final String NAMESTART = "/";
    private static final String NAMEEND = " ";
    private static final String SIMPLESTRINGEND = "\r\n";
    private static final String HEXSTRINGSTART = "<";
    private static final String HEXSTRINGEND = ">\r\n";

    public JOWriter(Map jm) {
        this.jmap = jm;
    }

    public String write(boolean indent) throws JobOptionsWriteException {
        this.depth = 0;
        this.indent = indent;
        if (this.jmap == null) {
            throw new JobOptionsWriteException(ErrorCode.NoJobOptionsMapFound);
        }
        StringWriter fout = new StringWriter();
        Map distMap = (Map)this.jmap.get("setdistillerparams");
        if (distMap == null) {
            throw new JobOptionsWriteException(ErrorCode.NoDistillerParamsMapFound);
        }
        Map pageMap = (Map)this.jmap.get("setpagedevice");
        if (pageMap == null) {
            throw new JobOptionsWriteException(ErrorCode.NoPageParamsMapFound);
        }
        this.writeMap(distMap, fout);
        fout.write("setdistillerparams\r\n");
        this.writeMap(pageMap, fout);
        fout.write("setpagedevice\r\n");
        return fout.toString();
    }

    private void write(Object val, StringWriter fo) throws JobOptionsWriteException {
        if (Map.class.isAssignableFrom(val.getClass())) {
            this.writeMap((Map)val, fo);
        } else if (List.class.isAssignableFrom(val.getClass())) {
            this.writeList((List)val, fo);
        } else {
            this.writePrimitive(val, fo);
        }
    }

    private void writeMap(Map m, StringWriter fo) throws JobOptionsWriteException {
        ++this.depth;
        fo.write("<<\r\n");
        for (Map.Entry entry : m.entrySet()) {
            this.currentKey = (String)entry.getKey();
            Object val = entry.getValue();
            if (val == null) continue;
            this.indentOutput(fo);
            fo.write("/" + this.currentKey + " ");
            this.write(val, fo);
        }
        --this.depth;
        this.indentOutput(fo);
        fo.write(">>\r\n");
    }

    private void writeList(List l, StringWriter fo) throws JobOptionsWriteException {
        ++this.depth;
        fo.write("[\r\n");
        ListIterator liter = l.listIterator();
        while (liter.hasNext()) {
            Object val = liter.next();
            this.indentOutput(fo);
            this.write(val, fo);
        }
        --this.depth;
        this.indentOutput(fo);
        fo.write("]\r\n");
    }

    private void writePrimitive(Object val, StringWriter fo) throws JobOptionsWriteException {
        if (val instanceof Boolean) {
            fo.write(val.toString() + "\r\n");
        } else if (val instanceof Double || val instanceof Integer) {
            fo.write(val.toString() + "\r\n");
        } else if (val instanceof String) {
            String strVal = (String)val;
            if (!ParamSpecs.isStringParam(this.currentKey)) {
                fo.write("/" + strVal + "\r\n");
            } else if (strVal.length() != 0 && this.isHexString(strVal)) {
                fo.write("<" + strVal + ">\r\n");
            } else {
                fo.write("(" + strVal.replaceAll("[(]", "\\\\050").replaceAll("[)]", "\\\\051") + ")\r\n");
            }
        } else {
            throw new JobOptionsWriteException(ErrorCode.UnknownDataClassFound);
        }
    }

    private void indentOutput(StringWriter fo) throws JobOptionsWriteException {
        if (!this.indent) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < this.depth; ++i) {
            sb.append('\t');
        }
        fo.write(sb.toString());
    }

    private boolean isHexString(String str) {
        boolean isHex = true;
        for (int i = 0; i < str.length(); ++i) {
            if (JOParser.isHexLetter(str.charAt(i))) continue;
            isHex = false;
            break;
        }
        return isHex;
    }
}