View Javadoc

1   package org.gnomekr.potron.web.tag;
2   
3   import java.io.IOException;
4   
5   import javax.faces.component.UIComponent;
6   import javax.faces.component.UIInput;
7   import javax.faces.component.UIOutput;
8   import javax.faces.component.html.HtmlOutputText;
9   import javax.faces.context.FacesContext;
10  
11  import org.apache.myfaces.renderkit.JSFAttr;
12  import org.apache.myfaces.renderkit.RendererUtils;
13  import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
14  
15  /***
16   * TruncateOutputTextRenderer.java
17   * @author Xavier Cho
18   * @version $Revision 1.1 $ $Date: 2005/07/19 18:01:50 $
19   * @jsf.render-kit
20   *          renderer-type = "TruncateOutputText"
21   *          renderer-class = "org.gnomekr.potron.web.tag.TruncateOutputTextTag"
22   */
23  public class TruncateOutputTextRenderer extends HtmlTextRenderer {
24  
25      /***
26       * @see org.apache.myfaces.renderkit.html.HtmlTextRendererBase#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
27       */
28      @Override
29      public void encodeEnd(FacesContext facesContext, UIComponent component)
30              throws IOException {
31          RendererUtils.checkParamValidity(facesContext, component, null);
32  
33          if (component instanceof UIInput) {
34              renderInput(facesContext, component);
35          } else if (component instanceof UIOutput) {
36              renderOutput(facesContext, component);
37          } else {
38              throw new IllegalArgumentException("Unsupported component class "
39                      + component.getClass().getName());
40          }
41      }
42  
43      protected static void renderOutput(
44              FacesContext facesContext,
45              UIComponent component) throws IOException {
46          String text = RendererUtils.getStringValue(facesContext, component);
47          boolean escape;
48          if (component instanceof HtmlOutputText) {
49              escape = ((HtmlOutputText) component).isEscape();
50          } else {
51              escape = RendererUtils.getBooleanAttribute(
52                      component,
53                      JSFAttr.ESCAPE_ATTR,
54                      true); //default is to escape
55          }
56  
57          int upper = RendererUtils.getIntegerAttribute(component, "upper", 0);
58          int lower = RendererUtils.getIntegerAttribute(component, "lower", 0);
59  
60          String appendEnd = (String) component.getAttributes().get("appendEnd");
61  
62          if (text != null && text.length() > upper) {
63              text = text.substring(0, upper - 1);
64              int index = text.lastIndexOf(' ');
65  
66              if (index > lower - 1) {
67                  text = text.substring(0, index);
68              }
69  
70              if (appendEnd != null) {
71                  text = text.concat(appendEnd);
72              }
73          }
74  
75          renderOutputText(facesContext, component, text, escape);
76      }
77  }