All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.objects.javascript.DefaultParser Maven / Gradle / Ivy

There is a newer version: 7.15.25
Show newest version
/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2017 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
 @LICENSE@
 *
 * ---------------
 * DefaultParser.java
 * ---------------
 */
package org.jpedal.objects.javascript;

import org.jpedal.objects.acroforms.AcroRenderer;
import org.jpedal.objects.acroforms.actions.ActionHandler;
import org.jpedal.objects.javascript.functions.*;
import org.jpedal.objects.raw.FormObject;

/**
 * generic version for no javascript engine which just implements AF commands in Java
 */
public class DefaultParser implements ExpressionEngine {

    public DefaultParser() {
    }

    AcroRenderer acro;

    /**
     * execute javascript and reset forms values
     */
    @Override
    public int execute(final FormObject form, final int type, final String code, final int eventType, final char keyPressed) {

        int messageCode = ActionHandler.NOMESSAGE;

        if (code instanceof String) {
            final String js = code;

            //convert into args array
            final String[] args = JSFunction.convertToArray(js);

            final String command = args[0];

            if (command.startsWith("AF")) {
                messageCode = handleAFCommands(form, command, js, args, eventType, keyPressed);
            }

        }

        return messageCode;
    }

    /**
     * java implementation to provide these functions
     *
     * @param command
     * @param js
     * @param args
     * @param eventType
     * @param keyPressed
     * @return
     */
    protected int handleAFCommands(final FormObject formObject, final String command, final String js, final String[] args, final int eventType, final char keyPressed) {

        int messageCode = ActionHandler.NOMESSAGE;

        //Workout type
        int type = JSFunction.UNKNOWN;

        if (js.contains("_Keystroke")) {
            type = JSFunction.KEYSTROKE;
        } else if (js.contains("_Validate")) {
            type = JSFunction.VALIDATE;
        } else if (js.contains("_Format")) {
            type = JSFunction.FORMAT;
        } else if (js.contains("_Calculate")) {
            type = JSFunction.CALCULATE;
        }

        if (eventType != ActionHandler.FOCUS_EVENT && (type == JSFunction.VALIDATE || type == JSFunction.FORMAT)) {
            JSFunction.debug("Not called on key event " + js);
            return messageCode;
        }

        if (js.startsWith("AFSpecial_")) {
            new AFSpecial(acro, formObject).execute(js, args, type, eventType, keyPressed);
        } else if (command.startsWith("AFPercent_")) {
            new AFPercent(acro, formObject).execute(js, args, type, eventType, keyPressed);
        } else if (command.startsWith("AFSimple_")) {
            new AFSimple(acro, formObject).execute(js, args, type, eventType, keyPressed);
        } else if (command.startsWith("AFDate_")) {
            new AFDate(acro, formObject).execute(js, args, type, eventType, keyPressed);
        } else if (js.startsWith("AFNumber_")) {
            messageCode = new AFNumber(acro, formObject).execute(js, args, type, eventType, keyPressed);
        } else if (js.startsWith("AFRange_")) {
            new AFRange(acro, formObject).execute(js, args, type, eventType, keyPressed);
        } else if (js.startsWith("AFTime_")) {
            new AFTime(acro, formObject).execute(js, args, type, eventType, keyPressed);
        } else {
            JSFunction.debug("Unknown command " + js);
        }

        return messageCode;
    }

    @Override
    public void closeFile() {

        flush();

    }

    @Override
    public boolean reportError(final int code, final Object[] args) {
        //does nothing in default
        return false;
    }

    @Override
    public int addCode(final String value) {
        //does nothing in default
        return 0;
    }

    @Override
    public void executeFunctions(final String jsCode, final FormObject formObject) {
        //does nothing in default
    }

    @Override
    public void dispose() {

        flush();
    }

    @Override
    public void setAcroRenderer(final AcroRenderer acro) {
        this.acro = acro;
    }

    /**
     * make sure the contaxt has been exited
     */
    public void flush() {
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy