1 20 package org.enhydra.barracuda.core.comp; 21 22 import java.util.*; 23 import java.io.*; 24 import java.net.*; 25 26 import org.w3c.dom.*; 27 28 49 public class TemplateDirective { 50 51 public final static String ITERATE_START = "Iterate_Start"; 53 public final static String ITERATE_NEXT = "Iterate_Next"; 54 public final static String ITERATE_END = "Iterate_End"; 55 public final static String GET_DATA = "Get_Data"; 56 public final static String SET_ATTR = "Set_Attr"; 57 public final static String DISCARD = "Discard"; 58 59 protected String cmd = null; 60 protected String modelName = null; 61 protected String keyName = null; 62 protected String keyData = null; 63 64 protected TemplateDirective() {} 65 66 public TemplateDirective(String icmd, String imodelName, String ikeyName, String ikeyData) { 69 cmd = icmd; 70 modelName = imodelName; 71 keyName = ikeyName; 72 keyData = ikeyData; 73 } 74 75 public String getCommand() { 76 return cmd; 77 } 78 79 public String getModelName() { 80 return modelName; 81 } 82 83 public String getKeyName() { 84 return keyName; 85 } 86 87 public String getKeyData() { 88 return keyData; 89 } 90 91 public String toString() { 92 StringBuffer sb = new StringBuffer (50); 93 sb.append("Dir::"+cmd); 94 if (modelName!=null) { 95 sb.append("."+modelName); 96 if (keyName!=null) { 97 sb.append("."+keyName); 98 if (keyData!=null) { 99 sb.append("."+keyData); 100 } 101 } 102 } 103 return sb.toString(); 104 } 105 106 public boolean equals(Object o) { 107 if (o==null) return false; 108 if (o==this) return true; 109 if (!(o instanceof TemplateDirective)) return false; 110 TemplateDirective td = (TemplateDirective) o; 111 if (cmd!=null && td.getCommand()!=null && !cmd.equals(td.getCommand())) return false; 112 if (modelName!=null && td.getModelName()!=null && !modelName.equals(td.getModelName())) return false; 113 if (keyName!=null && td.getKeyName()!=null && !keyName.equals(td.getKeyName())) return false; 114 if (keyData!=null && td.getKeyData()!=null && !keyData.equals(td.getKeyData())) return false; 115 return true; 116 } 117 118 126 public static synchronized List getAllInstances(String sourceStr) { 127 List directives = new ArrayList(); 128 StringTokenizer st = new StringTokenizer(sourceStr," "); 129 while (st.hasMoreTokens()) { 130 try { 131 directives.add(getInstance_private(st.nextToken())); 132 } catch (InvalidDirectiveException e) {} 133 } 134 return directives; 135 } 136 137 146 public static TemplateDirective getInstance(String sourceStr) throws InvalidDirectiveException { return getInstance_private(sourceStr); 150 } 151 152 private static TemplateDirective getInstance_private(String sourceStr) throws InvalidDirectiveException { 153 if (sourceStr==null || !(sourceStr.startsWith("Dir::"))) throw new InvalidDirectiveException("Invalid Directive:"+sourceStr+"...Directive must start with 'Dir::'"); 155 156 TemplateDirective td = new TemplateDirective(); 158 159 int pos0 = 5; 161 int pos1 = -1; 162 for (int cntr=0; cntr<4; cntr++) { 163 pos1 = sourceStr.indexOf(".",pos0); 164 if (pos1<0) pos1 = sourceStr.length(); 165 if (cntr==0 && pos0>-1 && pos1>-1 && pos0<pos1) { 166 td.cmd = sourceStr.substring(pos0,pos1); 167 } else if (cntr==1 && pos0>-1 && pos1>-1 && pos0<pos1) { 168 td.modelName = sourceStr.substring(pos0,pos1); 169 } else if (cntr==2 && pos0>-1 && pos1>-1 && pos0<pos1) { 170 td.keyName = sourceStr.substring(pos0,pos1); 171 } else if (cntr==3 && pos0>-1 && pos0<sourceStr.length()) { 172 td.keyData = sourceStr.substring(pos0); 173 } 174 pos0 = pos1+1; 175 } 176 177 return td; 178 } 179 180 181 public static void main(String [] args) { 182 try { 183 System.out.println ("TD 1="+TemplateDirective.getInstance("Dir::Get_Data.UserData.FirstName")); 184 System.out.println ("TD 2="+TemplateDirective.getInstance("Dir::Get_Data.UserData.LastName")); 185 System.out.println ("TD 3="+TemplateDirective.getInstance("Dir::Get_Data.Iterate_Start.UserData..max=50&min=25&foo=bar")); 186 System.out.println ("TD 4="+TemplateDirective.getInstance("Dir::Get_Data.Iterate_End")); 187 System.out.println ("TD 5="+TemplateDirective.getInstance("Dir::Custom_Directive.UserData..do=something")); 188 System.out.println ("TD 6="+TemplateDirective.getInstance("Dir::Veto_If_Null.UserData..Notes")); 189 TemplateDirective td = TemplateDirective.getInstance("Dir::Get_Data.Iterate_Start.UserData..max=50&min=25&foo=bar"); 190 System.out.println("cmd: "+td.getCommand()); 191 System.out.println("model:"+td.getModelName()); 192 System.out.println("key: "+td.getKeyName()); 193 System.out.println("data: "+td.getKeyData()); 194 } catch (InvalidDirectiveException e) { 195 System.out.println ("Failure:"+e); 196 } 197 198 } 199 200 } | Popular Tags |