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.IOException;
27 import java.io.UnsupportedEncodingException;
28 import java.io.OutputStream;
29 import java.io.InputStream;
30 
31 import org.openoffice.xmerge.util.Debug;
32 import org.openoffice.xmerge.converter.xml.sxc.pexcel.PocketExcelConstants;
33 
34 /**
35  * Represents a BoundSheet Record which describes the name of a worksheet
36  */
37 public class BoundSheet implements BIFFRecord {
38 
39     private byte	reserved;
40     private byte	cch;
41     private byte[]	sheetName;
42 
43 	/**
44 	 * Constructs a pocket Excel Document assigns it the document name passed in
45  	 *
46 	 * @param	name name of the worksheet represented
47 	 */
BoundSheet(String name)48     public BoundSheet(String name) throws IOException {
49 		setSheetName(name);
50 		reserved = 0;
51     }
52 
53 	/**
54  	 * Constructs a pocket Excel Document from the
55  	 * <code>InputStream</code> and assigns it the document name passed in
56  	 *
57  	 * @param	is InputStream containing a Pocket Excel Data file.
58  	 */
BoundSheet(InputStream is)59     public BoundSheet(InputStream is) throws IOException {
60 		read(is);
61     }
62 
63 	/**
64  	 * Sets the worksheet name. The sheetname length must be doubled as the
65 	 * String is stored in unicode format.
66  	 *
67  	 * @param	sheetName	worksheet name
68  	 */
setSheetName(String sheetName)69     void setSheetName(String sheetName) throws IOException {
70 		this.cch		= (byte) sheetName.length();
71 		this.sheetName	= new byte[cch*2];
72         this.sheetName	= sheetName.getBytes("UTF-16LE");
73     }
74 
getSheetName()75     public String getSheetName() {
76 		String name;
77 
78 		try {
79 			name = new String(sheetName, "UTF-16LE");
80 		} catch (UnsupportedEncodingException e){
81 			name = "unknown";
82 		}
83         return name;
84     }
85 
86      /**
87 	 * Get the hex code for this particular <code>BIFFRecord</code>
88 	 *
89 	 * @return the hex code for <code>BoundSheet</code>
90 	 */
getBiffType()91     public short getBiffType() {
92         return PocketExcelConstants.BOUND_SHEET;
93     }
94 
95      /**
96 	 * Write this particular <code>BIFFRecord</code> to the <code>OutputStream</code>
97 	 *
98 	 * @param output the <code>OutputStream</code>
99 	 */
write(OutputStream output)100     public void write(OutputStream output) throws IOException {
101 
102 		output.write(getBiffType());
103 		output.write(reserved);
104 		output.write(cch);
105 		output.write(sheetName);
106 
107 		Debug.log(Debug.TRACE,"Writing BoundSheet record");
108     }
109 
110 	/**
111  	 * Reads a BoundSheet from the <code>InputStream</code> The byte array
112 	 * must be twice the size of the String as it uses unicode.
113  	 *
114  	 * @param	input InputStream containing the record data
115  	 */
read(InputStream input)116     public int read(InputStream input) throws IOException {
117 
118         reserved			= (byte) input.read();
119         cch					= (byte) input.read();
120 		int numOfBytesRead = 2;
121 		int strLen = cch*2;
122 		sheetName	= new byte[strLen];
123         numOfBytesRead		+= input.read(sheetName, 0, strLen);
124 
125        	Debug.log(Debug.TRACE,"\tReserved : "+ reserved +
126                             " cch : " + cch +
127                             " sheetName : " + new String(sheetName,"UTF-16LE"));
128 
129         return numOfBytesRead;
130     }
131 
132 }
133