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

import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.exception.JobOptionsValidateException;
import com.adobe.pdfg.joboptions.parser.ParamSpecs;
import com.adobe.pdfg.logging.PDFGLogger;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

public class JOValidator {
    private static PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(JOValidator.class);
    private String currentKey = null;

    public void validate(Map paramMap) throws JobOptionsValidateException {
        if (paramMap == null) {
            return;
        }
        for (String this.currentKey : paramMap.keySet()) {
            Object value = paramMap.get(this.currentKey);
            if (value instanceof Map) {
                this.validate((Map)value);
                continue;
            }
            if (value instanceof List) {
                this.validate(this.currentKey, value, true);
                continue;
            }
            paramMap.put(this.currentKey, this.validate(this.currentKey, value, false));
        }
    }

    private Object validate(String key, Object value, boolean array) throws JobOptionsValidateException {
        try {
            Object retVal = null;
            ParamValidator pv = this.getValidatorClassInstance(key, value);
            if (pv != null) {
                if (array) {
                    Object firstVal;
                    ListIterator<Object> liter = ((List)value).listIterator();
                    if ((key.equals("AlwaysEmbed") || key.equals("NeverEmbed")) && liter.hasNext() && !((firstVal = liter.next()) instanceof Boolean)) {
                        pv.setValue(firstVal);
                        liter.set(pv.validate());
                    }
                    while (liter.hasNext()) {
                        pv.setValue(liter.next());
                        liter.set(pv.validate());
                    }
                } else {
                    retVal = pv.validate();
                }
            }
            return retVal;
        }
        catch (Exception e) {
            pdfgLogger.debug(e.getMessage(), null, (Throwable)e);
            throw new JobOptionsValidateException(key, value.toString());
        }
    }

    private ParamValidator getValidatorClassInstance(String key, Object value) {
        if (ParamSpecs.isBoolParam(key)) {
            return new BooleanValidator(value);
        }
        if (ParamSpecs.isStringParam(key) || ParamSpecs.isNameParam(key)) {
            return new StringValidator(value);
        }
        Object pspec = null;
        pspec = ParamSpecs.getIntParamSpec(key);
        if (pspec != null) {
            return new IntegerValidator(value, (ParamSpecs.IntParamSpec)pspec);
        }
        pspec = ParamSpecs.getIntSetParamSpec(key);
        if (pspec != null) {
            return new IntSetValidator(value, (ParamSpecs.IntSetParamSpec)pspec);
        }
        pspec = ParamSpecs.getStringSetParamSpec(key);
        if (pspec != null) {
            return new StringSetValidator(value, (ParamSpecs.StringSetParamSpec)pspec);
        }
        pspec = ParamSpecs.getNumParamSpec(key);
        if (pspec != null) {
            return new NumberValidator(value, (ParamSpecs.NumParamSpec)pspec);
        }
        for (int i = 0; i < ParamSpecs.NUMSETPARAMS.length; ++i) {
            if (!key.equals(ParamSpecs.NUMSETPARAMS[i].getKeyName())) continue;
            return new NumberSetValidator(value, ParamSpecs.NUMSETPARAMS[i]);
        }
        return null;
    }

    private class BooleanValidator
    extends ParamValidator {
        public BooleanValidator(Object val) {
            super();
            this.value = val;
        }

        public Object validate() throws JobOptionsValidateException {
            if (this.value == null) {
                throw new JobOptionsValidateException(ErrorCode.NullJobOptionValidatorKey, (Object)JOValidator.this.currentKey);
            }
            if (!(this.value instanceof Boolean)) {
                throw new JobOptionsValidateException(ErrorCode.InvalidKeyClassInJobOption, (Object)"", this.value.getClass(), (Object)"Boolean");
            }
            return this.value;
        }
    }

    private class IntegerValidator
    extends ParamValidator {
        ParamSpecs.IntParamSpec intSpec;

        public IntegerValidator(Object val, ParamSpecs.IntParamSpec ispc) {
            super();
            this.intSpec = null;
            this.value = val;
            this.intSpec = ispc;
        }

        public Object validate() throws JobOptionsValidateException {
            int intValue;
            if (this.value == null) {
                throw new JobOptionsValidateException(ErrorCode.NullJobOptionValidatorKey, (Object)JOValidator.this.currentKey);
            }
            if (!(this.value instanceof Double) && !(this.value instanceof Integer)) {
                throw new JobOptionsValidateException(ErrorCode.InvalidKeyClassInJobOption, (Object)JOValidator.this.currentKey, this.value.getClass(), (Object)"Double or Integer");
            }
            if (this.value instanceof Double) {
                Double bdval = (Double)this.value;
                if (bdval.compareTo(new Double(bdval.intValue())) != 0) {
                    throw new JobOptionsValidateException(ErrorCode.IntegerValueRequiredInJobOption, (Object)JOValidator.this.currentKey);
                }
                double d = (Double)this.value;
                intValue = d >= 0.0 ? (int)(d + 0.49) : (int)(d - 0.49);
            } else {
                intValue = (Integer)this.value;
            }
            if (intValue < this.intSpec.getMinLimit()) {
                return new Integer(this.intSpec.getMinLimit());
            }
            if (intValue > this.intSpec.getMaxLimit()) {
                return new Integer(this.intSpec.getMaxLimit());
            }
            return this.value;
        }
    }

