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.commons.exporter.util;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.faces.FacesException;
25  import javax.faces.component.EditableValueHolder;
26  import javax.faces.component.UIColumn;
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UIViewRoot;
29  import javax.faces.component.ValueHolder;
30  import javax.faces.component.html.HtmlDataTable;
31  import javax.faces.context.FacesContext;
32  import javax.faces.convert.Converter;
33  import javax.faces.el.PropertyNotFoundException;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  
39  public final class ComponentUtils
40  {
41  
42      private static final Log log = LogFactory.getLog(ComponentUtils.class);
43  
44      public static UIComponent findComponentById(FacesContext context,
45              UIComponent root, String id) {
46  
47          UIComponent component = null;
48  
49          for (int i = 0; i < root.getChildCount() && component == null; i++)
50          {
51              UIComponent child = (UIComponent) root.getChildren().get(i);
52              component = findComponentById(context, child, id);
53          }
54  
55          if (root.getId() != null)
56          {
57              if (component == null && root.getId().equals(id))
58              {
59                  component = root;
60              }
61          }
62          return component;
63      }
64  
65      public static String getPathToComponent(UIComponent component) {
66          StringBuffer buf = new StringBuffer();
67  
68          if (component == null)
69          {
70              buf.append("{Component-Path : ");
71              buf.append("[null]}");
72              return buf.toString();
73          }
74  
75          getPathToComponent(component, buf);
76  
77          buf.insert(0, "{Component-Path : ");
78          buf.append("}");
79  
80          return buf.toString();
81      }
82  
83      private static void getPathToComponent(UIComponent component,
84              StringBuffer buf) {
85  
86          if (component == null)
87              return;
88  
89          StringBuffer intBuf = new StringBuffer();
90  
91          intBuf.append("[Class: ");
92          intBuf.append(component.getClass().getName());
93          if (component instanceof UIViewRoot)
94          {
95              intBuf.append(",ViewId: ");
96              intBuf.append(((UIViewRoot) component).getViewId());
97          }
98          else
99          {
100             intBuf.append(",Id: ");
101             intBuf.append(component.getId());
102         }
103         intBuf.append("]");
104 
105         buf.insert(0, intBuf.toString());
106 
107         getPathToComponent(component.getParent(), buf);
108     }
109 
110     private static Object getValue(UIComponent component) {
111         Object value;
112         try
113         {
114             value = ((ValueHolder) component).getValue();
115         }
116         catch (Exception ex)
117         {
118             throw new FacesException(
119                     "Could not retrieve value of component with path : "
120                             + getPathToComponent(component), ex);
121         }
122         return value;
123     }
124 
125     public static String getStringValue(FacesContext facesContext,
126             UIComponent component) {
127         try
128         {
129             if (!(component instanceof ValueHolder))
130             {
131                 throw new IllegalArgumentException("Component : "
132                         + getPathToComponent(component)
133                         + "is not a ValueHolder");
134             }
135 
136             if (component instanceof EditableValueHolder)
137             {
138                 Object submittedValue = ((EditableValueHolder) component)
139                         .getSubmittedValue();
140                 if (submittedValue != null)
141                 {
142                     if (submittedValue instanceof String)
143                     {
144                         if (log.isDebugEnabled())
145                             log.debug("returning 1 '" + submittedValue + "'");
146                         return (String) submittedValue;
147                     }
148 
149                     throw new IllegalArgumentException(
150                             "Expected submitted value of type String for component : "
151                                     + getPathToComponent(component));
152                 }
153             }
154 
155             Object value;
156 
157             if (component instanceof EditableValueHolder)
158             {
159 
160                 EditableValueHolder holder = (EditableValueHolder) component;
161 
162                 if (holder.isLocalValueSet())
163                 {
164                     value = holder.getLocalValue();
165                 }
166                 else
167                 {
168                     value = getValue(component);
169                 }
170             }
171             else
172             {
173                 value = getValue(component);
174             }
175 
176             Converter converter = ((ValueHolder) component).getConverter();
177             if (converter == null && value != null)
178             {
179 
180                 try
181                 {
182                     converter = facesContext.getApplication().createConverter(
183                             value.getClass());
184                     if (log.isDebugEnabled())
185                         log.debug("the created converter is " + converter);
186                 }
187                 catch (FacesException e)
188                 {
189                     log.error("No converter for class "
190                             + value.getClass().getName()
191                             + " found (component id=" + component.getId()
192                             + ").");
193                     // converter stays null
194                 }
195             }
196 
197             if (converter == null)
198             {
199                 if (value == null)
200                 {
201                     if (log.isDebugEnabled())
202                         log.debug("returning an empty string");
203                     return "";
204                 }
205 
206                 if (log.isDebugEnabled())
207                     log.debug("returning an .toString");
208                 return value.toString();
209 
210             }
211 
212             if (log.isDebugEnabled())
213                 log.debug("returning converter get as string " + converter);
214             return converter.getAsString(facesContext, component, value);
215 
216         }
217         catch (PropertyNotFoundException ex)
218         {
219             log.error("Property not found - called by component : "
220                     + getPathToComponent(component), ex);
221 
222             throw ex;
223         }
224     }
225 
226     public static List getHTMLDataTableColumns(HtmlDataTable table) {
227         List columns = new ArrayList();
228 
229         for (int i = 0; i < table.getChildCount(); i++)
230         {
231             UIComponent child = (UIComponent) table.getChildren().get(i);
232             if (child instanceof UIColumn)
233             {
234                 columns.add(child);
235             }
236         }
237         return columns;
238     }
239 }