1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxc.pexcel.records;
25 
26 import java.io.OutputStream;
27 import java.io.InputStream;
28 import java.io.IOException;
29 import java.io.UnsupportedEncodingException;
30 
31 import org.openoffice.xmerge.util.Debug;
32 import org.openoffice.xmerge.util.EndianConverter;
33 import org.openoffice.xmerge.converter.xml.sxc.pexcel.PocketExcelConstants;
34 
35 
36 /**
37  * Represents a BIFF Record that describes the value of a formula that
38  * evaluates to a string
39  */
40 public class StringValue implements BIFFRecord {
41 
42     private byte[]	cch		= new byte[2];
43     private byte[]  rgch;
44 
45 	/**
46  	 * Constructs a StringValue Record from a <code>String</code>
47  	 *
48  	 * @param str
49  	 */
StringValue(String str)50     public StringValue(String str) throws IOException {
51     	cch = EndianConverter.writeShort((short) str.length());
52 		rgch = new byte[str.length()];
53 		rgch = str.getBytes("UTF-16LE");
54 	}
55 
56 	/**
57  	 * Constructs a StringValue Record from an <code>InputStream</code>
58  	 *
59  	 * @param	is InputStream containing a StringValue Record
60  	 */
StringValue(InputStream is)61     public StringValue(InputStream is) throws IOException {
62     	read(is);
63 	}
64 
65     /**
66 	 * Get the hex code for this particular <code>BIFFRecord</code>
67 	 *
68 	 * @return the hex code for <code>StringValue</code>
69 	 */
getBiffType()70     public short getBiffType() {
71         return PocketExcelConstants.FORMULA_STRING;
72     }
73 
74    	/**
75  	 * Reads a StringVlaue Record from an <code>InputStream</code>
76  	 *
77  	 * @param	input InputStream containing a StringValue Record
78  	 */
read(InputStream input)79     public int read(InputStream input) throws IOException {
80 
81 		cch[0] = (byte) input.read();
82 		cch[1] = (byte) input.read();
83         int numOfBytesRead = 1;
84 
85 		int strlen = EndianConverter.readShort(cch)*2;
86         rgch = new byte[strlen];
87         numOfBytesRead	+= input.read(rgch, 0, strlen);
88 
89         Debug.log(Debug.TRACE,"\tcch : "+ cch +
90                             " rgch : " + rgch);
91 
92         return numOfBytesRead;
93     }
94 
write(OutputStream output)95     public void write(OutputStream output) throws IOException {
96 
97 		output.write(getBiffType());
98 		output.write(cch);
99 		output.write(rgch);
100 
101 		Debug.log(Debug.TRACE,"Writing StringValue record");
102     }
103 
104 	/**
105 	 * Gets the <code>String</code> representing the cells contents
106 	 *
107 	 * @return the <code>String</code> representing the cells contents
108 	 */
getString()109 	public String getString() throws IOException {
110 		String name;
111 
112 		try {
113 			name = new String(rgch, "UTF-16LE");
114 		} catch (UnsupportedEncodingException e){
115 			name = "unknown";
116 		}
117         return name;
118 	}
119 
120 }
121