KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Context.java,v 1.6 2002/07/03 23:03:44 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 java.io.*;
54 import java.util.*;
55 import java.lang.reflect.*;
56 import org.openlaszlo.iv.flash.util.*;
57
58 /**
59  * Abstract generator context<BR>
60  * Contains variables with theirs values and reference to parent context<BR>
61  *
62  * @author Dmitry Skavish
63  * @author James Taylor
64  */

65
66 public abstract class Context {
67
68     private Context parent;
69
70     /**
71      * Sets parent context
72      *
73      * @param parent parent context
74      */

75     public void setParent( Context parent ) {
76         this.parent = parent;
77     }
78
79     /**
80      * Returns parent context
81      *
82      * @return parent context
83      */

84     public Context getParent() {
85         return parent;
86     }
87
88     /**
89      * Retrieves value by name
90      *
91      * @param name expression to evaluate in this context
92      * @return value of variable or null
93      */

94     public abstract String JavaDoc getValue( String JavaDoc name );
95
96     /**
97      * Queries the parent context for the value
98      *
99      * @param expr expression to evaluate in parent context
100      * @return the value of evaluating the expression in the parent context,
101      * or null if there is no parent context.
102      */

103     protected String JavaDoc getValueFromParent( String JavaDoc expr ) {
104         if ( parent != null ) {
105             return parent.getValue( expr );
106         } else {
107             return null;
108         }
109     }
110
111     /**
112      * Traverse the tree of context and find the root - CommandContext
113      *
114      * @return found CommandContext or null
115      */

116     public CommandContext getCommandContext() {
117         if( this instanceof CommandContext ) return (CommandContext) this;
118         if( parent != null ) return parent.getCommandContext();
119         return null;
120     }
121
122     /**
123      * Applies this context to the specified string<BR>
124      * In the specified string replaces all constructions like: {variable_name}
125      * with corresponding variable's value or empty string if variable not found<BR>
126      * {{ treated as a single brace
127      *
128      * @param s string to replace in
129      * @return string with all variables replaced by their values
130      */

131     public String JavaDoc apply( String JavaDoc s ) {
132         if( s == null ) return s;
133         int start = s.indexOf('{');
134         if( start == -1 ) return s;
135
136         //System.out.println( "length="+s.length()+", apply='"+s+"'" );
137
int pos = 0;
138         int from = 0;
139         int slen = s.length();
140         int extra = 10;
141         char[] sa = new char[slen + extra];
142
143         for(;;) {
144             int start1 = start+1;
145             if( start1 < slen && s.charAt(start1) == '{' ) {
146                 // case: {{
147
s.getChars(from, start1, sa, pos);
148                 pos += start1-from;
149                 from = start1+1;
150             } else {
151                 int cnt = 1;
152                 int end = start1;
153                 while( end<slen && cnt>0 ) {
154                     char ch = s.charAt(end++);
155                     if( ch == '}' ) cnt--;
156                     else if( ch == '{' ) cnt++;
157                 }
158                 end--;
159                 if( end >= slen ) {
160                     s.getChars(from, start, sa, pos);
161                     pos += start-from;
162                     break;
163                 }
164                 s.getChars(from, end, sa, pos);
165                 pos += start-from;
166
167                 // get variable name
168
String JavaDoc varName = new String JavaDoc( sa, pos+1, end-start1 );
169                 String JavaDoc value = null;
170
171                 int varlen = varName.length();
172                 if( varlen>0 ) {
173                     char ch = varName.charAt(0);
174                     if( ch == '$' ) { // check for command
175
String JavaDoc cmdCall = apply(varName).trim();
176                         CommandContext cmdexec = getCommandContext();
177                         if( cmdexec == null ) {
178                             value = getValue(cmdCall);
179                         } else {
180                             value = cmdexec.executeCommand(this, cmdCall);
181                         }
182                     } else if( ch == '#' && varlen > 1 ) { // check for inline javascript, convinient way to use js
183
String JavaDoc js_text = varName.substring(1);
184                         value = Util.executeJSString(this, js_text, null);
185                     } else {
186                         String JavaDoc varName1 = apply(varName).trim();
187                         value = getValue(varName1);
188                     }
189                 }
190
191                 if( value != null ) {
192                     int vlen = value.length();
193                     int vnlen = varName.length() + 2; // including braces
194

195                     // bug fixed - by yoonforh 2002-05-28 13:39:34
196
if( vlen > vnlen ) {
197                         extra -= (vlen - vnlen);
198                         if( extra < 0 ) {
199                             extra = -extra;
200                             char[] t = new char[sa.length + extra * 2];
201                             System.arraycopy(sa, 0, t, 0, sa.length);
202                             sa = t;
203                         }
204                     }
205                     value.getChars(0, vlen, sa, pos);
206                     pos += vlen;
207                 }
208                 from = end+1;
209             }
210             start = s.indexOf('{', from);
211             if( start == -1 ) {
212                 s.getChars(from, slen, sa, pos);
213                 pos += slen-from;
214                 break;
215             }
216         }
217
218         return new String JavaDoc(sa, 0, pos);
219     }
220 }
221
Popular Tags