KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > TemplateDirective


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TemplateDirective.java,v 1.6 2004/02/01 05:16:27 christianc Exp $
19  */

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 /**
29  * This class defines a TemplateDirective. A TemplateDirective is
30  * created from a string (usually in the HTML class attribute) and
31  * takes the form
32  * <strong><emph>Dir::&lt;command&gt;.&lt;model&gt;.&lt;key&gt;.&lt;props&gt;...</emph></strong>
33  * where:
34  * <ul>
35  * <li>&lt;command&gt; = the name of the command (custom directives are allowed)</li>
36  * <li>&lt;model&gt; = the name of the model</li>
37  * <li>&lt;key&gt; = the name of the key within the model</li>
38  * <li>&lt;data&gt; = any additional data (in any format) that may be associated with the directive</li>
39  * </ul>
40  * <p>As an example, the following are valid directives:
41  * <ul>
42  * <li><strong><emph>Dir::Get_Data.UserData.FirstName</emph></strong></li>
43  * <li><strong><emph>Dir::Get_Data.UserData.LastName</emph></strong></li>
44  * <li><strong><emph>Dir::Get_Data.Iterate_Start.UserData..max=50&min=25&foo=bar</emph></strong></li>
45  * <li><strong><emph>Dir::Get_Data.Iterate_End</emph></strong></li>
46  * <li><strong><emph>Dir::Custom_Directive.UserData..do=something</emph></strong></li>
47  * </ul>
48  */

49 public class TemplateDirective {
50
51     //supported directives commands
52
public final static String JavaDoc ITERATE_START = "Iterate_Start";
53     public final static String JavaDoc ITERATE_NEXT = "Iterate_Next";
54     public final static String JavaDoc ITERATE_END = "Iterate_End";
55     public final static String JavaDoc GET_DATA = "Get_Data";
56     public final static String JavaDoc SET_ATTR = "Set_Attr";
57     public final static String JavaDoc DISCARD = "Discard";
58
59     protected String JavaDoc cmd = null;
60     protected String JavaDoc modelName = null;
61     protected String JavaDoc keyName = null;
62     protected String JavaDoc keyData = null;
63
64     protected TemplateDirective() {}
65     
66     //csc_030703.1 - added to make it easier to programatically create directives,
67
//witout having to parse a String
68
public TemplateDirective(String JavaDoc icmd, String JavaDoc imodelName, String JavaDoc ikeyName, String JavaDoc ikeyData) {
69         cmd = icmd;
70         modelName = imodelName;
71         keyName = ikeyName;
72         keyData = ikeyData;
73     }
74
75     public String JavaDoc getCommand() {
76         return cmd;
77     }
78     
79     public String JavaDoc getModelName() {
80         return modelName;
81     }
82     
83     public String JavaDoc getKeyName() {
84         return keyName;
85     }
86     
87     public String JavaDoc getKeyData() {
88         return keyData;
89     }
90
91     public String JavaDoc toString() {
92         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc 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     /**
119      * Given a space delimited string of directive commands, convert
120      * each segment into a TemplateDirective and return all the
121      * valid directives in a List. Invalid directives are silently ignored.
122      *
123      * @param sourceStr a space delimited string of directive commands
124      * @return a List of TemplateDirectives
125      */

126     public static synchronized List getAllInstances(String JavaDoc 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     /**
138      * Given a string of the form
139      * <strong><emph>Dir::&lt;command&gt;.&lt;model&gt;.&lt;key&gt;.&lt;props&gt;...</emph></strong>
140      * convert it into a TemplateDirective and return that object.
141      *
142      * @param sourceStr a space delimited string of directive commands
143      * @return the corresponding TemplateDirectives
144      * @throws InvalidDirectiveException if the String cannot be converted
145      */

146 //csc_030703.1 - I see no reason why this needs to be synchronized
147
//csc_030703.1 public static synchronized TemplateDirective getInstance(String sourceStr) throws InvalidDirectiveException {
148
public static TemplateDirective getInstance(String JavaDoc sourceStr) throws InvalidDirectiveException { //csc_030703.1
149
return getInstance_private(sourceStr);
150     }
151
152     private static TemplateDirective getInstance_private(String JavaDoc sourceStr) throws InvalidDirectiveException {
153         //make sure it starts with Dir::
154
if (sourceStr==null || !(sourceStr.startsWith("Dir::"))) throw new InvalidDirectiveException("Invalid Directive:"+sourceStr+"...Directive must start with 'Dir::'");
155     
156         //create a new TemplateDirective
157
TemplateDirective td = new TemplateDirective();
158     
159         //create a String tokenizer and parse on periods
160
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 JavaDoc[] 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