1 package com.teamkonzept.lib.templates; 2 3 import com.teamkonzept.lib.*; 4 5 import java.io.*; 6 import java.util.*; 7 import java.text.*; 8 import com.teamkonzept.international.LanguageManager; 9 10 15 public class TKLanguageTag extends TKLabelTag 16 { 17 18 19 public static final int TAG_TYPE = TKDateTag.TAG_TYPE+1; 20 21 22 String paramsString = null; 23 24 25 TKVector params = null; 26 27 31 public TKLanguageTag( TKTemplateSyntax parent, String def, boolean hasSubTags ) throws TKTemplateSyntaxException 32 { 33 super( parent, def, hasSubTags ); 34 if( hasSubTags ) paramsString = def; 35 else label = getParams (def,false,null); 36 } 37 38 41 public String apply( TKTemplateData td ) throws TKTemplateSyntaxException 42 { 43 return getText(td); 44 } 45 46 public void apply(TKTemplateData td, Writer writer) 47 throws TKTemplateSyntaxException, IOException 48 { 49 50 writer.write(getText(td)); 51 } 52 53 protected void makeParam (StringBuffer name, StringBuffer value, boolean hasSubTags, TKTemplateData td) 54 throws TKTemplateSyntaxException 55 { 56 String valString = value == null ? "" : value.toString(); 57 58 if (hasSubTags) { 59 60 TKTemplateSyntax syntax = parent.newChild (valString,parent.getSource(),true); 61 TKTemplateData subTd = td; 62 String decoding = (String )subTd.getVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING") ; 63 if (subTd != null) 64 { 65 subTd.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",TKAnsiConverter.CONV_ID); 67 } 68 valString = syntax.apply (subTd); 69 if (decoding != null) 71 subTd.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",decoding); 72 else 73 subTd.removeVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING"); 74 } 75 params.addElement( valString); 76 } 77 78 protected String getParams( String def, boolean hasSubTags, 79 TKTemplateData td) throws TKTemplateSyntaxException{ 80 params = new TKVector(); 81 int pos = def.indexOf(':'); 82 if (pos < 0) { 83 if (hasSubTags){ 84 return parent.newChild(def,parent.getSource(),hasSubTags).apply (td); 85 } 86 else{ 87 return def; 88 } 89 } 90 String label = def.substring( 0, pos++ ); 91 if (hasSubTags){ 92 label = parent.newChild(label,parent.getSource(),hasSubTags).apply (td); 93 } 94 StringBuffer name = null; 95 StringBuffer value = null; 96 boolean inName = true; 97 while (pos < def.length()) { 98 char chr = def.charAt (pos++); 99 if (chr == ';') { 100 if (name != null){ 101 makeParam (name, value, hasSubTags, td); 102 } 103 inName = true; 104 name = null; 105 value = null; 106 continue; 107 } 108 if (chr == '=') { 109 inName = false; 110 continue; 111 } 112 if ((chr == '\\') && (pos < def.length())){ 113 chr = def.charAt (pos++); 114 } 115 (inName ? (name == null ? (name = new StringBuffer ()) : name) : 116 (value == null ? (value = new StringBuffer ()) : value)) .append (chr); 117 } 118 if (name != null){ 119 makeParam (name, value, hasSubTags, td); 120 } 121 return label; 122 } 123 124 128 private String getText(TKTemplateData td) throws TKTemplateSyntaxException{ 129 String label = null; 130 if (paramsString != null){ 131 label = getParams(paramsString, true, td); 132 } 133 else{ 134 label = getRealLabel( td ); 135 } 136 label = label.trim(); 137 int index = label.indexOf("."); 138 if (index < 1){ 139 return "wrong_format"; 140 } 141 String context = label.substring(0, index); 142 String ident = label.substring(index + 1, label.length()); 143 Object [] args = new Object [params.size()]; 144 params.copyInto(args); 145 147 148 String back = LanguageManager.getText(context, ident, args); 149 String encoding = parent.getDefaultEncoding(td); 150 TKConverter encoder = TKConverter.getConverter( encoding ); 151 if (encoder == null){ 152 throw new Error ("missing converter for encoding "+encoding); 153 } 154 return new String ( encoder.stringToBytes (back),0); 155 156 } 157 } 158 | Popular Tags |