Coverage Report - org.apache.tapestry.enhance.ClientIdPropertyWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
ClientIdPropertyWorker
0%
0/32
0%
0/8
2
 
 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  
 package org.apache.tapestry.enhance;
 15  
 
 16  
 import org.apache.hivemind.ErrorLog;
 17  
 import org.apache.hivemind.Location;
 18  
 import org.apache.hivemind.service.MethodSignature;
 19  
 import org.apache.tapestry.IComponent;
 20  
 import org.apache.tapestry.event.PageDetachListener;
 21  
 import org.apache.tapestry.spec.IComponentSpecification;
 22  
 
 23  
 import java.lang.reflect.Modifier;
 24  
 import java.util.Iterator;
 25  
 
 26  
 
 27  
 /**
 28  
  * Enhances the {@link org.apache.tapestry.IComponent#getClientId()} property.
 29  
  */
 30  0
 public class ClientIdPropertyWorker implements EnhancementWorker
 31  
 {
 32  
     private static final String PROPERTY_NAME = "clientId";
 33  
     
 34  
     private ErrorLog _errorLog;
 35  
     
 36  
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
 37  
     {
 38  0
         Location location = spec.getLocation();
 39  
         
 40  0
         Iterator i = op.findUnclaimedAbstractProperties().iterator();
 41  
 
 42  0
         while (i.hasNext())
 43  
         {
 44  0
             String name = (String) i.next();
 45  
             
 46  0
             if (name.equals(PROPERTY_NAME)) {
 47  
                 
 48  
                 try
 49  
                 {
 50  0
                     createProperty(op, name, location);
 51  
                 }
 52  0
                 catch (Exception ex)
 53  
                 {
 54  0
                     _errorLog.error(
 55  
                             EnhanceMessages.errorAddingProperty(name, op.getBaseClass(), ex),
 56  
                             location,
 57  
                             ex);
 58  0
                 }
 59  
                 
 60  0
                 break;
 61  
             }
 62  0
         }
 63  0
     }
 64  
 
 65  
     private void createProperty(EnhancementOperation op, String name, Location location)
 66  
     {
 67  
         // This won't be null because its always for existing properties.
 68  
 
 69  0
         Class propertyType = op.getPropertyType(name);
 70  
 
 71  0
         String fieldName = "_$" + name;
 72  0
         String defaultFieldName = fieldName + "$defaultValue";
 73  
 
 74  0
         op.addField(fieldName, propertyType);
 75  0
         op.addField(defaultFieldName, propertyType);
 76  
         
 77  0
         String methodName = op.getAccessorMethodName(name);
 78  
         
 79  
         // Build special getter logic 
 80  
         // if the client id is null "peek" at the next possible unique id
 81  
         
 82  0
         StringBuffer str = new StringBuffer();
 83  
         
 84  0
         str.append(" if(").append(fieldName).append(" == null){")
 85  
         .append(" if (getPage() == null) { return null; }")
 86  
         .append(" String tempId = getSpecifiedId();")
 87  
         .append(" if (tempId == null) { return null; }")
 88  
         .append(" return getPage().getRequestCycle().peekUniqueId(org.apache.tapestry.TapestryUtils#convertTapestryIdToNMToken(tempId));")
 89  
         .append("} else { ");
 90  
         
 91  
         // else return the existing clientId
 92  
         
 93  0
         str.append("return ").append(fieldName).append(";");
 94  0
         str.append("}");
 95  
         
 96  0
         op.addMethod(
 97  
                 Modifier.PUBLIC,
 98  
                 new MethodSignature(propertyType, methodName, null, null),
 99  
                 str.toString(),
 100  
                 location);
 101  
         
 102  0
         EnhanceUtils.createSimpleMutator(op, fieldName, name, propertyType, location);
 103  
         
 104  
         // Copy the real attribute into the default attribute inside finish load
 105  
         // (allowing a default value to be set inside finishLoad()).
 106  
 
 107  0
         op.extendMethodImplementation(
 108  0
                 IComponent.class,
 109  
                 EnhanceUtils.FINISH_LOAD_SIGNATURE,
 110  
                 defaultFieldName + " = " + fieldName + ";");
 111  
 
 112  
         // On page detach, restore the attribute to its default value.
 113  
 
 114  0
         op.extendMethodImplementation(
 115  
                 PageDetachListener.class,
 116  
                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
 117  
                 fieldName + " = " + defaultFieldName + ";");
 118  
 
 119  
         // This is not all that necessary, but is proper.
 120  
 
 121  0
         op.claimProperty(name);
 122  0
     }
 123  
 
 124  
     public void setErrorLog(ErrorLog errorLog)
 125  
     {
 126  0
         _errorLog = errorLog;
 127  0
     }
 128  
 }