add forbidden api checks for non-Locale toLowerCase and toUpperCase

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1815994 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-11-21 22:10:48 +00:00
parent 0675acb64a
commit 268bcdbc2d
7 changed files with 482 additions and 445 deletions

View File

@ -17,6 +17,8 @@
package org.apache.poi.sl.usermodel;
import org.apache.poi.util.StringUtil;
/**
* known preset shape geometries in PresentationML
*/
@ -299,7 +301,7 @@ public enum ShapeType {
toLower = false;
continue;
}
sb.append(toLower ? Character.toLowerCase(ch) : Character.toUpperCase(ch));
sb.append(toLower ? StringUtil.toLowerCase(ch) : StringUtil.toUpperCase(ch));
toLower = true;
}

View File

@ -27,6 +27,7 @@ import java.util.Locale;
import java.util.regex.Matcher;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil;
/**
* Formats a date value.
@ -110,9 +111,9 @@ public class CellDateFormatter extends CellFormatter {
// am/pm marker
mStart = -1;
showAmPm = true;
showM = Character.toLowerCase(part.charAt(1)) == 'm';
showM = StringUtil.toLowerCase(part.charAt(1)).equals("m");
// For some reason "am/pm" becomes AM or PM, but "a/p" becomes a or p
amPmUpper = showM || Character.isUpperCase(part.charAt(0));
amPmUpper = showM || StringUtil.isUpperCase(part.charAt(0));
return "a";
}
@ -199,11 +200,11 @@ public class CellDateFormatter extends CellFormatter {
if (!doneAm) {
if (showAmPm) {
if (amPmUpper) {
toAppendTo.append(Character.toString(ch).toUpperCase(LocaleUtil.getUserLocale()));
toAppendTo.append(StringUtil.toUpperCase(ch));
if (showM)
toAppendTo.append('M');
} else {
toAppendTo.append(Character.toString(ch).toLowerCase(LocaleUtil.getUserLocale()));
toAppendTo.append(StringUtil.toLowerCase(ch));
if (showM)
toAppendTo.append('m');
}

View File

@ -18,6 +18,7 @@ package org.apache.poi.ss.format;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil;
import javax.swing.*;
@ -341,7 +342,7 @@ public class CellFormatPart {
char c1 = repl.charAt(0);
char c2 = 0;
if (repl.length() > 1)
c2 = Character.toLowerCase(repl.charAt(1));
c2 = StringUtil.toLowerCase(repl.charAt(1)).charAt(0);
switch (c1) {
case '@':

View File

@ -46,7 +46,7 @@ public class StringUtil {
* Given a byte array of 16-bit unicode characters in Little Endian
* format (most important byte last), return a Java String representation
* of it.
*
* <p>
* { 0x16, 0x00 } -0x16
*
* @param string the byte array to be converted
@ -55,10 +55,10 @@ public class StringUtil {
* 1 ] contain the first 16-bit unicode character
* @param len the length of the final string
* @return the converted string, never <code>null</code>.
* @exception ArrayIndexOutOfBoundsException if offset is out of bounds for
* @throws ArrayIndexOutOfBoundsException if offset is out of bounds for
* the byte array (i.e., is negative or is greater than or equal to
* string.length)
* @exception IllegalArgumentException if len is too large (i.e.,
* @throws IllegalArgumentException if len is too large (i.e.,
* there is not enough data in string to create a String of that
* length)
*/
@ -81,14 +81,16 @@ public class StringUtil {
* Given a byte array of 16-bit unicode characters in little endian
* format (most important byte last), return a Java String representation
* of it.
*
* <p>
* { 0x16, 0x00 } -0x16
*
* @param string the byte array to be converted
* @return the converted string, never <code>null</code>
*/
public static String getFromUnicodeLE(byte[] string) {
if(string.length == 0) { return ""; }
if (string.length == 0) {
return "";
}
return getFromUnicodeLE(string, 0, string.length / 2);
}
@ -134,7 +136,7 @@ public class StringUtil {
* <li>byte[]/char[] characterData</li>
* </ol>
* For this encoding, the is16BitFlag is always present even if nChars==0.
*
* <p>
* This structure is also known as a XLUnicodeString.
*/
public static String readUnicodeString(LittleEndianInput in) {
@ -146,6 +148,7 @@ public class StringUtil {
}
return readUnicodeLE(in, nChars);
}
/**
* InputStream <tt>in</tt> is expected to contain:
* <ol>
@ -165,6 +168,7 @@ public class StringUtil {
}
return readUnicodeLE(in, nChars);
}
/**
* OutputStream <tt>out</tt> will get:
* <ol>
@ -185,6 +189,7 @@ public class StringUtil {
putCompressedUnicode(value, out);
}
}
/**
* OutputStream <tt>out</tt> will get:
* <ol>
@ -249,6 +254,7 @@ public class StringUtil {
byte[] bytes = input.getBytes(UTF16LE);
System.arraycopy(bytes, 0, output, offset, bytes.length);
}
public static void putUnicodeLE(String input, LittleEndianOutput out) {
byte[] bytes = input.getBytes(UTF16LE);
out.write(bytes);
@ -316,6 +322,7 @@ public class StringUtil {
public static class StringsIterator implements Iterator<String> {
private String[] strings = {};
private int position;
public StringsIterator(String[] strings) {
if (strings != null) {
this.strings = strings.clone();
@ -325,6 +332,7 @@ public class StringUtil {
public boolean hasNext() {
return position < strings.length;
}
public String next() {
int ourPos = position++;
if (ourPos >= strings.length) {
@ -332,9 +340,26 @@ public class StringUtil {
}
return strings[ourPos];
}
public void remove() {}
public void remove() {
}
}
@Internal
public static String toLowerCase(char c) {
return Character.toString(c).toLowerCase(LocaleUtil.getUserLocale());
}
@Internal
public static String toUpperCase(char c) {
return Character.toString(c).toUpperCase(LocaleUtil.getUserLocale());
}
@Internal
public static boolean isUpperCase(char c) {
String s = Character.toString(c);
return s.toUpperCase(LocaleUtil.getUserLocale()).equals(s);
}
/**
* Some strings may contain encoded characters of the unicode private use area.
@ -343,7 +368,6 @@ public class StringUtil {
*
* @param string the original string
* @return the string with mapped characters
*
* @see <a href="http://www.alanwood.net/unicode/private_use_area.html#symbol">Private Use Area (symbol)</a>
* @see <a href="http://www.alanwood.net/demos/symbol.html">Symbol font - Unicode alternatives for Greek and special characters in HTML</a>
*/

View File

@ -29,6 +29,8 @@ import javax.xml.namespace.QName;
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
@ -130,7 +132,7 @@ public final class XSSFPasswordHelper {
if (prefix == null || prefix.isEmpty()) {
return new QName(name);
} else {
return new QName(prefix+Character.toUpperCase(name.charAt(0))+name.substring(1));
return new QName(prefix + StringUtil.toUpperCase(name.charAt(0)) + name.substring(1));
}
}
}

View File

@ -102,8 +102,10 @@ java.lang.Character#codePointAt(char[],int) @ Implicit end offset is error-prone
@defaultMessage specify a locale when using toUpperCase / to LowerCase
#java.lang.Character#toLowerCase(char)
#java.lang.Character#toUpperCase(char)
java.lang.Character#isLowerCase(char)
java.lang.Character#isUpperCase(char)
java.lang.Character#toLowerCase(char)
java.lang.Character#toUpperCase(char)
java.lang.String#toLowerCase()
java.lang.String#toUpperCase()

View File

@ -18,6 +18,7 @@
package org.apache.poi.hwpf.dev;
import org.apache.poi.util.Internal;
import org.apache.poi.util.StringUtil;
/**
* Helper functions for the record transformations. Used during model classes
@ -172,7 +173,7 @@ public class RecordUtil
{
StringBuilder fieldName = new StringBuilder();
toIdentifier( name, fieldName );
fieldName.setCharAt( 0, Character.toUpperCase( fieldName.charAt( 0 ) ) );
fieldName.setCharAt( 0, toUpperCase( fieldName.charAt(0) ) );
pad( fieldName, padTo );
return fieldName.toString();
@ -183,7 +184,7 @@ public class RecordUtil
StringBuilder result = new StringBuilder();
result.append( type );
result = pad( result, padTo );
result.setCharAt( 0, Character.toUpperCase( result.charAt( 0 ) ) );
result.setCharAt( 0, toUpperCase( result.charAt( 0 ) ) );
return result.toString();
}
@ -202,7 +203,7 @@ public class RecordUtil
if ( name.charAt( i ) == ' ' )
fieldName.append( '_' );
else
fieldName.append( Character.toUpperCase( name.charAt( i ) ) );
fieldName.append( toUpperCase( name.charAt( i ) ) );
}
}
@ -211,12 +212,16 @@ public class RecordUtil
for ( int i = 0; i < name.length(); i++ )
{
if ( name.charAt( i ) == ' ' )
fieldName.append( Character.toUpperCase( name.charAt( ++i ) ) );
fieldName.append( toUpperCase( name.charAt( ++i ) ) );
else
fieldName.append( name.charAt( i ) );
}
}
private static char toUpperCase(char c) {
return StringUtil.toUpperCase(c).charAt(0);
}
public RecordUtil()
{
}