package com.kiasoft.util.tools; /* ------------------------------------------------------------------ * Kiasoft, Inc. * http://kiasoft.com * * Copyright (c) 2004-2006 Kiasoft, Inc. All Rights Reserved. * ------------------------------------------------------------------ */ import java.util.StringTokenizer; import org.apache.velocity.context.Context; import org.apache.regexp.RE; import org.apache.turbine.services.pull.ApplicationTool; import org.apache.turbine.util.RunData; import org.apache.turbine.util.Log; import org.apache.turbine.util.pool.Recyclable; /** * We are just extending the built in flux tool * */ public class JavascriptTool implements Recyclable, ApplicationTool { /** The object containing request specific data */ private RunData data; public static String doubleQuote = "\""; public static String escapedDoubleQuote = "\\\""; public static String singleQuote = "'"; public static String escapedSingleQuote = "\\'"; public void init(Object data) { this.data = (RunData)data; } /** * nulls out the issue and user objects */ public void refresh() { // do not need since it is a request tool } /** * Constructor does initialization stuff */ public JavascriptTool() { Log.info("Initializing JavascriptTool"); } /** * Fix line returns so they work in javascript */ public String fixBreaks(String fixme) { String output = ""; StringTokenizer st = new StringTokenizer(fixme, "\r\n" ); while ( st.hasMoreTokens() ) { output += st.nextToken() + "\\n"; } return output; } /** * Escape quotation marks so they work in javascript fields */ public String escapeQuotes(String fixme) { if ( fixme != null ) { fixme = fixme.trim(); try { // first replace double quotes with single quotes RE r = new RE(doubleQuote); fixme = r.subst( fixme, escapedDoubleQuote, r.REPLACE_ALL ); // now escape all the single quotes r = new RE(singleQuote); fixme = r.subst( fixme, escapedSingleQuote, r.REPLACE_ALL ); return fixme; } catch ( Exception e ) { Log.error("Could not parse string [" + fixme + "] for escaping quotes: " + e.toString() ); return ""; } } else { return ""; } } // ****************** Recyclable implementation ************************ private boolean disposed; /** * Recycles the object for a new client. Recycle methods with * parameters must be added to implementing object and they will be * automatically called by pool implementations when the object is * taken from the pool for a new client. The parameters must * correspond to the parameters of the constructors of the object. * For new objects, constructors can call their corresponding recycle * methods whenever applicable. * The recycle methods must call their super. */ public void recycle() { disposed = false; } /** * Disposes the object after use. The method is called * when the object is returned to its pool. * The dispose method must call its super. */ public void dispose() { data = null; disposed = true; } /** * Checks whether the recyclable has been disposed. * @return true, if the recyclable is disposed. */ public boolean isDisposed() { return disposed; } }