Coverage Report - org.apache.myfaces.view.facelets.compiler.CheckDuplicateIdFaceletUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckDuplicateIdFaceletUtils
0%
0/47
0%
0/26
4.4
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.view.facelets.compiler;
 20  
 
 21  
 import java.util.HashSet;
 22  
 import java.util.Set;
 23  
 
 24  
 import javax.faces.component.UIComponent;
 25  
 import javax.faces.context.FacesContext;
 26  
 import javax.faces.event.ExceptionQueuedEvent;
 27  
 import javax.faces.event.ExceptionQueuedEventContext;
 28  
 import javax.faces.view.Location;
 29  
 
 30  
 import org.apache.myfaces.shared.renderkit.RendererUtils;
 31  
 
 32  
 /**
 33  
  *
 34  
  * @author Leonardo Uribe
 35  
  */
 36  0
 public final class CheckDuplicateIdFaceletUtils
 37  
 {
 38  
     
 39  
     public static void checkIdsStatefulComponents (FacesContext context, UIComponent view)
 40  
     {
 41  0
         checkIdsStatefulComponents (context, view, new HashSet<String>());
 42  0
     }
 43  
 
 44  
     private static void checkIdsStatefulComponents (FacesContext context, 
 45  
             UIComponent component, Set<String> existingIds)
 46  
     {
 47  
         String id;
 48  
         
 49  0
         if (component == null)
 50  
         {
 51  0
             return;
 52  
         }
 53  
         
 54  
         // Need to use this form of the client ID method so we generate the client-side ID.
 55  
         
 56  0
         id = component.getClientId (context);
 57  
         
 58  0
         if (!existingIds.add (id))
 59  
         {
 60  0
             DuplicateIdException duplicateIdException = createAndQueueException(context, component, id);
 61  0
             throw duplicateIdException;
 62  
         }
 63  
         
 64  0
         int facetCount = component.getFacetCount();
 65  0
         if (facetCount > 0)
 66  
         {
 67  0
             for (UIComponent facet : component.getFacets().values())
 68  
             {
 69  0
                 if (!(facet instanceof UILeaf))
 70  
                 {
 71  0
                     checkIdsStatefulComponents (context, facet, existingIds);
 72  
                 }
 73  0
             }
 74  
         }
 75  0
         for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
 76  
         {
 77  0
             UIComponent child = component.getChildren().get(i);
 78  0
             if (!(child instanceof UILeaf))
 79  
             {
 80  0
                 checkIdsStatefulComponents (context, child, existingIds);
 81  
             }
 82  
         }
 83  0
     }
 84  
 
 85  
     public static void checkIds (FacesContext context, UIComponent view)
 86  
     {
 87  0
         checkIds (context, view, new HashSet<String>());
 88  0
     }
 89  
     
 90  
     private static void checkIds (FacesContext context, UIComponent component, Set<String> existingIds)
 91  
     {
 92  
         String id;
 93  
         
 94  0
         if (component == null)
 95  
         {
 96  0
             return;
 97  
         }
 98  
         
 99  
         // Need to use this form of the client ID method so we generate the client-side ID.
 100  
         
 101  0
         id = component.getClientId (context);
 102  
         
 103  0
         if (!existingIds.add (id))
 104  
         {
 105  0
             DuplicateIdException duplicateIdException = createAndQueueException(context, component, id);
 106  0
             throw duplicateIdException;
 107  
         }
 108  
         
 109  0
         int facetCount = component.getFacetCount();
 110  0
         if (facetCount > 0)
 111  
         {
 112  0
             for (UIComponent facet : component.getFacets().values())
 113  
             {
 114  0
                 checkIds (context, facet, existingIds);
 115  0
             }
 116  
         }
 117  0
         for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
 118  
         {
 119  0
             UIComponent child = component.getChildren().get(i);
 120  0
             checkIds (context, child, existingIds);
 121  
         }
 122  0
     }
 123  
 
 124  
     private static DuplicateIdException createAndQueueException(FacesContext context, UIComponent component, String id)
 125  
     {
 126  0
         String message = "Component with duplicate id \"" + id + "\" found. The first component is ";
 127  
 
 128  
         
 129  
         // We report as problematic the second component. The client (an exception handler mostly)
 130  
         // has the possibility to report all about the second component, 
 131  
         // but the first component is hard to find, especially in large view with tons of naming containers
 132  
         // So we do here two things:
 133  
         // 1) provide an info about the first component in exception message 
 134  0
         UIComponent firstComponent = context.getViewRoot().findComponent(id);
 135  0
         Location location = (Location) firstComponent.getAttributes().get(UIComponent.VIEW_LOCATION_KEY);
 136  0
         if (location != null)
 137  
         {
 138  0
             message += location.toString();
 139  
         }
 140  
         else
 141  
         {
 142  
             // location is not available in production mode or if the component
 143  
             // doesn't come from Facelets VDL.
 144  0
             message += RendererUtils.getPathToComponent(firstComponent);
 145  
         }
 146  
         
 147  
         // 2) we store the first commponent in exception attributes
 148  0
         DuplicateIdException duplicateIdException = new DuplicateIdException 
 149  
                 (message, firstComponent, component);
 150  
         
 151  0
         ExceptionQueuedEventContext exceptionContext 
 152  
         = new ExceptionQueuedEventContext(context, duplicateIdException,
 153  
                 component, context.getCurrentPhaseId());
 154  
 
 155  
         
 156  0
         context.getApplication().publishEvent(context, ExceptionQueuedEvent.class, exceptionContext);
 157  0
         return duplicateIdException;
 158  
     }
 159  
 }