Author: ajamtli
Date: 2006-05-19 12:37:49 +0200 (Fri, 19 May 2006)
New Revision: 2941

Added:
   
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/RemovePrefixDirective.java
Log:
Velocity directive to strip away the given prefix from a string.

Added: 
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/RemovePrefixDirective.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/RemovePrefixDirective.java
                          (rev 0)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/view/velocity/RemovePrefixDirective.java
  2006-05-19 10:37:49 UTC (rev 2941)
@@ -0,0 +1,78 @@
+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 remove a prefix if it exists.
+ *
+ * <code>
+ * #removePrefix('this is the string to be checked', 'prefix to be removed')
+ * </code>
+ *
+ *
+ * @author ajamtli
+ */
+public final class RemovePrefixDirective extends Directive {
+    
+    /** Logger. */
+    private static transient Log log = 
LogFactory.getLog(RemovePrefixDirective.class);
+
+    /** Name of directive. */
+    private static final String NAME = "removePrefix";
+
+    /**
+     * [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 String prefix = node.jjtGetChild(1).value(context).toString();
+       
+        String returnString = s;
+        if (s.startsWith(prefix)) {
+            returnString = s.substring(prefix.length());
+            returnString = returnString.trim();
+        }
+        
+        writer.write(returnString);
+
+        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