OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

package org.openconcerto.modules.label.filter;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

public class EANDocumentFilter extends LimitDocumentFilter {

    public EANDocumentFilter() {
        super(14);
    }

    @Override
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
        super.insertString(fb, offset, clean(text), attr);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
        super.replace(fb, offset, length, clean(text), attrs);
    }

    public static String clean(String s) {
        final int length = s.length();
        final StringBuilder b = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            final char charAt = s.charAt(i);
            if (Character.isDigit(charAt)) {
                b.append(s.charAt(i));
            } else {
                b.append('0');
            }
        }
        return b.toString();
    }

}