📄 inputhtmlrenderer.java
字号:
/* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.myfaces.custom.inputHtml;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;import javax.faces.context.ResponseWriter;import javax.faces.convert.ConverterException;import org.apache.myfaces.component.UserRoleUtils;import org.apache.myfaces.component.html.util.AddResource;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: svieujot $) * @version $Revision: 1.1 $ $Date: 2005/03/26 20:31:37 $ * $Log: InputHtmlRenderer.java,v $ * Revision 1.1 2005/03/26 20:31:37 svieujot * Rename x:htmlEditor to x:inputHtml. * * Revision 1.26 2005/03/15 21:24:41 svieujot * Style handling improvements. * * 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 * * Revision 1.23 2005/03/09 04:07:22 svieujot * htmlEditor : Kupu 1.2rc2 update * * Revision 1.22 2005/02/08 14:24:45 svieujot * Temporarily hide unimplemented functionalities (internal images library & internal links library). * * Revision 1.21 2005/02/07 01:40:42 svieujot * style attribute fix. * * Revision 1.20 2005/02/06 19:45:32 svieujot * Add allowExternalLinks attribute. * * Revision 1.19 2005/02/05 23:07:45 svieujot * x:htmlEditor Add full page mode (zoom) & bugfixes * * Revision 1.18 2005/02/05 18:51:21 svieujot * x:htmlEditor : Upgrade to Kupu 1.2rc1, remove formularMode (too experimental), bugfixes. * * Revision 1.17 2005/01/02 20:39:16 svieujot * HtmlEditor can now process HTML documents and HTML fragments. * * Revision 1.16 2004/12/27 04:11:11 mmarinschek * Data Table stores the state of facets of children; script tag is rendered with type attribute instead of language attribute, popup works better as a column in a data table * * Revision 1.15 2004/12/10 02:16:26 svieujot * Start implementing UserRoleAware. * * Revision 1.14 2004/12/09 05:19:12 svieujot * Bugfix for submitted values that were not taken care of. * * Revision 1.13 2004/12/08 04:36:27 svieujot * Cancel last *source attributes, and make style and styleClass more modular. * * Revision 1.12 2004/12/08 04:13:56 svieujot * Add styleSource and styleClassSource for the htmlEditor source window. * * Revision 1.11 2004/12/06 04:26:07 svieujot * Make HtmlEditor UserRoleAware. * * Revision 1.10 2004/12/06 01:03:42 svieujot * Bugfix : getter now use boolean instead of Boolean, and setters created. * * Revision 1.9 2004/12/04 03:50:44 svieujot * Remove bug for IE * * Revision 1.8 2004/12/04 03:26:28 svieujot * Various bug fixes * * Revision 1.7 2004/12/04 02:09:42 svieujot * Several small fixes. * * Revision 1.6 2004/12/04 00:40:25 svieujot * htmlEditor : add style and styleClass attributes. * * Revision 1.5 2004/12/04 00:20:00 svieujot * htmlEditor : Add a formular mode, and more sensible defaults. * * Revision 1.4 2004/12/03 21:59:09 svieujot * Initial set of htmlEditor attributes. * * Revision 1.3 2004/12/03 12:55:30 svieujot * Get ride of the save button / process. * * Revision 1.2 2004/12/03 08:31:28 manolito * writer.append copy'n'paste error? * * Revision 1.1 2004/12/02 22:28:30 svieujot * Add an x:htmlEditor based on the Kupu library. */public class InputHtmlRenderer extends HtmlRenderer { // TODO : Finish Disabled mode. // TODO : Automatic Fallback for non kupu capable browsers (Safari, smartphones, non javascript, ...). // TODO : Make Image & Link Library work. protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) { if( !UserRoleUtils.isEnabledOnUserRole(uiComponent) ) return false; return ((InputHtml)uiComponent).isDisabled(); } private static boolean useFallback(InputHtml editor){ // TODO : Handle fallback="auto" return editor.getFallback().equals("true"); } public void encodeEnd(FacesContext context, UIComponent uiComponent) throws IOException { RendererUtils.checkParamValidity(context, uiComponent, InputHtml.class); InputHtml editor = (InputHtml) uiComponent; if( useFallback(editor) ) encodeEndFallBackMode(context, editor); else encodeEndNormalMode(context, editor); } private void encodeEndFallBackMode(FacesContext context, InputHtml 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, InputHtml editor) throws IOException { String clientId = editor.getClientId(context); String formId; { UIComponent tmpComponent = editor.getParent(); while(!(tmpComponent instanceof UIForm) ){ tmpComponent = tmpComponent.getParent(); } formId = tmpComponent.getClientId(context); } AddResource.addStyleSheet(InputHtmlRenderer.class, "kupustyles.css", context);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -