org.broadleafcommerce.common.util.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of broadleaf-common Show documentation
Show all versions of broadleaf-common Show documentation
A collection of classes shared by broadleaf profile, cms, admin, and core.
/*
* Copyright 2008-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.broadleafcommerce.common.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
public class StringUtil {
public static long getChecksum(String test) {
try {
byte buffer[] = test.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
byte readBuffer[] = new byte[buffer.length];
cis.read(readBuffer);
return cis.getChecksum().getValue();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static double determineSimilarity(String test1, String test2) {
String first = new String(test1);
first = first.replaceAll("[ \\t\\n\\r\\f\\v\\/'-]", "");
Long originalChecksum = StringUtil.getChecksum(first);
String second = new String(test2);
second = second.replaceAll("[ \\t\\n\\r\\f\\v\\/'-]", "");
Long myChecksum = StringUtil.getChecksum(second);
StatCalc calc = new StatCalc();
calc.enter(originalChecksum);
calc.enter(myChecksum);
return calc.getStandardDeviation();
}
/**
* Protect against HTTP Response Splitting
* @return
*/
public static String cleanseUrlString(String input){
return removeSpecialCharacters(decodeUrl(input));
}
public static String decodeUrl(String encodedUrl) {
try {
return encodedUrl == null ? null : URLDecoder.decode(encodedUrl, "UTF-8");
} catch (UnsupportedEncodingException e) {
// this should not happen
e.printStackTrace();
return encodedUrl;
}
}
public static String removeSpecialCharacters(String input) {
if (input != null) {
input = input.replaceAll("[ \\r\\n]", "");
}
return input;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy