Coverage Report - org.apache.tapestry.script.LetToken
 
Classes in this File Line Coverage Branch Coverage Complexity
LetToken
100% 
100% 
2.667
 
 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;
 18  
 
 19  
 import org.apache.hivemind.Location;
 20  
 
 21  
 /**
 22  
  * Allows for the creation of new symbols that can be used in the script or
 23  
  * returned to the caller.
 24  
  * <p>
 25  
  * The &lt;let&gt; tag wraps around static text and &lt;insert&gt; elements. The
 26  
  * results are trimmed.
 27  
  * 
 28  
  * @author Howard Lewis Ship
 29  
  * @since 0.2.9
 30  
  */
 31  
 
 32  
 class LetToken extends AbstractToken
 33  
 {
 34  
 
 35  
     private String _key;
 36  
     private boolean _unique;
 37  14
     private int _bufferLengthHighwater = 20;
 38  
 
 39  
     public LetToken(String key, boolean unique, Location location)
 40  
     {
 41  14
         super(location);
 42  
 
 43  14
         _key = key;
 44  14
         _unique = unique;
 45  14
     }
 46  
 
 47  
     public void write(StringBuffer buffer, ScriptSession session)
 48  
     {
 49  14
         if (buffer != null) throw new IllegalArgumentException();
 50  
 
 51  14
         StringBuffer useBuffer = new StringBuffer(_bufferLengthHighwater);
 52  
 
 53  14
         writeChildren(useBuffer, session);
 54  
 
 55  
         // Store the symbol back into the root set of symbols.
 56  
 
 57  14
         Map symbols = session.getSymbols();
 58  
 
 59  14
         String value = useBuffer.toString().trim();
 60  
 
 61  14
         if (_unique) {
 62  6
             value = session.getUniqueString(value);
 63  
             
 64  
             // unique in scripts is mostly used to generate javascript identifiers
 65  6
             value = makeValidIdentifier(value);
 66  
         }
 67  
 
 68  14
         symbols.put(_key, value);
 69  
 
 70  
         // Store the buffer length from this run for the next run, since its
 71  
         // going to be approximately the right size.
 72  
 
 73  14
         _bufferLengthHighwater = Math.max(_bufferLengthHighwater, useBuffer
 74  
                 .length());
 75  14
     }
 76  
     
 77  
     /**
 78  
      * Replaces hyphens, colons and periods (which are allowed in html 
 79  
      * but not in javascript identifiers) with underscores.
 80  
      */
 81  
     private static String makeValidIdentifier(String id) {
 82  6
         char[] chars = id.toCharArray();
 83  70
         for (int i=0; i<chars.length; i++) {
 84  64
             char c = chars[i];
 85  64
             if (c==':' || c=='-' || c=='.')
 86  5
                 chars[i]='_';
 87  
         }
 88  6
         return new String(chars);
 89  
     }
 90  
 }