home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
DatePatternToMaskXLator.java
< prev
next >
Wrap
Text File
|
1998-12-09
|
22KB
|
671 lines
/*
* @(#DatePatternToMaskXLator.java
*
* Copyright (c) 1998 Symantec Corporation. All Rights Reserved.
*
*/
package com.symantec.itools.swing;
import java.text.SimpleDateFormat;
import java.io.Serializable;
/**
* DatePatternToMaskXLator is a translator that can be used to translate
* date patterns such as "yyyy.MM.dd G 'at' hh:mm:ss z" to an appropriate mask.
* The pattern should not be a localized pattern.
* <p>
* The mask could then be used in masked fields and formatted text fields. The
* patterns are interpreted using the format syntax specified in
* java.text.SimpleDateFormat. The translator also allows some customization to
* suit specific needs.
* <p>
*
* @author Vasudev J. Rao
* @version 1.0
* @see java.text.SimpleDateFormat
* @see com.symantec.itools.swing.JDateMaskedField
* @see com.symantec.itools.swing.JMaskedTextField
*/
public class DatePatternToMaskXLator implements Serializable {
public static final int PRECEDE_MASKTOKENS_IN_LITERAL_WITH_ESC = 0;
public static final int PRECEDE_ALLCHARS_IN_LITERAL_WITH_ESC = 1;
static final String INVALID_POLICY = "invalid policy";
protected char maskNumericCharacter = '9';
protected char maskAlphaCharacter = 'C';
protected char maskEscapeCharacter = '\\';
//these are all mask filter + command characters
protected char[] maskTokens = { '0' , '9' , '#' ,
'L' , '?' , 'A' ,
'a' , '&' , 'C' ,
'.' , ',' , ':' ,
';' , '-' , '/' ,
'<' , '>' , '\\' };
protected int escapeSyntaxPolicy = PRECEDE_MASKTOKENS_IN_LITERAL_WITH_ESC ;
/**
* Default constructor. Constructs a date pattern to mask translator using
* '9' for mask numeric character, 'C' for mask alpha character and '\\' for
* mask escape character.
*/
public DatePatternToMaskXLator ( ) {
this( '9' , 'C' , '\\' );
}
/**
* Constructor to construct a translator using the specified characters
* for numeric and alpha mask and mask escape characters.
*/
public DatePatternToMaskXLator ( char numericMask, char alphaMask, char esc ) {
super();
setMaskNumericCharacter ( numericMask );
setMaskAlphaCharacter ( alphaMask );
setMaskEscapeCharacter ( esc );
}
/**
* Sets a character to be used for mask numeric character. By default
* it is '9'. It may be set to any other character ( for eg., '0', '#', etc).
*/
public void setMaskNumericCharacter ( char c ) {
maskNumericCharacter = c;
}
/**
* Returns the character used for mask numeric filter.
*/
public char getMaskNumericCharacter ( ) {
return maskNumericCharacter ;
}
/**
* Sets a character to be used for mask alpha character.By default
* it is 'C'. It may be set to any other character ( for eg., 'A', 'a', etc).
*/
public void setMaskAlphaCharacter ( char c ) {
maskAlphaCharacter = c;
}
/**
* Returns the character used for mask alpha filter.
*/
public char getMaskAlphaCharacter ( ) {
return maskAlphaCharacter ;
}
/**
* Sets a character to be used for mask escape character.By default
* it is '\\'.
*/
public void setMaskEscapeCharacter ( char c ) {
maskEscapeCharacter = c;
}
/**
* Returns the character used for mask escape character.
*/
public char getMaskEscapeCharacter ( ) {
return maskEscapeCharacter ;
}
/**
* Sets an array of characters as reserved mask tokens. If the escape
* syntax policy is PRECEDE_MASKTOKENS_IN_LITERAL_WITH_ESC, this list is
* checked to determine whether or not to precede a character with the
* escape character.
*/
public void setMaskTokens ( char [] maskTokens ) {
this.maskTokens = maskTokens ;
}
/**
* Returns an array of characters recognized as reserved mask tokens.
*/
public char[] getMaskTokens ( ) {
return this.maskTokens ;
}
/**
* Sets the escape syntax policy. Can be one of
* PRECEDE_MASKTOKENS_IN_LITERAL_WITH_ESC or PRECEDE_ALLCHARS_IN_LITERAL_WITH_ESC
* <p>
* If the escape syntax policy is PRECEDE_MASKTOKENS_IN_LITERAL_WITH_ESC,
* if a reserved mask token occurs within a literal, it is preceded with
* the mask escape character.
* <p>
* If the escape syntax policy is PRECEDE_ALLCHARS_IN_LITERAL_WITH_ESC,
* all character within a literal are preceded with mask escape character.
*
* @throws IllegalArgumentException if the policy is invalid.
*/
public void setEscapeSyntaxPolicy ( int policy ) {
if ( policy == PRECEDE_MASKTOKENS_IN_LITERAL_WITH_ESC ||
policy == PRECEDE_ALLCHARS_IN_LITERAL_WITH_ESC ) {
escapeSyntaxPolicy = policy ;
}
else {
throw new IllegalArgumentException ( INVALID_POLICY ) ;
}
}
/**
* Returns the currentescape syntax policy.
*/
public int getEscapeSyntaxPolicy ( ) {
return escapeSyntaxPolicy ;
}
/**
* Get the mask representation for a DateFormats pattern.
*/
public String getMaskForDatePattern ( String pattern ){
// Note:
// String tokenizer cannot be used here!! Because we may have
// strings like "hh 'o''clock' a, zzzz"
// in which case the tokens would be "hh ","o","clock" and " a,zzzz".
// We will not know that we need a "'" embedded b/w "o" and "clock".
StringBuffer retBuf = new StringBuffer();
int lastQuotePos = -1 ; // quote position stored away in the last iteration
int index = 0; // used to exit out of the while loop.
while ( index < pattern.length() ) {
int quotePos = pattern.indexOf( "'" , lastQuotePos + 1 );
if ( quotePos != -1 ) {// found a quote!
if ( lastQuotePos + 1 == quotePos ) {
// found successive quotes. quote is escape char for date formats
// ==> quote is reqd as a literal. so, append one.
retBuf.append ('\'');
}
else {
// found the first sub-pattern
String tempString = pattern.substring( lastQuotePos + 1 , quotePos );
retBuf.append ( getMaskForDateSubPattern ( tempString ) );
}
index = quotePos + 1;//skip to the next sub pattern.
// find if there is a matching closing quote
// if found we know that it is a string literal
int nextQuotePos = pattern.indexOf( "'" , quotePos + 1 );
if ( nextQuotePos != -1 ) {
//found!!!
if ( quotePos + 1 == nextQuotePos ) {
//successive quotes. should not come here normally.
//@todo: check this logic again. kinda confusing!!
retBuf.append ('\'');
}
else {
//we have a literal embedded in the pattern.
String tempLiteral = pattern.substring( quotePos + 1 , nextQuotePos );
retBuf.append ( getMaskLiteralString ( tempLiteral ) );
}
quotePos = nextQuotePos ;
index = nextQuotePos + 1;//skip to the next sub string.
}
else{
//should not normally come here!!
retBuf.append ( getMaskEscapeCharacter( ) );
index = quotePos + 1;
}
lastQuotePos = quotePos;
}
else{
//if no matching quote was found,
// get the mask for the remaining pattern.
String tempString = pattern.substring( lastQuotePos + 1 , pattern.length() );
retBuf.append ( getMaskForDateSubPattern ( tempString ) );
index = pattern.length();
}
}
return retBuf.toString();
}
//PROTECTED SECTION
/**
* Get the mask representation for a DateFormats sub-pattern.
*/
protected String getMaskForDateSubPattern ( String pattern ){
StringBuffer retMask = new StringBuffer();
//first char
char prevChar = pattern.charAt ( 0 ) ;
int repeatCount = 1;
for ( int index = 1 ; index < pattern.length() ; index ++ ) {
//second char to last_but_one char
char thisChar = pattern.charAt( index ) ;
if ( thisChar == prevChar ) {
//repeated chars in a pattern have some significance.
//so accumulate the count
repeatCount ++ ;
}
else {
// a diff character from the last one.
//so for the prev character and its count, get the mask.
retMask.append ( getMaskStringForPatternChars ( prevChar , repeatCount ) );
//set this char as the prev char for the next iteration
prevChar = thisChar ;
repeatCount = 1;//set the accumulator to 1
}
}
//handle last character
retMask.append ( getMaskStringForPatternChars ( prevChar , repeatCount) ) ;
return retMask.toString() ;
}
/**
* Get the equivalent mask string for a DateFormatSymbol character.
*/
protected String getMaskStringForPatternChars ( char c , int count ) {
StringBuffer retBuffer = new StringBuffer ( ) ;
int length = 0;
char maskChar = getMaskNumericCharacter( ) ;
switch ( c ) {
case 'y': //year (Number) 1996
if ( count < 4 ) {
length = 2;
}
else {
length = 4;
}
break;
case 'd': //day in month (Number) 10
case 'h': //hour in am/pm (1~12) (Number) 12
case 'H': //hour in day (0~23) (Number) 0
case 'm': //minute in hour (Number) 30
case 's': //second in minute (Number) 55
case 'k': //hour in day (1~24) (Number) 24
case 'K': //hour in am/pm (0~11) (Number) 0
case 'w': //week in year (Number) 27
length = 2;
break;
case 'S': //millisecond (Number) 978
case 'D': //day in year (Number) 189
length = 3;
break;
case 'F': //day of week in month (Number) 2 (2nd Wed in July)
case 'W': //week in month (Number) 2
length = 1;
break;
case 'G': //era designator (Text) AD
length = getEraStringLength() ;
maskChar = getMaskAlphaCharacter( ) ;
break;
case 'E': //day in week (Text) Tuesday
if ( count < 4 ) {
length = getShortWeekDaysStringLength ();
}
else {
length = getWeekDaysStringLength ();
}
maskChar = getMaskAlphaCharacter( ) ;
break;
case 'a': //am/pm marker (Text) PM
length = getAmPmStringLength ();
maskChar = getMaskAlphaCharacter( ) ;
break;
case 'z': //time zone (Text) Pacific Standard Time
if ( count < 4 ) {
length = getShortZoneStringLength ();
}
else {
length = getZoneStringLength ();
}
maskChar = getMaskAlphaCharacter( ) ;
break;
case 'M': //month in year (Text & Number) July & 07
if ( count < 3 ) {
length = 2 ;
}
else if ( count == 3 ) {
length = getShortMonthStringLength ();
maskChar = getMaskAlphaCharacter( ) ;
}
else {
length = getMonthStringLength ();
maskChar = getMaskAlphaCharacter( ) ;
}
break;
case '\'': //escape for text (Delimiter)
//normally would not come here.
//Because we handle "'" differently.
length = 1 ;
maskChar = getMaskEscapeCharacter( ) ;
break;
default:
length = count ;
maskChar = c ;
break;
}
for ( int i = 0 ; i < length ; i ++ ) {
retBuffer.append ( maskChar ) ;
}
return retBuffer.toString() ;
}
// purpose of this method is to check and find if the literal has any
// mask key word. If yes, prefix them with "\";
// Mask key word is either a filter or a command character
protected String getMaskLiteralString ( String input ) {
StringBuffer retString = new StringBuffer();
for ( int index = 0 ; index < input.length() ; index ++ ) {
char tempChar = input.charAt( index ) ;
if ( getEscapeSyntaxPolicy () == PRECEDE_MASKTOKENS_IN_LITERAL_WITH_ESC ) {
if ( isCharacterMaskToken ( tempChar ) ) {
retString.append( getMaskEscapeCharacter() ).append( tempChar );
}
else {
retString.append( tempChar );
}
}
else {
retString.append( getMaskEscapeCharacter() ).append( tempChar );
}
}
return retString.toString();
}
//helper methods
protected boolean isCharacterMaskToken ( char c ) {
boolean retBool = false ;
for ( int i = 0 ; i < maskTokens.length ; i ++ ) {
if ( c == maskTokens [i] ) {
retBool = true;
break;
}
}
return retBool;
}
protected java.text.DateFormatSymbols getDateFormatSymbols(){
//SimpleDateFormat sdf = ( SimpleDateFormat ) getFormatter ( getType () , getFormattingStyle() );
SimpleDateFormat sdf = new SimpleDateFormat ( );
return sdf.getDateFormatSymbols();
}
protected int getAmPmStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [] AmPmStrings;
if ( dfs == null ){
retLength = 2;
}
else {
AmPmStrings = dfs. getAmPmStrings ();
retLength = getMaximumLength ( AmPmStrings );
}
return retLength;
}
protected int getEraStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [] eraStrings;
if ( dfs == null ){
retLength = 2;
}
else {
eraStrings = dfs. getEras ();
retLength = getMaximumLength ( eraStrings );
}
return retLength;
}
protected int getMonthStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [] monthStrings;
if ( dfs == null ){
retLength = 12;
}
else {
monthStrings = dfs. getMonths ();
retLength = getMaximumLength ( monthStrings );
}
return retLength;
}
protected int getShortMonthStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [] monthStrings;
if ( dfs == null ){
retLength = 4;
}
else {
monthStrings = dfs. getShortMonths ();
retLength = getMaximumLength ( monthStrings );
}
return retLength;
}
protected int getShortWeekDaysStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [] weekDayStrings;
if ( dfs == null ){
retLength = 3;
}
else {
weekDayStrings = dfs. getShortWeekdays ();
retLength = getMaximumLength ( weekDayStrings );
}
return retLength;
}
protected int getWeekDaysStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [] weekDayStrings;
if ( dfs == null ){
retLength = 10;
}
else {
weekDayStrings = dfs. getWeekdays ();
retLength = getMaximumLength ( weekDayStrings );
}
return retLength;
}
/*
The items in the 2D array looks like this:
item at : 0 , 0 : PST
item at : 0 , 1 : Pacific Standard Time
item at : 0 , 2 : PST
item at : 0 , 3 : Pacific Daylight Time
item at : 0 , 4 : PDT
item at : 1 , 0 : America/Los_Angeles
item at : 1 , 1 : Pacific Standard Time
item at : 1 , 2 : PST
item at : 1 , 3 : Pacific Daylight Time
item at : 1 , 4 : PDT
item at : 2 , 0 : MST
item at : 2 , 1 : Mountain Standard Time
item at : 2 , 2 : MST
item at : 2 , 3 : Mountain Daylight Time
item at : 2 , 4 : MDT
.....
.....
*/
protected int getZoneStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [][] zoneStrings;
if ( dfs == null ){
retLength = 4;
}
else {
zoneStrings = dfs. getZoneStrings ();
for ( int i = 0 ; i < zoneStrings.length ; i++ ) {
for ( int j = 1 /* skip item at 0 */;
j < ( zoneStrings [i] ). length ;
j = j + 2 ) {
retLength = java.lang.Math.max ( retLength , ( zoneStrings [i][j] ).length() );
}
}
}
return retLength;
}
protected int getShortZoneStringLength( ){
int retLength = 0;
java.text.DateFormatSymbols dfs = getDateFormatSymbols();
String [][] zoneStrings;
if ( dfs == null ){
retLength = 4;
}
else {
zoneStrings = dfs. getZoneStrings ();
for ( int i = 0 ; i < zoneStrings.length ; i++ ) {
for ( int j = 2 /* skip item at 0 & 1 */;
j < ( zoneStrings [i] ). length ;
j = j + 2 ) {
retLength = java.lang.Math.max ( retLength , ( zoneStrings [i][j] ).length() );
}
}
}
return retLength;
}
//private section
private boolean debug = false ;
private int getMaximumLength( String [] strArr ){
int retLength = 0;
for ( int i = 0 ; i < strArr.length ; i ++ ){
retLength = java.lang.Math.max ( retLength , strArr [ i ].length() );
}
return retLength;
}
}//end class DatePatternToMaskXLator