KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > templates > TKEvalTag


1 package com.teamkonzept.lib.templates;
2
3 import java.io.*;
4
5 import com.teamkonzept.lib.*;
6 import com.teamkonzept.lib.math.*;
7 import org.apache.log4j.Category;
8
9 /**
10  * @author $Author: mischa $
11  * @version $Revision: 1.10 $
12 */

13 public class TKEvalTag extends TKLabelTag
14 {
15     private static final Category CAT = Category.getInstance(TKEvalTag.class);
16     
17     public static final int TAG_TYPE = TKSetTag.TAG_TYPE+1;
18
19     /**
20      * Konstruktor 1
21      * Ein Syntaxbaum fuer ein Template wird erzeugt.
22      */

23     public TKEvalTag( TKTemplateSyntax parent, String JavaDoc name, boolean hasSubTags ) throws TKTemplateSyntaxException
24     {
25         super( parent, name, hasSubTags );
26     }
27     
28     /*******************************************************************
29     /**
30      *
31      */

32     public String JavaDoc apply( TKTemplateData td ) throws TKTemplateSyntaxException
33     {
34         String JavaDoc expression = getLabel( td );
35         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
36         for (int i = 0; i < expression.length(); i++) {
37
38             char chr = expression.charAt(i);
39             if (chr == '\\')
40             {
41                 if ((i+1) >= expression.length()) break;
42                 else chr = expression.charAt(i);
43             }
44             buf.append(chr);
45         }
46         
47         Eval ev = new Eval();
48         String JavaDoc result = "";
49         try {
50             result = Double.toString( ev.eval( buf.toString() ) );
51             if( result.endsWith(".0") ) {
52                 result = result.substring( 0, result.length()-2 );
53             }
54         }
55         catch( Exception JavaDoc e ) {
56             CAT.error( "error in eval-expression \""+expression+"\": ",e);
57         }
58         return result;
59     }
60     
61     public void apply(TKTemplateData td, Writer writer)
62         throws TKTemplateSyntaxException, IOException
63     {
64         String JavaDoc expression = getLabel( td );
65         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
66         for (int i = 0; i < expression.length(); i++)
67         {
68             char chr = expression.charAt(i);
69             if (chr == '\\')
70             {
71                 if ((i+1) >= expression.length()) break;
72                 else chr = expression.charAt(i);
73             }
74             buf.append(chr);
75         }
76         
77         Eval ev = new Eval();
78         String JavaDoc result = "";
79         try {
80             result = Double.toString( ev.eval( buf.toString() ) );
81             if( result.endsWith(".0") ) {
82                 result = result.substring( 0, result.length()-2 );
83             }
84         }
85         catch( Exception JavaDoc e ) {
86             CAT.error( "error in eval-expression \""+expression+"\": ",e);
87         }
88         
89         writer.write(result);
90     }
91
92
93 }
94
95
Popular Tags