/* * Copyright (C) 2000 - 2008 TagServlet Ltd * * This file is part of the BlueDragon Java Open Source Project. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package com.naryx.tagfusion.expression.function.string; import com.naryx.tagfusion.cfm.engine.cfArgStructData; import com.naryx.tagfusion.cfm.engine.cfData; import com.naryx.tagfusion.cfm.engine.cfSession; import com.naryx.tagfusion.cfm.engine.cfStringData; import com.naryx.tagfusion.cfm.engine.cfmRunTimeException; import com.naryx.tagfusion.expression.function.functionBase; public class reEscape extends functionBase { private static final long serialVersionUID = 1L; public reEscape() { min = 1; max = 1; setNamedParams( new String[] { "string" } ); } public String[] getParamInfo(){ return new String[]{ "string to escape" }; } public java.util.Map getInfo(){ return makeInfo( "string", "Returns a string that that has been escaped and safe to use in RegEx patterns. The characters we escape are $, {, }, (, ), [, ], ^, ., *, +, ?, &, and \\.", ReturnType.STRING ); } public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { String escapedStr = getNamedStringParam(argStruct, "string", ""); // a single regex to escape all the chars we need to escapedStr = escapedStr.replaceAll( "([\\\\\\?\\+\\$\\{\\}\\(\\)\\[\\]\\^\\.\\*\\&])", "\\\\$1" ); return new cfStringData( escapedStr ); } }