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  
20  package org.apache.myfaces.custom.graphicimagedynamic.util;
21  
22  import java.util.Map;
23  
24  import javax.faces.FacesException;
25  import javax.faces.context.ExternalContext;
26  import javax.faces.context.FacesContext;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.myfaces.custom.graphicimagedynamic.GraphicImageDynamic;
30  import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
31  
32  /**
33   * This class is a utility class used by the 
34   * GraphicsImageDynamic component.
35   */
36  public class GraphicsImageDynamicHelper {
37      
38      /**
39       * This method is used for getting the ImageRenderer
40       * from the image render class.
41       * @param imageRendererClassName
42       * @return the image renderer
43       */
44      public static ImageRenderer getImageRendererFromClassName(
45              String imageRendererClassName) {
46          ImageRenderer imageRenderer;
47          try {
48              Class rendererClass = ClassUtils
49                      .classForName(imageRendererClassName);
50              if (!ImageRenderer.class.isAssignableFrom(rendererClass)) {
51                  throw new FacesException("Image renderer class ["
52                          + imageRendererClassName + "] does not implement "
53                          + ImageRenderer.class.getName());
54              }
55              try {
56                  imageRenderer = (ImageRenderer) rendererClass.newInstance();
57              } catch (InstantiationException e) {
58                  throw new FacesException(
59                          "could not instantiate image renderer class "
60                                  + imageRendererClassName + " : "
61                                  + e.getMessage(), e);
62              } catch (IllegalAccessException e) {
63                  throw new FacesException(
64                          "could not instantiate image renderer class "
65                                  + imageRendererClassName + " : "
66                                  + e.getMessage(), e);
67              }
68          } catch (ClassNotFoundException e) {
69              throw new FacesException("image renderer class not found: "
70                      + e.getMessage(), e);
71          }
72          return imageRenderer;
73      }
74  
75      /**
76       * This method gets the image renderer from the ValueBinding.
77       * @param facesContext
78       * @param rendererValueBinding
79       * @return the ImageRenderer.
80       */
81      public static ImageRenderer getImageRendererFromValueBinding(
82              FacesContext facesContext, String rendererValueBinding) {
83          return (ImageRenderer) facesContext.getApplication()
84                  .createValueBinding(rendererValueBinding).getValue(
85                          facesContext);
86      }    
87      
88      
89      /**
90       * This method is used for creating the image context.
91       * @param facesContext
92       * @return the imageContext.
93       */
94      public static ImageContext createImageContext(FacesContext facesContext,
95              Log log) {
96  
97          ExternalContext externalContext = facesContext.getExternalContext();
98          final Map requestMap = externalContext.getRequestParameterMap();
99          Object value = requestMap.get(GraphicImageDynamic.WIDTH_PARAM);
100         Integer height = null;
101         Integer width = null;
102 
103         if (value != null) {
104             try {
105                 width = Integer.valueOf(value.toString());
106             } catch (NumberFormatException e) {
107                 log.error("Invalid value for image width : " + value + ", "
108                         + e.getMessage(), e);
109             }
110         }
111 
112         value = requestMap.get(GraphicImageDynamic.HEIGHT_PARAM);
113         if (value != null) {
114             try {
115                 height = Integer.valueOf(value.toString());
116             } catch (NumberFormatException e) {
117                 log.error("Invalid value for image height : " + value + ", "
118                         + e.getMessage(), e);
119             }
120         }
121 
122         return new SimpleImageContext(requestMap, width, height);
123     }
124 }