001: package com.teamkonzept.lib.templates;
002:
003: import com.teamkonzept.lib.*;
004:
005: import java.io.*;
006: import java.util.*;
007: import java.text.*;
008: import com.teamkonzept.international.LanguageManager;
009:
010: /**
011: * Tag fuer sprachabhängige Texte
012: * @author $Author: alex $
013: * @version $Revision: 1.20 $
014: */
015: public class TKLanguageTag extends TKLabelTag {
016:
017: /** Identifier des Tags */
018: public static final int TAG_TYPE = TKDateTag.TAG_TYPE + 1;
019:
020: /** bei Subtags wird hier der Parameter String bis zum apply gehalten */
021: String paramsString = null;
022:
023: /** Parameter werden hier gehalten */
024: TKVector params = null;
025:
026: /**
027: * Konstruktor 1
028: * Ein Syntaxbaum fuer ein Template wird erzeugt.
029: */
030: public TKLanguageTag(TKTemplateSyntax parent, String def,
031: boolean hasSubTags) throws TKTemplateSyntaxException {
032: super (parent, def, hasSubTags);
033: if (hasSubTags)
034: paramsString = def;
035: else
036: label = getParams(def, false, null);
037: }
038:
039: /**
040: *
041: */
042: public String apply(TKTemplateData td)
043: throws TKTemplateSyntaxException {
044: return getText(td);
045: }
046:
047: public void apply(TKTemplateData td, Writer writer)
048: throws TKTemplateSyntaxException, IOException {
049:
050: writer.write(getText(td));
051: }
052:
053: protected void makeParam(StringBuffer name, StringBuffer value,
054: boolean hasSubTags, TKTemplateData td)
055: throws TKTemplateSyntaxException {
056: String valString = value == null ? "" : value.toString();
057:
058: if (hasSubTags) {
059:
060: TKTemplateSyntax syntax = parent.newChild(valString, parent
061: .getSource(), true);
062: TKTemplateData subTd = td;
063: String decoding = (String) subTd
064: .getVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING");
065: if (subTd != null) {
066: // muss hier sein, da ansonsten spätere Encoder falsche Ergebnisse liefern
067: subTd.setLocalVariable(
068: "TEMPLATE_SYNTAX_DEFAULT_ENCODING",
069: TKAnsiConverter.CONV_ID);
070: }
071: valString = syntax.apply(subTd);
072: // Decoding wieder zruecksetzen
073: if (decoding != null)
074: subTd.setLocalVariable(
075: "TEMPLATE_SYNTAX_DEFAULT_ENCODING", decoding);
076: else
077: subTd
078: .removeVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING");
079: }
080: params.addElement(valString);
081: }
082:
083: protected String getParams(String def, boolean hasSubTags,
084: TKTemplateData td) throws TKTemplateSyntaxException {
085: params = new TKVector();
086: int pos = def.indexOf(':');
087: if (pos < 0) {
088: if (hasSubTags) {
089: return parent.newChild(def, parent.getSource(),
090: hasSubTags).apply(td);
091: } else {
092: return def;
093: }
094: }
095: String label = def.substring(0, pos++);
096: if (hasSubTags) {
097: label = parent.newChild(label, parent.getSource(),
098: hasSubTags).apply(td);
099: }
100: StringBuffer name = null;
101: StringBuffer value = null;
102: boolean inName = true;
103: while (pos < def.length()) {
104: char chr = def.charAt(pos++);
105: if (chr == ';') {
106: if (name != null) {
107: makeParam(name, value, hasSubTags, td);
108: }
109: inName = true;
110: name = null;
111: value = null;
112: continue;
113: }
114: if (chr == '=') {
115: inName = false;
116: continue;
117: }
118: if ((chr == '\\') && (pos < def.length())) {
119: chr = def.charAt(pos++);
120: }
121: (inName ? (name == null ? (name = new StringBuffer())
122: : name)
123: : (value == null ? (value = new StringBuffer())
124: : value)).append(chr);
125: }
126: if (name != null) {
127: makeParam(name, value, hasSubTags, td);
128: }
129: return label;
130: }
131:
132: /**
133: liefert den localisierten String zurueck
134: Format des labels ist CONTEXT.IDENTIFIER
135: */
136: private String getText(TKTemplateData td)
137: throws TKTemplateSyntaxException {
138: String label = null;
139: if (paramsString != null) {
140: label = getParams(paramsString, true, td);
141: } else {
142: label = getRealLabel(td);
143: }
144: label = label.trim();
145: int index = label.indexOf(".");
146: if (index < 1) {
147: return "wrong_format";
148: }
149: String context = label.substring(0, index);
150: String ident = label.substring(index + 1, label.length());
151: Object[] args = new Object[params.size()];
152: params.copyInto(args);
153: /**for (int j = 0; j < args.length; j++)
154: System.out.println(args[j]); */
155:
156: String back = LanguageManager.getText(context, ident, args);
157: String encoding = parent.getDefaultEncoding(td);
158: TKConverter encoder = TKConverter.getConverter(encoding);
159: if (encoder == null) {
160: throw new Error("missing converter for encoding "
161: + encoding);
162: }
163: return new String(encoder.stringToBytes(back), 0);
164:
165: }
166: }
|