You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2013/10/17 19:49:14 UTC

svn commit: r1533175 - in /myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler: CheckDuplicateIdFaceletUtils.java DuplicateIdException.java

Author: lu4242
Date: Thu Oct 17 17:49:14 2013
New Revision: 1533175

URL: http://svn.apache.org/r1533175
Log:
MYFACES-3506 Improve exception handling for "duplicate id "id" found" (thanks to Martin Koci for provide this patch)

Added:
    myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DuplicateIdException.java   (with props)
Modified:
    myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/CheckDuplicateIdFaceletUtils.java

Modified: myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/CheckDuplicateIdFaceletUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/CheckDuplicateIdFaceletUtils.java?rev=1533175&r1=1533174&r2=1533175&view=diff
==============================================================================
--- myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/CheckDuplicateIdFaceletUtils.java (original)
+++ myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/CheckDuplicateIdFaceletUtils.java Thu Oct 17 17:49:14 2013
@@ -20,8 +20,14 @@ package org.apache.myfaces.view.facelets
 
 import java.util.HashSet;
 import java.util.Set;
+
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
+import javax.faces.event.ExceptionQueuedEvent;
+import javax.faces.event.ExceptionQueuedEventContext;
+import javax.faces.view.Location;
+
+import org.apache.myfaces.shared.renderkit.RendererUtils;
 
 /**
  *
@@ -51,7 +57,8 @@ public final class CheckDuplicateIdFacel
         
         if (existingIds.contains (id))
         {
-            throw new IllegalStateException ("component with duplicate id \"" + id + "\" found");
+            DuplicateIdException duplicateIdException = createAndQueueException(context, component, id);
+            throw duplicateIdException;
         }
         
         existingIds.add (id);
@@ -97,7 +104,8 @@ public final class CheckDuplicateIdFacel
         
         if (existingIds.contains (id))
         {
-            throw new IllegalStateException ("component with duplicate id \"" + id + "\" found");
+            DuplicateIdException duplicateIdException = createAndQueueException(context, component, id);
+            throw duplicateIdException;
         }
         
         existingIds.add (id);
@@ -116,4 +124,40 @@ public final class CheckDuplicateIdFacel
             checkIds (context, child, existingIds);
         }
     }
+
+    private static DuplicateIdException createAndQueueException(FacesContext context, UIComponent component, String id)
+    {
+        String message = "Component with duplicate id \"" + id + "\" found. The first component is ";
+
+        
+        // We report as problematic the second component. The client (an exception handler mostly)
+        // has the possibility to report all about the second component, 
+        // but the first component is hard to find, especially in large view with tons of naming containers
+        // So we do here two things:
+        // 1) provide an info about the first component in exception message 
+        UIComponent firstComponent = context.getViewRoot().findComponent(id);
+        Location location = (Location) firstComponent.getAttributes().get(UIComponent.VIEW_LOCATION_KEY);
+        if (location != null)
+        {
+            message += location.toString();
+        }
+        else
+        {
+            // location is not available in production mode or if the component
+            // doesn't come from Facelets VDL.
+            message += RendererUtils.getPathToComponent(firstComponent);
+        }
+        
+        // 2) we store the first commponent in exception attributes
+        DuplicateIdException duplicateIdException = new DuplicateIdException 
+                (message, firstComponent, component);
+        
+        ExceptionQueuedEventContext exceptionContext 
+        = new ExceptionQueuedEventContext(context, duplicateIdException,
+                component, context.getCurrentPhaseId());
+
+        
+        context.getApplication().publishEvent(context, ExceptionQueuedEvent.class, exceptionContext);
+        return duplicateIdException;
+    }
 }

Added: myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DuplicateIdException.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DuplicateIdException.java?rev=1533175&view=auto
==============================================================================
--- myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DuplicateIdException.java (added)
+++ myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DuplicateIdException.java Thu Oct 17 17:49:14 2013
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.view.facelets.compiler;
+
+import javax.faces.FacesException;
+import javax.faces.component.UIComponent;
+
+/**
+ * Indicates duplicate id as specified in spec 7.7.3 State Saving Methods.
+ * 
+ * @author martinkoci
+ */
+public class DuplicateIdException extends FacesException
+{
+
+    private final UIComponent firstComponent;
+
+    private final UIComponent secondComponent;
+
+    public DuplicateIdException(String message, UIComponent firstComponent, UIComponent secondComponent)
+    {
+        super(message);
+        this.firstComponent = firstComponent;
+        this.secondComponent = secondComponent;
+    }
+
+    public UIComponent getFirstComponent()
+    {
+        return firstComponent;
+    }
+
+    public UIComponent getSecondComponent()
+    {
+        return secondComponent;
+    }
+
+}

Propchange: myfaces/core/branches/2.1.x/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DuplicateIdException.java
------------------------------------------------------------------------------
    svn:eol-style = native