1 19 20 package jxl.write.biff; 21 22 import java.util.HashMap ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.io.IOException ; 26 27 import common.Assert; 28 29 33 class SharedStrings 34 { 35 38 private HashMap strings; 39 40 43 private ArrayList stringList; 44 45 48 private int totalOccurrences; 49 50 53 public SharedStrings() 54 { 55 strings = new HashMap (100); 56 stringList = new ArrayList (100); 57 totalOccurrences = 0; 58 } 59 60 68 public int getIndex(String s) 69 { 70 Integer i = (Integer ) strings.get(s); 71 72 if (i == null) 73 { 74 i = new Integer (strings.size()); 75 strings.put(s, i); 76 stringList.add(s); 77 } 78 79 totalOccurrences++; 80 81 return i.intValue(); 82 } 83 84 90 public String get(int i) 91 { 92 return (String ) stringList.get(i); 93 } 94 95 101 public void write(File outputFile) throws IOException 102 { 103 int numStrings = 0; 106 int charsLeft = 0; 107 String 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 i = stringList.iterator(); 113 int stringIndex = 0; 114 while (i.hasNext() && charsLeft == 0) 115 { 116 curString = (String ) i.next(); 117 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 SSTContinueRecord cont = createContinueRecord(curString, 131 charsLeft, 132 outputFile); 133 134 while (i.hasNext()) 136 { 137 curString = (String ) 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 162 private SSTContinueRecord createContinueRecord 163 (String curString, int charsLeft, File outputFile) throws IOException 164 { 165 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 |