View Javadoc

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  	//public SortedMap kmap = new TreeMap();
49  	private Dictionary dict = null;
50  	
51  	//private ArrayList fullIDList = new ArrayList();
52  	
53  	//private String logfile_name = null;
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  		//logfile_name=Utility.getProjectLogFileName(props);
70  		//message="";
71  		
72  		
73  		List dListArray = DictionaryUtility.getDictionaryNameList(dList);
74  		boolean parsingFailed = false;
75  			
76  		// loop through libraries and append individual keywords and associating properties
77  		// onto the treemap
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  				// do nothing
85  			}
86  			
87  		}// loop through each dictionary
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 		//remove trailing delimiter, just in case
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 			//do nothing
110 		}
111 		return parseStatus;
112 	}
113 	
114 	
115 	public int addSingleDictionary (Properties props, MainWindow window, URL dictionaryURL) {	
116 		int parseStatus = 0;
117 		//logfile_name=Utility.getProjectLogFileName(props);
118 		//message="";
119 		
120 		// loop through each data dictionary to get a list of object/element names and
121 		// definition
122 		try {
123 			
124 			dict = DictionaryParser.parse(dictionaryURL);
125 
126 			// set WDD status type to WDDSTATUSTYPE in the memory while this project is open
127 			// this is how we distinguish a definition is from WDD
128 			if ((dictionaryURL.toString()).equals(Utility.toURL(Utility.getProjectWDDFileName(props)).toString()))
129 				DictionaryUtility.setStatusType(dict, LTDTKeys.WDDSTATUSTYPE);
130 			
131 			// parse status can be either PASS or FAIL
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 		//String nameSpace = null;
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 				// next dictionary entry
190 				Object key = it.next();	
191 				identifier = key.toString().trim();
192 				
193 				// derive VALUE portion of the TreeMap entry
194 				// The Value portion include following information:
195 				//   identifier - can be either namespace:identifier or identifier 
196 				//   kwdType    - object, element, or group
197 				//   kwdDef     - defintion for a keyword, retrieved from dictinary
198 				//   dictURL    - URL string for this dictionary
199 				
200 				KeywordProperty kp = new KeywordProperty();
201 				
202 				// fill in identifier
203 				/// kp.identifier = identifier; 
204 				kp.setIdentifier(identifier);
205 				
206 				// fill in definition
207 				Definition def = dict.getDefinition(identifier);	
208 				///kp.kwdDef = def;
209 
210 				kp.setKwdDef(def);
211 				
212 				// fill in keyword type
213 				if (def instanceof ObjectDefinition) {	
214 					///kp.kwdType = LTDTKeys.OBJECT_TYPE;
215 					kp.setKwdType(LTDTKeys.OBJECT_TYPE);
216 				}
217 				else if (def instanceof ElementDefinition) {		
218 					///kp.kwdType = LTDTKeys.ELEMENT_TYPE;
219 					kp.setKwdType(LTDTKeys.ELEMENT_TYPE);
220 				}
221 				else if (def instanceof GroupDefinition) {
222 					///kp.kwdType = LTDTKeys.GROUP_TYPE;
223 					kp.setKwdType(LTDTKeys.GROUP_TYPE);
224 				}
225 				
226 				// fill in dictURL
227 				kp.setDictURL(url);
228 				
229 				//fullIDList.add(identifier);	 
230 
231 				// construct KEY portion of the TreeMap entry				
232 				// identifier is for search on dictionary 
233 				// keyword only or namespace:keyword				
234 				String treeEntryID;  // format of treeEntryID is "identifier:namespace--url"
235 				String str=null;
236 
237 				// if no leading namespace in identifier, it's PDSDD
238 				if (identifier.indexOf(":")==-1) {
239 					str = identifier;
240 				}
241 					
242 				// otherwiser, swap namespace and identifier before storing it to the sorted treemap
243 				else {
244 					str = DictionaryUtility.swap(identifier);						
245 				}
246 						
247 				// attach dictionary URL string to ensure uniqueness
248 				// if it's a WDD keyword, append WDD instead
249 
250 				if ((LTDTKeys.WDDSTATUSTYPE).equals(def.getStatusType())) {
251 					treeEntryID = str+"--WDD";
252 				}
253 				else {
254 					treeEntryID = str+"--"+url.toString();  
255 				}
256 
257 				// add one entry
258 				this.put(treeEntryID, kp);
259 
260 				count++;
261 									
262 			} // iterate through list of identifiers for one dictionary 
263 
264 						
265 			allDefs.clear();
266 			
267 		} // if allDefs is not null
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 }