123 lines
2.9 KiB
Java
123 lines
2.9 KiB
Java
/*
|
|
* 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 <http://www.mozilla.org/MPL/>.
|
|
*
|
|
* 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 Community System.
|
|
*
|
|
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
|
* 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;
|
|
|
|
public class StringUtil
|
|
{
|
|
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;
|
|
StringBuffer buf = new StringBuffer();
|
|
for (int i=0; i<str.length(); i++)
|
|
{ // loop through the string encoding each character in turn
|
|
switch (str.charAt(i))
|
|
{
|
|
case '"':
|
|
buf.append(""");
|
|
break;
|
|
|
|
case '&':
|
|
buf.append("&");
|
|
break;
|
|
|
|
case '<':
|
|
buf.append("<");
|
|
break;
|
|
|
|
case '>':
|
|
buf.append(">");
|
|
break;
|
|
|
|
default:
|
|
buf.append(str.charAt(i));
|
|
break;
|
|
|
|
} // end switch
|
|
|
|
} // end for
|
|
|
|
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())
|
|
work = null;
|
|
else
|
|
work = work.substring(ndx);
|
|
|
|
// do the next find
|
|
if (work!=null)
|
|
ndx = work.indexOf(find);
|
|
else
|
|
ndx = -1;
|
|
|
|
} // end while
|
|
|
|
if (work!=null)
|
|
b.append(work);
|
|
return b.toString();
|
|
|
|
} // end replaceAllInstances
|
|
|
|
} // end class StringUtil
|