1 23 package org.infoglue.deliver.taglib.common; 24 25 import javax.servlet.jsp.JspException ; 26 27 import org.infoglue.deliver.taglib.AbstractTag; 28 29 32 33 public class CropTextTag extends AbstractTag 34 { 35 38 private static final long serialVersionUID = 8603406098980150888L; 39 40 43 private String text; 44 45 48 private int startIndex = 0; 49 50 53 private int maxLength = -1; 54 55 58 private String suffix = "..."; 59 60 63 private boolean adjustForEntities = true; 64 65 68 public CropTextTag() 69 { 70 super(); 71 } 72 73 79 public int doEndTag() throws JspException 80 { 81 if(adjustForEntities) 82 { 83 int startEntity = text.indexOf("&"); 84 while(startEntity > -1 && startEntity < maxLength) 85 { 86 int endEntity = text.indexOf(";", startEntity); 87 if(endEntity != -1) 88 { 89 maxLength = maxLength + (endEntity - startEntity); 90 } 91 if(endEntity != -1) 92 startEntity = text.indexOf("&", endEntity); 93 else 94 startEntity = text.indexOf("&", startEntity + 1); 95 } 96 } 97 98 String modifiedText = text; 99 if(maxLength > -1) 100 { 101 if(text.length() > (startIndex + maxLength)) 102 modifiedText = text.substring(startIndex, maxLength); 103 else 104 modifiedText = text.substring(startIndex); 105 } 106 else 107 { 108 modifiedText = text.substring(startIndex); 109 } 110 111 modifiedText += suffix; 112 113 setResultAttribute(modifiedText); 114 115 return EVAL_PAGE; 116 } 117 118 public void setMaxLength(String maxLength) throws JspException 119 { 120 this.maxLength = evaluateInteger("cropText", "maxLength", maxLength).intValue(); 121 } 122 123 public void setStartIndex(String startIndex) throws JspException 124 { 125 this.startIndex = evaluateInteger("cropText", "startIndex", startIndex).intValue(); 126 } 127 128 public void setSuffix(String suffix) throws JspException 129 { 130 this.suffix = evaluateString("cropText", "suffix", suffix); 131 } 132 133 public void setText(String text) throws JspException 134 { 135 this.text = evaluateString("cropText", "text", text); 136 } 137 138 public void setAdjustForEntities(boolean adjustForEntities) 139 { 140 this.adjustForEntities = adjustForEntities; 141 } 142 } 143 | Popular Tags |