    private class IntSetValidator
    extends ParamValidator {
        ParamSpecs.IntSetParamSpec isSpec;

        public IntSetValidator(Object val, ParamSpecs.IntSetParamSpec is) {
            super();
            this.isSpec = null;
            this.value = val;
            this.isSpec = is;
        }

        public Object validate() throws JobOptionsValidateException {
            double d;
            if (this.value == null) {
                throw new JobOptionsValidateException(ErrorCode.NullJobOptionValidatorKey, (Object)JOValidator.this.currentKey);
            }
            if (!(this.value instanceof Double) && !(this.value instanceof Integer)) {
                throw new JobOptionsValidateException(ErrorCode.InvalidKeyClassInJobOption, (Object)JOValidator.this.currentKey, this.value.getClass(), (Object)"Double or Integer");
            }
            int intValue = this.value instanceof Double ? ((d = ((Double)this.value).doubleValue()) >= 0.0 ? (int)(d + 0.49) : (int)(d - 0.49)) : (Integer)this.value;
            int[] vset = this.isSpec.getValueSet();
            for (int i = 0; i < vset.length; ++i) {
                if (intValue != vset[i]) continue;
                return this.value;
            }
            throw new JobOptionsValidateException(ErrorCode.InvalidValueInJobOption2, (Object)JOValidator.this.currentKey);
        }
    }

    private class NumberSetValidator
    extends ParamValidator {
        ParamSpecs.StringSetParamSpec strsetSpec;

        public NumberSetValidator(Object val, ParamSpecs.StringSetParamSpec sspc) {
            super();
            this.strsetSpec = null;
            this.value = val;
            this.strsetSpec = sspc;
        }

        public Object validate() throws JobOptionsValidateException {
            if (this.value == null) {
                throw new JobOptionsValidateException(ErrorCode.NullJobOptionValidatorKey, (Object)JOValidator.this.currentKey);
            }
            if (!(this.value instanceof Double)) {
                throw new JobOptionsValidateException(ErrorCode.InvalidKeyClassInJobOption, (Object)JOValidator.this.currentKey, this.value.getClass(), (Object)"Double");
            }
            Double bdval = (Double)this.value;
            String[] vset = this.strsetSpec.getValueSet();
            for (int i = 0; i < vset.length; ++i) {
                if (bdval.compareTo(new Double(vset[i])) != 0) continue;
                return this.value;
            }
            throw new JobOptionsValidateException(ErrorCode.InvalidValueInJobOption, (Object)JOValidator.this.currentKey, (Object)bdval);
        }
    }

    private class StringSetValidator
    extends ParamValidator {
        ParamSpecs.StringSetParamSpec strsetSpec;

        public StringSetValidator(Object val, ParamSpecs.StringSetParamSpec sspc) {
            super();
            this.strsetSpec = null;
            this.value = val;
            this.strsetSpec = sspc;
        }

        public Object validate() throws JobOptionsValidateException {
            if (this.value == null) {
                throw new JobOptionsValidateException(ErrorCode.NullJobOptionValidatorKey, (Object)JOValidator.this.currentKey);
            }
            if (!(this.value instanceof String)) {
                throw new JobOptionsValidateException(ErrorCode.InvalidKeyClassInJobOption, (Object)JOValidator.this.currentKey, this.value.getClass(), (Object)"Double");
            }
            String strval = (String)this.value;
            String[] vset = this.strsetSpec.getValueSet();
            for (int i = 0; i < vset.length; ++i) {
                if (!strval.equals(vset[i])) continue;
                return this.value;
            }
            throw new JobOptionsValidateException(ErrorCode.InvalidValueInJobOption, (Object)JOValidator.this.currentKey, (Object)strval);
        }
    }

    private class NumberValidator
    extends ParamValidator {
        ParamSpecs.NumParamSpec numSpec;

        public NumberValidator(Object val, ParamSpecs.NumParamSpec nSpec) {
            super();
            this.numSpec = null;
            this.value = val;
            this.numSpec = nSpec;
        }

        public Object validate() throws JobOptionsValidateException {
            if (this.value == null) {
                throw new JobOptionsValidateException(ErrorCode.NullJobOptionValidatorKey, (Object)JOValidator.this.currentKey);
            }
            if (!(this.value instanceof Double)) {
                throw new JobOptionsValidateException(ErrorCode.InvalidKeyClassInJobOption, (Object)JOValidator.this.currentKey, this.value.getClass(), (Object)"Double");
            }
            Double bdval = (Double)this.value;
            if (bdval.compareTo(new Double(this.numSpec.getMinLimit())) < 0) {
                return new Double(this.numSpec.getMinLimit());
            }
            if (bdval.compareTo(new Double(this.numSpec.getMaxLimit())) > 0) {
                return new Double(this.numSpec.getMaxLimit());
            }
            return this.value;
        }
    }

    private class StringValidator
    extends ParamValidator {
        public StringValidator(Object val) {
            super();
            this.value = val;
        }

        public Object validate() throws JobOptionsValidateException {
            if (this.value == null) {
                throw new JobOptionsValidateException(ErrorCode.NullJobOptionValidatorKey, (Object)JOValidator.this.currentKey);
            }
            if (!(this.value instanceof String)) {
                throw new JobOptionsValidateException(ErrorCode.InvalidKeyClassInJobOption, (Object)JOValidator.this.currentKey, this.value.getClass(), (Object)"String");
            }
            return this.value;
        }
    }

    private abstract class ParamValidator {
        Object value;

        private ParamValidator() {
            this.value = null;
        }

        public abstract Object validate() throws JobOptionsValidateException;

        public Object getValue() {
            return this.value;
        }

        public void setValue(Object value) {
            this.value = value;
        }
    }

}