home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-01-12 | 5.3 KB | 153 lines |
- /*
- * @(#DateMaskHelper.java
- *
- * Copyright (c) 1998 Symantec Corporation. All Rights Reserved.
- *
- */
- package com.symantec.itools.swing;
-
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.io.Serializable;
-
- /**
- * DateMaskHelper is a helper class for date masked components.
- * @author Vasudev J. Rao
- * @version 1.0
- * @see com.symantec.itools.swing.JDateMaskedField
- * @see com.symantec.itools.awt.DateMaskedField
- * @see symantec.itools.db.awt.DBDateMaskedField
- */
- public final class DateMaskHelper implements Serializable {
-
- /** Do not let anyone instantiate this class */
- private DateMaskHelper() {
- }
-
- // If the mask is 99/99/9999 , JMaskedTextField expects data to be
- // presented as 12311998 and not 12/31/1998
- // This method accomplishes this. Also if the data is like 1/1/1998
- // it returns 1 1 1998
- // This method may further be improved to return 01011998 in such cases
- // and append spaces only for character mask
-
- public static String getMatchedText ( String s ,
- String mask,
- char numMask,
- char alphaMask,
- char esc ) {
-
- StringBuffer buf = new StringBuffer ();
-
- if ( s != null && s.length() > 0 ) {
-
- //String mask = getMask();
- java.text.StringCharacterIterator iterator = new java.text.StringCharacterIterator ( mask );
- int lastIndex = -1;
- int lastMatchingPos = -1;
-
- while ( iterator.next() != iterator.DONE ) {
-
- StringBuffer subBuf = new StringBuffer();
- char currChar = iterator.current();
- int currIndex = iterator.getIndex();
-
- if ( currChar == numMask ||
- currChar == alphaMask ||
- currChar == esc ) {
-
- }
- else {
- //means we have some other character
- int matchingPos = s.indexOf ( currChar , lastMatchingPos + 1 );
- if (matchingPos == -1) {
- // Can't find character in string: string not valid.
- throw new IllegalArgumentException ("String does not match mask");
- }
- String subField = s.substring ( lastMatchingPos + 1 , matchingPos );
- String subMask = mask.substring ( lastIndex + 1 , currIndex ) ;
-
- if ( !subField.equals(" ") ) {
-
- int padCount = subMask.length() - subField.length() ;
-
- subBuf.append ( subField );
- for ( int i = 0; i < padCount ; i ++ ){
- //pad the input with spaces
- subBuf.append( " " );
- }
- //truncate extra chars if any
- //this would handle cases when padcount < 0
- subBuf.setLength ( subMask.length() );
- }
- //store away the current indexes
- lastMatchingPos = matchingPos;
- lastIndex = currIndex;
- }
- buf.append( subBuf );
-
- }
-
- //last sub field:
- String subField = s.substring ( lastMatchingPos + 1 );
- buf.append ( subField ) ;
-
- }
- if ( debug ) {
- System.out.println ( "buf : " + buf );
- }
- return buf.toString();
-
- }
-
- public static java.util.Date parseDateValue ( String s , int type ) {
-
- java.util.Date retDate = null;
- DateFormat df;
- DateMaskFormatter dmf = new DateMaskFormatter();
-
- df = dmf.getInstance ( type , DateFormat.DEFAULT ) ;
- try {
- retDate = df.parse ( s );
- }
- catch ( Exception e ){
- }
- if ( retDate == null ) {
- df = dmf.getInstance ( type , DateFormat.FULL ) ;
- try {
- retDate = df.parse ( s );
- }
- catch ( Exception e ){
- }
- }
- if ( retDate == null ) {
- df = dmf.getInstance ( type , DateFormat.LONG ) ;
- try {
- retDate = df.parse ( s );
- }
- catch ( Exception e ){
- }
- }
- if ( retDate == null ) {
- df = dmf.getInstance ( type , DateFormat.MEDIUM ) ;
- try {
- retDate = df.parse ( s );
- }
- catch ( Exception e ){
- }
- }
- if ( retDate == null ) {
- df = dmf.getInstance ( type , DateFormat.SHORT ) ;
- try {
- retDate = df.parse ( s );
- }
- catch ( Exception e ){
- }
- }
- return retDate;
-
- }
-
- //private section
- private static boolean debug = false ;
- }