Skip to content

Commit b7e620f

Browse files
committed
CIFDataParser allow selective reading
1 parent 5155425 commit b7e620f

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

sources/net.sf.j2s.java.core/src/javajs/util/CifDataParser.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ public String getFileHeader() {
242242
*/
243243
@Override
244244
public Map<String, Object> getAllCifData() {
245+
return getAllCifDataType();
246+
}
247+
public Map<String, Object> getAllCifDataType(String... types) {
248+
if (types != null) {
249+
if (types.length == 0)
250+
types = null;
251+
else
252+
for (int i = 0; i < types.length; i++)
253+
types[i] = fixKey(types[i]);
254+
}
245255
line = "";
246256
String key;
247257
Map<String, Object> data = null, data0 = null;
@@ -259,7 +269,7 @@ public Map<String, Object> getAllCifData() {
259269
continue;
260270
}
261271
if (key.startsWith("loop_")) {
262-
getAllCifLoopData(data);
272+
getAllCifLoopData(data, types);
263273
continue;
264274
}
265275
if (key.startsWith("save_")) {
@@ -286,7 +296,9 @@ public Map<String, Object> getAllCifData() {
286296
if (value == null) {
287297
System.out.println("CIF ERROR ? end of file; data missing: " + key);
288298
} else {
289-
data.put(fixKey(key), value);
299+
key = fixKey(key);
300+
if (types == null || checkKey(types, key))
301+
data.put(key, value);
290302
}
291303
}
292304
}
@@ -305,33 +317,49 @@ public Map<String, Object> getAllCifData() {
305317
}
306318

307319
/**
308-
* create our own list of keywords and for each one create a list
309-
* of data associated with that keyword. For example, a list of all
310-
* x coordinates, then a list of all y coordinates, etc.
320+
* create our own list of keywords and for each one create a list of data
321+
* associated with that keyword. For example, a list of all x coordinates,
322+
* then a list of all y coordinates, etc.
311323
*
312324
* @param data
313325
* @throws Exception
314326
*/
315327
@SuppressWarnings("unchecked")
316-
private void getAllCifLoopData(Map<String, Object> data) throws Exception {
328+
private void getAllCifLoopData(Map<String, Object> data, String[] types)
329+
throws Exception {
317330
String key;
318331
Lst<String> keyWords = new Lst<String>();
319332
Object o;
320-
while ((o = peekToken()) != null && o instanceof String && ((String) o).charAt(0) == '_') {
333+
boolean skipping = false;
334+
while ((o = peekToken()) != null && o instanceof String
335+
&& ((String) o).charAt(0) == '_') {
321336
key = fixKey((String) getTokenPeeked());
322337
keyWords.addLast(key);
338+
if (types == null || checkKey(types, key))
323339
data.put(key, new Lst<String>());
340+
else
341+
skipping = true;
324342
}
325343
columnCount = keyWords.size();
326344
if (columnCount == 0)
327345
return;
328346
isLoop = true;
347+
if (skipping)
348+
skipLoop(false);
349+
else
329350
while (getData())
330351
for (int i = 0; i < columnCount; i++)
331352
((Lst<Object>)data.get(keyWords.get(i))).addLast(columnData[i]);
332353
isLoop = false;
333354
}
334355

356+
private boolean checkKey(String[] types, String key) {
357+
for (int i = 0; i < types.length; i++)
358+
if (key.startsWith(types[i]))
359+
return true;
360+
return false;
361+
}
362+
335363
@Override
336364
public String readLine() {
337365
try {
@@ -893,6 +921,7 @@ protected Object getQuotedStringOrObject(char ch) {
893921
} catch (Exception e) {
894922
System.out.println("exception in CifDataParser ; " + e);
895923
}
924+
return "[";
896925
case ']':
897926
ich++;
898927
return "]";

0 commit comments

Comments
 (0)