Author: gmazza
Date: Sun Apr  6 03:25:28 2014
New Revision: 1585239

URL: http://svn.apache.org/r1585239
Log:
Better consolidation of Jakarta taglib code that Roller is sitll using.

Removed:
    
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/StringW.java
    roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/XmlW.java
Modified:
    
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/StringTagSupport.java
    
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/TruncateNicelyTag.java
    
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/WordWrapTag.java

Modified: 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/StringTagSupport.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/StringTagSupport.java?rev=1585239&r1=1585238&r2=1585239&view=diff
==============================================================================
--- 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/StringTagSupport.java
 (original)
+++ 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/StringTagSupport.java
 Sun Apr  6 03:25:28 2014
@@ -33,13 +33,9 @@ import javax.servlet.jsp.tagext.BodyTagS
  *             out to the html page.
  * </dd>
  * </dl> 
- * 
- * @author [email protected]
  */
 public abstract class StringTagSupport extends BodyTagSupport {
 
-
-
     /**
      * PageContext attribute to store the result in.
      */
@@ -73,15 +69,6 @@ public abstract class StringTagSupport e
      */
     public int doEndTag() throws JspException {
 
-       /*
-        *  Although most of the tags that extends must have a body, some 
don't, like RandomStringTag
-        *  So I'm removing the code below...
-     */
-                
-//       if( (bodyContent == null) && (!canBeEmpty()) ) {
- //           return EVAL_PAGE;
- //      }
- 
         String text = "";
         if(bodyContent != null) {
             StringWriter body = new StringWriter();
@@ -101,15 +88,7 @@ public abstract class StringTagSupport e
           // then, try to transform it
           text = changeString(text);
         
-          // TODO: RandomString is not working if body is set...
-          /*
-            System.err.println("...."+text+"....");
-            if ( text  != null  ) {
-               System.out.println( "length = " + text.length());
-            }
-          */
-        
-          if(this.var == null) {
+          if (this.var == null) {
             JspWriter writer = pageContext.getOut();
             try {
               writer.print(text);

Modified: 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/TruncateNicelyTag.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/TruncateNicelyTag.java?rev=1585239&r1=1585238&r2=1585239&view=diff
==============================================================================
--- 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/TruncateNicelyTag.java
 (original)
+++ 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/TruncateNicelyTag.java
 Sun Apr  6 03:25:28 2014
@@ -19,6 +19,8 @@ import javax.servlet.jsp.JspException;
 import org.apache.commons.lang3.math.NumberUtils;
 
 /**
+ * Code grabbed from retired Jakarta Tablibs project: 
http://jakarta.apache.org/taglibs/string/
+ *
  * A more intelligent substring.  It attempts to cut off a string after
  * a space, following predefined or user-supplied lower and upper limits,
  * useful for making short descriptions from long text.  Can also strip
@@ -38,15 +40,14 @@ import org.apache.commons.lang3.math.Num
  *             String to append to end of truncated string.
  * </dd>
  * </dl>
- * 
+ *
  * @author [email protected]
  */
 public class TruncateNicelyTag extends StringTagSupport {
 
-       private String stripMarkup;
     private String lower;
     private String upper;
-       private String appendToEnd;
+    private String appendToEnd;
 
     public TruncateNicelyTag() {
         super();
@@ -54,7 +55,8 @@ public class TruncateNicelyTag extends S
 
     /**
      * Get the lower property
-        * @return String lower property
+     *
+     * @return String lower property
      */
     public String getLower() {
         return this.lower;
@@ -62,15 +64,17 @@ public class TruncateNicelyTag extends S
 
     /**
      * Set the upper property
+     *
      * @param lower String property
      */
-    public void setLower(String l) {
-        this.lower = l;
+    public void setLower(String lower) {
+        this.lower = lower;
     }
 
     /**
      * Get the upper property
-        * @return String upper property
+     *
+     * @return String upper property
      */
     public String getUpper() {
         return this.upper;
@@ -78,34 +82,114 @@ public class TruncateNicelyTag extends S
 
     /**
      * Set the upper property
+     *
      * @param upper String property
      */
-    public void setUpper(String u) {
-        this.upper = u;
+    public void setUpper(String upper) {
+        this.upper = upper;
     }
 
-       public String getAppendToEnd() {
-               return this.appendToEnd;
-       }
+    public String getAppendToEnd() {
+        return this.appendToEnd;
+    }
 
-       public void setAppendToEnd(String s) {
-               this.appendToEnd = s;
-       }
+    public void setAppendToEnd(String s) {
+        this.appendToEnd = s;
+    }
 
     public String changeString(String text) throws JspException {
-                                       
-               int l = NumberUtils.toInt(lower);
-               int u = NumberUtils.toInt(upper);
-       
-               return StringW.truncateNicely(text, l, u, this.appendToEnd);
+
+        int l = NumberUtils.toInt(lower);
+        int u = NumberUtils.toInt(upper);
+
+        return truncateNicely(text, l, u, this.appendToEnd);
     }
 
     public void initAttributes() {
 
-               this.lower = "10";
-               this.upper = "-1";
-               this.appendToEnd = "...";
+        this.lower = "10";
+        this.upper = "-1";
+        this.appendToEnd = "...";
+
+    }
 
+    /**
+     * Truncates a string nicely. It will search for the first space
+     * after the lower limit and truncate the string there.  It will
+     * also append any string passed as a parameter to the end of the
+     * string.  The hard limit can be specified to forcibily truncate a
+     * string (in the case of an extremely long word or such).  All
+     * HTML/XML markup will be stripped from the string prior to
+     * processing for truncation.
+     *
+     * @param str         String the string to be truncated.
+     * @param lower       int value of the lower limit.
+     * @param upper       int value of the upper limit, -1 if no limit is
+     *                    desired. If the uppper limit is lower than the
+     *                    lower limit, it will be adjusted to be same as
+     *                    the lower limit.
+     * @param appendToEnd String to be appended to the end of the
+     *                    truncated string.
+     *                    This is appended ONLY if the string was indeed
+     *                    truncated. The append is does not count towards
+     *                    any lower/upper limits.
+     */
+    private static String truncateNicely(String str, int lower, int upper, 
String appendToEnd)
+    {
+        // strip markup from the string
+        str = removeXml(str);
+
+        // quickly adjust the upper if it is set lower than 'lower'
+        if(upper < lower) {
+            upper = lower;
+        }
+
+        // now determine if the string fits within the upper limit
+        // if it does, go straight to return, do not pass 'go' and collect $200
+        if(str.length() > upper) {
+            // the magic location int
+            int loc;
+
+            // first we determine where the next space appears after lower
+            loc = str.lastIndexOf(' ', upper);
+
+            // now we'll see if the location is greater than the lower limit
+            if(loc >= lower) {
+                // yes it was, so we'll cut it off here
+                str = str.substring(0, loc);
+            } else {
+                // no it wasn't, so we'll cut it off at the upper limit
+                str = str.substring(0, upper);
+            }
+
+            // the string was truncated, so we append the appendToEnd String
+            str = str + appendToEnd;
+        }
+
+        return str;
+    }
+
+    /**
+     * Remove any xml tags from a String.
+     */
+    private static String removeXml(String str) {
+        int sz = str.length();
+        StringBuilder buffer = new StringBuilder(sz);
+        boolean inTag = false;
+        for (int i=0; i<sz; i++) {
+            char ch = str.charAt(i);
+            if (ch == '<') {
+                inTag = true;
+            } else
+            if (ch == '>') {
+                inTag = false;
+                continue;
+            }
+            if (!inTag) {
+                buffer.append(ch);
+            }
+        }
+        return buffer.toString();
     }
 
 }

Modified: 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/WordWrapTag.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/WordWrapTag.java?rev=1585239&r1=1585238&r2=1585239&view=diff
==============================================================================
--- 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/WordWrapTag.java
 (original)
+++ 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/tags/WordWrapTag.java
 Sun Apr  6 03:25:28 2014
@@ -16,13 +16,17 @@
 package org.apache.roller.weblogger.ui.tags;
 
 import javax.servlet.jsp.JspException;
+
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.math.NumberUtils;
 
 /**
- * Word-wrap a String. This involves formatting a long 
+ * Code grabbed from retired Jakarta Tablibs project: 
http://jakarta.apache.org/taglibs/string/
+ *
+ * Word-wrap a String. This involves formatting a long
  * String to fit within a certain character width of page.
- * A delimiter may be passed in to put at the end of each 
- * line and a splitting character can be specified for when 
+ * A delimiter may be passed in to put at the end of each
+ * line and a splitting character can be specified for when
  * a word has to be cut in half.
  *
  * <dl>
@@ -43,8 +47,7 @@ import org.apache.commons.lang3.math.Num
  *             Default is true.
  * </dd>
  * </dl>
- * 
- * @author [email protected]
+ *
  */
 public class WordWrapTag extends StringTagSupport {
 
@@ -132,18 +135,111 @@ public class WordWrapTag extends StringT
     }
 
     public String changeString(String text) throws JspException {
-        return StringW.wordWrap(text, NumberUtils.toInt(width), delimiter, 
split, delimiterInside );
+        return wordWrap(text, NumberUtils.toInt(width), delimiter, split, 
delimiterInside);
     }
 
     public void initAttributes() {
-
         this.width = "80";
-
         this.delimiter = "\n";
-
         this.split = "-";
-
         this.delimiterInside = true;
     }
 
+    /**
+     * Word-wrap a string.
+     *
+     * @param str         String to word-wrap
+     * @param width       int to wrap at
+     * @param delim       String to use to separate lines
+     * @param split       String to use to split a word greater than width long
+     * @param delimInside wheter or not delim should be included in chunk 
before length reaches width.
+     * @return String that has been word wrapped
+     */
+    public static String wordWrap(String str, int width, String delim,
+                                  String split, boolean delimInside) {
+        int sz = str.length();
+
+        /// shift width up one. mainly as it makes the logic easier
+        width++;
+
+        // our best guess as to an initial size
+        StringBuilder buffer = new StringBuilder(sz / width * delim.length() + 
sz);
+
+        // every line might include a delim on the end
+        if (delimInside) {
+            width = width - delim.length();
+        } else {
+            width--;
+        }
+
+        int idx;
+        String substr;
+
+        // beware: i is rolled-back inside the loop
+        for (int i = 0; i < sz; i += width) {
+
+            // on the last line
+            if (i > sz - width) {
+                buffer.append(str.substring(i));
+                break;
+            }
+
+            // the current line
+            substr = str.substring(i, i + width);
+
+            // is the delim already on the line
+            idx = substr.indexOf(delim);
+            if (idx != -1) {
+                buffer.append(substr.substring(0, idx));
+                buffer.append(delim);
+                i -= width - idx - delim.length();
+
+                // Erase a space after a delim. Is this too obscure?
+                if (substr.length() > idx + 1 && (substr.charAt(idx + 1) != 
'\n') && Character.isWhitespace(substr.charAt(idx + 1))) {
+                    i++;
+                }
+                continue;
+            }
+
+            idx = -1;
+
+            // figure out where the last space is
+            char[] chrs = substr.toCharArray();
+            for (int j = width; j > 0; j--) {
+                if (Character.isWhitespace(chrs[j - 1])) {
+                    idx = j;
+                    break;
+                }
+            }
+
+            // idx is the last whitespace on the line.
+            if (idx == -1) {
+                for (int j = width; j > 0; j--) {
+                    if (chrs[j - 1] == '-') {
+                        idx = j;
+                        break;
+                    }
+                }
+                if (idx == -1) {
+                    buffer.append(substr);
+                    buffer.append(delim);
+                } else {
+                    if (idx != width) {
+                        idx++;
+                    }
+                    buffer.append(substr.substring(0, idx));
+                    buffer.append(delim);
+                    i -= width - idx;
+                }
+            } else {
+                // insert spaces
+                buffer.append(substr.substring(0, idx));
+                buffer.append(StringUtils.repeat(" ", width - idx));
+                buffer.append(delim);
+                i -= width - idx;
+            }
+        }
+
+        return buffer.toString();
+    }
 }


Reply via email to