Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: AStringFmt.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.fmt;
import at.spardat.enterprise.util.*;
/**
* A formatter to validate the length and char set in strings.
*/
public class AStringFmt extends IFmt {
/**
* Defines that this formatter makes all characters uppercase
*/
public static final int UPPER_CASE = IFmt.LAST_STYLE * 2;
/**
* Defines that this formatter makes all characters lowercase
* using the method Character.
*/
public static final int LOWER_CASE = UPPER_CASE * 2;
/**
* Defines an upper bound on a specified maximum length.
* If you specify -1 (unlimited) for the maximum length in the constructor,
* the maximum length is set to this constant.
*/
public static final int MAX_MAX_LEN = 65535;
// the maximum length of the string
private int maxLen_;
/**
* the mininum length of the string;
*/
private int minLen_ = 0;
/**
* Returns a String formatter to limit the input length.
*
* @param maxLen the maximum length of an accepted string. May be -1, then
* the length is unlimited.
*/
public static AStringFmt getInstance (int maxLen) {
return new AStringFmt (maxLen);
}
/**
* Returns a String formatter to limit the input length.
*
* @param maxLen the maximum length of an accepted string. May be -1, then
* the length is unlimited.
* @param style may be MANDATORY which forces the input not to be empty or
* UPPER_CASE or LOWER_CASE.
*/
public static AStringFmt getInstance (int maxLen, int style) {
return new AStringFmt (maxLen, style);
}
/**
* Returns a String formatter to limit the input length.
*
* @param maxLen the maximum length of an accepted string. May be -1, then
* the length is unlimited.
* @param range a string denoting one or more character ranges.
* See {@link AStringFmtRange} for a syntax description.
*/
public static AStringFmt getInstance (int maxLen, String range) {
return new AStringFmtRange (maxLen, range);
}
/**
* Returns a String formatter to limit the input length.
*
* @param maxLen the maximum length of an accepted string. May be -1, then
* the length is unlimited.
* @param range a string denoting one or more character ranges.
* See {@link AStringFmtRange} for a syntax description.
* @param style may be MANDATORY which forces the input not be empty or
* UPPER_CASE or LOWER_CASE.
*/
public static AStringFmt getInstance (int maxLen, String range, int style) {
return new AStringFmtRange (maxLen, range, style);
}
/**
* Returns a String formatter to limit the input length, the range of valid characters
* and a regular expression (according to the java.util.regex packet) for input validation.
*
* @param maxLen the maximum length of an accepted string. May be -1, then
* the length is unlimited.
* @param range a string denoting one or more character ranges.
* See {@link AStringFmtRange} for a syntax description.
* @param style may be MANDATORY which forces the input not be empty or
* UPPER_CASE or LOWER_CASE.
* @param regex a regular expression according to the java.util.regex packet.
* The input string is validated by this expression.
* @param bundleKey the resource bundle key of the error message,
* which is shown in the case of a negative evalution of the regular expression.
* @param resBundle the resource bundle name from which the error message
* for negative evalution of the regular expression is taken.
*/
public static AStringFmt getInstance (int maxLen, String range, int style, String regex, String bundleKey, String resBundle) {
return new AStringFmtRangeRegEx (maxLen, range, style, regex, bundleKey, resBundle);
}
/**
* Constructs a length limited String formatter.
*
* @param maxLen the maximum length of an accepted string. May be -1, then
* the length is unlimited.
*/
public AStringFmt (int maxLen) {
setMaxLen (maxLen);
}
/**
* Constructs a length limited String formatter.
*
* @param maxLen the maximum length of an accepted string or -1, if no
* length limitation should be imposed.
* @param style may be MANDATORY.
*/
public AStringFmt (int maxLen, int style) {
this (maxLen);
style_ = style;
}
/**
* Sets the maximum string length this formatter accepts.
*
* @param maxLen the maximum length of an accepted string. May be -1, then
* the length is unlimited (theoretically), but practically
* set to MAX_MAX_LEN.
*/
public void setMaxLen (int maxLen) {
if (maxLen <= -1 || maxLen > MAX_MAX_LEN) maxLen = MAX_MAX_LEN;
maxLen_ = maxLen;
}
/**
* Returns the maximum length a string accepted by this formatter may have.
*
* @return max number of characters. Returns MAX_MAX_LEN if you
* have set an unlimited maximum size.
*/
public int getMaxLen () {
return maxLen_;
}
/**
* Sets the minimum length a string accepted must have. Note that this
* does not control if a empty string is allowed or not. For this purpose,
* use the MANDATORY-style. A AStringFmt with a minLen
* of 5 does accept strings of length 0 and greater than or equal to
* 5 if MANDATORY is not set.
*/
public void setMinLen (int minLen) {
minLen_ = minLen;
}
/**
* Returns the minimum length.
*/
public int getMinLen () {
return minLen_;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#parse(String)
*/
public String parse (String external) {
String internal = parse2 (external);
checkMandatory (internal);
return internal;
}
/**
* Does the parse without the mandatory-check
*/
public String parse2 (String external) {
if (external == null) return "";
external = convertCase (external);
if (external.length() > maxLen_)
throw new FmtParseException ("AStringLen", String.valueOf(maxLen_));
if (external.length() < minLen_ && external.length() != 0)
throw new FmtParseException ("AStringMinLen", String.valueOf(minLen_));
return external;
}
/**
* Makes in upper- or lowercase, depending on styles.
*/
private String convertCase (String in) {
if ((style_ & UPPER_CASE) != 0) return in.toUpperCase();
if ((style_ & LOWER_CASE) != 0) return in.toLowerCase();
return in;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#format(String)
*/
public String format (String internal) {
if (internal == null) return "";
return convertCase(internal);
}
/**
* @see at.spardat.enterprise.fmt.IFmt#maxLenOfExternal()
*/
public int maxLenOfExternal() {
return maxLen_;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#isLegalExternalChar(char)
*/
public boolean isLegalExternalChar (char aChar) {
return true;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#isLegalInternal(String)
*/
public boolean isLegalInternal (String internal) {
if (internal == null) return true;
return internal.length() <= maxLen_;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#isOneWay()
*/
public boolean isOneWay() {
return false;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#mayBeAppliedTo(byte)
*/
public boolean mayBeAppliedTo (byte type) {
return type == Types.T_STRING;
}
}