KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > context > CommandContext


1 /*
2  * $Id: CommandContext.java,v 1.3 2002/04/04 06:38:47 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.context;
52
53 import org.openlaszlo.iv.flash.util.CommandExecutor;
54 import java.util.*;
55
56 /**
57  * Context which can execute commands as well as substitute variables.
58  *
59  * @author Dmitry Skavish
60  * @see org.openlaszlo.iv.flash.util.CommandExecutor
61  */

62 public class CommandContext extends StandardContext {
63
64     private CommandExecutor executor;
65
66     /**
67      * Creates command context without parent.
68      *
69      * @param executor command executor used to execute commands
70      */

71     public CommandContext( CommandExecutor executor ) {
72         this( null, executor );
73     }
74
75     /**
76      * Creates command context with parent.
77      *
78      * @param parent parent context
79      * @param executor command executor used to execute commands
80      */

81     public CommandContext( Context parent, CommandExecutor executor ) {
82         setParent( parent );
83         this.executor = executor;
84     }
85
86     public String JavaDoc getValue( String JavaDoc cmd ) {
87         if( cmd.length() != 0 && cmd.charAt(0) == '$' ) {
88             String JavaDoc res = executeCommand(this, cmd);
89             if( res != null ) return res;
90         }
91
92         return super.getValue( cmd );
93     }
94
95     /**
96      * Executes specified command.<p>
97      * Supported the following syntax:<BR>
98      * <CODE>$command_name[([param1[,param2...]])]</CODE><br>
99      * parameters can be commands as well, for example:<br>
100      * <code>$command1($command2(p1,p2),p3)</code>
101      *
102      * @param context execution context (the context from which the command was invoked)
103      * @param cmd command with possible parameters
104      * @return result of command execution, never null
105      */

106     public String JavaDoc executeCommand( Context context, String JavaDoc cmd ) {
107         //System.out.println( "execute command: '"+cmd+"'" );
108
int idx = cmd.indexOf('(');
109         String JavaDoc name;
110         Vector parms = null;
111         if( idx == -1 ) {
112             name = cmd.substring(1).trim();
113         } else {
114             name = cmd.substring(1,idx).trim();
115             int i = idx+1;
116             parms = new Vector();
117             int l = cmd.length();
118             int cnt = 0;
119             int start = i;
120             while( i<l && cnt>=0 ) {
121                 char ch = cmd.charAt(i);
122                 switch( ch ) {
123                     case '(':
124                         cnt++;
125                         break;
126                     case ')':
127                         cnt--;
128                         break;
129                     case ',':
130                         if( cnt == 0 ) {
131                             String JavaDoc s = cmd.substring(start,i).trim();
132                             parms.addElement(s);
133                             start = i+1;
134                         }
135                         break;
136                 }
137                 i++;
138             }
139             i--;
140             String JavaDoc s = cmd.substring(start,i).trim();
141             if( parms.size() != 0 || s.length() != 0 ) {
142                 parms.addElement(s);
143             } else {
144                 parms = null;
145             }
146         }
147         if( parms != null ) {
148             for( int i=0; i<parms.size(); i++ ) {
149                 String JavaDoc c = (String JavaDoc) parms.elementAt(i);
150                 if( c.length() != 0 && c.charAt(0) == '$' ) {
151                     String JavaDoc res = executeCommand(context, c);
152                     if( res == null ) res = "";
153                     parms.setElementAt(res,i);
154                 }
155             }
156         }
157         return executor.execute(context, name, parms);
158     }
159
160 }
161
162
163
Popular Tags