001: package com.teamkonzept.lib.templates;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: import com.teamkonzept.lib.*;
007:
008: /**
009: * SwitchTag:
010: * <TK_SWITCH:name:[alt=]tmpl;...>
011: * @author $Author: alex $
012: * @version $Revision: 1.12 $
013: */
014: public class TKSwitchTag extends TKTag {
015: public static final int TAG_TYPE = TKNotTag.TAG_TYPE + 1;;
016: public static final String ATTR_LABEL = "label";
017: public static final String ATTR_SIZE = "size";
018: public static final String ATTR_NAME = "attrName";
019: public static final String ATTR_LOCATION = "attrLocation";
020:
021: public static final String SYNTAX_ALTERNATIVES = "alternatives";
022:
023: public String label = null;
024: public TKHashtable alternatives = null;
025: public TKTemplateSyntax alternativesSyntax = null;
026:
027: /**
028: * Konstruktor 1
029: * Ein Syntaxbaum fuer ein Template wird erzeugt.
030: *
031: * @param String def, Name des Tags
032: * @param boolean hasSubTags
033: */
034: public TKSwitchTag(TKTemplateSyntax parent, String def,
035: boolean hasSubTags) throws TKTemplateSyntaxException {
036: super (parent);
037: if (hasSubTags) {
038: alternativesSyntax = parent.newChild(def, parent
039: .getSource(), hasSubTags);
040: } else {
041: alternatives = new TKHashtable();
042: label = getAlternatives(def, alternatives);
043: }
044: }
045:
046: /**
047: holt sich die CASE Bloecke
048: */
049: protected static String getAlternatives(String def,
050: TKHashtable alternatives) {
051: int pos = def.indexOf(':');
052: String label = def.substring(0, pos);
053: Enumeration alts = new StringTokenizer(def.substring(pos + 1),
054: ";");
055: while (alts.hasMoreElements()) {
056: String currAlt = ((String) alts.nextElement()).trim();
057: if (currAlt.length() > 0) {
058: pos = currAlt.indexOf('=');
059: if (pos < 0) {
060: alternatives.put(currAlt, currAlt);
061: } else {
062: alternatives.put(currAlt.substring(0, pos).trim(),
063: currAlt.substring(pos + 1).trim());
064: }
065: }
066: }
067: return label;
068: }
069:
070: /**
071: * Die apply-Methode von TKSyntax wird aufgerufen und das entsprechende
072: * Template wird included.
073: *
074: * @param TKTemplateData td
075: */
076: public String apply(TKTemplateData td)
077: throws TKTemplateSyntaxException {
078: String tmpLabel = label;
079: TKHashtable tmpAlternatives = alternatives;
080:
081: if (tmpLabel == null) {
082: tmpAlternatives = new TKHashtable();
083: tmpLabel = getAlternatives(alternativesSyntax.apply(td),
084: tmpAlternatives);
085: }
086:
087: Object selection = td.getVariable(tmpLabel);
088: if (selection == null)
089: return null;
090:
091: String theFile = (String) tmpAlternatives.get(selection);
092: if (theFile == null)
093: return null;
094:
095: // TKLog.println ("TKSwitchTag.apply, fn="+theFile);
096:
097: TKTemplateSyntax syntax = TKTemplateCache.getSyntax(theFile,
098: parent);
099: return syntax.apply(td);
100: }
101:
102: /**
103: * Die apply-Methode von TKSyntax wird aufgerufen und das entsprechende
104: * Template wird included.
105: *
106: * @param td TKTemplateData
107: * @param writer the Writer to which the result is written
108: */
109: public void apply(TKTemplateData td, Writer writer)
110: throws TKTemplateSyntaxException, IOException {
111: String tmpLabel = label;
112: TKHashtable tmpAlternatives = alternatives;
113:
114: if (tmpLabel == null) {
115: tmpAlternatives = new TKHashtable();
116: tmpLabel = getAlternatives(alternativesSyntax.apply(td),
117: tmpAlternatives);
118: }
119:
120: Object selection = td.getVariable(tmpLabel);
121: if (selection == null)
122: return;
123:
124: String theFile = (String) tmpAlternatives.get(selection);
125: if (theFile == null)
126: return;
127:
128: // TKLog.println ("TKSwitchTag.apply, fn="+theFile);
129:
130: TKTemplateSyntax syntax = TKTemplateCache.getSyntax(theFile,
131: parent);
132: syntax.apply(td, writer);
133: }
134: }
|