com.codename1.ui.html.HTMLInputFormat Maven / Gradle / Ivy
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores
* CA 94065 USA or visit www.oracle.com if you need additional information or
* have any questions.
*/
package com.codename1.ui.html;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextField;
import com.codename1.ui.plaf.UIManager;
import java.util.Enumeration;
import java.util.Vector;
/**
* This class implements HTML's input format restrictions.
* These restrictions can be provided in the FORMAT attribute of the INPUT tag and are relevant for textfields.
* However as the FORMAT tag was deprectaed it is more standard to supply them in the '-wap-input-format' of the input field CSS or Style.
*
* @author Ofir Leitner
*/
class HTMLInputFormat {
/**
* The allowed literals in an input format defintion
*/
private static char[] literals = {'a','A','n','N','x','X','m','M'};
/**
* The matching allowed character set for each literal
*/
private static int[] literalConstraints =
{FormatConstraint.TYPE_LOWERCASE|FormatConstraint.TYPE_SYMBOL,
FormatConstraint.TYPE_UPPERCASE|FormatConstraint.TYPE_SYMBOL,
FormatConstraint.TYPE_NUMERIC|FormatConstraint.TYPE_SYMBOL,
FormatConstraint.TYPE_NUMERIC,
FormatConstraint.TYPE_LOWERCASE|FormatConstraint.TYPE_NUMERIC|FormatConstraint.TYPE_SYMBOL,
FormatConstraint.TYPE_UPPERCASE|FormatConstraint.TYPE_NUMERIC|FormatConstraint.TYPE_SYMBOL,
FormatConstraint.TYPE_LOWERCASE|FormatConstraint.TYPE_ANY,
FormatConstraint.TYPE_UPPERCASE|FormatConstraint.TYPE_ANY
};
private int minLength;
private int maxLength;
private Vector formatConstraints = new Vector();
/**
* This static method is used to create an HTMLInputFormat
*
* @param formatString The string representing the format defintion (As taken from the HTML/CSS)
* @return An HTMLInputFormat object containing all the constraints or null if there are none or if the string is invalid
*/
static HTMLInputFormat getInputFormat(String formatString) {
if (formatString==null) {
return null;
}
try {
HTMLInputFormat format=new HTMLInputFormat(formatString);
if (format.formatConstraints.size()==0) {
return null;
}
return format;
} catch (Exception e) {
System.out.println(e.getMessage()+" at input format string "+formatString);
return null;
}
}
/**
* A private constructor, to obtain an HTMLInputFormat object use HTMLInputFormat.getInputFormat
*
* @param formatString The string representing the format defintion (As taken from the HTML/CSS)
*/
private HTMLInputFormat(String formatString) {
String count="";
for(int i=0;i='0') && (c<='9')) {
if (count.equals("*")) {
throw new IllegalArgumentException("Malformed format string. Count indicators cannot appear after the wildcard *");
} else {
count+=c;
}
} else {
int constraint=-1;
for(int j=0;jmaxLength) || (str.length()=str.length()) {
break;
}
}
}
if (i='0') && (c<='9')) ||
(((constraint & FormatConstraint.TYPE_UPPERCASE)!=0) && (c>='A') && (c<='Z')) ||
(((constraint & FormatConstraint.TYPE_LOWERCASE)!=0) && (c>='a') && (c<='z'))) {
return true;
}
if ((constraint & FormatConstraint.TYPE_SYMBOL)!=0) {
char[] symbols=TextField.getSymbolTable();
for(int i=0;i