KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > inputHtml > InputHtml


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.inputHtml;
17
18 import javax.faces.context.FacesContext;
19 import javax.faces.el.ValueBinding;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.myfaces.component.html.ext.HtmlInputText;
24 import org.apache.myfaces.renderkit.RendererUtils;
25
26 /**
27  * HTML Editor using the kupu library.
28  * http://kupu.oscom.org/
29  *
30  * @author Sylvain Vieujot (latest modification by $Author: svieujot $)
31  * @version $Revision: 1.1 $ $Date: 2005/03/26 20:31:37 $
32  * $Log: InputHtml.java,v $
33  * Revision 1.1 2005/03/26 20:31:37 svieujot
34  * Rename x:htmlEditor to x:inputHtml.
35  *
36  * Revision 1.20 2005/03/22 02:13:28 svieujot
37  * htmlEditor : Add a showAllToolBoxes short cut.
38  *
39  * Revision 1.19 2005/03/15 05:24:03 svieujot
40  * Add a fallback textarea mode to the htmlEditor.
41  *
42  * Revision 1.18 2005/03/09 04:07:22 svieujot
43  * htmlEditor : Kupu 1.2rc2 update
44  *
45  * Revision 1.17 2005/02/06 19:45:32 svieujot
46  * Add allowExternalLinks attribute.
47  *
48  * Revision 1.16 2005/02/05 18:51:21 svieujot
49  * x:htmlEditor : Upgrade to Kupu 1.2rc1, remove formularMode (too experimental), bugfixes.
50  *
51  * Revision 1.15 2005/01/03 03:49:31 svieujot
52  * trim returned text
53  *
54  * Revision 1.14 2005/01/02 20:39:16 svieujot
55  * HtmlEditor can now process HTML documents and HTML fragments.
56  *
57  * Revision 1.13 2004/12/24 13:21:50 matzew
58  * organized imports
59  *
60  * Revision 1.12 2004/12/09 05:16:44 svieujot
61  * Simplify by extending html.ext.HtmlInputText instead of extending UIInput and implementing UserRolesAware.
62  *
63  * Revision 1.11 2004/12/08 04:36:27 svieujot
64  * Cancel last *source attributes, and make style and styleClass more modular.
65  *
66  * Revision 1.10 2004/12/08 04:13:56 svieujot
67  * Add styleSource and styleClassSource for the htmlEditor source window.
68  *
69  * Revision 1.9 2004/12/06 04:26:07 svieujot
70  * Make HtmlEditor UserRoleAware.
71  *
72  * Revision 1.8 2004/12/06 03:34:57 svieujot
73  * Typo
74  *
75  * Revision 1.7 2004/12/06 01:03:42 svieujot
76  * Bugfix : getter now use boolean instead of Boolean, and setters created.
77  *
78  * Revision 1.6 2004/12/04 22:25:36 svieujot
79  * *** empty log message ***
80  *
81  * Revision 1.5 2004/12/04 03:26:28 svieujot
82  * Various bug fixes
83  *
84  * Revision 1.4 2004/12/04 00:40:25 svieujot
85  * htmlEditor : add style and styleClass attributes.
86  *
87  * Revision 1.3 2004/12/04 00:20:00 svieujot
88  * htmlEditor : Add a formular mode, and more sensible defaults.
89  *
90  * Revision 1.2 2004/12/03 21:59:09 svieujot
91  * Initial set of htmlEditor attributes.
92  *
93  * Revision 1.1 2004/12/02 22:28:30 svieujot
94  * Add an x:htmlEditor based on the Kupu library.
95  */

