KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > stringvalueexpression > stringfunction


1 package com.daffodilwoods.daffodildb.server.sql99.expression.
2     stringvalueexpression;
3
4 import com.daffodilwoods.database.resource.*;
5 import com.daffodilwoods.daffodildb.server.sql99.token.*;
6 import com.daffodilwoods.daffodildb.server.sql99.utils.ByteComparison;
7 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails;
8 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
9 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession;
10 import com.daffodilwoods.daffodildb.server.sql99.common.ParameterInfo;
11 import com.daffodilwoods.daffodildb.utils.field.*;
12 import com.daffodilwoods.daffodildb.server.sql99.common.*;
13 import com.daffodilwoods.daffodildb.utils.*;
14 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.
15
    AbstractRowValueExpression;
16 import in.co.daffodil.db.jdbc.DatabaseProperties;
17 import java.util.*;
18 import java.text.*;
19 import java.sql.Date JavaDoc;
20 import java.sql.*;
21
22 public class stringfunction
23     extends AbstractStringValueExpression
24     implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter,
25     charactervaluefunction, Datatypes {
26
27   public Srightparen_1874859514 _Srightparen_18748595140;
28   public stringfunctionparameters _stringfunctionparameters1;
29   public Sleftparen653880241 _Sleftparen6538802412;
30   public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223;
31   String JavaDoc romanZeroToNine[] = {
32       "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
33   static HashMap mapOfFormats;
34   static HashMap mapForSpecialFormats;
35
36   static {
37     mapOfFormats = new HashMap();
38     mapForSpecialFormats = new HashMap();
39     initializeMapValues();
40     initializeMapForSpecialFormats();
41   }
42
43   /*
44    The to_char function converts a number or date to a string.
45
46      The syntax for the to_char function is:
47      to_char (value, [format_mask], [nls_language] )
48      value can either be a number or date that will be converted to a string.
49      format_mask is optional. This is the format that will be used to convert value to a string.
50      nls_language is optional. This is the nls language used to convert value to a string.
51
52      Examples - Numbers
53
54      The following are number examples for the to_char function.
55
56     to_char (1210.73, '9999.9') would return '1210.7'
57     to_char (1210.73, '9,999.99') would return '1,210.73'
58     to_char (1210.73, '$9,999.00') would return '$1,210.73'
59     to_char (21, '000099') would return '000021'
60
61     The following are date examples for the to_char function.
62
63    to_char (sysdate, 'yyyy/mm/dd'); would return '2003/07/09'
64    to_char (sysdate, 'Month DD, YYYY'); would return 'July 09, 2003'
65    to_char (sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003'
66    to_char (sysdate, 'MON DDth, YYYY'); would return 'JUL 09TH, 2003'
67    to_char (sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003'
68    to_char (sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'
69
70    You will notice that in some examples, the format_mask parameter begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the examples below.
71
72    to_char (sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003'
73    to_char (sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003'
74    to_char (sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'
75
76    The zeros have been suppressed so that the day component shows as "9" as opposed to "09".
77    */

78
79   public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.
80
      DException {
81     FieldBase[] field = GeneralPurposeStaticClass.changeIntoFieldBase( (
82         Object JavaDoc[]) _stringfunctionparameters1.run(object));
83     FieldBase expression = field[0];
84     FieldBase format0 = field[1];
85     if (expression.isNull()) {
86       return new FieldLiteral(FieldUtility.NULLBUFFERRANGE,
87                               Datatypes.BIGDECIMAL);
88     }
89     String JavaDoc format = "";
90     if (format0 != null) {
91       if (!format0.isNull()) {
92         format = (String JavaDoc) (format0.getObject());
93       }
94     }
95     int expressiontype = getExpressionDataType(object);
96
97     return getResult(expression.getObject(), expressiontype, format);
98
99   }
100
101   private int getExpressionDataType(Object JavaDoc object) throws DException {
102     return getDataTypeForByte(_stringfunctionparameters1.getByteComparison(
103         object));
104   }
105
106   private Object JavaDoc getResult(Object JavaDoc object, int type, String JavaDoc format) throws
107       DException {
108
109     switch (type) {
110       case BYTE:
111       case TINYINT:
112       case INTEGER:
113       case INT:
114       case SHORT:
115       case SMALLINT:
116       case REAL:
117       case DOUBLE:
118       case FLOAT:
119       case LONG:
120       case BIGINT:
121       case DOUBLEPRECISION:
122       case BIGDECIMAL:
123       case DEC:
124       case DECIMAL:
125       case NUMERIC:
126         Number JavaDoc expresion = (Number JavaDoc) object;
127         return new FieldStringLiteral(numberFromat(expresion.toString(), format),
128                                       CHARACTER);
129       case CHARACTER:
130       case VARCHAR:
131       case CHAR:
132       case CHARACTERVARYING:
133         return new FieldStringLiteral( (String JavaDoc) object, CHARACTER);
134       case DATE:
135         Date JavaDoc date = (Date JavaDoc) object;
136         Date JavaDoc d1 = new com.daffodilwoods.daffodildb.utils.DBDate(date.getTime());
137         return new FieldStringLiteral(getDateAndTimeResult(d1, format), CHARACTER);
138       case TIMESTAMP:
139         Timestamp timestamp = (Timestamp) object;
140         return new FieldStringLiteral(getDateAndTimeResult(timestamp, format),
141                                 CHARACTER);
142       case TIME:
143       default:
144         throw new DException("DSE4108",
145                              new Object JavaDoc[] {StaticClass.getDataTypeName(type),
146                              "STRINGFUNCTION"});
147     }
148
149   }
150
151   /*
152    Oracle Number formats
153    A number format may be used as part a TO_CHAR or TO_DATE function to return a number in the specified format.
154
155    Format Description Example
156    9 Number
157                        (suppress leading/trailing zeros) 9999=' 123'
158                        Includes a leading - for negative no's
159                        or one leading space for pos no's
160
161    0 Number
162                        including leading/trailing zeros 09999=0123
163    9999D00=123.00
164
165    FM Supress all leading /trailing blanks FM9999=123
166    $ Include a leading $ $999=$123
167    B Over-ride the '0' format and replace
168    leading 0's with blank spaces ' ' B9999=' 123'
169    S Include poth positive+ and S999 =+123
170    negative - signs 999S =123+
171
172    PR Indicate SIGN with <brackets> PR999=<123>
173    PR999=' 123 '
174    MI Indicate Minus SIGN 999MI=123-
175    RN or rn Return upper or lower case
176                       roman numberal RN99=XI
177    D Position of decimal point(.) 99D99=12.34
178    G Group separator (often a comma)
179    in desired position 9G999=1,234
180    , Return a comma in desired position 9,999=1,234
181    . Return a period in desired position 99.99=12.34
182    C ISO currency symbol C99
183    L Local currency symbol L99
184    */

185
186   private String JavaDoc numberFromat(String JavaDoc number, String JavaDoc format) throws DException {
187     try {
188       if (format.equals("")) {
189         return number;
190       }
191       if (format.startsWith("FM")) {
192         return number.trim();
193       }
194       if (format.startsWith("$")) {
195         return "$" + number;
196       }
197       if (format.startsWith("B")) {
198         return removeLeadingZero(number.trim());
199       }
200       if (format.indexOf("S") != -1) {
201         return addPlusWithNumber(number, format);
202       }
203       if (format.startsWith("PR")) {
204         return "<" + number + ">";
205       }
206       if (format.toLowerCase().indexOf("mi") != -1) {
207         return addMinusWithNumber(number, format);
208       }
209       if (format.toLowerCase().startsWith("rn")) {
210         return getRomans(number);
211       }
212       if (format.toLowerCase().indexOf("d") != -1) {
213         return positionOfDecimalPoint(number, format);
214       }
215       if (format.indexOf(",") != -1) {
216         number = positionOfComma(number, format);
217         if (format.indexOf(".") != -1) {
218           return WithAndWithoutPeriod(number, format);
219         }
220       }
221       if (format.toLowerCase().indexOf("g") != -1) {
222         return positionOfComma_G(number, format);
223       }
224       if (format.startsWith("C")) {
225         return "C" + number;
226       }
227       if (format.startsWith("L")) {
228         return "L" + number;
229       }
230       if (format.startsWith("0")) {
231         return includingLeadingZero(number, format);
232       }
233       return WithAndWithoutPeriod(number, format);
234     }
235     catch (Exception JavaDoc ex) {
236       throw new DException("DSE568",new Object JavaDoc[] {format});
237     }
238   }
239
240
241   private String JavaDoc addPlusWithNumber(String JavaDoc expression,String JavaDoc format) {
242     if (format.startsWith("S")) {
243       if (!expression.startsWith("-"))
244         return "+" + expression;
245     }
246    return expression+"+";
247   }
248
249   private String JavaDoc addMinusWithNumber(String JavaDoc expression,String JavaDoc format) {
250     if (format.startsWith("MI")) {
251       if (!expression.startsWith("-"))
252         return "-" + expression;
253     }
254    return expression+"-";
255   }
256
257   private String JavaDoc includingLeadingZero(String JavaDoc expression, String JavaDoc format) {
258     char[] formatArray = format.toCharArray();
259     int i = 0;
260     boolean flag = true, firstTime = true;
261     StringBuffer JavaDoc temp = new StringBuffer JavaDoc();
262     for (; i < formatArray.length; i++) {
263       if (formatArray[i] == '0' && flag)
264         temp.append('0');
265       else
266         flag = false;
267       if (!flag && firstTime) {
268         firstTime = false;
269         temp.append(expression);
270       }
271       if (formatArray[i] == 'D') {
272         flag = true;
273         temp.append('.');
274       }
275     }
276     return temp.toString();
277
278   }
279
280   private String JavaDoc positionOfDecimalPoint(String JavaDoc expression, String JavaDoc format) {
281     StringBuffer JavaDoc sb =new StringBuffer JavaDoc();
282    int indexInExpression = expression.indexOf(".");
283    int indexInFormat = format.toLowerCase().indexOf("d");
284    if(indexInExpression == -1){
285      if(expression.length() <= format.length() )
286       return " "+expression;
287      for (int i = 0; i < expression.length(); i++)
288        sb.append("#");
289      return sb.toString() ;
290    }
291     for (int i = 0; i < expression.length(); i++) {
292     char charToAppend = expression.charAt(i);
293     if(charToAppend!='.') {
294       if (i == indexInFormat)
295         sb.append(".");
296       sb.append(charToAppend);
297     }
298
299     }
300     return sb.toString();
301   }
302
303   private String JavaDoc positionOfComma(String JavaDoc expression, String JavaDoc format) {
304     int indexOfG = format.indexOf(",");
305     StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
306      for (int i = 0; i < expression.length(); i++) {
307      char charToAppend = expression.charAt(i);
308      if(charToAppend!='.') {
309        if (i == indexOfG)
310          sb.append(",");
311        sb.append(charToAppend);
312      }
313      }
314      return sb.toString();
315   }
316
317   private String JavaDoc BKOfLastCode_positionOfComma_G(String JavaDoc expression, String JavaDoc format) {
318       int indexOfG = format.toLowerCase().indexOf("G");
319       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
320        for (int i = 0; i < expression.length(); i++) {
321        char charToAppend = expression.charAt(i);
322        if(charToAppend!='.') {
323          if (i == indexOfG)
324            sb.append(",");
325          sb.append(charToAppend);
326        }
327        }
328        return sb.toString();
329     }
330
331     private String JavaDoc positionOfComma_G(String JavaDoc expression, String JavaDoc format) {
332       int indexOfG = format.toLowerCase().indexOf("G");
333       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
334       if(expression.length()==format.length()){
335         for (int i = 0; i < expression.length(); i++) {
336          char charToAppend = expression.charAt(i);
337          char charInForamt =format.charAt(i);
338          if(charInForamt ==','||i==indexOfG)
339           sb.append(",");
340          sb.append(charToAppend);
341         }
342        return sb.toString();
343       }
344       for (int i = 0; i < expression.length(); i++) {
345         sb.append("#");
346       }
347       return sb.toString();
348     }
349
350   private String JavaDoc WithAndWithoutPeriod(String JavaDoc expression, String JavaDoc format) throws DException {
351     StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
352     int indexInExpression = expression.indexOf(".");
353     int indexInFormat = format.indexOf(".");
354     if(indexInFormat == -1 && indexInExpression == -1){
355       if(expression.length() <= format.length() )
356        return " "+expression;
357       for (int i = 0; i < expression.length(); i++)
358         sb.append("#");
359       return sb.toString() ;
360     }
361     if(indexInFormat == -1 && indexInExpression != -1) {
362       return expression.substring(0,indexInExpression);
363     }
364     boolean flag = indexInExpression > indexInFormat ;
365     if(flag)
366       for (int i = 0; i < indexInExpression; i++)
367         sb.append("#") ;
368     else
369       sb.append( indexInExpression == -1 ? expression : expression.substring(0, indexInExpression));
370     for (int i = 0,j=sb.length() ; i < format.substring (indexInFormat,format.length() ).length() ; i++,j++) {
371       sb.append(flag ? "#" : ((j >= expression.length()) ? "0" : ""+expression.charAt(j)) ) ;
372     }
373     return sb.toString();
374   }
375
376   /*
377      I V X L C D M
378      1 5 10 50 100 500 1000
379      Because the Romans didn?t have symbols for the numbers
380      2, 3, 4, 6, 7, 8, or 9,
381      they developed a set of rules
382      using the placement of these seven
383      letter-numerals to add and subtract values from
384      each other in order to represent a number.
385    */

386   private String JavaDoc getRomans(String JavaDoc expression) {
387     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
388     long num = (new Double JavaDoc(Math.ceil(Double.parseDouble(expression))).longValue());
389     ArrayList list = new ArrayList();
390     int count = 0, val = 0;
391     if (num > 0) {
392       do {
393         val = (int) num % 10;
394         num = num / 10;
395         val = (val * (int) Math.pow(10, count));
396         list.add(new Integer JavaDoc(val));
397         count++;
398       }
399       while (num > 0);
400     }
401     else {
402       return "#";
403     }
404     Integer JavaDoc[] intArr = new Integer JavaDoc[list.size()];
405     list.toArray(intArr);
406     for (int i = intArr.length - 1; i >= 0; i--) {
407       val = intArr[i].intValue();
408       romanEquivalentOfANumber(val, sb);
409     }
410     return sb.toString();
411   }
412
413   private void romanEquivalentOfANumber(int number, StringBuffer JavaDoc sb) {
414     if (number <= 9)
415       sb.append(romanZeroToNine[number]);
416     else {
417       switch (number) {
418         case 10:
419           sb.append("X");break;
420         case 20:
421           sb.append("XX");break;
422         case 30:
423           sb.append("XXX");break;
424         case 40:
425           sb.append("XL"); break;
426         case 50:
427           sb.append("L"); break;
428         case 60:
429           sb.append("LX"); break;
430         case 70:
431           sb.append("LXX"); break;
432         case 80:
433           sb.append("LXXX"); break;
434         case 90:
435           sb.append("XC"); break;
436         case 100:
437           sb.append("C"); break;
438         case 200:
439           sb.append("CC"); break;
440         case 300:
441           sb.append("CCC"); break;
442         case 400:
443           sb.append("CD"); break;
444         case 500:
445           sb.append("D"); break;
446         case 600:
447           sb.append("DC"); break;
448         case 700:
449           sb.append("DCC"); break;
450         case 800:
451           sb.append("DCCC"); break;
452         case 900:
453           sb.append("CM"); break;
454         case 1000:
455           sb.append("M"); break;
456         case 2000:
457           sb.append("MM"); break;
458         case 3000:
459           sb.append("MMM"); break;
460         default:
461           sb.append("#"); break;
462       }
463     }
464   }
465
466   public String JavaDoc removeLeadingZero(String JavaDoc value) throws DException {
467     do {
468       value = value.replaceFirst("0", "");
469     }
470     while (value.startsWith("0"));
471     return value.trim();
472   }
473
474   /*
475       When a date format is used by TO_CHAR or TO_DATE they return part of the date/time. When used by TRUNC they will return the first day of the period. When used by ROUND the values will round up at mid year/mid month (July 1 or 16th day)
476       CC Century ,
477       SCC Century BC prefixed with -,
478       YYYY Year 2001,SYYY Year BC prefixed with -
479       IYYY ISO Year 2001
480       YY Year 01
481       RR Year 01 rollover for Y2K compatibility *
482       YEAR Year spelled out
483       SYEAR Year spelled out BC prefixed with -
484       BC BC/AD Indicator *
485       Q Quarter : Jan-Mar=1, Apr-Jun=2
486       MM Month of year 01, 02...12
487       RM Roman Month I, II...XII *
488       MONTH In full [January ]...[December ]
489       FMMONTH In full [January]...[December]
490
491    Important Note : -
492    -----------------
493
494                     You will notice that in some
495                     examples,
496                     the format_mask parameter
497                     begins with "FM".This means
498                     that zeros and blanks are
499                     suppressed.This can be seen
500                     in the examples below.
501
502       MON JAN, FEB
503       WW Week of year 1-52
504       W Week of month 1-5
505       IW ISO std week of year
506       DDD Day of year 1-366 *
507       DD Day of month 1-31
508       D Day of week 1-7
509       DAY In full [Monday ]...[Sunday ]
510       FMDAY In full [Monday]...[Sunday]
511       DY MON...SUN
512       DDTH Ordinal Day 7TH
513       DDSPTH Spell out ordinal SEVENTH
514       J Julian Day (days since 31/12/4713)
515       HH Hours of day (1-12)
516       HH12 Hours of day (1-12)
517       HH24 Hours of day (1-24)
518       SPHH Spell out SEVEN
519       AM am or pm *
520       PM am or pm *
521       A.M. a.m. or p.m. *
522       P.M. a.m. or p.m. *
523       MI Minutes 0-59
524       SS Seconds 0-59 *
525       SSSS Seconds past midnight (0-86399) *
526
527       The following punctuation -/,.;: can be included in any date format
528       any other chars can be included "in quotes"
529
530    * Formats marked with * can only be used with TO_CHAR or TO_DATE not TRUNC() or ROUND()
531
532       Date formats that are spelled out in characters will adopt the capitalisation of the format
533       e.g.
534       'MONTH' =JANUARY
535       'Month' = January
536    */

537   public String JavaDoc getDateAndTimeResult(Object JavaDoc dateOrTimestamp, String JavaDoc format) throws
538       DException {
539     if(format.equals("")){
540       return dateOrTimestamp.toString();
541     }
542     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
543     DaffodilStringTokenizer st = new DaffodilStringTokenizer(format);
544     while (st.hasMoreTokens()) {
545       String JavaDoc reqformat = (String JavaDoc) mapOfFormats.get(st.token);
546       if (reqformat != null) {
547         result.append(getDateTimeValue(dateOrTimestamp, reqformat));
548       }
549       else {
550         String JavaDoc[] entry = (String JavaDoc[]) mapForSpecialFormats.get(st.token);
551         if (entry == null)
552           throw new DException("DSE568",
553                                new Object JavaDoc[] {format});
554         if (entry.length > 2) {
555           result.append(getDateTimeValue(dateOrTimestamp, entry[0]));
556           result.append(".");
557           result.append(getDateTimeValue(dateOrTimestamp, entry[2]));
558         }
559         else {
560           result.append(specialHandling(dateOrTimestamp, entry));
561         }
562       }
563       result.append(st.getDelemeter());
564     }
565     return result.toString();
566   }
567
568   static void initializeMapForSpecialFormats() {
569     mapForSpecialFormats.put("SS.ssss", new String JavaDoc[] {"s", "dot", "SSS"});
570     mapForSpecialFormats.put("SS.SSSS", new String JavaDoc[] {"s", "dot", "SSS"});
571     mapForSpecialFormats.put("ss.ssss", new String JavaDoc[] {"s", "dot", "SSS"});
572     mapForSpecialFormats.put("ss.SSSS", new String JavaDoc[] {"s", "dot", "SSS"});
573     mapForSpecialFormats.put("FMmon", new String JavaDoc[] {"MMM", "lower"});
574     mapForSpecialFormats.put("mon", new String JavaDoc[] {"MMM", "lower"});
575     mapForSpecialFormats.put("month", new String JavaDoc[] {"MMMM", "lower"});
576     mapForSpecialFormats.put("FMmonth", new String JavaDoc[] {"MMMM", "lower"});
577     mapForSpecialFormats.put("day", new String JavaDoc[] {"E", "lower"});
578     mapForSpecialFormats.put("FMday", new String JavaDoc[] {"E", "lower"});
579     mapForSpecialFormats.put("FMMon", new String JavaDoc[] {"MMMM", "InitCap"});
580     mapForSpecialFormats.put("Mon", new String JavaDoc[] {"MMMM", "InitCap"});
581     mapForSpecialFormats.put("Month", new String JavaDoc[] {"MMMM", "InitCap"});
582     mapForSpecialFormats.put("FMMonth", new String JavaDoc[] {"MMMM", "InitCap"});
583     mapForSpecialFormats.put("Day", new String JavaDoc[] {"E", "InitCap"});
584     mapForSpecialFormats.put("FMDay", new String JavaDoc[] {"E", "InitCap"});
585     mapForSpecialFormats.put("DDSPTH", new String JavaDoc[] {"d", "TH"});
586     mapForSpecialFormats.put("DDDth", new String JavaDoc[] {"d", "th"});
587     mapForSpecialFormats.put("DDth", new String JavaDoc[] {"d", "th"});
588     mapForSpecialFormats.put("DDTH", new String JavaDoc[] {"d", "TH"});
589     mapForSpecialFormats.put("ddth", new String JavaDoc[] {"d", "th"});
590    mapForSpecialFormats.put("ddTH", new String JavaDoc[] {"d", "TH"});
591     mapForSpecialFormats.put("q", new String JavaDoc[] {"", "q"});
592     mapForSpecialFormats.put("Q", new String JavaDoc[] {"", "q"});
593     mapForSpecialFormats.put("cc", new String JavaDoc[] {"", "cc"});
594     mapForSpecialFormats.put("CC", new String JavaDoc[] {"", "cc"});
595     mapForSpecialFormats.put("scc", new String JavaDoc[] {"", "scc"});
596     mapForSpecialFormats.put("SCC", new String JavaDoc[] {"", "scc"});
597
598   }
599
600   static void initializeMapValues() {
601     mapOfFormats.put("BC", "G");
602     mapOfFormats.put("AC", "G");
603     mapOfFormats.put("RR", "yy");
604     mapOfFormats.put("YYYY", "yyyy");
605     mapOfFormats.put("yyyy", "yyyy");
606     mapOfFormats.put("IYYY", "yyyy");
607     mapOfFormats.put("YY", "yy");
608     mapOfFormats.put("yy", "yy");
609     mapOfFormats.put("YEAR", "yyyy");
610     mapOfFormats.put("year", "yyyy");
611     mapOfFormats.put("SYEAR", "yyyy");
612     mapOfFormats.put("syear", "yyyy");
613     mapOfFormats.put("MM", "M");
614     mapOfFormats.put("mm", "M");
615     mapOfFormats.put("Mm", "M");
616     mapOfFormats.put("mM", "M");
617     mapOfFormats.put("FMon", "MMM");
618     mapOfFormats.put("FMMon", "MMM");
619     mapOfFormats.put("FMMON", "MMM");
620     mapOfFormats.put("Mon", "MMM");
621     mapOfFormats.put("MON", "MMM");
622     mapOfFormats.put("FMonth", "MMMM");
623     mapOfFormats.put("FMMonth", "MMMM");
624     mapOfFormats.put("Month", "MMMM");
625     mapOfFormats.put("MONTH", "MMMM");
626
627     mapOfFormats.put("WW", "w");
628     mapOfFormats.put("IW", "w");
629     mapOfFormats.put("D", "dd");
630     mapOfFormats.put("DDD", "D");
631     mapOfFormats.put("DD", "d");
632     mapOfFormats.put("dd", "d");
633     mapOfFormats.put("j", "d");
634     mapOfFormats.put("DAY", "E");
635     mapOfFormats.put("Day", "E");
636     mapOfFormats.put("day", "E");
637     mapOfFormats.put("DY", "EEE");
638     mapOfFormats.put("FMDAY", "E");
639     mapOfFormats.put("HH", "h");
640     mapOfFormats.put("hh", "h");
641     mapOfFormats.put("HH12", "h");
642     mapOfFormats.put("hh12", "h");
643     mapOfFormats.put("HH24", "K");
644     mapOfFormats.put("hh24", "K");
645     mapOfFormats.put("MI", "m");
646     mapOfFormats.put("mi", "m");
647     mapOfFormats.put("SS", "s");
648     mapOfFormats.put("ss", "s");
649     mapOfFormats.put("SSSS", "S");
650     mapOfFormats.put(".ssss", "S");
651     mapOfFormats.put("FF", "SSS");
652     mapOfFormats.put("ff", "SSS");
653     mapOfFormats.put("AM", "a");
654     mapOfFormats.put("am", "a");
655     mapOfFormats.put("PM", "a");
656     mapOfFormats.put("pm", "a");
657     mapOfFormats.put("A.M.", "a");
658     mapOfFormats.put("a.m.", "a");
659     mapOfFormats.put("P.M.", "a");
660     mapOfFormats.put("p.m.", "a");
661
662   }
663
664   String JavaDoc specialHandling(Object JavaDoc dateOrTimestamp, String JavaDoc[] entry0) throws
665       DException {
666     String JavaDoc toReturn = getDateTimeValue(dateOrTimestamp, entry0[0]);
667     if (entry0[1].equalsIgnoreCase("lower")) {
668       return toReturn.toLowerCase();
669     }
670     if (entry0[1].equalsIgnoreCase("initcap")) {
671       return toReturn.replaceFirst("" + toReturn.charAt(0),
672                                    ("" + toReturn.charAt(0)).toUpperCase());
673     }
674     if (entry0[1].equalsIgnoreCase("th")) {
675       return toReturn += entry0[1];
676     }
677     if (entry0[1].equalsIgnoreCase("q")) {
678       return toReturn += getQuarter(dateOrTimestamp);
679     }
680     if (entry0[1].equalsIgnoreCase("scc") || entry0[1].equalsIgnoreCase("cc")) {
681       return toReturn += getCentuary(dateOrTimestamp);
682     }
683     return toReturn;
684   }
685
686   /**
687    * Return the date and time value according to
688    * format passed.
689    */

690   java.lang.String JavaDoc getDateTimeValue(Object JavaDoc dateOrTimestamp,
691                                     java.lang.String JavaDoc format) throws DException {
692     try {
693       SimpleDateFormat formatter = new SimpleDateFormat(format);
694       return formatter.format(dateOrTimestamp);
695     }
696     catch (IllegalArgumentException JavaDoc ex) {
697       throw new DException("DSE568", new Object JavaDoc[] {format});
698     }
699   }
700
701   private String JavaDoc getQuarter(Object JavaDoc DateOftimestamp) throws DException{
702     int mm = 0;
703     if (DateOftimestamp instanceof Date JavaDoc) {
704       mm = ( (Date JavaDoc) DateOftimestamp).getMonth() + 1;
705     }
706     else {
707       mm = ( (Timestamp) DateOftimestamp).getMonth() + 1;
708     }
709     if (mm <= 3) {
710      mm = 1;
711    }
712    else if (mm <= 6) {
713      mm = 2;
714    }
715    else if (mm <= 9) {
716      mm = 3;
717    }
718    else if (mm <= 12) {
719      mm = 4;
720    }
721     return "" + mm;
722   }
723
724   private String JavaDoc getCentuary(Object JavaDoc dateOrTimestamp) throws DException {
725     int year = -1;
726     if (dateOrTimestamp instanceof Date JavaDoc) {
727       year = ( (Date JavaDoc) dateOrTimestamp).getYear() + 1900;
728     }
729     else {
730       year = ( (Timestamp) dateOrTimestamp).getYear() + 1900;
731     }
732     year = year % 100 == 0 ? year / 100 : (year/100 +1 ) ;
733     return "" + year;
734   }
735
736   public ByteComparison getByteComparison(Object JavaDoc object) throws DException {
737     ByteComparison byteComparison = new ByteComparison(false,new int[] {CHARACTER});
738     byteComparison.setSize(getColumnSize(object));
739     return byteComparison;
740   }
741
742   public ColumnDetails[] getChildColumnDetails() {
743     return columnDetails;
744   }
745
746   public _Reference[] checkSemantic(_ServerSession parent) throws DException {
747     _Reference[] ref = super.checkSemantic(parent);
748     if (ref != null) {
749       return ref;
750     }
751     int type = _stringfunctionparameters1.getByteComparison(parent).
752         getDataTypes()[0];
753     switch (type) {
754       case -1:
755       case BYTE:
756       case TINYINT:
757       case INTEGER:
758       case INT:
759       case SHORT:
760       case SMALLINT:
761       case REAL:
762       case DOUBLE:
763       case FLOAT:
764       case DOUBLEPRECISION:
765       case LONG:
766       case BIGINT:
767       case BIGDECIMAL:
768       case DEC:
769       case DECIMAL:
770       case NUMERIC:
771         return ref;
772       default:
773         throw new DException("DSE4108",
774                              new Object JavaDoc[] {StaticClass.getDataTypeName(type),
775                              "STRINGFUNCTION"});
776     }
777   }
778
779   public ParameterInfo[] getParameterInfo() {
780     return new ParameterInfo[0];
781   }
782
783   public AbstractRowValueExpression[] getChilds() {
784     AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {
785         (AbstractRowValueExpression) _stringfunctionparameters1};
786     return childs;
787
788   }
789
790   public int getColumnSize(Object JavaDoc object) throws DException {
791     ColumnDetails[] columnDetails = getChildColumnDetails();
792     if (columnDetails[0].getQuestion()) {
793       return DatabaseProperties.maxCharLiteralLength;
794     }
795      if (columnDetails[0].getType() != TypeConstants.CONSTANT ||
796              columnDetails[0].getSize() != -5) {
797       return columnDetails[0].getSize();
798     }
799       FieldBase field = (FieldBase) _stringfunctionparameters1.run(object);
800       field.setDatatype(columnDetails[0].getDatatype());
801       return field.getLength();
802
803   }
804
805   public String JavaDoc getType() throws DException {
806     return (String JavaDoc) _SRESERVEDWORD12065439223.run(null);
807   }
808
809   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
810     stringfunction tempClass = new stringfunction();
811     tempClass._Srightparen_18748595140 = (Srightparen_1874859514)
812         _Srightparen_18748595140.clone();
813     tempClass._stringfunctionparameters1 = (stringfunctionparameters)
814         _stringfunctionparameters1.clone();
815     tempClass._Sleftparen6538802412 = (Sleftparen653880241)
816         _Sleftparen6538802412.clone();
817     tempClass._SRESERVEDWORD12065439223 = (SRESERVEDWORD1206543922)
818         _SRESERVEDWORD12065439223.clone();
819     return tempClass;
820
821   }
822
823   public String JavaDoc toString() {
824     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
825     sb.append(" ");
826     sb.append(_SRESERVEDWORD12065439223);
827     sb.append(" ");
828     sb.append(_Sleftparen6538802412);
829     sb.append(" ");
830     sb.append(_stringfunctionparameters1);
831     sb.append(" ");
832     sb.append(_Srightparen_18748595140);
833     return sb.toString();
834   }
835 }
836
837 class DaffodilStringTokenizer {
838   int position;
839   boolean tokenCompleted;
840   String JavaDoc token, delimeter;
841   char[] charArray;
842
843   DaffodilStringTokenizer(String JavaDoc toBeTokenized) {
844     charArray = toBeTokenized.toCharArray();
845     position = -1;
846   }
847
848   boolean hasMoreTokens() {
849     token = delimeter = "";
850     tokenCompleted = false;
851     while (++position < charArray.length) {
852       if (charArray[position] == ',' || charArray[position] == '/' ||
853           charArray[position] == ' ' || charArray[position] == '-') {
854         delimeter += charArray[position];
855         tokenCompleted = true;
856       }
857       else {
858         if (tokenCompleted) {
859           position--;
860           return true;
861         }
862         token += charArray[position];
863       }
864     }
865     return token.length() >= 1;
866   }
867
868   String JavaDoc getToken() {
869     return token;
870   }
871
872   String JavaDoc getDelemeter() {
873     return delimeter;
874   }
875
876 }
877
Popular Tags