1 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 96 public class InputHtml extends HtmlInputText { 97 public static final String COMPONENT_TYPE = "org.apache.myfaces.InputHtml"; 98 99 public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.InputHtml"; 100 101 private static final Log log = LogFactory.getLog(HtmlInputText.class); 102 103 private String _style; 104 private String _styleClass; 105 106 private String _fallback; 107 private String _type; 108 109 private Boolean _allowEditSource; 110 private Boolean _allowExternalLinks; 111 private Boolean _addKupuLogo; 112 113 private Boolean _showAllToolBoxes; 114 private Boolean _showPropertiesToolBox; 115 private Boolean _showLinksToolBox; 116 private Boolean _showImagesToolBox; 117 private Boolean _showTablesToolBox; 118 private Boolean _showCleanupExpressionsToolBox; 119 private Boolean _showDebugToolBox; 120 121 public InputHtml() { 122 setRendererType(DEFAULT_RENDERER_TYPE); 123 } 124 125 public String getFamily() { 126 return COMPONENT_FAMILY; 127 } 128 129 public Object saveState(FacesContext context) { 130 Object values[] = new Object [5]; 131 values[0] = super.saveState(context); 132 133 String [] display = new String [2]; 134 display[0] = _style; 135 display[1] = _styleClass; 136 137 values[1] = display; 138 139 String [] types = new String [2]; 140 types[0] = _fallback; 141 types[1] = _type; 142 143 values[2] = types; 144 145 Boolean toolBarButtons[] = new Boolean [3]; 146 toolBarButtons[0] = _allowEditSource; 147 toolBarButtons[1] = _allowExternalLinks; 148 toolBarButtons[2] = _addKupuLogo; 149 150 values[3] = toolBarButtons; 151 152 Boolean toolBoxes[] = new Boolean [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 state) { 167 Object values[] = (Object []) state; 168 super.restoreState(context, values[0]); 169 170 String [] display = (String []) values[1]; 171 _style = display[0]; 172 _styleClass = display[1]; 173 174 String [] types = (String []) values[2]; 175 _fallback = types[0]; 176 _type = types[1]; 177 178 Boolean [] toolBarButtons = (Boolean []) values[3]; 179 _allowEditSource = toolBarButtons[0]; 180 _allowExternalLinks = toolBarButtons[1]; 181 _addKupuLogo = toolBarButtons[2]; 182 183 Boolean [] toolBoxes = (Boolean []) 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 getStyle(){ 194 if (_style != null) 195 return _style; 196 ValueBinding vb = getValueBinding("style"); 197 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 198 } 199 public void setStyle(String style){ 200 this._style = style; 201 } 202 203 public String getStyleClass(){ 204 if (_styleClass != null) 205 return _styleClass; 206 ValueBinding vb = getValueBinding("styleClass"); 207 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 208 } 209 public void setStyleClass(String styleClass){ 210 this._styleClass = styleClass; 211 } 212 213 public String 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 _fallback){ 220 this._fallback = _fallback; 221 } 222 223 public String getType(){ 224 if (_type != null) 225 return _type; 226 ValueBinding vb = getValueBinding("type"); 227 return vb != null ? (String )vb.getValue(getFacesContext()) : "fragment"; 228 } 229 public void setType(String _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 )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 )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 )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 )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 )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 )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 )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 )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 )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 )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 getValueAsHtmlDocument(FacesContext context){ 365 String 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 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 getValueFromDocument(String 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 String 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; 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 |