96 public class InputHtml extends HtmlInputText {
97     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.InputHtml";
98
99     public static final String JavaDoc DEFAULT_RENDERER_TYPE = "org.apache.myfaces.InputHtml";
100     
101     private static final Log log = LogFactory.getLog(HtmlInputText.class);
102     
103     private String JavaDoc _style;
104     private String JavaDoc _styleClass;
105     
106     private String JavaDoc _fallback;
107     private String JavaDoc _type;
108     
109     private Boolean JavaDoc _allowEditSource;
110     private Boolean JavaDoc _allowExternalLinks;
111     private Boolean JavaDoc _addKupuLogo;
112     
113     private Boolean JavaDoc _showAllToolBoxes;
114     private Boolean JavaDoc _showPropertiesToolBox;
115     private Boolean JavaDoc _showLinksToolBox;
116     private Boolean JavaDoc _showImagesToolBox;
117     private Boolean JavaDoc _showTablesToolBox;
118     private Boolean JavaDoc _showCleanupExpressionsToolBox;
119     private Boolean JavaDoc _showDebugToolBox;
120
121     public InputHtml() {
122         setRendererType(DEFAULT_RENDERER_TYPE);
123     }
124
125     public String JavaDoc getFamily() {
126         return COMPONENT_FAMILY;
127     }
128
129     public Object JavaDoc saveState(FacesContext context) {
130         Object JavaDoc values[] = new Object JavaDoc[5];
131         values[0] = super.saveState(context);
132         
133         String JavaDoc[] display = new String JavaDoc[2];
134         display[0] = _style;
135         display[1] = _styleClass;
136         
137         values[1] = display;
138         
139         String JavaDoc[] types = new String JavaDoc[2];
140         types[0] = _fallback;
141         types[1] = _type;
142         
143         values[2] = types;
144         
145         Boolean JavaDoc toolBarButtons[] = new Boolean JavaDoc[3];
146         toolBarButtons[0] = _allowEditSource;
147         toolBarButtons[1] = _allowExternalLinks;
148         toolBarButtons[2] = _addKupuLogo;
149         
150         values[3] = toolBarButtons;
151         
152         Boolean JavaDoc toolBoxes[] = new Boolean JavaDoc[7];
153         toolBoxes[0] = _showAllToolBoxes;
154         toolBoxes[1] = _showPropertiesToolBox;
155         toolBoxes[2] = _showLinksToolBox;
156         toolBoxes[3] = _showImagesToolBox;
157         toolBoxes[4] = _showTablesToolBox;
158         toolBoxes[5] = _showCleanupExpressionsToolBox;
159         toolBoxes[6] = _showDebugToolBox;
160         
161         values[4] = toolBoxes;
162         
163         return values;
164     }
165
166     public void restoreState(FacesContext context, Object JavaDoc state) {
167         Object JavaDoc values[] = (Object JavaDoc[]) state;
168         super.restoreState(context, values[0]);
169         
170         String JavaDoc[] display = (String JavaDoc[]) values[1];
171         _style = display[0];
172         _styleClass = display[1];
173         
174         String JavaDoc[] types = (String JavaDoc[]) values[2];
175         _fallback = types[0];
176         _type = types[1];
177         
178         Boolean JavaDoc[] toolBarButtons = (Boolean JavaDoc[]) values[3];
179         _allowEditSource = toolBarButtons[0];
180         _allowExternalLinks = toolBarButtons[1];
181         _addKupuLogo = toolBarButtons[2];
182         
183         Boolean JavaDoc[] toolBoxes = (Boolean JavaDoc[]) values[4];
184         _showAllToolBoxes = toolBoxes[0];
185         _showPropertiesToolBox = toolBoxes[1];
186         _showLinksToolBox = toolBoxes[2];
187         _showImagesToolBox = toolBoxes[3];
188         _showTablesToolBox = toolBoxes[4];
189         _showCleanupExpressionsToolBox = toolBoxes[5];
190         _showDebugToolBox = toolBoxes[6];
191     }
192     
193     public String JavaDoc getStyle(){
194         if (_style != null)
195             return _style;
196         ValueBinding vb = getValueBinding("style");
197         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
198     }
199     public void setStyle(String JavaDoc style){
200         this._style = style;
201     }
202     
203     public String JavaDoc getStyleClass(){
204         if (_styleClass != null)
205             return _styleClass;
206         ValueBinding vb = getValueBinding("styleClass");
207         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
208     }
209     public void setStyleClass(String JavaDoc styleClass){
210         this._styleClass = styleClass;
211     }
212     
213     public String JavaDoc getFallback(){
214         if (_fallback != null)
215             return _fallback;
216         ValueBinding vb = getValueBinding("fallback");
217         return vb != null ? vb.getValue(getFacesContext()).toString() : "false";
218     }
219     public void setFallback(String JavaDoc _fallback){
220         this._fallback = _fallback;
221     }
222     
223     public String JavaDoc getType(){
224         if (_type != null)
225             return _type;
226         ValueBinding vb = getValueBinding("type");
227         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : "fragment";
228     }
229     public void setType(String JavaDoc _type){
230         this._type = _type;
231     }
232     public boolean isTypeDocument(){
233         return getType().equals("document");
234     }
235     
236     public boolean isAllowEditSource(){
237         if (_allowEditSource != null)
238             return _allowEditSource.booleanValue();
239         ValueBinding vb = getValueBinding("allowEditSource");
240         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : true;
241     }
242     public void setAllowEditSource(boolean allowEditSource){
243         this._allowEditSource = Boolean.valueOf(allowEditSource);
244     }
245     
246     public boolean isAllowExternalLinks(){
247         if (_allowExternalLinks != null)
248             return _allowExternalLinks.booleanValue();
249         ValueBinding vb = getValueBinding("allowExternalLinks");
250         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : true;
251     }
252     public void setAllowExternalLinks(boolean allowExternalLinks){
253         this._allowExternalLinks = Boolean.valueOf(allowExternalLinks);
254     }
255     
256     public boolean isAddKupuLogo(){
257         if (_addKupuLogo != null)
258             return _addKupuLogo.booleanValue();
259         ValueBinding vb = getValueBinding("addKupuLogo");
260         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : true;
261     }
262     public void setAddKupuLogo(boolean addKupuLogo){
263         this._addKupuLogo = Boolean.valueOf(addKupuLogo);
264     }
265     
266     public boolean isShowAllToolBoxes(){
267         if (_showAllToolBoxes != null)
268             return _showAllToolBoxes.booleanValue();
269         ValueBinding vb = getValueBinding("showAllToolBoxes");
270         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : false;
271     }
272     public void setShowAllToolBoxes(boolean showAllToolBoxes){
273         this._showAllToolBoxes = Boolean.valueOf(showAllToolBoxes);
274     }
275     
276     public boolean isShowPropertiesToolBox(){
277         if( isShowAllToolBoxes() )
278             return true;
279
280         if (_showPropertiesToolBox != null)
281             return _showPropertiesToolBox.booleanValue();
282         ValueBinding vb = getValueBinding("showPropertiesToolBox");
283         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : false;
284     }
285     public void setShowPropertiesToolBox(boolean showPropertiesToolBox){
286         this._showPropertiesToolBox = Boolean.valueOf(showPropertiesToolBox);
287     }
288     
289     public boolean isShowLinksToolBox(){
290         if( isShowAllToolBoxes() )
291             return true;
292
293         if (_showLinksToolBox != null)
294             return _showLinksToolBox.booleanValue();
295         ValueBinding vb = getValueBinding("showLinksToolBox");
296         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : false;
297     }
298     public void setShowLinksToolBox(boolean showLinksToolBox){
299         this._showLinksToolBox = Boolean.valueOf(showLinksToolBox);
300     }
301     
302     public boolean isShowImagesToolBox(){
303         if( isShowAllToolBoxes() )
304             return true;
305
306         if (_showImagesToolBox != null)
307             return _showImagesToolBox.booleanValue();
308         ValueBinding vb = getValueBinding("showImagesToolBox");
309         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : false;
310     }
311     public void setShowImagesToolBox(boolean showImagesToolBox){
312         this._showImagesToolBox = Boolean.valueOf(showImagesToolBox);
313     }
314     
315     public boolean isShowTablesToolBox(){
316         if( isShowAllToolBoxes() )
317             return true;
318
319         if (_showTablesToolBox != null)
320             return _showTablesToolBox.booleanValue();
321         ValueBinding vb = getValueBinding("showTablesToolBox");
322         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : false;
323     }
324     public void setShowTablesToolBox(boolean showTablesToolBox){
325         this._showTablesToolBox = Boolean.valueOf(showTablesToolBox);
326     }
327     
328     public boolean isShowCleanupExpressionsToolBox(){
329         if( isShowAllToolBoxes() )
330             return true;
331
332         if (_showCleanupExpressionsToolBox != null)
333             return _showCleanupExpressionsToolBox.booleanValue();
334         ValueBinding vb = getValueBinding("showCleanupExpressionsToolBox");
335         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : false;
336     }
337     public void setShowCleanupExpressionsToolBox(boolean showCleanupExpressionsToolBox){
338         this._showCleanupExpressionsToolBox = Boolean.valueOf(showCleanupExpressionsToolBox);
339     }
340     
341     public boolean isShowDebugToolBox(){
342         if( isShowAllToolBoxes() )
343             return true;
344
345         if (_showDebugToolBox != null)
346             return _showDebugToolBox.booleanValue();
347         ValueBinding vb = getValueBinding("showDebugToolBox");
348         return vb != null ? ((Boolean JavaDoc)vb.getValue(getFacesContext())).booleanValue() : false;
349     }
350     public void setShowDebugToolBox(boolean showTablesToolBox){
351         this._showDebugToolBox = Boolean.valueOf(showTablesToolBox);
352     }
353     
354     public boolean isShowAnyToolBox(){
355         return isShowAllToolBoxes()
356             || isShowPropertiesToolBox()
357             || isShowLinksToolBox()
358             || isShowImagesToolBox()
359             || isShowTablesToolBox()
360             || isShowCleanupExpressionsToolBox()
361             || isShowDebugToolBox();
362     }
363
364     public String JavaDoc getValueAsHtmlDocument(FacesContext context){
365         String JavaDoc val = RendererUtils.getStringValue(context, this);
366         if( isHtmlDocument( val ) )
367             return val;
368         
369         return "<html><body>"+(val==null ? "" : val)+"</body></html>";
370     }
371     
372     private static boolean isHtmlDocument(String JavaDoc text){
373         if( text == null )
374             return false;
375         
376         if( text.indexOf("<body>")!=-1 || text.indexOf("<body ")!=-1
377             || text.indexOf("<BODY>")!=-1 || text.indexOf("<BODY ")!=-1 )
378             return true;
379         
380         return false;
381     }
382     
383     public String JavaDoc getValueFromDocument(String JavaDoc text){
384         if( text == null )
385             return "";
386         
387         if( isTypeDocument() )
388             return text.trim();
389         
390         if( !isHtmlDocument(text) )
391             return text.trim();
392      
393         // Extract the fragment from the document.
394
String JavaDoc lcText = text.toLowerCase();
395         int textLength = lcText.length();
396         int bodyStartIndex = 0;
397         while(bodyStartIndex < textLength){
398             bodyStartIndex = lcText.indexOf("<body");
399             if( bodyStartIndex == -1 )
400                 break; // not found.
401

402             bodyStartIndex += 5;
403             char c = lcText.charAt(bodyStartIndex);
404             if( c=='>' ){
405                 break;
406             }
407             
408             if( c!=' ' && c!='\t' )
409                 continue;
410             
411             bodyStartIndex = lcText.indexOf('>', bodyStartIndex);
412         }
413         bodyStartIndex++;
414         
415         int bodyEndIndex = lcText.lastIndexOf("</body>")-1;
416         
417         if( bodyStartIndex<0 || bodyEndIndex<0
418            || bodyStartIndex > bodyEndIndex
419            || bodyStartIndex>=textLength || bodyEndIndex>=textLength ){
420             log.warn("Couldn't extract HTML body from :\n"+text);
421             return text.trim();
422         }
423         
424         return text.substring(bodyStartIndex, bodyEndIndex+1).trim();
425     }
426 }
Popular Tags