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 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 25 public abstract String apply( TKTemplateData td ) throws TKTemplateSyntaxException; 26 27 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 45 public void apply( TKTemplateData td, PrintStream ps ) throws TKTemplateSyntaxException, IOException 46 { 47 apply( td, new PrintWriter(ps)); 48 } 49 50 51 57 public String expandVariable (String name, TKTemplateData td, String encoding) 58 throws TKTemplateSyntaxException { 59 60 Object resob = td.getVariable( name ); 62 63 if (resob == null) return null; 65 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 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 if (encoding == null) return resob.toString(); 94 95 TKConverter encoder = TKConverter.getConverter( encoding ); 97 if (encoder == null) throw new Error ("missing converter for encoding "+encoding); 98 99 return new String ( encoder.stringToBytes (resob.toString()),0); 100 } 101 102 109 public void expandVariable (String name, TKTemplateData td, String encoding, Writer writer) 110 throws TKTemplateSyntaxException, IOException 111 { 112 113 Object resob = td.getVariable( name ); 115 116 if (resob == null) return; 118 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 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 if (encoding == null) { 149 writer.write(resob.toString()); 150 return; 151 } 152 153 TKConverter encoder = TKConverter.getConverter( encoding ); 155 if (encoder == null) throw new Error ("missing converter for encoding "+encoding); 156 157 writer.write(new String ( encoder.stringToBytes (resob.toString()),0)); 158 return; 159 } 160 161 173 public static void checkError(TKTemplateSyntax aText, String type, String 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 |