Author: mickw
Date: 2006-05-12 12:41:38 +0200 (Fri, 12 May 2006)
New Revision: 2897

Added:
   
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/ChopStringDirective.java
Log:
should have been added

Added: 
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/ChopStringDirective.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/ChopStringDirective.java
                            (rev 0)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/ChopStringDirective.java
    2006-05-12 10:41:38 UTC (rev 2897)
@@ -0,0 +1,93 @@
+package no.schibstedsok.front.searchportal.view.velocity;
+
+import java.io.IOException;
+
+import java.io.Writer;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.directive.Directive;
+import org.apache.velocity.runtime.parser.Token;
+import org.apache.velocity.runtime.parser.node.Node;
+
+/**
+ *
+ * A velocity directive to chop a string
+ *
+ * <code>
+ * #chopString('this string is being chopped!' 20)
+ * returns the string: "this string is.."
+ * </code>
+ *
+ *
+ * @author thomas
+ */
+public final class ChopStringDirective extends Directive {
+
+    private static transient Log log = 
LogFactory.getLog(ChopStringDirective.class);
+
+    private static final String NAME = "chopString";
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public String getName() {
+        return NAME;
+    }
+
+   /**
+     * [EMAIL PROTECTED]
+     */
+    public int getType() {
+        return LINE;
+    }
+
+   /**
+     * [EMAIL PROTECTED]
+     */
+    public boolean render(final InternalContextAdapter context, final Writer 
writer, final Node node) throws IOException, ResourceNotFoundException, 
ParseErrorException, MethodInvocationException {
+        if (node.jjtGetNumChildren() != 2) {
+            rsvc.error("#" + getName() + " - wrong number of arguments");
+            return false;
+        }
+
+        final String s = node.jjtGetChild(0).value(context).toString();
+        final int length = 
Integer.parseInt(node.jjtGetChild(1).value(context).toString());
+
+        String choppedString = "";
+        if (s.length() <= length)
+            choppedString = s;
+        else {
+            final String sub = s.substring(0, length);
+            final String lastChar = Character.toString(sub.charAt(sub.length() 
- 1));
+            if (lastChar.equals("."))
+                choppedString = sub.substring(0, length) + "..";
+            else if (lastChar.equals(" "))
+                choppedString = sub.substring(0, length) + " ...";
+            else {
+               final int lastSpace = sub.lastIndexOf(" ");
+
+               if (lastSpace >= 0) {
+                   choppedString = sub.substring(0, sub.lastIndexOf(" ")) + " 
...";
+               } else {
+                   choppedString = sub.substring(0, length) + "...";
+               }
+           }
+        }
+
+        writer.write(choppedString);
+
+        final Token lastToken = node.getLastToken();
+
+        if (lastToken.image.endsWith("\n")) {
+            writer.write("\n");
+        }
+
+        return true;
+
+    }
+}

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to