KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.teamkonzept.lib.templates;
2
3 import java.io.*;
4
5 import com.teamkonzept.lib.*;
6 import org.apache.log4j.Category;
7
8 /**
9  * Die Abstrakte Klasse Methode TKTag stellt Variablen fuer alle Subklassen zur
10  * Verfuegung und die Methode apply wird definiert.
11  * @author $Author: alex $
12  * @version $Revision: 1.17 $
13  */

14 public abstract class TKTag
15 {
16     private static final Category CATEGORY = Category.getInstance(TKTag.class);
17
18     public static final TKVector EMPTY_LIST = new TKVector();
19
20     TKTemplateSyntax parent;
21
22     /**
23      * Signaturen der abstrakten Methoden
24      */

25     public abstract String JavaDoc apply( TKTemplateData td ) throws TKTemplateSyntaxException;
26     
27     /**
28      * Expandiert das Tag mithilfe der uebergebenen TKTemplateData und
29      * schreibt diese in einen character output stream.
30      * @param td die template daten
31      * @param writer der character output stream
32      */

33     public abstract void apply(TKTemplateData td, Writer writer)
34         throws TKTemplateSyntaxException, IOException;
35
36
37     public TKTag( TKTemplateSyntax parent )
38     {
39         this.parent = parent;
40     }
41     
42     /**
43      *
44      */

45     public void apply( TKTemplateData td, PrintStream ps ) throws TKTemplateSyntaxException, IOException
46     {
47          apply( td, new PrintWriter(ps));
48     }
49     
50
51     /**
52         Die Variable name wird mit den Daten in td und dem übergebenen encoding expandiert
53         @param name Name der Varaiablen
54         @param td Daten, in denen der Wert der Variablen steckt
55         @param encoding
56     */

57     public String JavaDoc expandVariable (String JavaDoc name, TKTemplateData td, String JavaDoc encoding)
58         throws TKTemplateSyntaxException {
59
60         // Value holen
61
Object JavaDoc resob = td.getVariable( name );
62
63         if (resob == null) return null; // ist nicht da
64

65         // ist es eine subsyntax ??
66
if (resob instanceof TKTemplateSyntax) {
67
68             TKTemplateSyntax syntax = (TKTemplateSyntax) resob;
69             TKTemplateData subTd = new TKTemplateData (td);
70                 
71             if (encoding != null)
72                 subTd.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",encoding);
73                 
74             return syntax.apply (subTd);
75         }
76
77         // oder sogar ein template ???
78
if (resob instanceof TKTemplate) {
79         
80             TKTemplate template = (TKTemplate) resob;
81
82             TKTemplateData subTd = new TKTemplateData ();
83             subTd.setListIterator(template.td.getListIterator());
84             subTd.merge (template.td);
85                 
86             if (encoding != null)
87                 subTd.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",encoding);
88
89             return template.syntax.apply (subTd);
90         }
91         
92         // kein encoding, Variable wird als String zurückgegeben
93
if (encoding == null) return resob.toString();
94
95         // doch encoded
96
TKConverter encoder = TKConverter.getConverter( encoding );
97         if (encoder == null) throw new Error JavaDoc("missing converter for encoding "+encoding);
98         
99         return new String JavaDoc( encoder.stringToBytes (resob.toString()),0);
100     }
101     
102     /**
103         Die Variable name wird mit den Daten in td und dem übergebenen encoding expandiert
104         @param name Name der Varaiablen
105         @param td Daten, in denen der Wert der Variablen steckt
106         @param encoding
107         @param writer character output stream in den die expandierte Variable geschrieben wird
108     */

109     public void expandVariable (String JavaDoc name, TKTemplateData td, String JavaDoc encoding, Writer writer)
110         throws TKTemplateSyntaxException, IOException
111     {
112
113         // Value holen
114
Object JavaDoc resob = td.getVariable( name );
115
116         if (resob == null) return; // ist nicht da
117

118         // ist es eine subsyntax ??
119
if (resob instanceof TKTemplateSyntax) {
120
121             TKTemplateSyntax syntax = (TKTemplateSyntax) resob;
122             TKTemplateData subTd = new TKTemplateData (td);
123                 
124             if (encoding != null)
125                 subTd.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",encoding);
126                 
127             syntax.apply (subTd, writer);
128             return;
129         }
130
131         // oder sogar ein template ???
132
if (resob instanceof TKTemplate) {
133         
134             TKTemplate template = (TKTemplate) resob;
135
136             TKTemplateData subTd = new TKTemplateData ();
137             subTd.setListIterator(template.td.getListIterator());
138             subTd.merge (template.td);
139                 
140             if (encoding != null)
141                 subTd.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",encoding);
142
143             template.syntax.apply (subTd, writer);
144             return;
145         }
146         
147         // kein encoding, Variable wird als String geschrieben
148
if (encoding == null) {
149             writer.write(resob.toString());
150             return;
151         }
152
153         // doch encoded
154
TKConverter encoder = TKConverter.getConverter( encoding );
155         if (encoder == null) throw new Error JavaDoc("missing converter for encoding "+encoding);
156         
157         writer.write(new String JavaDoc( encoder.stringToBytes (resob.toString()),0));
158         return;
159     }
160
161     /*******************************************************************
162     /**
163      * Die TKTemplateSyntaxException faengt Fehler bei der Erstellung
164      * der Syntaxstruktur eines Templates ab, welche durch die Instanziierung eines
165      * Templateobjekts erzeugt wird. Sobald ein Syntaxobjekt nicht korrekt erzeugt
166      * wurde, soll eine Fehlermeldung ausgegeben werden. Dies geshieht, wenn das
167      * entsprechende EndTag nicht vorhanden ist.
168      *
169      * @param TKTemplateSyntax aText, das Syntaxobjekt des Tags
170      * @param String type, Name des Tags (z.B. CASE);
171      * @param String def, label des Tags (z.B. CASE:XXX => def==XXX)
172      */

173     public static void checkError(TKTemplateSyntax aText, String JavaDoc type, String JavaDoc def) throws TKTemplateSyntaxException
174     {
175
176         if(aText.tags.size() > 0) {
177             if(aText.tags.lastElement() instanceof TKEndTag) {
178                 TKEndTag aEndTag = (TKEndTag)aText.tags.lastElement();
179                 if( !aEndTag.type.equals(type) ) {
180                     CATEGORY.error("TKTag.checkError: can't find End Tag of \""+type+"\", current: \""+aEndTag.type+"\"");
181                     throw new TKTemplateSyntaxException(aText.getSource(),"WRONGEND",type, def);
182                 }
183             }
184             else {
185                 CATEGORY.error("TKTag.checkError: can't find End Tag of \""+type+"\"");
186                 throw new TKTemplateSyntaxException(aText.getSource(),"NOEND",type, def);
187             }
188
189         }
190         else {
191             CATEGORY.error ("TKTag.checkError: can't find End Tag of \""+type+"\"");
192             throw new TKTemplateSyntaxException(aText.getSource(),"NOEND",type, def);
193         }
194
195         return;
196     }
197     
198 }
199
200
201
Popular Tags