Converted ConstantValueParser to use plain Strings instead of UnicodeStrings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@711513 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-11-05 05:45:17 +00:00
parent 36c4c0bacf
commit 54885b5650
5 changed files with 20 additions and 16 deletions

View File

@ -17,8 +17,6 @@
package org.apache.poi.hssf.record.constant;
import org.apache.poi.hssf.record.UnicodeString;
import org.apache.poi.hssf.record.UnicodeString.UnicodeRecordStats;
import org.apache.poi.util.LittleEndianInput;
import org.apache.poi.util.LittleEndianOutput;
import org.apache.poi.util.StringUtil;
@ -65,7 +63,7 @@ public final class ConstantValueParser {
case TYPE_NUMBER:
return new Double(in.readDouble());
case TYPE_STRING:
return new UnicodeString(StringUtil.readUnicodeString(in));
return StringUtil.readUnicodeString(in);
case TYPE_BOOLEAN:
return readBoolean(in);
case TYPE_ERROR_CODE:
@ -111,10 +109,8 @@ public final class ConstantValueParser {
if(cls == Boolean.class || cls == Double.class || cls == ErrorConstant.class) {
return 8;
}
UnicodeString strVal = (UnicodeString)object;
UnicodeRecordStats urs = new UnicodeRecordStats();
strVal.getRecordSize(urs);
return urs.recordSize;
String strVal = (String)object;
return StringUtil.getEncodedSize(strVal);
}
public static void encode(LittleEndianOutput out, Object[] values) {
@ -142,10 +138,10 @@ public final class ConstantValueParser {
out.writeDouble(dVal.doubleValue());
return;
}
if (value instanceof UnicodeString) {
UnicodeString usVal = (UnicodeString) value;
if (value instanceof String) {
String val = (String) value;
out.writeByte(TYPE_STRING);
StringUtil.writeUnicodeString(out, usVal.getString());
StringUtil.writeUnicodeString(out, val);
return;
}
if (value instanceof ErrorConstant) {

View File

@ -206,8 +206,8 @@ public final class ArrayPtg extends Ptg {
if (o == null) {
throw new RuntimeException("Array item cannot be null");
}
if (o instanceof UnicodeString) {
return "\"" + ((UnicodeString)o).getString() + "\"";
if (o instanceof String) {
return "\"" + (String)o + "\"";
}
if (o instanceof Double) {
return ((Double)o).toString();

View File

@ -162,6 +162,15 @@ public class StringUtil {
}
}
/**
* @return the number of bytes that would be written by {@link #writeUnicodeString(LittleEndianOutput, String)}
*/
public static int getEncodedSize(String value) {
int result = 2 + 1;
result += value.length() * (StringUtil.hasMultibyte(value) ? 2 : 1);
return result;
}
/**
* Takes a unicode (java) string, and returns it as 8 bit data (in ISO-8859-1
* codepage).

View File

@ -22,7 +22,6 @@ import java.util.Arrays;
import junit.framework.TestCase;
import org.apache.poi.hssf.record.TestcaseRecordInputStream;
import org.apache.poi.hssf.record.UnicodeString;
import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.LittleEndianByteArrayOutputStream;
@ -36,7 +35,7 @@ public final class TestConstantValueParser extends TestCase {
Boolean.TRUE,
null,
new Double(1.1),
new UnicodeString("Sample text"),
"Sample text",
ErrorConstant.valueOf(HSSFErrorConstants.ERROR_DIV_0),
};
private static final byte[] SAMPLE_ENCODING = HexRead.readFromString(

View File

@ -66,10 +66,10 @@ public final class TestArrayPtg extends TestCase {
assertEquals(Boolean.TRUE, values[0][0]);
assertEquals(new UnicodeString("ABCD"), values[0][1]);
assertEquals("ABCD", values[0][1]);
assertEquals(new Double(0), values[1][0]);
assertEquals(Boolean.FALSE, values[1][1]);
assertEquals(new UnicodeString("FG"), values[1][2]);
assertEquals("FG", values[1][2]);
byte[] outBuf = new byte[ENCODED_CONSTANT_DATA.length];
ptg.writeTokenValueBytes(new LittleEndianByteArrayOutputStream(outBuf, 0));