Coverage Report - org.apache.tapestry.script.LetToken
 
Classes in this File Line Coverage Branch Coverage Complexity
LetToken
0%
0/22
0%
0/12
3.333
 
 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  0
     private int _bufferLengthHighwater = 20;
 38  
 
 39  
     public LetToken(String key, boolean unique, Location location)
 40  
     {
 41  0
         super(location);
 42  
 
 43  0
         _key = key;
 44  0
         _unique = unique;
 45  0
     }
 46  
 
 47  
     public void write(StringBuffer buffer, ScriptSession session)
 48  
     {
 49  0
         if (buffer != null) throw new IllegalArgumentException();
 50  
 
 51  0
         StringBuffer useBuffer = new StringBuffer(_bufferLengthHighwater);
 52  
 
 53  0
         writeChildren(useBuffer, session);
 54  
 
 55  
         // Store the symbol back into the root set of symbols.
 56  
 
 57  0
         Map symbols = session.getSymbols();
 58  
 
 59  0
         String value = useBuffer.toString().trim();
 60  
 
 61  0
         if (_unique) {
 62  0
             value = session.getUniqueString(value);
 63  
             
 64  
             // unique in scripts is mostly used to generate javascript identifiers
 65  0
             value = makeValidIdentifier(value);
 66  
         }
 67  
 
 68  0
         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  0
         _bufferLengthHighwater = Math.max(_bufferLengthHighwater, useBuffer
 74  
                 .length());
 75  0
     }
 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  0
         char[] chars = id.toCharArray();
 83  0
         for (int i=0; i<chars.length; i++) {
 84  0
             char c = chars[i];
 85  0
             if (c==':' || c=='-' || c=='.')
 86  0
                 chars[i]='_';
 87  
         }
 88  0
         return new String(chars);
 89  
     }
 90  
 }