private void Initialize(XmlElement unitTableElement) { var xmlElement = unitTableElement[Constants.ELEMENT_UNITS]; if (xmlElement != null) { foreach (XmlElement unitElement in xmlElement.ChildNodes) { int code; string name, symbol, plural; CreateUnit(unitElement, out code, out name, out symbol, out plural); Unit.AddUnit(code, name, symbol, plural); } } var element = unitTableElement[Constants.ELEMENT_ConversionS]; if (element != null) { foreach (XmlElement conversionElement in element.ChildNodes) { int srcCode, destCode; LinearConverter converter = CreateConversion(conversionElement, out srcCode, out destCode); ConversionTable.AddConversion(srcCode, destCode, converter); } } }
public void AddConversion(int srcCode, int destCode, LinearConverter converter) { _dicConversionTable[FormatKey(srcCode, destCode)] = converter; if (!_dicConversionTable.ContainsKey(FormatKey(destCode, srcCode)) && converter.AllowInverse) { _dicConversionTable[FormatKey(destCode, srcCode)] = converter.Inverse; } }
private static LinearConverter CreateConversion(XmlElement conversionElement, out int srcCode, out int destCode) { LinearConverter result = null; try { srcCode = int.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_SRCCODE].Value); destCode = int.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_DESTCODE].Value); } catch (Exception ex) { throw new ConversionCreationException("Creation tag is not correct", ex); } if (conversionElement.Name == Constants.ELEMENT_LINEAR) { try { double factor = conversionElement.Attributes[Constants.ATTRIBUTE_FACTOR] == null ? 1d : double.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_FACTOR].Value); double deltha = conversionElement.Attributes[Constants.ATTRIBUTE_DELTHA] == null ? 0d : double.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_DELTHA].Value); result = new LinearConverter(factor, deltha); } catch (Exception ex) { throw new ConversionCreationException("Creation tag is not correct", ex); } } if (result == null) { throw new ConversionCreationException("Creation tag is not correct"); } return(result); }
private static LinearConverter CreateConversion(XmlElement conversionElement, out int srcCode, out int destCode) { LinearConverter result = null; try { srcCode = int.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_SRCCODE].Value); destCode = int.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_DESTCODE].Value); } catch (Exception ex) { throw new ConversionCreationException("Creation tag is not correct", ex); } if (conversionElement.Name == Constants.ELEMENT_LINEAR) { try { double factor = conversionElement.Attributes[Constants.ATTRIBUTE_FACTOR] == null ? 1d : double.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_FACTOR].Value); double deltha = conversionElement.Attributes[Constants.ATTRIBUTE_DELTHA] == null ? 0d : double.Parse(conversionElement.Attributes[Constants.ATTRIBUTE_DELTHA].Value); result = new LinearConverter(factor, deltha); } catch (Exception ex) { throw new ConversionCreationException("Creation tag is not correct", ex); } } if (result == null) throw new ConversionCreationException("Creation tag is not correct"); return result; }
public void AddConversion(int srcCode, int destCode, LinearConverter converter) { _dicConversionTable[FormatKey(srcCode, destCode)] = converter; if (!_dicConversionTable.ContainsKey(FormatKey(destCode, srcCode)) && converter.AllowInverse) _dicConversionTable[FormatKey(destCode, srcCode)] = converter.Inverse; }