Forgot to check in support functions for yet ANOTHER way that Excel handles strings (makes 3-4th way)

PR:
Obtained from:
Submitted by:
Reviewed by:


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352638 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew C. Oliver 2002-05-19 17:54:07 +00:00
parent eef90ca3f3
commit 13935d307f

View File

@ -1,5 +1,6 @@
/* ==================================================================== /*
* ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2002 The Apache Software Foundation. All rights * Copyright (c) 2002 The Apache Software Foundation. All rights
@ -52,7 +53,6 @@
* information on the Apache Software Foundation, please see * information on the Apache Software Foundation, please see
* <http://www.apache.org/>. * <http://www.apache.org/>.
*/ */
package org.apache.poi.util; package org.apache.poi.util;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -61,114 +61,148 @@ import java.text.NumberFormat;
import java.text.FieldPosition; import java.text.FieldPosition;
/** /**
* Title: String Utility * Title: String Utility Description: Collection of string handling utilities
* Description: Collection of string handling utilities *
*@author Andrew C. Oliver *@author Andrew C. Oliver
*@created May 10, 2002
*@version 1.0 *@version 1.0
*/ */
public class StringUtil public class StringUtil {
{ /**
private StringUtil() * Constructor for the StringUtil object
{ */
} private StringUtil() { }
/** /**
* given a byte array of 16-bit unicode characters, compress to * given a byte array of 16-bit unicode characters, compress to 8-bit and
* 8-bit and return a string * return a string
* *
*@param string the byte array to be converted *@param string the byte array to be converted
* @param offset the initial offset into the byte array. it is *@param offset the initial offset into the
* assumed that string[ offset ] and string[ offset * byte array. it is assumed that string[ offset ] and string[ offset +
* + 1 ] contain the first 16-bit unicode character * 1 ] contain the first 16-bit unicode character
* @len the length of the final string
*
*@param len *@param len
*@return the converted string *@return the converted string
* *@exception ArrayIndexOutOfBoundsException if offset is out of bounds for
* @exception ArrayIndexOutOfBoundsException if offset is out of * the byte array (i.e., is negative or is greater than or equal to
* bounds for the byte array (i.e., is negative or is * string.length)
* greater than or equal to string.length)
*@exception IllegalArgumentException if len is too large (i.e., *@exception IllegalArgumentException if len is too large (i.e.,
* there is not enough data in string to create a * there is not enough data in string to create a String of that
* String of that length) * length)
*@len the length of the final string
*/ */
public static String getFromUnicode(final byte [] string, public static String getFromUnicodeHigh(final byte[] string,
final int offset, final int len) final int offset, final int len)
throws ArrayIndexOutOfBoundsException, IllegalArgumentException throws ArrayIndexOutOfBoundsException, IllegalArgumentException {
{ if ((offset < 0) || (offset >= string.length)) {
if ((offset < 0) || (offset >= string.length))
{
throw new ArrayIndexOutOfBoundsException("Illegal offset"); throw new ArrayIndexOutOfBoundsException("Illegal offset");
} }
if ((len < 0) || (((string.length - offset) / 2) < len)) if ((len < 0) || (((string.length - offset) / 2) < len)) {
{
throw new IllegalArgumentException("Illegal length"); throw new IllegalArgumentException("Illegal length");
} }
byte[] bstring = new byte[len]; byte[] bstring = new byte[len];
int index = offset + 1; // start with low bits. int index = offset;
// start with high bits.
for (int k = 0; k < len; k++) for (int k = 0; k < len; k++) {
{
bstring[k] = string[index]; bstring[k] = string[index];
index += 2; index += 2;
} }
return new String(bstring); return new String(bstring);
} }
/** /**
* given a byte array of 16-bit unicode characters, compress to * given a byte array of 16-bit unicode characters, compress to 8-bit and
* 8-bit and return a string * return a string
* *
*@param string the byte array to be converted *@param string the byte array to be converted
*@param offset the initial offset into the
* byte array. it is assumed that string[ offset ] and string[ offset +
* 1 ] contain the first 16-bit unicode character
*@param len
*@return the converted string
*@exception 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.,
* there is not enough data in string to create a String of that
* length)
*@len the length of the final string
*/
public static String getFromUnicode(final byte[] string,
final int offset, final int len)
throws ArrayIndexOutOfBoundsException, IllegalArgumentException {
if ((offset < 0) || (offset >= string.length)) {
throw new ArrayIndexOutOfBoundsException("Illegal offset");
}
if ((len < 0) || (((string.length - offset) / 2) < len)) {
throw new IllegalArgumentException("Illegal length");
}
byte[] bstring = new byte[len];
int index = offset + 1;
// start with low bits.
for (int k = 0; k < len; k++) {
bstring[k] = string[index];
index += 2;
}
return new String(bstring);
}
/**
* given a byte array of 16-bit unicode characters, compress to 8-bit and
* return a string
* *
*@param string the byte array to be converted
*@return the converted string *@return the converted string
*/ */
public static String getFromUnicode(final byte [] string) public static String getFromUnicode(final byte[] string) {
{
return getFromUnicode(string, 0, string.length / 2); return getFromUnicode(string, 0, string.length / 2);
} }
/** /**
* write compressed unicode * write compressed unicode
* *
*@param input the String containing the data to be written *@param input the String containing the data to be written
*@param output the byte array to which the data is to be written *@param output the byte array to which the data is to be written
* @param offset an offset into the byte arrat at which the data *@param offset an offset into the byte arrat at which the data is start
* is start when written * when written
*/ */
public static void putCompressedUnicode(final String input, public static void putCompressedUnicode(final String input,
final byte[] output, final byte[] output,
final int offset) final int offset) {
{
int strlen = input.length(); int strlen = input.length();
for (int k = 0; k < strlen; k++) for (int k = 0; k < strlen; k++) {
{
output[offset + k] = (byte) input.charAt(k); output[offset + k] = (byte) input.charAt(k);
} }
} }
/** /**
* Write uncompressed unicode * Write uncompressed unicode
* *
* @param input the String containing the unicode data to be *@param input the String containing the unicode data to be written
* written
*@param output the byte array to hold the uncompressed unicode *@param output the byte array to hold the uncompressed unicode
*@param offset the offset to start writing into the byte array *@param offset the offset to start writing into the byte array
*/ */
public static void putUncompressedUnicode(final String input, public static void putUncompressedUnicode(final String input,
final byte[] output, final byte[] output,
final int offset) final int offset) {
{
int strlen = input.length(); int strlen = input.length();
for (int k = 0; k < strlen; k++) for (int k = 0; k < strlen; k++) {
{
char c = input.charAt(k); char c = input.charAt(k);
output[offset + (2 * k)] = (byte) c; output[offset + (2 * k)] = (byte) c;
@ -176,42 +210,59 @@ public class StringUtil
} }
} }
public static String format(String message, Object [] params) /**
{ * Write uncompressed unicode
*
*@param input the String containing the unicode data to be written
*@param output the byte array to hold the uncompressed unicode
*@param offset the offset to start writing into the byte array
*/
public static void putUncompressedUnicodeHigh(final String input,
final byte[] output,
final int offset) {
int strlen = input.length();
for (int k = 0; k < strlen; k++) {
char c = input.charAt(k);
output[offset + (2 * k)] = (byte) (c >> 8);
output[offset + (2 * k)] = (byte) c;
}
}
/**
* Description of the Method
*
*@param message Description of the Parameter
*@param params Description of the Parameter
*@return Description of the Return Value
*/
public static String format(String message, Object[] params) {
int currentParamNumber = 0; int currentParamNumber = 0;
StringBuffer formattedMessage = new StringBuffer(); StringBuffer formattedMessage = new StringBuffer();
for (int i = 0; i < message.length(); i++) for (int i = 0; i < message.length(); i++) {
{ if (message.charAt(i) == '%') {
if (message.charAt(i) == '%') if (currentParamNumber >= params.length) {
{
if (currentParamNumber >= params.length)
{
formattedMessage.append("?missing data?"); formattedMessage.append("?missing data?");
} } else if ((params[currentParamNumber] instanceof Number)
else if ((params[ currentParamNumber ] instanceof Number) && (i + 1 < message.length())) {
&& (i + 1 < message.length()))
{
i += matchOptionalFormatting( i += matchOptionalFormatting(
(Number) params[currentParamNumber++], (Number) params[currentParamNumber++],
message.substring(i + 1), formattedMessage); message.substring(i + 1), formattedMessage);
} else {
formattedMessage.append(params[currentParamNumber++].toString());
} }
else } else {
{
formattedMessage
.append(params[ currentParamNumber++ ].toString());
}
}
else
{
if ((message.charAt(i) == '\\') && (i + 1 < message.length()) if ((message.charAt(i) == '\\') && (i + 1 < message.length())
&& (message.charAt(i + 1) == '%')) && (message.charAt(i + 1) == '%')) {
{
formattedMessage.append('%'); formattedMessage.append('%');
i++; i++;
} } else {
else
{
formattedMessage.append(message.charAt(i)); formattedMessage.append(message.charAt(i));
} }
} }
@ -219,38 +270,35 @@ public class StringUtil
return formattedMessage.toString(); return formattedMessage.toString();
} }
/**
* Description of the Method
*
*@param number Description of the Parameter
*@param formatting Description of the Parameter
*@param outputTo Description of the Parameter
*@return Description of the Return Value
*/
private static int matchOptionalFormatting(Number number, private static int matchOptionalFormatting(Number number,
String formatting, String formatting,
StringBuffer outputTo) StringBuffer outputTo) {
{
NumberFormat numberFormat = NumberFormat.getInstance(); NumberFormat numberFormat = NumberFormat.getInstance();
if ((0 < formatting.length()) if ((0 < formatting.length())
&& Character.isDigit(formatting.charAt(0))) && Character.isDigit(formatting.charAt(0))) {
{ numberFormat.setMinimumIntegerDigits(Integer.parseInt(formatting.charAt(0) + ""));
numberFormat
.setMinimumIntegerDigits(Integer
.parseInt(formatting.charAt(0) + ""));
if ((2 < formatting.length()) && (formatting.charAt(1) == '.') if ((2 < formatting.length()) && (formatting.charAt(1) == '.')
&& Character.isDigit(formatting.charAt(2))) && Character.isDigit(formatting.charAt(2))) {
{ numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(2) + ""));
numberFormat
.setMaximumFractionDigits(Integer
.parseInt(formatting.charAt(2) + ""));
numberFormat.format(number, outputTo, new FieldPosition(0)); numberFormat.format(number, outputTo, new FieldPosition(0));
return 3; return 3;
} }
numberFormat.format(number, outputTo, new FieldPosition(0)); numberFormat.format(number, outputTo, new FieldPosition(0));
return 1; return 1;
} } else if ((0 < formatting.length()) && (formatting.charAt(0) == '.')) {
else if ((0 < formatting.length()) && (formatting.charAt(0) == '.'))
{
if ((1 < formatting.length()) if ((1 < formatting.length())
&& Character.isDigit(formatting.charAt(1))) && Character.isDigit(formatting.charAt(1))) {
{ numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(1) + ""));
numberFormat
.setMaximumFractionDigits(Integer
.parseInt(formatting.charAt(1) + ""));
numberFormat.format(number, outputTo, new FieldPosition(0)); numberFormat.format(number, outputTo, new FieldPosition(0));
return 2; return 2;
} }