/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.util; import java.util.*; public class StringUtil { /*-------------------------------------------------------------------------------- * Static data members *-------------------------------------------------------------------------------- */ private static final String VAR_START = "${"; private static final String VAR_END = "}"; private static final char[] HTML_ENCODE_CHARS = { '"', '&', '<', '>' }; /*-------------------------------------------------------------------------------- * External static operations *-------------------------------------------------------------------------------- */ public static String encodeStringSQL(String str) { if (str==null) return null; int ndx = str.indexOf('\''); if (ndx<0) return str; StringBuffer buf = new StringBuffer(); while (ndx>=0) { // convert each single quote mark to a pair of them if (ndx>0) buf.append(str.substring(0,ndx)); buf.append("''"); str = str.substring(ndx+1); ndx = str.indexOf('\''); } // end while buf.append(str); return buf.toString(); } // end encodeStringSQL public static String encodeHTML(String str) { if (str==null) return null; AnyCharMatcher nhc = new AnyCharMatcher(HTML_ENCODE_CHARS); int ndx = nhc.get(str); if (ndx<0) return str; // trivial short-circuit case StringBuffer buf = new StringBuffer(); while (ndx>=0) { // append the matched "head" and then the encoded character if (ndx>0) buf.append(str.substring(0,ndx)); switch (str.charAt(ndx++)) { case '"': buf.append("""); break; case '&': buf.append("&"); break; case '<': buf.append("<"); break; case '>': buf.append(">"); break; } // end switch if (ndx==str.length()) return buf.toString(); // munched the entire string - all done! str = str.substring(ndx); ndx = nhc.get(str); } // end while buf.append(str); // append the unmatched tail return buf.toString(); } // end encodeHTML public static boolean isStringEmpty(String s) { return ((s==null) || (s.length()==0)); } // end isStringEmpty public static String replaceAllInstances(String base, String find, String replace) { String work = base; int ndx = work.indexOf(find); if (ndx<0) return work; // trivial case StringBuffer b = new StringBuffer(); while (ndx>=0) { // break off the first part of the string, then insert the replacement if (ndx>0) b.append(work.substring(0,ndx)); b.append(replace); // break off the tail end ndx += find.length(); if (ndx==work.length()) return b.toString(); // entire string munched - we're done! work = work.substring(ndx); ndx = work.indexOf(find); } // end while // append the unmatched "tail" of the work string and then return result b.append(work); return b.toString(); } // end replaceAllInstances public static String replaceAllVariables(String base, Map vars) { String work = base; boolean did_replace, retest; retest = true; do { // main loop for replacing all variables did_replace = false; Iterator it = vars.keySet().iterator(); while (it.hasNext()) { // variable start is there... if (retest) { // only perform this test on the first iteration and after we know we've replaced a variable if (work.indexOf(VAR_START)<0) return work; // no more variables in text - all done! retest = false; } // end if // get variable name and see if it's present String vname = it.next().toString(); if (work.indexOf(VAR_START + vname + VAR_END)>=0) { // OK, this variable is in place work = replaceAllInstances(work,VAR_START + vname + VAR_END,vars.get(vname).toString()); did_replace = true; retest = true; } // end if } // end while } while (did_replace); // end do return work; // all done! } // end replaceAllVariables } // end class StringUtil