View Javadoc

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.renderkit;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.logging.Level;
25  import java.util.logging.Logger;
26  
27  import javax.faces.FacesException;
28  import javax.faces.context.FacesContext;
29  import javax.faces.render.RenderKit;
30  import javax.faces.render.RenderKitFactory;
31  
32  /**
33   * RenderKitFactory implementation as defined in Spec. JSF.7.3
34   * 
35   * @author Manfred Geiler (latest modification by $Author$)
36   * @version $Revision$ $Date$
37   */
38  public class RenderKitFactoryImpl extends RenderKitFactory
39  {
40      private static final Logger log = Logger.getLogger(RenderKitFactoryImpl.class.getName());
41  
42      private Map<String, RenderKit> _renderkits = new HashMap<String, RenderKit>();
43  
44      public RenderKitFactoryImpl()
45      {
46      }
47  
48      public void purgeRenderKit()
49      {
50          _renderkits.clear();
51      }
52  
53      @Override
54      public void addRenderKit(String renderKitId, RenderKit renderKit)
55      {
56          if (renderKitId == null)
57          {
58              throw new NullPointerException("renderKitId");
59          }
60          if (renderKit == null)
61          {
62              throw new NullPointerException("renderKit");
63          }
64          if (log.isLoggable(Level.INFO))
65          {
66              if (_renderkits.containsKey(renderKitId))
67              {
68                  log.info("RenderKit with renderKitId '" + renderKitId + "' was replaced.");
69              }
70          }
71          _renderkits.put(renderKitId, renderKit);
72      }
73  
74      @Override
75      public RenderKit getRenderKit(FacesContext context, String renderKitId) throws FacesException
76      {
77          if (renderKitId == null)
78          {
79              throw new NullPointerException("renderKitId");
80          }
81          RenderKit renderkit = _renderkits.get(renderKitId);
82          if (renderkit == null)
83          {
84              // throw new IllegalArgumentException("Unknown RenderKit '" + renderKitId + "'.");
85              // JSF Spec API Doc says:
86              // "If there is no registered RenderKit for the specified identifier, return null"
87              // vs "IllegalArgumentException - if no RenderKit instance can be returned for the specified identifier"
88              // First sentence is more precise, so we just log a warning
89              log.warning("Unknown RenderKit '" + renderKitId + "'.");
90          }
91          return renderkit;
92      }
93  
94      @Override
95      public Iterator<String> getRenderKitIds()
96      {
97          return _renderkits.keySet().iterator();
98      }
99  }