001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.record.formula;
019:
020: import org.apache.poi.hssf.model.Workbook;
021: import org.apache.poi.util.BitField;
022: import org.apache.poi.util.BitFieldFactory;
023: import org.apache.poi.util.StringUtil;
024: import org.apache.poi.hssf.record.RecordInputStream;
025:
026: /**
027: * Number
028: * Stores a String value in a formula value stored in the format <length 2 bytes>char[]
029: * @author Werner Froidevaux
030: * @author Jason Height (jheight at chariot dot net dot au)
031: * @author Bernard Chesnoy
032: */
033:
034: public class StringPtg extends Ptg {
035: public final static int SIZE = 9;
036: public final static byte sid = 0x17;
037: //NOTE: OO doc says 16bit lenght, but BiffViewer says 8
038: // Book says something totally different, so dont look there!
039: int field_1_length;
040: byte field_2_options;
041: BitField fHighByte = BitFieldFactory.getInstance(0x01);
042: private String field_3_string;
043:
044: private StringPtg() {
045: //Required for clone methods
046: }
047:
048: /** Create a StringPtg from a byte array read from disk */
049: public StringPtg(RecordInputStream in) {
050: field_1_length = in.readByte() & 0xFF;
051: field_2_options = in.readByte();
052: if (fHighByte.isSet(field_2_options)) {
053: field_3_string = in.readUnicodeLEString(field_1_length);
054: } else {
055: field_3_string = in.readCompressedUnicode(field_1_length);
056: }
057:
058: //setValue(new String(data, offset+3, data[offset+1] + 256*data[offset+2]));
059: }
060:
061: /** Create a StringPtg from a string representation of the number
062: * Number format is not checked, it is expected to be validated in the parser
063: * that calls this method.
064: * @param value : String representation of a floating point number
065: */
066: public StringPtg(String value) {
067: if (value.length() > 255) {
068: throw new IllegalArgumentException(
069: "String literals in formulas cant be bigger than 255 characters ASCII");
070: }
071: this .field_2_options = 0;
072: field_2_options = (byte) this .fHighByte.setBoolean(
073: field_2_options, StringUtil.hasMultibyte(value));
074: this .field_3_string = value;
075: this .field_1_length = value.length(); //for the moment, we support only ASCII strings in formulas we create
076: }
077:
078: /*
079: public void setValue(String value)
080: {
081: field_1_value = value;
082: }*/
083:
084: public String getValue() {
085: return field_3_string;
086: }
087:
088: public void writeBytes(byte[] array, int offset) {
089: array[offset + 0] = sid;
090: array[offset + 1] = (byte) field_1_length;
091: array[offset + 2] = field_2_options;
092: if (fHighByte.isSet(field_2_options)) {
093: StringUtil.putUnicodeLE(getValue(), array, offset + 3);
094: } else {
095: StringUtil.putCompressedUnicode(getValue(), array,
096: offset + 3);
097: }
098: }
099:
100: public int getSize() {
101: if (fHighByte.isSet(field_2_options)) {
102: return 2 * field_1_length + 3;
103: } else {
104: return field_1_length + 3;
105: }
106: }
107:
108: public String toFormulaString(Workbook book) {
109: return "\"" + getValue() + "\"";
110: }
111:
112: public byte getDefaultOperandClass() {
113: return Ptg.CLASS_VALUE;
114: }
115:
116: public Object clone() {
117: StringPtg ptg = new StringPtg();
118: ptg.field_1_length = field_1_length;
119: ptg.field_2_options = field_2_options;
120: ptg.field_3_string = field_3_string;
121: return ptg;
122: }
123:
124: }
|