Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / CompoundDocument.js
1 Clazz.declarePackage ("JU");
2 Clazz.load (["JU.BinaryDocument", "$.CompoundDocHeader", "$.Lst"], "JU.CompoundDocument", ["java.io.DataInputStream", "JU.CompoundDocDirEntry", "$.SB", "$.ZipData"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.header = null;
5 this.directory = null;
6 this.rootEntry = null;
7 this.SAT = null;
8 this.SSAT = null;
9 this.sectorSize = 0;
10 this.shortSectorSize = 0;
11 this.nShortSectorsPerStandardSector = 0;
12 this.nIntPerSector = 0;
13 this.nDirEntriesperSector = 0;
14 this.data = null;
15 Clazz.instantialize (this, arguments);
16 }, JU, "CompoundDocument", JU.BinaryDocument);
17 Clazz.prepareFields (c$, function () {
18 this.header =  new JU.CompoundDocHeader (this);
19 this.directory =  new JU.Lst ();
20 });
21 Clazz.makeConstructor (c$, 
22 function () {
23 Clazz.superConstructor (this, JU.CompoundDocument);
24 this.isBigEndian = true;
25 });
26 Clazz.overrideMethod (c$, "setStream", 
27 function (jzt, bis, isBigEndian) {
28 this.jzt = jzt;
29 if (!this.isRandom) {
30 this.stream =  new java.io.DataInputStream (bis);
31 }this.stream.mark (2147483647);
32 if (!this.readHeader ()) return;
33 this.getSectorAllocationTable ();
34 this.getShortSectorAllocationTable ();
35 this.getDirectoryTable ();
36 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~B");
37 Clazz.defineMethod (c$, "getDirectory", 
38 function () {
39 return this.directory;
40 });
41 Clazz.defineMethod (c$, "getDirectoryListing", 
42 function (separator) {
43 var str = "";
44 for (var i = 0; i < this.directory.size (); i++) {
45 var thisEntry = this.directory.get (i);
46 if (!thisEntry.isEmpty) str += separator + thisEntry.entryName + "\tlen=" + thisEntry.lenStream + "\tSID=" + thisEntry.SIDfirstSector + (thisEntry.isStandard ? "\tfileOffset=" + this.getOffset (thisEntry.SIDfirstSector) : "");
47 }
48 return str;
49 }, "~S");
50 Clazz.defineMethod (c$, "getAllData", 
51 function () {
52 return this.getAllDataFiles (null, null);
53 });
54 Clazz.overrideMethod (c$, "getAllDataMapped", 
55 function (prefix, binaryFileList, fileData) {
56 fileData.put ("#Directory_Listing", this.getDirectoryListing ("|"));
57 binaryFileList = "|" + binaryFileList + "|";
58 for (var i = 0; i < this.directory.size (); i++) {
59 var thisEntry = this.directory.get (i);
60 if (!thisEntry.isEmpty && thisEntry.entryType != 5) {
61 var name = thisEntry.entryName;
62 System.out.println ("CompoundDocument file " + name);
63 var isBinary = (binaryFileList.indexOf ("|" + name + "|") >= 0);
64 if (isBinary) name += ":asBinaryString";
65 var data =  new JU.SB ();
66 data.append ("BEGIN Directory Entry ").append (name).append ("\n");
67 data.appendSB (this.getEntryAsString (thisEntry, isBinary));
68 data.append ("\nEND Directory Entry ").append (name).append ("\n");
69 fileData.put (prefix + "/" + name, data.toString ());
70 }}
71 this.close ();
72 }, "~S,~S,java.util.Map");
73 Clazz.overrideMethod (c$, "getAllDataFiles", 
74 function (binaryFileList, firstFile) {
75 if (firstFile != null) {
76 for (var i = 0; i < this.directory.size (); i++) {
77 var thisEntry = this.directory.get (i);
78 if (thisEntry.entryName.equals (firstFile)) {
79 this.directory.remove (i);
80 this.directory.add (1, thisEntry);
81 break;
82 }}
83 }this.data =  new JU.SB ();
84 this.data.append ("Compound Document File Directory: ");
85 this.data.append (this.getDirectoryListing ("|"));
86 this.data.append ("\n");
87 binaryFileList = "|" + binaryFileList + "|";
88 for (var i = 0; i < this.directory.size (); i++) {
89 var thisEntry = this.directory.get (i);
90 if (!thisEntry.isEmpty && thisEntry.entryType != 5) {
91 var name = thisEntry.entryName;
92 if (name.endsWith (".gz")) name = name.substring (0, name.length - 3);
93 this.data.append ("BEGIN Directory Entry ").append (name).append ("\n");
94 this.data.appendSB (this.getEntryAsString (thisEntry, binaryFileList.indexOf ("|" + thisEntry.entryName + "|") >= 0));
95 this.data.append ("\n");
96 this.data.append ("END Directory Entry ").append (thisEntry.entryName).append ("\n");
97 }}
98 this.close ();
99 return this.data;
100 }, "~S,~S");
101 Clazz.defineMethod (c$, "getFileAsString", 
102 function (entryName) {
103 for (var i = 0; i < this.directory.size (); i++) {
104 var thisEntry = this.directory.get (i);
105 if (thisEntry.entryName.equals (entryName)) return this.getEntryAsString (thisEntry, false);
106 }
107 return  new JU.SB ();
108 }, "~S");
109 Clazz.defineMethod (c$, "getOffset", 
110  function (SID) {
111 return (SID + 1) * this.sectorSize;
112 }, "~N");
113 Clazz.defineMethod (c$, "gotoSector", 
114  function (SID) {
115 this.seek (this.getOffset (SID));
116 }, "~N");
117 Clazz.defineMethod (c$, "readHeader", 
118  function () {
119 if (!this.header.readData ()) return false;
120 this.sectorSize = 1 << this.header.sectorPower;
121 this.shortSectorSize = 1 << this.header.shortSectorPower;
122 this.nShortSectorsPerStandardSector = Clazz.doubleToInt (this.sectorSize / this.shortSectorSize);
123 this.nIntPerSector = Clazz.doubleToInt (this.sectorSize / 4);
124 this.nDirEntriesperSector = Clazz.doubleToInt (this.sectorSize / 128);
125 return true;
126 });
127 Clazz.defineMethod (c$, "getSectorAllocationTable", 
128  function () {
129 var nSID = 0;
130 var thisSID;
131 this.SAT =  Clazz.newIntArray (this.header.nSATsectors * this.nIntPerSector + 109, 0);
132 try {
133 for (var i = 0; i < 109; i++) {
134 thisSID = this.header.MSAT0[i];
135 if (thisSID < 0) break;
136 this.gotoSector (thisSID);
137 for (var j = 0; j < this.nIntPerSector; j++) {
138 this.SAT[nSID++] = this.readInt ();
139 }
140 }
141 var nMaster = this.header.nAdditionalMATsectors;
142 thisSID = this.header.SID_MSAT_next;
143 var MSAT =  Clazz.newIntArray (this.nIntPerSector, 0);
144 out : while (nMaster-- > 0 && thisSID >= 0) {
145 this.gotoSector (thisSID);
146 for (var i = 0; i < this.nIntPerSector; i++) MSAT[i] = this.readInt ();
147
148 for (var i = 0; i < this.nIntPerSector - 1; i++) {
149 thisSID = MSAT[i];
150 if (thisSID < 0) break out;
151 this.gotoSector (thisSID);
152 for (var j = this.nIntPerSector; --j >= 0; ) this.SAT[nSID++] = this.readInt ();
153
154 }
155 thisSID = MSAT[this.nIntPerSector - 1];
156 }
157 } catch (e) {
158 if (Clazz.exceptionOf (e, Exception)) {
159 System.out.println (e.toString ());
160 } else {
161 throw e;
162 }
163 }
164 });
165 Clazz.defineMethod (c$, "getShortSectorAllocationTable", 
166  function () {
167 var nSSID = 0;
168 var thisSID = this.header.SID_SSAT_start;
169 var nMax = this.header.nSSATsectors * this.nIntPerSector;
170 this.SSAT =  Clazz.newIntArray (nMax, 0);
171 try {
172 while (thisSID > 0 && nSSID < nMax) {
173 this.gotoSector (thisSID);
174 for (var j = 0; j < this.nIntPerSector; j++) {
175 this.SSAT[nSSID++] = this.readInt ();
176 }
177 thisSID = this.SAT[thisSID];
178 }
179 } catch (e) {
180 if (Clazz.exceptionOf (e, Exception)) {
181 System.out.println (e.toString ());
182 } else {
183 throw e;
184 }
185 }
186 });
187 Clazz.defineMethod (c$, "getDirectoryTable", 
188  function () {
189 var thisSID = this.header.SID_DIR_start;
190 var thisEntry;
191 this.rootEntry = null;
192 try {
193 while (thisSID > 0) {
194 this.gotoSector (thisSID);
195 for (var j = this.nDirEntriesperSector; --j >= 0; ) {
196 thisEntry =  new JU.CompoundDocDirEntry (this);
197 thisEntry.readData ();
198 if (thisEntry.lenStream > 0) {
199 this.directory.addLast (thisEntry);
200 }if (thisEntry.entryType == 5) this.rootEntry = thisEntry;
201 }
202 thisSID = this.SAT[thisSID];
203 }
204 } catch (e) {
205 if (Clazz.exceptionOf (e, Exception)) {
206 System.out.println (e.toString ());
207 } else {
208 throw e;
209 }
210 }
211 });
212 Clazz.defineMethod (c$, "getEntryAsString", 
213  function (thisEntry, asBinaryString) {
214 if (thisEntry.isEmpty) return  new JU.SB ();
215 return (thisEntry.isStandard ? this.getStandardStringData (thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString) : this.getShortStringData (thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString));
216 }, "JU.CompoundDocDirEntry,~B");
217 Clazz.defineMethod (c$, "getStandardStringData", 
218  function (thisSID, nBytes, asBinaryString) {
219 var data =  new JU.SB ();
220 var byteBuf =  Clazz.newByteArray (this.sectorSize, 0);
221 var gzipData =  new JU.ZipData (nBytes);
222 try {
223 while (thisSID > 0 && nBytes > 0) {
224 this.gotoSector (thisSID);
225 nBytes = this.getSectorData (data, byteBuf, this.sectorSize, nBytes, asBinaryString, gzipData);
226 thisSID = this.SAT[thisSID];
227 }
228 if (nBytes == -9999) return  new JU.SB ();
229 } catch (e) {
230 if (Clazz.exceptionOf (e, Exception)) {
231 System.out.println (e.toString ());
232 } else {
233 throw e;
234 }
235 }
236 if (gzipData.isEnabled) gzipData.addTo (this.jzt, data);
237 return data;
238 }, "~N,~N,~B");
239 Clazz.defineMethod (c$, "getSectorData", 
240  function (data, byteBuf, nSectorBytes, nBytes, asBinaryString, gzipData) {
241 this.readByteArray (byteBuf, 0, byteBuf.length);
242 var n = gzipData.addBytes (byteBuf, nSectorBytes, nBytes);
243 if (n >= 0) return n;
244 if (asBinaryString) {
245 for (var i = 0; i < nSectorBytes; i++) {
246 data.append (Integer.toHexString (byteBuf[i] & 0xFF)).appendC (' ');
247 if (--nBytes < 1) break;
248 }
249 } else {
250 for (var i = 0; i < nSectorBytes; i++) {
251 if (byteBuf[i] == 0) return -9999;
252 data.appendC (String.fromCharCode (byteBuf[i]));
253 if (--nBytes < 1) break;
254 }
255 }return nBytes;
256 }, "JU.SB,~A,~N,~N,~B,JU.ZipData");
257 Clazz.defineMethod (c$, "getShortStringData", 
258  function (shortSID, nBytes, asBinaryString) {
259 var data =  new JU.SB ();
260 if (this.rootEntry == null) return data;
261 var thisSID = this.rootEntry.SIDfirstSector;
262 var ptShort = 0;
263 var byteBuf =  Clazz.newByteArray (this.shortSectorSize, 0);
264 var gzipData =  new JU.ZipData (nBytes);
265 try {
266 while (thisSID >= 0 && shortSID >= 0 && nBytes > 0) {
267 while (shortSID - ptShort >= this.nShortSectorsPerStandardSector) {
268 ptShort += this.nShortSectorsPerStandardSector;
269 thisSID = this.SAT[thisSID];
270 }
271 this.seek (this.getOffset (thisSID) + (shortSID - ptShort) * this.shortSectorSize);
272 nBytes = this.getSectorData (data, byteBuf, this.shortSectorSize, nBytes, asBinaryString, gzipData);
273 shortSID = this.SSAT[shortSID];
274 }
275 } catch (e) {
276 if (Clazz.exceptionOf (e, Exception)) {
277 System.out.println (data.toString ());
278 System.out.println ("reader error in CompoundDocument " + e.toString ());
279 } else {
280 throw e;
281 }
282 }
283 if (gzipData.isEnabled) gzipData.addTo (this.jzt, data);
284 return data;
285 }, "~N,~N,~B");
286 });