private static EntryDictionary ReadDictionary(string databaseLocator, string type, Dictionary <string, EntryDictionary> cache) { EntryDictionary dictionary; // to distinguish between OWL: QSAR & REACT if (type.Contains("_React")) { databaseLocator += "." + type.Substring(0, type.Length - 6); } else { databaseLocator += "." + type; } if (cache.ContainsKey(databaseLocator)) { return(cache[databaseLocator]); } else { Trace.TraceInformation($"Reading dictionary from {databaseLocator}"); try { var reader = new StreamReader(ResourceLoader.GetAsStream(databaseLocator)); switch (type) { case "owl": dictionary = OWLFile.Unmarshal(reader); break; case "owl_React": dictionary = OWLReact.Unmarshal(reader); break; default: // assume XML using Castor dictionary = EntryDictionary.Unmarshal(reader); break; } } catch (Exception exception) { dictionary = null; Trace.TraceError($"Could not read dictionary {databaseLocator}"); Debug.WriteLine(exception); } cache[databaseLocator] = dictionary; return(dictionary); } }
public static new EntryDictionary Unmarshal(TextReader reader) { EntryDictionary dict = new OWLReact(); try { XDocument doc = XDocument.Load(reader); XElement root = doc.Root; Debug.WriteLine($"Found root element: {root.Name}"); // Extract ownNS from root element // final string ownNS = root.GetBaseURI(); string ownNS = root.Attribute("xmlns").Value; dict.NS = ownNS; Debug.WriteLine($"Found ontology namespace: {ownNS}"); // process the defined facts var entries = root.Elements(); foreach (var entry in entries) { if (entry.Name.NamespaceName.Equals(ownNS, StringComparison.Ordinal)) { EntryReact dbEntry = Unmarshal(entry, ownNS); dict.AddEntry(dbEntry); Debug.WriteLine($"Added entry: {dbEntry}"); } else { Debug.WriteLine($"Found a non-fact: {entry.Name.ToString()}"); } } } catch (XmlException ex) { Trace.TraceError($"Dictionary is not well-formed: {ex.Message}"); Debug.WriteLine($"Error at line {ex.LineNumber}, column {ex.LinePosition}"); dict = null; } catch (IOException ex) { Trace.TraceError($"Due to an IOException, the parser could not check:{ex.Message}"); Debug.WriteLine(ex); dict = null; } return(dict); }