001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
003     * agreements. See the NOTICE file distributed with this work for additional information regarding
004     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
005     * "License"); you may not use this file except in compliance with the License. You may obtain a
006     * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
007     * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
008     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
009     * for the specific language governing permissions and limitations under the License.
010     */
011    package javax.portlet.faces.component;
012    
013    import java.io.Serializable;
014    
015    import javax.faces.context.FacesContext;
016    import javax.faces.component.NamingContainer;
017    import javax.faces.component.UIViewRoot;
018    import javax.faces.context.ExternalContext;
019    
020    import javax.portlet.faces.annotation.PortletNamingContainer;
021    
022    /**
023     * Bridge ViewRoot that implements NamingContainer which uses the ExternalContext.encodeNamespace to
024     * introduce the consumer namespace into tree components.
025     */
026    @PortletNamingContainer
027    public class PortletNamingContainerUIViewRoot extends UIViewRoot implements Serializable
028    {
029    
030      //TODO: This should be regenerated each time this is modified.  Can this be added to maven?
031      private static final long   serialVersionUID = -4524288011655837711L;
032      private static final String SEPARATOR        = (new Character(NamingContainer.SEPARATOR_CHAR))
033                                                                                                    .toString();
034    
035      public PortletNamingContainerUIViewRoot()
036      {
037        super();
038      }
039    
040      public PortletNamingContainerUIViewRoot(UIViewRoot viewRootToReplace)
041      {
042        super();
043        setViewId(viewRootToReplace.getViewId());
044        setLocale(viewRootToReplace.getLocale());
045        setRenderKitId(viewRootToReplace.getRenderKitId());
046      }
047    
048      // Implement the method that satisfies NamingContainer
049    
050      public static String getContainerClientId(FacesContext context, String additionalId)
051      {
052        ExternalContext ec = context.getExternalContext();
053        String namespace = ec.encodeNamespace(SEPARATOR);
054    
055        /*
056         * In servlet world encodeNamespace does nothing -- so if we get back what we sent in then do
057         * not perturn the NamingContainer Id
058         */
059        if (namespace.length() > 1)
060        {
061          if (additionalId != null)
062          {
063            return namespace + additionalId;
064          }
065          else
066          {
067            return namespace;
068          }
069        }
070        else
071        {
072          return null;
073        }
074      }
075    
076      // Implement the method that satisfies NamingContainer
077    
078      @Override
079      public String getContainerClientId(FacesContext context)
080      {
081        return PortletNamingContainerUIViewRoot
082                                               .getContainerClientId(
083                                                                     context,
084                                                                     super
085                                                                          .getContainerClientId(context));
086      }
087    
088    }