Changeset 110 for branches/generator/URules.pas
- Timestamp:
- Aug 8, 2017, 11:15:41 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/generator/URules.pas
r109 r110 10 10 type 11 11 12 { TRuleItem } 13 14 TRuleItem = class 15 Name: string; 16 procedure LoadFromXmlNode(Node: TDOMNode); 17 procedure SaveToXmlNode(Node: TDOMNode); 18 end; 19 20 { TRuleItems } 21 22 TRuleItems = class(TFPGObjectList<TRuleItem>) 23 procedure LoadFromXmlNode(Node: TDOMNode); 24 procedure SaveToXmlNode(Node: TDOMNode); 25 end; 26 12 27 { TRule } 13 28 14 29 TRule = class 15 30 Name: string; 16 procedure SaveToXmlNode(Node: TDOMNode); 17 procedure LoadFromXmlNode(Node: TDOMNode); 31 Items: TRuleItems; 32 constructor Create; 33 destructor Destroy; override; 34 procedure LoadFromXmlNode(Node: TDOMNode); 35 procedure SaveToXmlNode(Node: TDOMNode); 36 function GetBNF: string; 18 37 end; 19 38 … … 21 40 22 41 TRules = class(TFPGObjectList<TRule>) 42 FileName: string; 43 procedure LoadFromXmlNode(Node: TDOMNode); 44 procedure SaveToXmlNode(Node: TDOMNode); 23 45 procedure LoadFromXmlFile(FileName: string); 24 46 procedure SaveToXmlFile(FileName: string); 47 function GetBNF: string; 25 48 end; 26 49 27 50 implementation 28 51 52 { TRuleItem } 53 54 procedure TRuleItem.SaveToXmlNode(Node: TDOMNode); 55 begin 56 WriteString(Node, 'Name', Name); 57 end; 58 59 procedure TRuleItem.LoadFromXmlNode(Node: TDOMNode); 60 begin 61 Name := ReadString(Node, 'Name', ''); 62 end; 63 64 { TRuleItems } 65 66 procedure TRuleItems.SaveToXmlNode(Node: TDOMNode); 67 var 68 RuleItem: TRuleItem; 69 RuleItemNode: TDOMNode; 70 begin 71 for RuleItem in Self do begin 72 RuleItemNode := Node.OwnerDocument.CreateElement('RuleItem'); 73 RuleItem.SaveToXmlNode(RuleItemNode); 74 Node.AppendChild(RuleItemNode); 75 end; 76 end; 77 78 procedure TRuleItems.LoadFromXmlNode(Node: TDOMNode); 79 var 80 RuleItemNode: TDOMNode; 81 RuleItem: TRuleItem; 82 begin 83 RuleItemNode := Node.FirstChild; 84 while Assigned(RuleItemNode) do begin 85 if RuleItemNode.NodeName = 'RuleItem' then begin 86 RuleItem := TRuleItem.Create; 87 RuleItem.LoadFromXmlNode(RuleItemNode); 88 Add(RuleItem); 89 end; 90 RuleItemNode := RuleItemNode.NextSibling; 91 end; 92 end; 93 29 94 { TRule } 30 95 31 96 procedure TRule.SaveToXmlNode(Node: TDOMNode); 97 var 98 RuleItemsNode: TDOMNode; 32 99 begin 33 100 WriteString(Node, 'Name', Name); 101 102 RuleItemsNode := Node.OwnerDocument.CreateElement('RuleItems'); 103 Node.AppendChild(RuleItemsNode); 104 Items.SaveToXmlNode(RuleItemsNode); 105 end; 106 107 function TRule.GetBNF: string; 108 var 109 Item: TRuleItem; 110 begin 111 Result := Name + ' ::='; 112 for Item in Items do begin 113 Result := Result + ' ' + Item.Name; 114 end; 115 end; 116 117 constructor TRule.Create; 118 begin 119 Items := TRuleItems.Create; 120 end; 121 122 destructor TRule.Destroy; 123 begin 124 FreeAndNil(Items); 125 inherited Destroy; 34 126 end; 35 127 36 128 procedure TRule.LoadFromXmlNode(Node: TDOMNode); 129 var 130 ItemsNode: TDOMNode; 37 131 begin 38 132 Name := ReadString(Node, 'Name', ''); 133 134 ItemsNode := Node.FindNode('RuleItems'); 135 if Assigned(ItemsNode) then begin 136 Items.LoadFromXmlNode(ItemsNode); 137 end; 39 138 end; 40 139 41 140 { TRules } 141 142 procedure TRules.SaveToXmlNode(Node: TDOMNode); 143 var 144 Rule: TRule; 145 RuleNode: TDOMNode; 146 begin 147 for Rule in Self do begin 148 RuleNode := Node.OwnerDocument.CreateElement('Rule'); 149 Rule.SaveToXmlNode(RuleNode); 150 Node.AppendChild(RuleNode); 151 end; 152 end; 153 154 procedure TRules.LoadFromXmlNode(Node: TDOMNode); 155 var 156 RuleNode: TDOMNode; 157 Rule: TRule; 158 begin 159 RuleNode := Node.FirstChild; 160 while Assigned(RuleNode) do begin 161 if RuleNode.NodeName = 'Rule' then begin 162 Rule := TRule.Create; 163 Rule.LoadFromXmlNode(RuleNode); 164 Add(Rule); 165 end; 166 RuleNode := RuleNode.NextSibling; 167 end; 168 end; 42 169 43 170 procedure TRules.LoadFromXmlFile(FileName: string); … … 46 173 RootNode: TDOMNode; 47 174 RulesNode: TDOMNode; 48 RuleNode: TDOMNode; 49 Rule: TRule; 50 begin 175 begin 176 Self.FileName := FileName; 51 177 Clear; 52 178 try … … 57 183 RulesNode := RootNode.FindNode('Rules'); 58 184 if Assigned(RulesNode) then begin 59 RuleNode := RulesNode.FirstChild; 60 while Assigned(RuleNode) do begin 61 if RuleNode.NodeName = 'Rule' then begin 62 Rule := TRule.Create; 63 Rule.LoadFromXmlNode(RuleNode); 64 Add(Rule); 65 end; 66 RuleNode := RuleNode.NextSibling; 67 end; 185 LoadFromXmlNode(RulesNode); 68 186 end; 69 187 end; … … 78 196 RootNode: TDOMNode; 79 197 RulesNode: TDOMNode; 80 RuleNode: TDOMNode; 81 Rule: TRule; 82 begin 198 begin 199 Self.FileName := FileName; 83 200 Doc := TXMLDocument.Create; 84 201 try … … 88 205 RulesNode := Doc.CreateElement('Rules'); 89 206 RootNode.AppendChild(RulesNode); 90 91 for Rule in Self do begin 92 RuleNode := Doc.CreateElement('Rule'); 93 Rule.SaveToXmlNode(RuleNode); 94 RulesNode.AppendChild(RuleNode); 95 end; 207 SaveToXmlNode(RulesNode); 96 208 97 209 WriteXMLFile(Doc, FileName); … … 101 213 end; 102 214 215 function TRules.GetBNF: string; 216 var 217 Rule: TRule; 218 begin 219 Result := ''; 220 for Rule in Self do begin 221 Result := Result + Rule.GetBNF + LineEnding; 222 end; 223 end; 224 103 225 end. 104 226
Note:
See TracChangeset
for help on using the changeset viewer.