KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > script > LetToken


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.script;
16
17 import java.util.Map JavaDoc;
18
19 import org.apache.hivemind.Location;
20
21 /**
22  * Allows for the creation of new symbols that can be used in the script
23  * or returned to the caller.
24  *
25  * <p>The &lt;let&gt; tag wraps around static text and &lt;insert&gt;
26  * elements. The results are trimmed.
27  *
28  * @author Howard Lewis Ship
29  * @since 0.2.9
30  *
31  **/

32
33 class LetToken extends AbstractToken
34 {
35     private String JavaDoc _key;
36     private boolean _unique;
37     private int _bufferLengthHighwater = 20;
38
39     public LetToken(String JavaDoc key, boolean unique, Location location)
40     {
41         super(location);
42
43         _key = key;
44         _unique = unique;
45     }
46
47     public void write(StringBuffer JavaDoc buffer, ScriptSession session)
48     {
49         if (buffer != null)
50             throw new IllegalArgumentException JavaDoc();
51
52         buffer = new StringBuffer JavaDoc(_bufferLengthHighwater);
53
54         writeChildren(buffer, session);
55
56         // Store the symbol back into the root set of symbols.
57

58         Map JavaDoc symbols = session.getSymbols();
59
60         String JavaDoc value = buffer.toString().trim();
61
62         if (_unique)
63             value = session.getUniqueString(value);
64
65         symbols.put(_key, value);
66
67         // Store the buffer length from this run for the next run, since its
68
// going to be approximately the right size.
69

70         _bufferLengthHighwater = Math.max(_bufferLengthHighwater, buffer.length());
71     }
72 }
Popular Tags