KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > formula > StringPtg


1 /* ====================================================================
2    Copyright 2003-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.record.formula;
18
19 import org.apache.poi.hssf.model.Workbook;
20 import org.apache.poi.util.BitField;
21 import org.apache.poi.util.StringUtil;
22
23 /**
24  * Number
25  * Stores a String value in a formula value stored in the format <length 2 bytes>char[]
26  * @author Werner Froidevaux
27  * @author Jason Height (jheight at chariot dot net dot au)
28  * @author Bernard Chesnoy
29  */

30
31 public class StringPtg
32     extends Ptg
33 {
34     public final static int SIZE = 9;
35     public final static byte sid = 0x17;
36     //NOTE: OO doc says 16bit lenght, but BiffViewer says 8
37
// Book says something totally different, so dont look there!
38
int field_1_length;
39     byte field_2_options;
40     BitField fHighByte = new BitField(0x01);
41     private String JavaDoc field_3_string;
42
43     private StringPtg() {
44       //Required for clone methods
45
}
46
47     /** Create a StringPtg from a byte array read from disk */
48     public StringPtg(byte [] data, int offset)
49     {
50         offset++;
51         field_1_length = data[offset] & 0xFF;
52         field_2_options = data[offset+1];
53         if (fHighByte.isSet(field_2_options)) {
54             field_3_string= StringUtil.getFromUnicodeLE(data,offset+2,field_1_length);
55         }else {
56             field_3_string=StringUtil.getFromCompressedUnicode(data,offset+2,field_1_length);
57         }
58
59         //setValue(new String(data, offset+3, data[offset+1] + 256*data[offset+2]));
60
}
61
62     /** Create a StringPtg from a string representation of the number
63      * Number format is not checked, it is expected to be validated in the parser
64      * that calls this method.
65      * @param value : String representation of a floating point number
66      */

67     public StringPtg(String JavaDoc value) {
68         if (value.length() >255) {
69             throw new IllegalArgumentException JavaDoc("String literals in formulas cant be bigger than 255 characters ASCII");
70         }
71         this.field_2_options=0;
72         this.fHighByte.setBoolean(field_2_options, false);
73         this.field_3_string=value;
74         this.field_1_length=value.length(); //for the moment, we support only ASCII strings in formulas we create
75
}
76
77     /*
78     public void setValue(String value)
79     {
80         field_1_value = value;
81     }*/

82
83
84     public String JavaDoc getValue()
85     {
86         return field_3_string;
87     }
88
89     public void writeBytes(byte [] array, int offset)
90     {
91         array[ offset + 0 ] = sid;
92         array[ offset + 1 ] = (byte)field_1_length;
93         array[ offset + 2 ] = field_2_options;
94         if (fHighByte.isSet(field_2_options)) {
95             StringUtil.putUnicodeLE(getValue(),array,offset+3);
96         }else {
97             StringUtil.putCompressedUnicode(getValue(),array,offset+3);
98         }
99     }
100
101     public int getSize()
102     {
103         if (fHighByte.isSet(field_2_options)) {
104             return 2*field_1_length+3;
105         } else {
106             return field_1_length+3;
107         }
108     }
109
110     public String JavaDoc toFormulaString(Workbook book)
111     {
112         return "\""+getValue()+"\"";
113     }
114     public byte getDefaultOperandClass() {
115        return Ptg.CLASS_VALUE;
116    }
117
118    public Object JavaDoc clone() {
119      StringPtg ptg = new StringPtg();
120      ptg.field_1_length = field_1_length;
121      ptg.field_2_options=field_2_options;
122      ptg.field_3_string=field_3_string;
123      return ptg;
124    }
125
126 }
127
128
Popular Tags