KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > SharedStrings


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.write.biff;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import common.Assert;
28
29 /**
30  * The list of available shared strings. This class contains
31  * the labels used for the entire spreadsheet
32  */

33 class SharedStrings
34 {
35   /**
36    * All the strings in the spreadsheet, keyed on the string itself
37    */

38   private HashMap JavaDoc strings;
39
40   /**
41    * Contains the same strings, held in a list
42    */

43   private ArrayList JavaDoc stringList;
44
45   /**
46    * The total occurrence of strings in the workbook
47    */

48   private int totalOccurrences;
49
50   /**
51    * Constructor
52    */

53   public SharedStrings()
54   {
55     strings = new HashMap JavaDoc(100);
56     stringList = new ArrayList JavaDoc(100);
57     totalOccurrences = 0;
58   }
59
60   /**
61    * Gets the index for the string passed in. If the string is already
62    * present, then returns the index of that string, otherwise
63    * creates a new key-index mapping
64    *
65    * @param s the string whose index we want
66    * @return the index of the string
67    */

68   public int getIndex(String JavaDoc s)
69   {
70     Integer JavaDoc i = (Integer JavaDoc) strings.get(s);
71
72     if (i == null)
73     {
74       i = new Integer JavaDoc(strings.size());
75       strings.put(s, i);
76       stringList.add(s);
77     }
78
79     totalOccurrences++;
80
81     return i.intValue();
82   }
83
84   /**
85    * Gets the string at the specified index
86    *
87    * @param i the index of the string
88    * @return the string at the specified index
89    */

90   public String JavaDoc get(int i)
91   {
92     return (String JavaDoc) stringList.get(i);
93   }
94
95   /**
96    * Writes out the shared string table
97    *
98    * @param outputFile the binary output file
99    * @exception IOException
100    */

101   public void write(File outputFile) throws IOException JavaDoc
102   {
103     // Thanks to Guenther for contributing the ExtSST implementation portion
104
// of this method
105
int numStrings = 0;
106     int charsLeft = 0;
107     String JavaDoc curString = null;
108     SSTRecord sst = new SSTRecord(totalOccurrences, stringList.size());
109     ExtendedSSTRecord extsst = new ExtendedSSTRecord(stringList.size());
110     int bucketSize = extsst.getNumberOfStringsPerBucket();
111
112     Iterator JavaDoc i = stringList.iterator();
113     int stringIndex = 0;
114     while (i.hasNext() && charsLeft == 0)
115     {
116       curString = (String JavaDoc) i.next();
117       // offset + header bytes
118
int relativePosition = sst.getOffset() + 4;
119       charsLeft = sst.add(curString);
120       if ((stringIndex % bucketSize) == 0) {
121         extsst.addString(outputFile.getPos(), relativePosition);
122       }
123       stringIndex++;
124     }
125     outputFile.write(sst);
126
127     if (charsLeft != 0 || i.hasNext())
128     {
129       // Add the remainder of the string to the continue record
130
SSTContinueRecord cont = createContinueRecord(curString,
131                                                     charsLeft,
132                                                     outputFile);
133
134       // Carry on looping through the array until all the strings are done
135
while (i.hasNext())
136       {
137         curString = (String JavaDoc) i.next();
138         int relativePosition = cont.getOffset() + 4;
139         charsLeft = cont.add(curString);
140         if ((stringIndex % bucketSize) == 0) {
141           extsst.addString(outputFile.getPos(), relativePosition);
142         }
143         stringIndex++;
144
145         if (charsLeft != 0)
146         {
147           outputFile.write(cont);
148           cont = createContinueRecord(curString, charsLeft, outputFile);
149         }
150       }
151
152       outputFile.write(cont);
153     }
154
155     outputFile.write(extsst);
156   }
157
158   /**
159    * Creates and returns a continue record using the left over bits and
160    * pieces
161    */

162   private SSTContinueRecord createContinueRecord
163     (String JavaDoc curString, int charsLeft, File outputFile) throws IOException JavaDoc
164   {
165     // Set up the remainder of the string in the continue record
166
SSTContinueRecord cont = null;
167     while (charsLeft != 0)
168     {
169       cont = new SSTContinueRecord();
170
171       if (charsLeft == curString.length())
172       {
173         charsLeft = cont.setFirstString(curString, true);
174       }
175       else
176       {
177         charsLeft = cont.setFirstString
178           (curString.substring(curString.length() - charsLeft), false);
179       }
180
181       if (charsLeft != 0)
182       {
183         outputFile.write(cont);
184         cont = new SSTContinueRecord();
185       }
186     }
187
188     return cont;
189   }
190 }
191
Popular Tags