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 TKSwitchTag extends TKTag { 15 public static final int TAG_TYPE = TKNotTag.TAG_TYPE+1; ; 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 SYNTAX_ALTERNATIVES = "alternatives"; 22 23 public String label = null; 24 public TKHashtable alternatives = null; 25 public TKTemplateSyntax alternativesSyntax = null; 26 27 34 public TKSwitchTag( TKTemplateSyntax parent, String def, boolean hasSubTags ) throws TKTemplateSyntaxException 35 { 36 super( parent ); 37 if( hasSubTags ) { 38 alternativesSyntax = parent.newChild( def, parent.getSource(), hasSubTags ); 39 } 40 else { 41 alternatives = new TKHashtable(); 42 label = getAlternatives( def, alternatives ); 43 } 44 } 45 46 49 protected static String getAlternatives( String def, TKHashtable alternatives ) 50 { 51 int pos = def.indexOf(':'); 52 String label = def.substring( 0, pos ); 53 Enumeration alts = new StringTokenizer( def.substring( pos+1 ), ";" ); 54 while( alts.hasMoreElements() ) { 55 String currAlt = ((String ) alts.nextElement()).trim(); 56 if( currAlt.length() > 0 ) { 57 pos = currAlt.indexOf('='); 58 if( pos < 0 ) { 59 alternatives.put( currAlt, currAlt ); 60 } 61 else { 62 alternatives.put( 63 currAlt.substring( 0, pos ).trim(), 64 currAlt.substring( pos+1 ).trim() 65 ); 66 } 67 } 68 } 69 return label; 70 } 71 72 78 public String apply( TKTemplateData td ) throws TKTemplateSyntaxException 79 { 80 String tmpLabel = label; 81 TKHashtable tmpAlternatives = alternatives; 82 83 if( tmpLabel == null ) { 84 tmpAlternatives = new TKHashtable(); 85 tmpLabel = getAlternatives( alternativesSyntax.apply( td ), tmpAlternatives ); 86 } 87 88 Object selection = td.getVariable(tmpLabel); 89 if( selection == null ) return null; 90 91 String theFile = (String ) tmpAlternatives.get( selection ); 92 if( theFile == null ) return null; 93 94 96 TKTemplateSyntax syntax = TKTemplateCache.getSyntax( theFile, parent ); 97 return syntax.apply( td ); 98 } 99 100 101 108 public void apply(TKTemplateData td, Writer writer) 109 throws TKTemplateSyntaxException, IOException 110 { 111 String tmpLabel = label; 112 TKHashtable tmpAlternatives = alternatives; 113 114 if( tmpLabel == null ) { 115 tmpAlternatives = new TKHashtable(); 116 tmpLabel = getAlternatives( alternativesSyntax.apply( td ), tmpAlternatives ); 117 } 118 119 Object selection = td.getVariable(tmpLabel); 120 if( selection == null ) 121 return; 122 123 String theFile = (String ) tmpAlternatives.get( selection ); 124 if( theFile == null ) 125 return; 126 127 129 TKTemplateSyntax syntax = TKTemplateCache.getSyntax( theFile, parent ); 130 syntax.apply( td, writer ); 131 } 132 } 133 134 | Popular Tags |