1 package com.teamkonzept.lib.templates; 2 3 import java.io.*; 4 import java.util.*; 5 6 import com.teamkonzept.lib.*; 7 8 14 public class TKTagTagBase extends TKTag { 15 16 public static final String ATTR_LABEL = "label"; 17 public static final String ATTR_SIZE = "size"; 18 public static final String ATTR_NAME = "attrName"; 19 public static final String ATTR_LOCATION= "attrLocation"; 20 21 public static final String PARAMS_STRING = "PARAMS"; 22 23 public String label = null; 24 public String paramsString = null; 25 public TKHashtable params = null; 26 public TKTemplateSyntax argumentText = null; 27 28 public int tagType () { return -1; }; 29 30 private static String identifierFirstCodes = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 31 private static String digitCodes = "0123456789"; 32 private static String identifierCodes = identifierFirstCodes+digitCodes; 33 40 public TKTagTagBase( TKTemplateSyntax parent, String def, boolean hasSubTags) throws TKTemplateSyntaxException 41 { 42 super (parent); 43 44 if( hasSubTags ) paramsString = def; 45 else label = getParams (def,false,null); 46 } 47 48 protected void makeParam (StringBuffer name, StringBuffer value, boolean hasSubTags, TKTemplateData td) 49 throws TKTemplateSyntaxException { 50 51 String valString = value == null ? "" : value.toString(); 52 53 if (hasSubTags) { 54 55 TKTemplateSyntax syntax = parent.newChild (valString,parent.getSource(),true); 56 57 if (td != null) { 58 59 TKTemplateData subTd = new TKTemplateData (td); 60 61 td = subTd; 62 td.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",TKAnsiConverter.CONV_ID); 63 } 64 65 valString = syntax.apply (td); 66 } 67 68 params.put(name.toString().trim(), valString); 69 } 70 71 protected String getParams( String def, boolean hasSubTags, TKTemplateData td) throws TKTemplateSyntaxException 72 { 73 params = new TKHashtable(); 74 75 int pos = def.indexOf(':'); 76 if (pos < 0) { 77 78 if (hasSubTags) return parent.newChild(def,parent.getSource(),hasSubTags).apply (td); 79 else return def; 80 } 81 82 String label = def.substring( 0, pos++ ); 83 if (hasSubTags) label = parent.newChild(label,parent.getSource(),hasSubTags).apply (td); 84 85 StringBuffer name = null; 86 StringBuffer value = null; 87 boolean inName = true; 88 89 while (pos < def.length()) { 90 91 char chr = def.charAt (pos++); 92 93 if (chr == ';') { 94 95 if (name != null) makeParam (name, value, hasSubTags, td); 96 inName = true; 97 name = null; 98 value = null; 99 continue; 100 } 101 102 if (chr == '=') { inName = false; continue; } 103 104 if ((chr == '\\') && (pos < def.length())) chr = def.charAt (pos++); 105 106 (inName ? (name == null ? (name = new StringBuffer ()) : name) : 107 (value == null ? (value = new StringBuffer ()) : value)) .append (chr); 108 } 109 110 if (name != null) makeParam (name, value, hasSubTags, td); 111 112 return label; 113 } 114 115 116 public static boolean quotingRequired (String text) { 117 118 boolean isId = true; 119 boolean isInt = true; 120 boolean isFloat = true; 121 122 if (text == null) return false; 123 124 int pos = 0; 125 while (pos < text.length()) { 126 127 char chr = text.charAt (pos++); 128 129 if ((pos == 0) && (identifierFirstCodes.indexOf(chr) == -1)) isId = false; 130 else if ((pos != 0) && (identifierCodes.indexOf(chr) == -1)) isId = false; 131 132 if (chr == '.') 133 { 134 if (!isInt) isFloat = false; 135 else isInt = false; 136 } 137 else if (digitCodes.indexOf(chr) == -1) 138 { 139 isInt = false; 140 isFloat = false; 141 } 142 143 if (!isId && !isInt && !isFloat) return true; 144 } 145 146 return false; 147 } 148 149 public static String par2quotes (String text) { 150 151 if (text == null) return null; 152 153 StringBuffer buf = new StringBuffer (); 154 buf.append ('"'); 155 156 int pos = 0; 157 while (pos < text.length()) { 158 159 char chr = text.charAt (pos++); 160 161 if ((chr == '\\') || (chr == '"')) buf.append ('\\'); 162 163 buf.append (chr); 164 } 165 166 buf.append ('"'); 167 return buf.toString(); 168 } 169 170 public String wrapValue (String value) { 171 172 if (value == null) return null; 173 else if (quotingRequired (value)) return par2quotes(value); 174 else return value; 175 } 176 177 184 public String apply( TKTemplateData td ) throws TKTemplateSyntaxException 185 { 186 if (paramsString != null) label = getParams(paramsString,true,td); 187 188 String markupName = label; 189 Object value = td.getVariable("TAG_DEFINITION_"+markupName); 190 191 if (value == null) { 192 193 StringBuffer result = new StringBuffer (); 194 result.append('<').append(markupName); 195 196 Enumeration keys = params.keys(); 197 while( keys.hasMoreElements() ) { 198 199 String name = (String ) keys.nextElement(); 200 String val = (String ) params.get(name); 201 202 if ((name == null) || (name.length() == 0)) continue; 203 result.append(' ').append (name); 204 205 if ((val != null) && (val.length() != 0)) 206 result.append('=').append (wrapValue(val)); 207 } 208 209 result.append('>'); 210 211 if (tagType() == TKTagTag.TAG_TYPE) { 212 213 if (argumentText != null) result.append (argumentText.apply(td)); 214 result.append("</").append(markupName).append('>'); 215 } 216 217 return result.toString(); 218 } 219 220 if (value instanceof String ) return (String ) value; 221 222 if (!(value instanceof TKTemplateSyntax)) return null; 223 224 TKTemplateSyntax definitionText = (TKTemplateSyntax) value; 225 226 TKTemplateData subTd = new TKTemplateData (td); 227 228 if (argumentText != null) { 229 TKTemplate template = argumentText.getNewTemplate(); 230 template.td = td; 231 subTd.setLocalVariable("TAG_ARGUMENT",template); 232 } 233 234 Enumeration keys = params.keys(); 235 while( keys.hasMoreElements() ) { 236 237 String name = (String ) keys.nextElement(); 238 String val = (String ) params.get(name); 239 240 subTd.setLocalVariable("TAG_PARAM_"+name,val); 241 } 242 243 return definitionText.apply (subTd); 244 } 245 246 253 public void apply(TKTemplateData td, Writer writer) 254 throws TKTemplateSyntaxException, IOException 255 { 256 if (paramsString != null) label = getParams(paramsString,true,td); 257 258 String markupName = label; 259 Object value = td.getVariable("TAG_DEFINITION_"+markupName); 260 261 if (value == null) { 262 263 StringBuffer result = new StringBuffer (); 264 result.append('<').append(markupName); 265 266 Enumeration keys = params.keys(); 267 while( keys.hasMoreElements() ) { 268 269 String name = (String ) keys.nextElement(); 270 String val = (String ) params.get(name); 271 272 if ((name == null) || (name.length() == 0)) continue; 273 result.append(' ').append (name); 274 275 if ((val != null) && (val.length() != 0)) 276 result.append('=').append (wrapValue(val)); 277 } 278 279 result.append('>'); 280 281 if (tagType() == TKTagTag.TAG_TYPE) { 282 283 if (argumentText != null) result.append (argumentText.apply(td)); 284 result.append("</").append(markupName).append('>'); 285 } 286 287 writer.write(result.toString()); 288 return; 289 290 } 291 292 if (value instanceof String ) { 293 writer.write((String ) value); 294 return; 295 } 296 297 if (!(value instanceof TKTemplateSyntax)) { 298 return; 299 } 300 301 TKTemplateSyntax definitionText = (TKTemplateSyntax) value; 302 303 TKTemplateData subTd = new TKTemplateData (td); 304 305 if (argumentText != null) { 306 TKTemplate template = argumentText.getNewTemplate(); 307 template.td = td; 308 subTd.setLocalVariable("TAG_ARGUMENT",template); 309 } 310 311 Enumeration keys = params.keys(); 312 while( keys.hasMoreElements() ) { 313 314 String name = (String ) keys.nextElement(); 315 String val = (String ) params.get(name); 316 317 subTd.setLocalVariable("TAG_PARAM_"+name,val); 318 } 319 320 definitionText.apply (subTd, writer); 321 return; 322 } 323 324 } 325 326 | Popular Tags |