Android Open Source - esr-scanner Esr Validation






From Project

Back to project page esr-scanner.

License

The source code is released under:

Apache License

If you think the Android project esr-scanner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Lukas Landis/*w  ww.  j av a 2 s.c  o  m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ch.luklanis.esscan.paymentslip;

import android.util.Log;

public class EsrValidation extends PsValidation {
    private static final String TAG = "ESR Validation";
    private static final int STEP_COUNT = 3;

    private static final char[] CONTROL_CHARS_IN_STEP = {'>', '+', '>'};

    private static final int[][] VALID_LENGTHS_IN_STEP = {{4, 14}, {28, 17}, {10, -1}};

    private static final String[] STEP_FORMAT = {"%s", "%s", " %s"};

    public EsrValidation() {
        completeCode = new String[STEP_COUNT];
    }

    @Override
    public int getStepCount() {
        return STEP_COUNT;
    }

    @Override
    public boolean validate(String text) {
        try {
            resetStartSearchIndexes();
            text = preformatText(text);
            // Log.d(TAG, String.format("text: %s", text));
            String related;
            while (!(related = getNextRelatedText(text)).isEmpty()) {
                // Log.d(TAG, String.format("related: %s", related));

                for (int validLength : VALID_LENGTHS_IN_STEP[currentStep]) {
                    if (validLength == -1 || related.length() < (validLength + 1)) {
                        continue;
                    }

                    if (related.charAt(related.length() - (validLength + 1)) != (currentStep > 0 ? CONTROL_CHARS_IN_STEP[currentStep - 1] : SEPERATOR)) {
                        continue;
                    }

                    String possiblePart = related.substring(related.length() - validLength,
                            related.length());

                    if (hasNonDigts(possiblePart, possiblePart.length() - 1)) {
                        continue;
                    }

                    int[] digits = getDigitsFromText(possiblePart, possiblePart.length() - 1);
                    int[] withoutCheckDigit = new int[digits.length - 1];

                    System.arraycopy(digits, 0, withoutCheckDigit, 0, withoutCheckDigit.length);

                    int checkDigit = getCheckDigit(withoutCheckDigit);

                    if (checkDigit == digits[digits.length - 1] && additionalStepTest(possiblePart)) {
                        completeCode[currentStep] = String.format(STEP_FORMAT[currentStep],
                                possiblePart);
                        return true;
                    }
                }
            }
        } catch (Exception exc) {
            Log.e(TAG, exc.toString());
            return false;
        }

        return false;
    }

    protected boolean additionalStepTest(String related) {
        if (currentStep == 0) {
            int esrType = Integer.parseInt(related.substring(0, 2));

            switch (esrType) {
                case 4:
                case 14:
                case 31:
                case 33:
                    return related.length() == 4;
                default:
                    return related.length() > 4;
            }
        }

        return true;
    }

    @Override
    public String getCurrentRelatedText() {
        return relatedText;
    }

    @Override
    public String getNextRelatedText(String text) {
        if (text == null || text == "") {
            return "";
        }

        relatedText = text;

//        if (currentStep > 0) {
//            indexOfControlCharBefore = relatedText.indexOf(String.valueOf(CONTROL_CHARS_IN_STEP[currentStep - 1]), indexOfControlCharBefore);
//
//            if (indexOfControlCharBefore != -1 && indexOfControlCharBefore < (relatedText.length() - 1)) {
//                relatedText = relatedText.substring(indexOfControlCharBefore + 1);
//            } else {
//                return "";
//            }
//        }

//        indexOfCurrentControlChar = relatedText.indexOf(String.valueOf(CONTROL_CHARS_IN_STEP[currentStep]), indexOfControlCharBefore + indexOfCurrentControlChar + 1);
        indexOfCurrentControlChar = relatedText.indexOf(String.valueOf(CONTROL_CHARS_IN_STEP[currentStep]),
                indexOfCurrentControlChar + 1);

        if (indexOfCurrentControlChar != -1) {
            relatedText = relatedText.substring(0, indexOfCurrentControlChar + 1);
        } else {
            return "";
        }

        return relatedText;
    }

    @Override
    public String getSpokenType() {
        return EsrResult.PS_TYPE_NAME;
    }
}




Java Source Code List

ch.luklanis.android.common.PlatformSupportManager.java
ch.luklanis.android.common.executor.AsyncTaskExecInterface.java
ch.luklanis.android.common.executor.AsyncTaskExecManager.java
ch.luklanis.android.common.executor.DefaultAsyncTaskExecInterface.java
ch.luklanis.android.common.executor.HoneycombAsyncTaskExecInterface.java
ch.luklanis.esscan.BeepManager.java
ch.luklanis.esscan.CaptureActivityHandler.java
ch.luklanis.esscan.CaptureActivity.java
ch.luklanis.esscan.DecodeHandler.java
ch.luklanis.esscan.DecodeThread.java
ch.luklanis.esscan.EsrBaseActivity.java
ch.luklanis.esscan.FinishListener.java
ch.luklanis.esscan.HelpActivity.java
ch.luklanis.esscan.IBase.java
ch.luklanis.esscan.Intents.java
ch.luklanis.esscan.LuminanceSource.java
ch.luklanis.esscan.OcrCharacterHelper.java
ch.luklanis.esscan.OcrInitAsyncTask.java
ch.luklanis.esscan.OcrRecognizeAsyncTask.java
ch.luklanis.esscan.OcrResultFailure.java
ch.luklanis.esscan.OcrResultText.java
ch.luklanis.esscan.OcrResult.java
ch.luklanis.esscan.PlanarYUVLuminanceSource.java
ch.luklanis.esscan.PreferencesActivity.java
ch.luklanis.esscan.ViewfinderView.java
ch.luklanis.esscan.camera.AutoFocusManager.java
ch.luklanis.esscan.camera.CameraConfigurationManager.java
ch.luklanis.esscan.camera.CameraManager.java
ch.luklanis.esscan.camera.PreviewCallback.java
ch.luklanis.esscan.camera.exposure.DefaultExposureInterface.java
ch.luklanis.esscan.camera.exposure.ExposureInterface.java
ch.luklanis.esscan.camera.exposure.ExposureManager.java
ch.luklanis.esscan.camera.exposure.FroyoExposureInterface.java
ch.luklanis.esscan.camera.open.DefaultOpenCameraInterface.java
ch.luklanis.esscan.camera.open.GingerbreadOpenCameraInterface.java
ch.luklanis.esscan.camera.open.OpenCameraInterface.java
ch.luklanis.esscan.camera.open.OpenCameraManager.java
ch.luklanis.esscan.codesend.Crypto.java
ch.luklanis.esscan.codesend.ESRSenderHttp.java
ch.luklanis.esscan.codesend.IEsrSender.java
ch.luklanis.esscan.dialogs.BankProfileDialogFragment.java
ch.luklanis.esscan.dialogs.BankProfileListDialog.java
ch.luklanis.esscan.dialogs.CancelOkDialog.java
ch.luklanis.esscan.dialogs.OkDialog.java
ch.luklanis.esscan.dialogs.OptionalOkDialog.java
ch.luklanis.esscan.history.BankProfile.java
ch.luklanis.esscan.history.DBHelper.java
ch.luklanis.esscan.history.GetHistoryAsyncTask.java
ch.luklanis.esscan.history.HistoryActivity.java
ch.luklanis.esscan.history.HistoryExportUpdateAsyncTask.java
ch.luklanis.esscan.history.HistoryFragment.java
ch.luklanis.esscan.history.HistoryItemAdapter.java
ch.luklanis.esscan.history.HistoryItem.java
ch.luklanis.esscan.history.HistoryManager.java
ch.luklanis.esscan.history.PsDetailActivity.java
ch.luklanis.esscan.history.PsDetailFragment.java
ch.luklanis.esscan.ime.ScannerIME.java
ch.luklanis.esscan.paymentslip.DTAFileCreator.java
ch.luklanis.esscan.paymentslip.EsIbanResult.java
ch.luklanis.esscan.paymentslip.EsIbanValidation.java
ch.luklanis.esscan.paymentslip.EsrResult.java
ch.luklanis.esscan.paymentslip.EsrValidation.java
ch.luklanis.esscan.paymentslip.PsResult.java
ch.luklanis.esscan.paymentslip.PsValidation.java