Association.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. namespace csharp_sccd_compiler
  6. {
  7. public class Association : Visitable
  8. {
  9. /// <summary>
  10. /// Minimum cardinality of the association.
  11. /// </summary>
  12. public int min { get; private set; }
  13. /// <summary>
  14. /// Maximum cardinality of the association.
  15. /// </summary>
  16. /// <value>N is represented as -1.</value>
  17. public int max { get; private set; }
  18. public string to_class { get; private set; } // TODO perhaps try to replace the string by the actual class with a visitor? That's an extra error check.
  19. public string name { get; private set; }
  20. public Association(XElement xml)
  21. {
  22. XAttribute class_name_attribute = xml.Attribute("class");
  23. if (class_name_attribute == null)
  24. throw new CompilerException("Association missing class attribute.");
  25. this.to_class = class_name_attribute.Value.Trim();
  26. if (this.to_class == "")
  27. throw new CompilerException("Association has empty class attribute.");
  28. if (Constants.Reserved.Contains(this.to_class))
  29. throw new CompilerException(string.Format("Reserved word '{0}' used as class attribute for association.", this.to_class));
  30. XAttribute min_card_attribute = xml.Attribute("min");
  31. if (min_card_attribute == null)
  32. this.min = 0; //default value
  33. else
  34. {
  35. try
  36. {
  37. this.min = Convert.ToInt32(min_card_attribute.Value);
  38. if(this.min < 0)
  39. throw new FormatException();
  40. }
  41. catch(FormatException)
  42. {
  43. throw new CompilerException("Faulty minimum cardinality value in association.");
  44. }
  45. catch(OverflowException)
  46. {
  47. throw new CompilerException("Minimum cardinality of association is too large.");
  48. }
  49. }
  50. XAttribute max_card_attribute = xml.Attribute("max");
  51. if (max_card_attribute == null)
  52. this.max = -1; //default value TODO:use maxvalue?
  53. else
  54. {
  55. try
  56. {
  57. this.max = Convert.ToInt32(max_card_attribute.Value);
  58. if(this.max < this.min)
  59. throw new FormatException();
  60. }
  61. catch(FormatException)
  62. {
  63. throw new CompilerException("Faulty maximum cardinality value in association.");
  64. }
  65. catch(OverflowException)
  66. {
  67. throw new CompilerException("Maximum cardinality of association is too large.");
  68. }
  69. }
  70. XAttribute association_name_attribute = xml.Attribute("name");
  71. if (association_name_attribute == null)
  72. throw new CompilerException("Association missing name attribute.");
  73. this.name = association_name_attribute.Value.Trim();
  74. if (this.name == "")
  75. throw new CompilerException("Associaion has empty name attribute.");
  76. if (Constants.Reserved.Contains(this.name))
  77. throw new CompilerException(string.Format("Reserved word '{0}' used as class attribute for association.", this.name));
  78. }
  79. public override void accept(Visitor visitor)
  80. {
  81. visitor.visit (this);
  82. }
  83. }
  84. }