Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / CifDataParser.js
1 Clazz.declarePackage ("JU");
2 Clazz.load (["javajs.api.GenericCifDataParser", "java.util.Hashtable", "JU.SB"], "JU.CifDataParser", ["JU.Lst", "$.PT"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.reader = null;
5 this.br = null;
6 this.line = null;
7 this.str = null;
8 this.ich = 0;
9 this.cch = 0;
10 this.wasUnQuoted = false;
11 this.strPeeked = null;
12 this.ichPeeked = 0;
13 this.fieldCount = 0;
14 this.loopData = null;
15 this.fileHeader = null;
16 this.isHeader = true;
17 this.nullString = "\0";
18 this.fields = null;
19 Clazz.instantialize (this, arguments);
20 }, JU, "CifDataParser", null, javajs.api.GenericCifDataParser);
21 Clazz.prepareFields (c$, function () {
22 this.fileHeader =  new JU.SB ();
23 });
24 Clazz.defineMethod (c$, "setNullValue", 
25 function (nullString) {
26 this.nullString = nullString;
27 }, "~S");
28 Clazz.makeConstructor (c$, 
29 function () {
30 });
31 Clazz.overrideMethod (c$, "getLoopData", 
32 function (i) {
33 return this.loopData[i];
34 }, "~N");
35 Clazz.overrideMethod (c$, "getFieldCount", 
36 function () {
37 return this.fieldCount;
38 });
39 Clazz.overrideMethod (c$, "getField", 
40 function (i) {
41 return this.fields[i];
42 }, "~N");
43 Clazz.overrideMethod (c$, "set", 
44 function (reader, br) {
45 this.reader = reader;
46 this.br = br;
47 return this;
48 }, "javajs.api.GenericLineReader,java.io.BufferedReader");
49 Clazz.overrideMethod (c$, "getFileHeader", 
50 function () {
51 return this.fileHeader.toString ();
52 });
53 Clazz.overrideMethod (c$, "getAllCifData", 
54 function () {
55 this.line = "";
56 var key;
57 var data = null;
58 var allData =  new java.util.Hashtable ();
59 var models =  new JU.Lst ();
60 allData.put ("models", models);
61 try {
62 while ((key = this.getNextToken ()) != null) {
63 if (key.startsWith ("global_") || key.startsWith ("data_")) {
64 models.addLast (data =  new java.util.Hashtable ());
65 data.put ("name", key);
66 continue;
67 }if (key.startsWith ("loop_")) {
68 this.getAllCifLoopData (data);
69 continue;
70 }if (key.charAt (0) != '_') {
71 System.out.println ("CIF ERROR ? should be an underscore: " + key);
72 } else {
73 var value = this.getNextToken ();
74 if (value == null) {
75 System.out.println ("CIF ERROR ? end of file; data missing: " + key);
76 } else {
77 data.put (this.fixKey (key), value);
78 }}}
79 } catch (e) {
80 if (Clazz.exceptionOf (e, Exception)) {
81 } else {
82 throw e;
83 }
84 }
85 try {
86 if (this.br != null) this.br.close ();
87 } catch (e) {
88 if (Clazz.exceptionOf (e, Exception)) {
89 } else {
90 throw e;
91 }
92 }
93 return allData;
94 });
95 Clazz.defineMethod (c$, "getAllCifLoopData", 
96  function (data) {
97 var key;
98 var keyWords =  new JU.Lst ();
99 while ((key = this.peekToken ()) != null && key.charAt (0) == '_') {
100 key = this.fixKey (this.getTokenPeeked ());
101 keyWords.addLast (key);
102 data.put (key,  new JU.Lst ());
103 }
104 this.fieldCount = keyWords.size ();
105 if (this.fieldCount == 0) return;
106 this.loopData =  new Array (this.fieldCount);
107 while (this.getData ()) for (var i = 0; i < this.fieldCount; i++) (data.get (keyWords.get (i))).addLast (this.loopData[i]);
108
109
110 }, "java.util.Map");
111 Clazz.overrideMethod (c$, "readLine", 
112 function () {
113 try {
114 this.line = (this.reader == null ? this.br.readLine () : this.reader.readNextLine ());
115 if (this.line == null) return null;
116 if (this.isHeader) {
117 if (this.line.startsWith ("#")) this.fileHeader.append (this.line).appendC ('\n');
118  else this.isHeader = false;
119 }return this.line;
120 } catch (e) {
121 if (Clazz.exceptionOf (e, Exception)) {
122 return null;
123 } else {
124 throw e;
125 }
126 }
127 });
128 Clazz.overrideMethod (c$, "getData", 
129 function () {
130 for (var i = 0; i < this.fieldCount; ++i) if ((this.loopData[i] = this.getNextDataToken ()) == null) return false;
131
132 return (this.fieldCount > 0);
133 });
134 Clazz.overrideMethod (c$, "skipLoop", 
135 function () {
136 var str;
137 while ((str = this.peekToken ()) != null && str.charAt (0) == '_') this.getTokenPeeked ();
138
139 while (this.getNextDataToken () != null) {
140 }
141 });
142 Clazz.overrideMethod (c$, "getNextToken", 
143 function () {
144 while (!this.strHasMoreTokens ()) if (this.setStringNextLine () == null) return null;
145
146 return this.nextStrToken ();
147 });
148 Clazz.overrideMethod (c$, "getNextDataToken", 
149 function () {
150 var str = this.peekToken ();
151 if (str == null) return null;
152 if (this.wasUnQuoted) if (str.charAt (0) == '_' || str.startsWith ("loop_") || str.startsWith ("data_") || str.startsWith ("stop_") || str.startsWith ("global_")) return null;
153 return this.getTokenPeeked ();
154 });
155 Clazz.overrideMethod (c$, "peekToken", 
156 function () {
157 while (!this.strHasMoreTokens ()) if (this.setStringNextLine () == null) return null;
158
159 var ich = this.ich;
160 this.strPeeked = this.nextStrToken ();
161 this.ichPeeked = this.ich;
162 this.ich = ich;
163 return this.strPeeked;
164 });
165 Clazz.overrideMethod (c$, "getTokenPeeked", 
166 function () {
167 this.ich = this.ichPeeked;
168 return this.strPeeked;
169 });
170 Clazz.overrideMethod (c$, "fullTrim", 
171 function (str) {
172 var pt0 = -1;
173 var pt1 = str.length;
174 while (++pt0 < pt1 && JU.PT.isWhitespace (str.charAt (pt0))) {
175 }
176 while (--pt1 > pt0 && JU.PT.isWhitespace (str.charAt (pt1))) {
177 }
178 return str.substring (pt0, pt1 + 1);
179 }, "~S");
180 Clazz.overrideMethod (c$, "toUnicode", 
181 function (data) {
182 var pt;
183 try {
184 while ((pt = data.indexOf ('\\')) >= 0) {
185 var c = data.charCodeAt (pt + 1);
186 var ch = (c >= 65 && c <= 90 ? "ABX\u0394E\u03a6\u0393HI_K\u039bMNO\u03a0\u0398P\u03a3TY_\u03a9\u039e\u03a5Z".substring (c - 65, c - 64) : c >= 97 && c <= 122 ? "\u03b1\u03b2\u03c7\u03a4\u03a5\u03c6\u03b3\u03b7\u03b9_\u03ba\u03bb\u03bc\u03bd\u03bf\u03c0\u03b8\u03c1\u03c3\u03c4\u03c5_\u03c9\u03be\u03c5\u03b6".substring (c - 97, c - 96) : "_");
187 data = data.substring (0, pt) + ch + data.substring (pt + 2);
188 }
189 } catch (e) {
190 if (Clazz.exceptionOf (e, Exception)) {
191 } else {
192 throw e;
193 }
194 }
195 return data;
196 }, "~S");
197 Clazz.overrideMethod (c$, "parseLoopParameters", 
198 function (fields, fieldOf, propertyOf) {
199 var propertyCount = 0;
200 if (fields == null) {
201 this.fields =  new Array (100);
202 } else {
203 if (!JU.CifDataParser.htFields.containsKey (fields[0])) for (var i = fields.length; --i >= 0; ) JU.CifDataParser.htFields.put (fields[i], Integer.$valueOf (i));
204
205 for (var i = fields.length; --i >= 0; ) fieldOf[i] = -1;
206
207 propertyCount = fields.length;
208 }this.fieldCount = 0;
209 while (true) {
210 var str = this.peekToken ();
211 if (str == null) {
212 this.fieldCount = 0;
213 break;
214 }if (str.charAt (0) != '_') break;
215 var pt = this.fieldCount++;
216 str = this.fixKey (this.getTokenPeeked ());
217 if (fields == null) {
218 this.fields[propertyOf[pt] = fieldOf[pt] = pt] = str;
219 continue;
220 }var iField = JU.CifDataParser.htFields.get (str);
221 var i = (iField == null ? -1 : iField.intValue ());
222 if ((propertyOf[pt] = i) != -1) fieldOf[i] = pt;
223 }
224 if (this.fieldCount > 0) this.loopData =  new Array (this.fieldCount);
225 return propertyCount;
226 }, "~A,~A,~A");
227 Clazz.overrideMethod (c$, "fixKey", 
228 function (key) {
229 return (JU.PT.rep (key.startsWith ("_magnetic") ? key.substring (9) : key.startsWith ("_jana") ? key.substring (5) : key, ".", "_").toLowerCase ());
230 }, "~S");
231 Clazz.defineMethod (c$, "setString", 
232  function (str) {
233 this.str = this.line = str;
234 this.cch = (str == null ? 0 : str.length);
235 this.ich = 0;
236 }, "~S");
237 Clazz.defineMethod (c$, "setStringNextLine", 
238  function () {
239 this.setString (this.readLine ());
240 if (this.line == null || this.line.length == 0) return this.line;
241 if (this.line.charAt (0) != ';') {
242 if (this.str.startsWith ("###non-st#")) this.ich = 10;
243 return this.line;
244 }this.ich = 1;
245 var str = '\1' + this.line.substring (1) + '\n';
246 while (this.readLine () != null) {
247 if (this.line.startsWith (";")) {
248 str = str.substring (0, str.length - 1) + '\1' + this.line.substring (1);
249 break;
250 }str += this.line + '\n';
251 }
252 this.setString (str);
253 return str;
254 });
255 Clazz.defineMethod (c$, "strHasMoreTokens", 
256  function () {
257 if (this.str == null) return false;
258 var ch = '#';
259 while (this.ich < this.cch && ((ch = this.str.charAt (this.ich)) == ' ' || ch == '\t')) ++this.ich;
260
261 return (this.ich < this.cch && ch != '#');
262 });
263 Clazz.defineMethod (c$, "nextStrToken", 
264  function () {
265 if (this.ich == this.cch) return null;
266 var ichStart = this.ich;
267 var ch = this.str.charAt (ichStart);
268 if (ch != '\'' && ch != '"' && ch != '\1') {
269 this.wasUnQuoted = true;
270 while (this.ich < this.cch && (ch = this.str.charAt (this.ich)) != ' ' && ch != '\t') ++this.ich;
271
272 if (this.ich == ichStart + 1) if (this.nullString != null && (this.str.charAt (ichStart) == '.' || this.str.charAt (ichStart) == '?')) return this.nullString;
273 var s = this.str.substring (ichStart, this.ich);
274 return s;
275 }this.wasUnQuoted = false;
276 var chOpeningQuote = ch;
277 var previousCharacterWasQuote = false;
278 while (++this.ich < this.cch) {
279 ch = this.str.charAt (this.ich);
280 if (previousCharacterWasQuote && (ch == ' ' || ch == '\t')) break;
281 previousCharacterWasQuote = (ch == chOpeningQuote);
282 }
283 if (this.ich == this.cch) {
284 if (previousCharacterWasQuote) return this.str.substring (ichStart + 1, this.ich - 1);
285 return this.str.substring (ichStart, this.ich);
286 }++this.ich;
287 return this.str.substring (ichStart + 1, this.ich - 2);
288 });
289 c$.htFields = c$.prototype.htFields =  new java.util.Hashtable ();
290 Clazz.defineStatics (c$,
291 "grABC", "ABX\u0394E\u03a6\u0393HI_K\u039bMNO\u03a0\u0398P\u03a3TY_\u03a9\u039e\u03a5Z",
292 "grabc", "\u03b1\u03b2\u03c7\u03a4\u03a5\u03c6\u03b3\u03b7\u03b9_\u03ba\u03bb\u03bc\u03bd\u03bf\u03c0\u03b8\u03c1\u03c3\u03c4\u03c5_\u03c9\u03be\u03c5\u03b6");
293 });