1 package gov.nasa.pds.ltdt.gui;
2
3 import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
4 import gov.nasa.pds.ltdt.gui.util.DictionaryUtility;
5 import gov.nasa.pds.ltdt.gui.util.Utility;
6 import gov.nasa.pds.tools.dict.Definition;
7 import gov.nasa.pds.tools.dict.Dictionary;
8 import gov.nasa.pds.tools.dict.ElementDefinition;
9 import gov.nasa.pds.tools.dict.GroupDefinition;
10 import gov.nasa.pds.tools.dict.ObjectDefinition;
11 import gov.nasa.pds.tools.dict.parser.DictionaryParser;
12 import gov.nasa.pds.tools.label.parser.ParseException;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Properties;
25 import java.util.SortedMap;
26 import java.util.TreeMap;
27
28 import javax.swing.JOptionPane;
29
30 /***
31 * A TreeMap (sorted Map) structure to hold generalized properties of object/elements
32 * of dictionaries in one place. The key is sorted by combining keyword:namespace
33 *
34 * This structure also supports expansion of dictionary keywords such as object, elements
35 *
36 * The standard representation of namespace:keyword is swapped intentionally in this class
37 * in order to support the requirement that the keyword list to be grouped and sorted
38 * by keyword names
39 *
40 * @author jwang
41 *
42 */
43 public class KeywordMap extends TreeMap {
44
45
46
47
48
49 private Dictionary dict = null;
50
51
52
53
54 private String message="";
55
56 public KeywordMap () {
57 super();
58 }
59
60 /***
61 * Given a list of dictionaries and insert all keywords to the keyword map
62 * @param props
63 * @param window
64 * @param dList A string of dictionary paths with : as delimeter
65 * @throws MalformedURLException
66 */
67 public void addDictionaries (Properties props, MainWindow window, String dList) {
68
69
70
71
72
73 List dListArray = DictionaryUtility.getDictionaryNameList(dList);
74 boolean parsingFailed = false;
75
76
77
78 for (int i=0; i<dListArray.size(); i++) {
79
80 try {
81 if (addSingleDictionary(props, window, Utility.toURL((String)dListArray.get(i)))!=LTDTKeys.SUCCESS) parsingFailed = true;
82 }
83 catch (MalformedURLException ex) {
84
85 }
86
87 }
88
89 }
90
91 /***
92 * Add keywords from an entire dictionary to the keyword map
93 * @param props
94 * @param window
95 * @param dictionaryString
96 * @return SUCCESS, WARNING
97 */
98
99 public int addSingleDictionary (Properties props, MainWindow window, String dictionaryString) {
100 int parseStatus = LTDTKeys.SUCCESS;
101
102
103 String dictionary=dictionaryString.split(LTDTKeys.DICTIONARY_DELIMITER)[0];
104 try {
105
106 parseStatus = addSingleDictionary(props, window, Utility.toURL(dictionary));
107 }
108 catch (MalformedURLException ex ) {
109
110 }
111 return parseStatus;
112 }
113
114
115 public int addSingleDictionary (Properties props, MainWindow window, URL dictionaryURL) {
116 int parseStatus = 0;
117
118
119
120
121
122 try {
123
124 dict = DictionaryParser.parse(dictionaryURL);
125
126
127
128 if ((dictionaryURL.toString()).equals(Utility.toURL(Utility.getProjectWDDFileName(props)).toString()))
129 DictionaryUtility.setStatusType(dict, LTDTKeys.WDDSTATUSTYPE);
130
131
132 if ("PASS".equals(dict.getStatus())) {
133 generateMapInfo(dictionaryURL, dict);
134
135 parseStatus= LTDTKeys.SUCCESS;
136
137 }
138 else if ("FAIL".equals(dict.getStatus())) {
139
140 JOptionPane.showMessageDialog(window, "An error occurred importing the specified data dictionary:\n"+
141 dictionaryURL.toString() + "\n\nPlease specify a valid data dictionary for import.",
142 "Dictionary Import Error", JOptionPane.ERROR_MESSAGE);
143
144 parseStatus = LTDTKeys.ERROR;
145
146 }
147
148 }
149 catch (ParseException pe) {
150
151 JOptionPane.showMessageDialog(window, "An error occurred importing the specified data dictionary:\n"+
152 dictionaryURL.toString() + "\n\nPlease specify a valid data dictionary for import.",
153 "Dictionary Import Error", JOptionPane.ERROR_MESSAGE);
154
155 parseStatus = LTDTKeys.ERROR;
156
157 }
158 catch (IOException ie) {
159 JOptionPane.showMessageDialog(window, "An error occurred importing the specified data dictionary:\n"+
160 dictionaryURL.toString() + "\n\nPlease specify a valid data dictionary for import.",
161 "Dictionary Import Error", JOptionPane.ERROR_MESSAGE);
162
163 parseStatus = LTDTKeys.ERROR;
164 }
165
166 return parseStatus;
167 }
168
169
170
171
172 /***
173 * Generate Keyword Map for one data dictionary, referred by url
174 * @param url
175 */
176 private void generateMapInfo(URL url, Dictionary dict) {
177
178
179 Map allDefs=null;
180 int kwdType = 0;
181 String identifier = null;
182
183 allDefs = dict.getDefinitions();
184
185 int count=0;
186 if (allDefs != null) {
187 for (Iterator it=allDefs.keySet().iterator(); it.hasNext(); ) {
188
189
190 Object key = it.next();
191 identifier = key.toString().trim();
192
193
194
195
196
197
198
199
200 KeywordProperty kp = new KeywordProperty();
201
202
203
204 kp.setIdentifier(identifier);
205
206
207 Definition def = dict.getDefinition(identifier);
208
209
210 kp.setKwdDef(def);
211
212
213 if (def instanceof ObjectDefinition) {
214
215 kp.setKwdType(LTDTKeys.OBJECT_TYPE);
216 }
217 else if (def instanceof ElementDefinition) {
218
219 kp.setKwdType(LTDTKeys.ELEMENT_TYPE);
220 }
221 else if (def instanceof GroupDefinition) {
222
223 kp.setKwdType(LTDTKeys.GROUP_TYPE);
224 }
225
226
227 kp.setDictURL(url);
228
229
230
231
232
233
234 String treeEntryID;
235 String str=null;
236
237
238 if (identifier.indexOf(":")==-1) {
239 str = identifier;
240 }
241
242
243 else {
244 str = DictionaryUtility.swap(identifier);
245 }
246
247
248
249
250 if ((LTDTKeys.WDDSTATUSTYPE).equals(def.getStatusType())) {
251 treeEntryID = str+"--WDD";
252 }
253 else {
254 treeEntryID = str+"--"+url.toString();
255 }
256
257
258 this.put(treeEntryID, kp);
259
260 count++;
261
262 }
263
264
265 allDefs.clear();
266
267 }
268
269 }
270
271
272 public ArrayList getTreeEntryIDList () {
273 ArrayList treeEntryIDList = new ArrayList();
274
275 for (Iterator it=this.keySet().iterator(); it.hasNext();) {
276 treeEntryIDList.add(it.next());
277 }
278
279 return treeEntryIDList;
280 }
281
282 ArrayList getTreeEntryIDDisplayList () {
283 ArrayList treeEntryIDList = this.getTreeEntryIDList();
284 ArrayList treeEntryIDDisplayList = new ArrayList();
285
286 int index = 0;
287 String temp;
288 String str[];
289 for (Iterator it=this.keySet().iterator(); it.hasNext();) {
290 temp = DictionaryUtility.swap((String)treeEntryIDList.get(index));
291 str = temp.split("--");
292 treeEntryIDDisplayList.add(str[0]);
293 }
294 return treeEntryIDDisplayList;
295 }
296
297
298 void reportDictionaryParsingStatus (MainWindow window, String logfile_name) {
299
300 if (logfile_name.length()!=0) {
301 try {
302 Utility.writeLogEntry(logfile_name, message);
303
304 }
305 catch (IOException e) {
306 JOptionPane.showMessageDialog(window,
307 "IOExeption. Error writing to project log "+logfile_name+"\n"+e.getMessage() );
308
309 }
310 }
311
312 }
313
314 }