From 8282eb0c9874b2707ef1279d3f946e1eb9cf9afc Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Wed, 5 Nov 2008 05:45:17 +0000 Subject: [PATCH] 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 --- .../record/constant/ConstantValueParser.java | 16 ++++++---------- .../apache/poi/hssf/record/formula/ArrayPtg.java | 4 ++-- src/java/org/apache/poi/util/StringUtil.java | 9 +++++++++ .../record/constant/TestConstantValueParser.java | 3 +-- .../poi/hssf/record/formula/TestArrayPtg.java | 4 ++-- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java b/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java index 8304eb0ee3..3fbbee12ac 100755 --- a/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java +++ b/src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java @@ -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) { diff --git a/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java b/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java index 3fcc23eda0..7d4f65acd9 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java @@ -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(); diff --git a/src/java/org/apache/poi/util/StringUtil.java b/src/java/org/apache/poi/util/StringUtil.java index 8a57d2340b..263f45e1e5 100644 --- a/src/java/org/apache/poi/util/StringUtil.java +++ b/src/java/org/apache/poi/util/StringUtil.java @@ -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). diff --git a/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java b/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java index a72d0e1aed..6ed2c6245a 100644 --- a/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java +++ b/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java @@ -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( diff --git a/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java b/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java index 94a16b3829..5dab4cca14 100644 --- a/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java +++ b/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java @@ -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)); -- 2.39.5