You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sv...@apache.org on 2005/03/15 06:24:03 UTC

cvs commit: incubator-myfaces/src/share/org/apache/myfaces/renderkit/html HtmlRendererUtils.java

svieujot    2005/03/14 21:24:03

  Modified:    src/components/org/apache/myfaces/custom/htmlEditor
                        HtmlEditorRenderer.java HtmlEditor.java
                        HtmlEditorTag.java
               tlds     myfaces_ext.tld
               doc      release-notes.txt
               src/share/org/apache/myfaces/renderkit/html
                        HtmlRendererUtils.java
  Log:
  Add a fallback textarea mode to the htmlEditor.
  
  Revision  Changes    Path
  1.25      +100 -3    incubator-myfaces/src/components/org/apache/myfaces/custom/htmlEditor/HtmlEditorRenderer.java
  
  Index: HtmlEditorRenderer.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/htmlEditor/HtmlEditorRenderer.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- HtmlEditorRenderer.java	9 Mar 2005 04:13:52 -0000	1.24
  +++ HtmlEditorRenderer.java	15 Mar 2005 05:24:03 -0000	1.25
  @@ -16,7 +16,9 @@
   package org.apache.myfaces.custom.htmlEditor;
   
   import java.io.IOException;
  +import java.util.Map;
   
  +import javax.faces.component.EditableValueHolder;
   import javax.faces.component.UIComponent;
   import javax.faces.component.UIForm;
   import javax.faces.context.FacesContext;
  @@ -25,16 +27,21 @@
   
   import org.apache.myfaces.component.UserRoleUtils;
   import org.apache.myfaces.component.html.util.AddResource;
  +import org.apache.myfaces.renderkit.JSFAttr;
   import org.apache.myfaces.renderkit.RendererUtils;
   import org.apache.myfaces.renderkit.html.HTML;
   import org.apache.myfaces.renderkit.html.HtmlRenderer;
   import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
  +import org.apache.myfaces.renderkit.html.util.HTMLEncoder;
   import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
   
   /**
    * @author Sylvain Vieujot (latest modification by $Author$)
    * @version $Revision$ $Date$
    * $Log$
  + * Revision 1.25  2005/03/15 05:24:03  svieujot
  + * Add a fallback textarea mode to the htmlEditor.
  + *
    * Revision 1.24  2005/03/09 04:13:52  svieujot
    * htmlEditor : remove unused scripts
    *
  @@ -118,11 +125,83 @@
   
           return ((HtmlEditor)uiComponent).isDisabled();
       }
  +	
  +	private static boolean useFallback(HtmlEditor editor){
  +		// TODO : Handle fallback="auto"
  +		return editor.getFallback().equals("true");
  +	}
       
       public void encodeEnd(FacesContext context, UIComponent uiComponent) throws IOException {
           RendererUtils.checkParamValidity(context, uiComponent, HtmlEditor.class);
           HtmlEditor editor = (HtmlEditor) uiComponent;
  -        String clientId = editor.getClientId(context);
  +		if( useFallback(editor) )
  +			encodeEndFallBackMode(context, editor);
  +		else
  +			encodeEndNormalMode(context, editor);
  +    }
  +	
  +	private void encodeEndFallBackMode(FacesContext context, HtmlEditor editor) throws IOException {
  +		String clientId = editor.getClientId(context);	
  +		// Use only a textarea
  +		ResponseWriter writer = context.getResponseWriter();
  +        writer.startElement(HTML.TEXTAREA_ELEM, editor);
  +
  +        writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
  +        HtmlRendererUtils.writeIdIfNecessary(writer, editor, context);
  +		
  +		if( editor.getStyle()!=null )
  +            writer.writeAttribute(HTML.STYLE_ATTR, editor.getStyle(), null);
  +		if( editor.getStyleClass()!=null )
  +            writer.writeAttribute(HTML.STYLE_CLASS_ATTR, editor.getStyleClass(), null);
  +
  +        if (isDisabled(context, editor))
  +            writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
  +
  +        String text = RendererUtils.getStringValue(context, editor);
  +        writer.write( htmlToPlainText( text ) );
  +
  +        writer.endElement(HTML.TEXTAREA_ELEM);
  +	}
  +	
  +	private static String htmlToPlainText(String html){
  +		return getHtmlBody( html ).replaceAll("<br.*>","\n");
  +	}
  +	
  +	private static String getHtmlBody(String html){
  +		String lcText = html.toLowerCase();
  +        int textLength = lcText.length();
  +        int bodyStartIndex = 0;
  +        while(bodyStartIndex < textLength){
  +            bodyStartIndex = lcText.indexOf("<body");
  +            if( bodyStartIndex == -1 )
  +                break; // not found.
  +            
  +            bodyStartIndex += 5;
  +            char c = lcText.charAt(bodyStartIndex);
  +            if( c=='>' ){
  +                break;
  +            }
  +            
  +            if( c!=' ' && c!='\t' )
  +                continue;
  +            
  +            bodyStartIndex = lcText.indexOf('>', bodyStartIndex);
  +        }
  +        bodyStartIndex++;
  +        
  +        int bodyEndIndex = lcText.lastIndexOf("</body>")-1;
  +        
  +        if( bodyStartIndex<0 || bodyEndIndex<0
  +           || bodyStartIndex > bodyEndIndex
  +           || bodyStartIndex>=textLength || bodyEndIndex>=textLength ){
  +            return html.trim();
  +        }
  +        
  +        return html.substring(bodyStartIndex, bodyEndIndex+1).trim();
  +	}
  +	
  +	private void encodeEndNormalMode(FacesContext context, HtmlEditor editor) throws IOException {
  +		String clientId = editor.getClientId(context);
           String formId;
           {
               UIComponent tmpComponent = editor.getParent();
  @@ -1146,7 +1225,25 @@
   
       public void decode(FacesContext facesContext, UIComponent uiComponent) {
           RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlEditor.class);
  -        HtmlRendererUtils.decodeUIInput(facesContext, uiComponent);
  +		HtmlEditor editor = (HtmlEditor) uiComponent;
  +
  +		Map paramMap = facesContext.getExternalContext()
  +	            .getRequestParameterMap();
  +	    String clientId = uiComponent.getClientId(facesContext);
  +	    if (paramMap.containsKey(clientId)) {
  +	        //request parameter found, set submittedValue
  +			String submitedText = (String)paramMap.get(clientId);
  +			String htmlText = ! useFallback(editor) ?
  +							submitedText :
  +							HTMLEncoder.encode(submitedText, true, true);
  +	        ((EditableValueHolder) uiComponent).setSubmittedValue( htmlText );
  +	    } else {
  +	        //request parameter not found, nothing to decode - set submitted value to empty
  +	        //if the component has not been disabled
  +	        if(!HtmlRendererUtils.isDisabledOrReadOnly(editor)) {
  +	            ((EditableValueHolder) uiComponent).setSubmittedValue( RendererUtils.EMPTY_STRING );
  +	        }
  +	    }
       }
       
       public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException {
  
  
  
  1.19      +24 -4     incubator-myfaces/src/components/org/apache/myfaces/custom/htmlEditor/HtmlEditor.java
  
  Index: HtmlEditor.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/htmlEditor/HtmlEditor.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- HtmlEditor.java	9 Mar 2005 04:07:22 -0000	1.18
  +++ HtmlEditor.java	15 Mar 2005 05:24:03 -0000	1.19
  @@ -30,6 +30,9 @@
    * @author Sylvain Vieujot (latest modification by $Author$)
    * @version $Revision$ $Date$
    * $Log$
  + * Revision 1.19  2005/03/15 05:24:03  svieujot
  + * Add a fallback textarea mode to the htmlEditor.
  + *
    * Revision 1.18  2005/03/09 04:07:22  svieujot
    * htmlEditor : Kupu 1.2rc2 update
    *
  @@ -94,6 +97,7 @@
       private String _style;
       private String _styleClass;
       
  +	private String _fallback;
       private String _type;
       
       private Boolean _allowEditSource;
  @@ -125,7 +129,11 @@
           
           values[1] = display;
           
  -        values[2] = _type;
  +		String[] types = new String[2];
  +		types[0] = _fallback;
  +		types[1] = _type;
  +		
  +        values[2] = types;
           
           Boolean toolBarButtons[] = new Boolean[3];
           toolBarButtons[0] = _allowEditSource;
  @@ -155,7 +163,9 @@
           _style = display[0];
           _styleClass = display[1];
           
  -        _type = (String) values[2];
  +		String[] types = (String[]) values[2];
  +		_fallback = types[0];
  +        _type = types[1];
           
           Boolean[] toolBarButtons = (Boolean[]) values[3];
           _allowEditSource = toolBarButtons[0];
  @@ -190,6 +200,16 @@
       public void setStyleClass(String styleClass){
      		this._styleClass = styleClass;
       }
  +	
  +    public String getFallback(){
  +        if (_fallback != null)
  +            return _fallback;
  +        ValueBinding vb = getValueBinding("fallback");
  +        return vb != null ? vb.getValue(getFacesContext()).toString() : "false";
  +    }
  +    public void setFallback(String _fallback){
  +        this._fallback = _fallback;
  +    }
       
       public String getType(){
           if (_type != null)
  @@ -331,7 +351,7 @@
           
           if( !isHtmlDocument(text) )
               return text.trim();
  -        
  +     
           // Extract the fragment from the document.
           String lcText = text.toLowerCase();
           int textLength = lcText.length();
  
  
  
  1.14      +13 -3     incubator-myfaces/src/components/org/apache/myfaces/custom/htmlEditor/HtmlEditorTag.java
  
  Index: HtmlEditorTag.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/htmlEditor/HtmlEditorTag.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- HtmlEditorTag.java	9 Mar 2005 04:07:22 -0000	1.13
  +++ HtmlEditorTag.java	15 Mar 2005 05:24:03 -0000	1.14
  @@ -24,6 +24,9 @@
    * @author Sylvain Vieujot (latest modification by $Author$)
    * @version $Revision$ $Date$
    * $Log$
  + * Revision 1.14  2005/03/15 05:24:03  svieujot
  + * Add a fallback textarea mode to the htmlEditor.
  + *
    * Revision 1.13  2005/03/09 04:07:22  svieujot
    * htmlEditor : Kupu 1.2rc2 update
    *
  @@ -67,7 +70,8 @@
       
       private String style;
       private String styleClass;
  -    
  +
  +    private String fallback;
       private String type;
       
       private String allowEditSource;
  @@ -88,6 +92,7 @@
           super.release();
           style=null;
           styleClass=null;
  +		fallback=null;
           type=null;
           allowEditSource=null;
           allowExternalLinks=null;
  @@ -108,6 +113,7 @@
           setStringProperty(component, "style", style);
           setStringProperty(component, "styleClass", styleClass);
           
  +		setStringProperty(component, "fallback", fallback);
           setBooleanProperty(component, "type", type);
           
           setBooleanProperty(component, "allowEditSource", allowEditSource);
  @@ -140,6 +146,10 @@
       public void setStyleClass(String styleClass){
           this.styleClass = styleClass;
       }
  +	
  +    public void setFallback(String fallback){
  +        this.fallback = fallback;
  +    }
   
       public void setType(String type){
           this.type = type;
  
  
  
  1.178     +10 -0     incubator-myfaces/tlds/myfaces_ext.tld
  
  Index: myfaces_ext.tld
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/tlds/myfaces_ext.tld,v
  retrieving revision 1.177
  retrieving revision 1.178
  diff -u -r1.177 -r1.178
  --- myfaces_ext.tld	15 Mar 2005 00:35:04 -0000	1.177
  +++ myfaces_ext.tld	15 Mar 2005 05:24:03 -0000	1.178
  @@ -944,6 +944,16 @@
               <rtexprvalue>false</rtexprvalue>
           </attribute>
           <attribute>
  +	        <name>fallback</name>
  +            <required>false</required>
  +            <rtexprvalue>false</rtexprvalue>
  +            <description>
  +            	Use a text area instead of the javascript HTML editor.
  +            	Default is false.
  +            	Use with caution.
  +            </description>
  +        </attribute>
  +        <attribute>
   	        <name>type</name>
               <required>false</required>
               <rtexprvalue>false</rtexprvalue>
  
  
  
  1.179     +1 -0      incubator-myfaces/doc/release-notes.txt
  
  Index: release-notes.txt
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/doc/release-notes.txt,v
  retrieving revision 1.178
  retrieving revision 1.179
  diff -u -r1.178 -r1.179
  --- release-notes.txt	15 Mar 2005 00:34:18 -0000	1.178
  +++ release-notes.txt	15 Mar 2005 05:24:03 -0000	1.179
  @@ -38,6 +38,7 @@
   * closed MYFACES-126 in Jira
   * closed MYFACES-128 in Jira
   * closed MYFACES-132 in Jira
  +* add a fallback textarea mode to the htmlEditor
   ----------------------------------------------------------------------
   Changes in Release 1.0.8 beta_RC2
   * added a new RenderKit, that renders WML. Thanks to Jiri Zaloudek for his contribution
  
  
  
  1.28      +5 -2      incubator-myfaces/src/share/org/apache/myfaces/renderkit/html/HtmlRendererUtils.java
  
  Index: HtmlRendererUtils.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/share/org/apache/myfaces/renderkit/html/HtmlRendererUtils.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- HtmlRendererUtils.java	24 Jan 2005 15:43:13 -0000	1.27
  +++ HtmlRendererUtils.java	15 Mar 2005 05:24:03 -0000	1.28
  @@ -48,6 +48,9 @@
    * @author Manfred Geiler (latest modification by $Author$)
    * @version $Revision$ $Date$
    * $Log$
  + * Revision 1.28  2005/03/15 05:24:03  svieujot
  + * Add a fallback textarea mode to the htmlEditor.
  + *
    * Revision 1.27  2005/01/24 15:43:13  svieujot
    * Adjust comments.
    *
  @@ -196,7 +199,7 @@
           }
       }
   
  -    private static boolean isDisabledOrReadOnly(UIComponent component)
  +    public static boolean isDisabledOrReadOnly(UIComponent component)
       {
           return isTrue(component.getAttributes().get("disabled")) ||
                       isTrue(component.getAttributes().get("readOnly"));