home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / DateMaskHelper.java < prev    next >
Text File  |  1999-01-12  |  5KB  |  153 lines

  1. /*
  2.  * @(#DateMaskHelper.java
  3.  *
  4.  * Copyright (c) 1998 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7. package com.symantec.itools.swing;
  8.  
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.io.Serializable;
  12.  
  13. /**
  14.  * DateMaskHelper is a helper class for date masked components. 
  15.  * @author Vasudev J. Rao
  16.  * @version 1.0 
  17.  * @see com.symantec.itools.swing.JDateMaskedField
  18.  * @see com.symantec.itools.awt.DateMaskedField
  19.  * @see symantec.itools.db.awt.DBDateMaskedField
  20.  */
  21. public final class DateMaskHelper implements Serializable {
  22.     
  23.     /** Do not let anyone instantiate this class */
  24.     private DateMaskHelper() {
  25. }
  26.     
  27.     // If the mask is 99/99/9999 , JMaskedTextField expects data to be 
  28.     // presented as 12311998 and not 12/31/1998
  29.     // This method accomplishes this. Also if the data is like 1/1/1998
  30.     // it returns 1 1 1998
  31.     // This method may further be improved to return 01011998 in such cases
  32.     // and append spaces only for character mask
  33.     
  34.     public static String getMatchedText (   String s , 
  35.                                             String mask, 
  36.                                             char numMask, 
  37.                                             char alphaMask, 
  38.                                             char esc ) {
  39.                                                 
  40.         StringBuffer buf = new StringBuffer ();
  41.         
  42.         if ( s != null && s.length() > 0 ) {
  43.             
  44.             //String mask = getMask();
  45.             java.text.StringCharacterIterator iterator = new java.text.StringCharacterIterator ( mask );
  46.             int lastIndex = -1;
  47.             int lastMatchingPos = -1;
  48.             
  49.             while ( iterator.next() != iterator.DONE ) {
  50.                 
  51.                 StringBuffer subBuf = new StringBuffer();
  52.                 char currChar = iterator.current();
  53.                 int currIndex = iterator.getIndex();
  54.                 
  55.                 if (    currChar == numMask ||
  56.                         currChar == alphaMask   ||
  57.                         currChar == esc ) {
  58.                                 
  59.                 }
  60.                 else {
  61.                     //means we have some other character
  62.                     int matchingPos = s.indexOf ( currChar , lastMatchingPos + 1 );
  63.                     if (matchingPos == -1) {
  64.                         // Can't find character in string: string not valid.
  65.                         throw new IllegalArgumentException ("String does not match mask");
  66.                     }
  67.                     String subField = s.substring ( lastMatchingPos + 1 , matchingPos );
  68.                     String subMask  = mask.substring ( lastIndex + 1 , currIndex ) ;
  69.                     
  70.                     if ( !subField.equals(" ") ) {
  71.                         
  72.                         int padCount = subMask.length() - subField.length() ;
  73.                         
  74.                         subBuf.append ( subField );
  75.                         for ( int i = 0; i < padCount ; i ++ ){
  76.                             //pad the input with spaces
  77.                             subBuf.append( " " );
  78.                         }
  79.                         //truncate extra chars if any
  80.                         //this would handle cases when padcount < 0
  81.                         subBuf.setLength ( subMask.length() );
  82.                     }
  83.                     //store away the current indexes
  84.                     lastMatchingPos = matchingPos;
  85.                     lastIndex = currIndex;
  86.                 }
  87.                 buf.append( subBuf );
  88.                 
  89.             }
  90.             
  91.             //last sub field:
  92.             String subField = s.substring ( lastMatchingPos + 1 );
  93.             buf.append ( subField ) ;
  94.             
  95.         }
  96.         if ( debug ) {
  97.             System.out.println ( "buf : " + buf );
  98.         }
  99.         return buf.toString();
  100.         
  101.     }
  102.     
  103.     public static java.util.Date parseDateValue ( String s , int type ) {
  104.         
  105.         java.util.Date retDate = null;
  106.         DateFormat df;
  107.         DateMaskFormatter dmf = new DateMaskFormatter();
  108.         
  109.         df = dmf.getInstance ( type , DateFormat.DEFAULT ) ;
  110.         try {
  111.             retDate = df.parse ( s );
  112.         }
  113.         catch ( Exception e ){
  114.         }
  115.         if ( retDate == null ) {
  116.             df = dmf.getInstance ( type , DateFormat.FULL ) ;
  117.             try {
  118.                 retDate = df.parse ( s );
  119.             }
  120.             catch ( Exception e ){
  121.             }
  122.         }
  123.         if ( retDate == null ) {
  124.             df = dmf.getInstance ( type , DateFormat.LONG ) ;
  125.             try {
  126.                 retDate = df.parse ( s );
  127.             }
  128.             catch ( Exception e ){
  129.             }
  130.         }
  131.         if ( retDate == null ) {
  132.             df = dmf.getInstance ( type , DateFormat.MEDIUM ) ;
  133.             try {
  134.                 retDate = df.parse ( s );
  135.             }
  136.             catch ( Exception e ){
  137.             }
  138.         }
  139.         if ( retDate == null ) {
  140.             df = dmf.getInstance ( type , DateFormat.SHORT ) ;
  141.             try {
  142.                 retDate = df.parse ( s );
  143.             }
  144.             catch ( Exception e ){
  145.             }
  146.         }
  147.         return retDate;
  148.         
  149.     }
  150.  
  151.     //private section
  152.     private static boolean debug = false ;
  153. }