KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > formula > StringValue


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff.formula;
21
22 import common.Logger;
23
24 import jxl.Cell;
25 import jxl.WorkbookSettings;
26 import jxl.biff.StringHelper;
27 import jxl.biff.IntegerHelper;
28
29 /**
30  * A string constant operand in a formula
31  */

32 class StringValue extends Operand implements ParsedThing
33 {
34   /**
35    * The logger
36    */

37   private final static Logger logger = Logger.getLogger(StringValue.class);
38
39   /**
40    * The string value
41    */

42   private String JavaDoc value;
43
44   /**
45    * The workbook settings
46    */

47   private WorkbookSettings settings;
48
49   /**
50    * Constructor
51    */

52   public StringValue(WorkbookSettings ws)
53   {
54     settings = ws;
55   }
56
57   /**
58    * Constructor used when parsing a string formula
59    *
60    * @param s the string token, including quote marks
61    */

62   public StringValue(String JavaDoc s)
63   {
64     // remove the quotes
65
value = s;
66   }
67
68   /**
69    * Reads the ptg data from the array starting at the specified position
70    *
71    * @param data the RPN array
72    * @param pos the current position in the array, excluding the ptg identifier
73    * @return the number of bytes read
74    */

75   public int read(byte[] data, int pos)
76   {
77     int length = IntegerHelper.getInt(data[pos], data[pos+1]);
78     int consumed = 2;
79
80     if ((data[pos+1] & 0x01) == 0)
81     {
82       value = StringHelper.getString(data, length, pos+2, settings);
83       consumed += length;
84     }
85     else
86     {
87       value = StringHelper.getUnicodeString(data, length, pos+2);
88       consumed += length * 2;
89     }
90
91     return consumed;
92   }
93
94   /**
95    * Gets the token representation of this item in RPN
96    *
97    * @return the bytes applicable to this formula
98    */

99   byte[] getBytes()
100   {
101     byte[] data = new byte[value.length() * 2 + 3];
102     data[0] = Token.STRING.getCode();
103     data[1] = (byte) (value.length());
104     data[2] = 0x01;
105     StringHelper.getUnicodeBytes(value, data, 3);
106
107     return data;
108   }
109
110   /**
111    * Abstract method implementation to get the string equivalent of this
112    * token
113    *
114    * @param buf the string to append to
115    */

116   public void getString(StringBuffer JavaDoc buf)
117   {
118     buf.append("\"");
119     buf.append(value);
120     buf.append("\"");
121   }
122 }
123
124
Popular Tags