...
In HTML, tags are case-insensitive, and can therefore be specified using uppercase, lowercase, or any mixture of cases. This noncompliant code example uses the locale-dependent String.toUpperCase()
method to convert an HTML tag to upper case, to check it for further processing. The code must ignore <SCRIPT>
tags, as they indicate code that is to be discarded. While the English locale would convert "
titlescript"
to "
TITLESCRIPT"
, the Turkish locale will convert "
titlescript"
to to "
TİTLESCRİPT"
, and the check will fail to prune scripts from further processing.
Code Block |
---|
|
public static void processTitleprocessTag(String tag) {
if (!tag.toUpperCase().equals("TITLESCRIPT")) {
return;
}
// process titletag
}
|
Compliant Solution (Explicit Locale)
...
Code Block |
---|
|
public static void processTitleprocessTag(String tag) {
if (!tag.toUpperCase(Locale.ENGLISH).equals("TITLESCRIPT")) {
return;
}
// process titletag
}
|
Specifying Locale.ROOT
is a suitable alternative under conditions where an English-specific locale would not be appropriate.
...
Code Block |
---|
|
public static void processTitleprocessTag(String tag) {
Locale.setDefault(Locale.ENGLISH);
if (!tag.toUpperCase().equals("TITLESCRIPT")) {
return;
}
// process titletag
}
|
Compliant Solution (String.equalsIgnoreCase()
)
...
Code Block |
---|
|
public static void processTitleprocessTag(String tag) {
if (!tag.equalsIgnoreCase("TITLESCRIPT")) {
return;
}
// process titletag
}
|
Noncompliant Code Example (FileReader
)
...