// // openmapi.org - CompactTeaSharp - MLog - XmlTypeMap.cs // // Copyright 2009 Topalis AG // // Author: Johannes Roith // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace CompactTeaSharp.Mlog { /// /// Processes the "nrpcgen" xml-file that contains additional /// information like type mappings, etc. /// public sealed class XmlTypeMap { private Dictionary typeMap; private List staticTypes; public XmlTypeMap (string fileName) { if (fileName != null) { typeMap = new Dictionary (); staticTypes = new List (); XElement xml = XElement.Load (fileName); var map = from type in xml.Elements ("map").Elements ("type") select type; foreach (var typeElement in map) typeMap [(string) typeElement.Attribute ("from")] = (string) typeElement.Attribute ("to"); var types = from type in xml.Elements ("staticDec").Elements ("type") select (string) type.Attribute ("name"); foreach (var type in types) staticTypes.Add (type); } } /// /// Maps a type from the .x file to a new name. /// public string Map (string key) { if (typeMap == null) return key; string result = null; if (typeMap.ContainsKey (key)) result = typeMap [key]; if (result == null) result = key; return result; } /// /// True if the type with name "key" must be decoded using a static method. /// public bool IsStatic (string key) { if (staticTypes == null) return false; return staticTypes.Contains (key); } } }