我有两种方法,一种是检查给定值是否为NULL,即字符串"NULL“或空字符串。另一个是用html编码的值替换html特定的字符,并插入空格以启用换行等(参见下面的代码)。第二个方法使用第一个方法在做任何工作之前检查给定的值。
public static String checkForNull(String aString, String aReturnValue)
{
String tBack = aString;
if ((aString == null) || (aString.equalsIgnoreCase("null")) || (aString.trim().equals("")))
{
tBack = aReturnValue;
}
return tBack;
}
public static String encodeAsHTMLEntities(String aValue, boolean aLineBreakAsBR, String[] aUnencodedParts,
boolean aInsertRatedSpace, int aMinimalWordSize)
{
StringBuilder tResult = new StringBuilder();
if(StringUtils.checkForNull(aValue, null) != null)
{
String tTempValue = aValue;
List<String> tUnencodedPartList = new ArrayList<String>();
if(aUnencodedParts != null)
{
tUnencodedPartList.addAll(Arrays.asList(aUnencodedParts));
}
/* Replace all linebreaks by HTML-tag if needed. */
if (aLineBreakAsBR == true)
{
tTempValue = tTempValue.replaceAll("\n", "<br />");
/* Add the br tag to the array containing parts that must not be encoded. */
tUnencodedPartList.add("<[Bb][Rr]\\s*[/]?>");
}
/* HTML-encode the value. */
int tCharsAfterLastSplitSymbol = 1;
Pattern tPattern = Pattern.compile("[\\s\\-,.;:]");
String tSplitterInvisible = "​";
String tSplitterVisible = "­";
if (aMinimalWordSize < 1)
{
aMinimalWordSize = Constants.MINIMAL_WORD_SIZE_BEFORE_SEPARATING;
}
for (int i = 0; i < tTempValue.length(); i++)
{
/* Test if we have an exception for the following value. */
boolean tIsAllowed = false;
String tStringToCheck = tTempValue.substring(i);
for (int t = 0; t < tUnencodedPartList.size() && tIsAllowed == false; t++)
{
String tUnencodedPart = tUnencodedPartList.get(t);
String tMatchingString = tStringToCheck.substring(0, tStringToCheck.length() - tStringToCheck.replaceFirst("^(" + tUnencodedPart + ")", "").length());
if (tMatchingString.length() > 0)
{
if (aInsertRatedSpace == true)
{
tResult.append(tSplitterInvisible);
}
tIsAllowed = true;
i += tMatchingString.length() - 1;
tResult.append(tMatchingString);
if (aInsertRatedSpace == true)
{
tResult.append(tSplitterInvisible);
}
}
}
if (tIsAllowed == false)
{
char tChar = tTempValue.charAt(i);
/* Add the encoded char */
tResult.append(encodeAsHTMLEntity(tChar));
/* Add splitter */
if (aInsertRatedSpace == true)
{
/* Check the character for beeing one of our split symbols */
Matcher tMatcher = tPattern.matcher(Character.toString(tChar));
String tSplitter = "";
if (tCharsAfterLastSplitSymbol >= aMinimalWordSize)
{
boolean tUseVisibleSplitter = true;
if (tMatcher.find())
{
tUseVisibleSplitter = false;
}
else
{
/* Check if next character matches to our reg exp */
if (tTempValue.length() >= (i + 2))
{
tChar = tTempValue.charAt(i+1);
tMatcher = tPattern.matcher(Character.toString(tChar));
if (tMatcher.find())
{
tUseVisibleSplitter = false;
}
}
/* Check if the next characters matches to one of our unencoded parts */
if (tUseVisibleSplitter)
{
String tNextStringToCheck = tTempValue.substring(i+1);
for (int t = 0; t < tUnencodedPartList.size() && tUseVisibleSplitter == true; t++)
{
String tUnencodedPart = tUnencodedPartList.get(t);
String tMatchingString = tNextStringToCheck.substring(0, tNextStringToCheck.length() - tNextStringToCheck.replaceFirst("^(" + tUnencodedPart + ")", "").length());
if (tMatchingString.length() > 0)
{
tUseVisibleSplitter = false;
}
}
}
}
/* Choose the correct splitting symbol */
if (tUseVisibleSplitter)
{
tSplitter = tSplitterVisible;
}
else
{
tSplitter = tSplitterInvisible;
}
tCharsAfterLastSplitSymbol = 1;
}
else
{
if (tMatcher.find())
{
tSplitter = tSplitterInvisible;
tCharsAfterLastSplitSymbol = 1;
}
else
{
tCharsAfterLastSplitSymbol++;
}
}
tResult.append(tSplitter);
}
}
else
{
tCharsAfterLastSplitSymbol = 1;
}
}
}
return tResult.toString();
}checkForNull()方法在添加System.out.println()以检查其返回时总是返回正确的值。在方法encodeAsHTMLEntities()中,对checkForNull()方法的调用突然停止工作,并且不再输入if(StringUtils.checkForNull(aValue, null) != null)。我可以将StringUtils.checkForNull(aValue, null)的结果放到另一个变量中,并打印这个变量,返回的值总是正确的。如果我使用If来检查这个变量是否为null,那么代码也会失败。
我发现了两种使代码工作的方法。第一种方法是这样编写checkForNull():
public static String checkForNull(String aString, String aReturnValue)
{
String tBack = aString;
if (aString == null)
{
tBack = aReturnValue;
}
else if (aString.equalsIgnoreCase("null")
{
tBack = aReturnValue;
}
els if (aString.trim().equals(""))
{
tBack = aReturnValue;
}
return tBack;
}第二个是在调试模式下运行代码,这意味着向vm添加调试参数或向代码中添加任何调试语句。(这就是使用System.out.println()的原因,因为任何调试尝试都解决了这个问题,所以在本例中调试器确实没有帮助)
该代码在应用程序中运行良好,多年来使用Java6 32位、Java6 64位和Java7 32位编译和运行都没有问题。此错误仅在使用Java7 64位版本编译和运行时发生(->测试了从7_5到7_40的几个补丁)
,有人知道这里的问题会是什么吗?如果有人感兴趣,我可以提供一个包含所有代码和一些带有测试字符串的文件的主类来重现错误。
编辑:进一步的测试表明错误只发生在windows系统上。linux上没有问题。
发布于 2013-10-21 07:41:48
解决了。这是Java中的一个bug,似乎是用Java7 Update45解决的。
https://stackoverflow.com/questions/19380736
复制相似问题