1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.potential.parsers;
39
40 import ffx.utilities.Keyword;
41
42 import java.io.BufferedReader;
43 import java.io.File;
44 import java.io.FileReader;
45 import java.io.IOException;
46 import java.util.Hashtable;
47 import java.util.logging.Level;
48 import java.util.logging.Logger;
49
50 import static java.lang.Integer.parseInt;
51
52
53
54
55
56
57
58 public class KeyFilter {
59
60 private static final Logger logger = Logger.getLogger(KeyFilter.class.getName());
61
62
63 public KeyFilter() {
64 }
65
66
67
68
69
70
71
72 public static Hashtable<String, Keyword> open(File keyFile) {
73 if (keyFile == null || !keyFile.exists() || !keyFile.canRead()) {
74 return null;
75 }
76 Hashtable<String, Keyword> keywordHash = loadSystemKeywords();
77 return open(keyFile, keywordHash);
78 }
79
80
81
82
83
84
85
86
87 public static Hashtable<String, Keyword> open(
88 File keyFile, Hashtable<String, Keyword> keywordHash) {
89 if (keyFile == null || !keyFile.exists() || !keyFile.canRead()) {
90 return null;
91 }
92 if (keywordHash == null) {
93 keywordHash = new Hashtable<>();
94 }
95 try (BufferedReader br = new BufferedReader(new FileReader(keyFile))) {
96 Keyword comments = new Keyword("COMMENTS");
97 keywordHash.put("COMMENTS", comments);
98 while (br.ready()) {
99 String s = br.readLine();
100 if (s == null) {
101 continue;
102 }
103 s = s.trim();
104 if (s.isEmpty()) {
105 continue;
106 }
107
108 if (s.startsWith("#") || s.toUpperCase().startsWith("ECHO")) {
109 comments.append(s);
110 } else {
111 int firstSpace = s.indexOf(" ");
112 String keyword, data;
113 if (firstSpace == -1) {
114 keyword = s.trim().toUpperCase();
115
116
117
118
119
120
121 if (keyword.equalsIgnoreCase("rattle")) {
122 data = "RATTLE";
123 } else {
124 data = null;
125 }
126 } else {
127 keyword = s.substring(0, firstSpace).toUpperCase();
128 data = s.substring(firstSpace).trim();
129 }
130 Keyword kd = keywordHash.get(keyword);
131 if (kd == null) {
132 kd = new Keyword(keyword);
133 keywordHash.put(keyword, kd);
134 }
135 if (data != null) {
136 kd.append(data);
137 }
138
139
140
141
142
143 if (keyword.equalsIgnoreCase("MULTIPOLE")) {
144 int[] mnum = {3, 1, 2, 3};
145 for (int i = 0; i < 4; i++) {
146 if (!br.ready()) {
147 System.out.println("Check for an invalid MULTIPOLE keyword.");
148 return null;
149 }
150 s = br.readLine();
151 if (s == null) {
152 logger.warning("Multipole format error.");
153 return null;
154 }
155 s = s.trim();
156 if (s.split(" +").length != mnum[i]) {
157 logger.warning("Multipole format error.");
158 return null;
159 }
160 kd.append(s);
161 }
162 } else if (keyword.equalsIgnoreCase("TORTORS")) {
163 String[] res = data.split(" +");
164 if (res.length < 7) {
165 logger.warning("TORTOR format error.");
166 return null;
167 }
168 int xres = parseInt(res[5]);
169 int yres = parseInt(res[6]);
170 for (int i = 0; i < xres * yres; i++) {
171 if (!br.ready()) {
172 System.out.println("Check for an invalid TORTOR keyword.");
173 return null;
174 }
175 s = br.readLine();
176 if (s == null) {
177 logger.warning("TORTOR format error.");
178 return null;
179 }
180 s = s.trim();
181 if (s.split(" +").length != 3) {
182 logger.warning("TORTOR format error.");
183 return null;
184 }
185 kd.append(s);
186 }
187 }
188 }
189 }
190 return keywordHash;
191 } catch (IOException e) {
192 System.err.println("Error reading Key File: " + e);
193 return null;
194 }
195 }
196
197
198
199
200
201
202 private static Hashtable<String, Keyword> loadSystemKeywords() {
203 File f = new File("/etc/ffx.conf");
204 Hashtable<String, Keyword> systemKeywords = new Hashtable<>();
205 if (f.exists() && f.canRead()) {
206 logger.info("Reading /etc/ffx.conf");
207 systemKeywords = KeyFilter.open(f, systemKeywords);
208 }
209 String path = System.getProperty("user.home") + File.separator + ".ffx/ffx.conf";
210 f = new File(path);
211 if (f.exists() && f.canRead()) {
212 logger.log(Level.INFO, "Reading {0}", path);
213 systemKeywords = KeyFilter.open(f, systemKeywords);
214 }
215 return systemKeywords;
216 }
217 }