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 () {
10 this.wasUnQuoted = false;
11 this.strPeeked = null;
15 this.fileHeader = null;
17 this.nullString = "\0";
19 Clazz.instantialize (this, arguments);
20 }, JU, "CifDataParser", null, javajs.api.GenericCifDataParser);
21 Clazz.prepareFields (c$, function () {
22 this.fileHeader = new JU.SB ();
24 Clazz.defineMethod (c$, "setNullValue",
25 function (nullString) {
26 this.nullString = nullString;
28 Clazz.makeConstructor (c$,
31 Clazz.overrideMethod (c$, "getLoopData",
33 return this.loopData[i];
35 Clazz.overrideMethod (c$, "getFieldCount",
37 return this.fieldCount;
39 Clazz.overrideMethod (c$, "getField",
41 return this.fields[i];
43 Clazz.overrideMethod (c$, "set",
44 function (reader, br) {
48 }, "javajs.api.GenericLineReader,java.io.BufferedReader");
49 Clazz.overrideMethod (c$, "getFileHeader",
51 return this.fileHeader.toString ();
53 Clazz.overrideMethod (c$, "getAllCifData",
58 var allData = new java.util.Hashtable ();
59 var models = new JU.Lst ();
60 allData.put ("models", models);
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);
67 }if (key.startsWith ("loop_")) {
68 this.getAllCifLoopData (data);
70 }if (key.charAt (0) != '_') {
71 System.out.println ("CIF ERROR ? should be an underscore: " + key);
73 var value = this.getNextToken ();
75 System.out.println ("CIF ERROR ? end of file; data missing: " + key);
77 data.put (this.fixKey (key), value);
80 if (Clazz.exceptionOf (e, Exception)) {
86 if (this.br != null) this.br.close ();
88 if (Clazz.exceptionOf (e, Exception)) {
95 Clazz.defineMethod (c$, "getAllCifLoopData",
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 ());
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]);
111 Clazz.overrideMethod (c$, "readLine",
114 this.line = (this.reader == null ? this.br.readLine () : this.reader.readNextLine ());
115 if (this.line == null) return null;
117 if (this.line.startsWith ("#")) this.fileHeader.append (this.line).appendC ('\n');
118 else this.isHeader = false;
121 if (Clazz.exceptionOf (e, Exception)) {
128 Clazz.overrideMethod (c$, "getData",
130 for (var i = 0; i < this.fieldCount; ++i) if ((this.loopData[i] = this.getNextDataToken ()) == null) return false;
132 return (this.fieldCount > 0);
134 Clazz.overrideMethod (c$, "skipLoop",
137 while ((str = this.peekToken ()) != null && str.charAt (0) == '_') this.getTokenPeeked ();
139 while (this.getNextDataToken () != null) {
142 Clazz.overrideMethod (c$, "getNextToken",
144 while (!this.strHasMoreTokens ()) if (this.setStringNextLine () == null) return null;
146 return this.nextStrToken ();
148 Clazz.overrideMethod (c$, "getNextDataToken",
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 ();
155 Clazz.overrideMethod (c$, "peekToken",
157 while (!this.strHasMoreTokens ()) if (this.setStringNextLine () == null) return null;
160 this.strPeeked = this.nextStrToken ();
161 this.ichPeeked = this.ich;
163 return this.strPeeked;
165 Clazz.overrideMethod (c$, "getTokenPeeked",
167 this.ich = this.ichPeeked;
168 return this.strPeeked;
170 Clazz.overrideMethod (c$, "fullTrim",
173 var pt1 = str.length;
174 while (++pt0 < pt1 && JU.PT.isWhitespace (str.charAt (pt0))) {
176 while (--pt1 > pt0 && JU.PT.isWhitespace (str.charAt (pt1))) {
178 return str.substring (pt0, pt1 + 1);
180 Clazz.overrideMethod (c$, "toUnicode",
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);
190 if (Clazz.exceptionOf (e, Exception)) {
197 Clazz.overrideMethod (c$, "parseLoopParameters",
198 function (fields, fieldOf, propertyOf) {
199 var propertyCount = 0;
200 if (fields == null) {
201 this.fields = new Array (100);
203 if (!JU.CifDataParser.htFields.containsKey (fields[0])) for (var i = fields.length; --i >= 0; ) JU.CifDataParser.htFields.put (fields[i], Integer.$valueOf (i));
205 for (var i = fields.length; --i >= 0; ) fieldOf[i] = -1;
207 propertyCount = fields.length;
208 }this.fieldCount = 0;
210 var str = this.peekToken ();
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;
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;
224 if (this.fieldCount > 0) this.loopData = new Array (this.fieldCount);
225 return propertyCount;
227 Clazz.overrideMethod (c$, "fixKey",
229 return (JU.PT.rep (key.startsWith ("_magnetic") ? key.substring (9) : key.startsWith ("_jana") ? key.substring (5) : key, ".", "_").toLowerCase ());
231 Clazz.defineMethod (c$, "setString",
233 this.str = this.line = str;
234 this.cch = (str == null ? 0 : str.length);
237 Clazz.defineMethod (c$, "setStringNextLine",
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;
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);
250 }str += this.line + '\n';
252 this.setString (str);
255 Clazz.defineMethod (c$, "strHasMoreTokens",
257 if (this.str == null) return false;
259 while (this.ich < this.cch && ((ch = this.str.charAt (this.ich)) == ' ' || ch == '\t')) ++this.ich;
261 return (this.ich < this.cch && ch != '#');
263 Clazz.defineMethod (c$, "nextStrToken",
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;
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);
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);
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);
287 return this.str.substring (ichStart + 1, this.ich - 2);
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");