PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/XML-Units/AttributeUnit.pp

http://my-units.googlecode.com/
Puppet | 431 lines | 369 code | 62 blank | 0 comment | 17 complexity | e5c189e64dbab4cabd86632bde480e9a MD5 | raw file
  1. unit AttributeUnit;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, CollectionUnit;
  6. type
  7. { ECharNotFoundInFile }
  8. ECharNotFoundInFile= class (Exception)
  9. public
  10. constructor Create (Ch: Char);
  11. end;
  12. { EAttributeNotFound }
  13. EAttributeNotFound= class (Exception)
  14. public
  15. constructor Create (Msg: String);
  16. end;
  17. TCharFile= file of char;
  18. { TAttribute }
  19. TAttribute= class (TObject)
  20. private
  21. FName: WideString;
  22. FValue: WideString;
  23. public
  24. property Name: WideString read FName write FName;
  25. property Value: WideString read FValue write FValue;
  26. constructor Create;
  27. constructor Create (AttName, AttValue: WideString);
  28. function ToString: WideString;
  29. end;
  30. { TAttributeCollection }
  31. TAttributeCollection= class (TBaseCollection)
  32. private
  33. function GetAttribute (Index: Integer): TAttribute;
  34. function GetAttributeByName (AttrName: String): TAttribute;
  35. public
  36. property Attribute [Index: Integer]: TAttribute read GetAttribute;
  37. property AttribByName [AttrName: String]: TAttribute read GetAttributeByName;
  38. constructor Create;
  39. function ToString: String;
  40. procedure Add (NewAttribute: TAttribute);
  41. procedure SaveToFile (var OutputFile: TextFile);
  42. end;
  43. var
  44. LastChar: Char;
  45. implementation
  46. procedure Expect (Ch: Char; var InputFile: TCharFile);
  47. var
  48. TempCh: Char;
  49. begin
  50. TempCh:= LastChar;
  51. while TempCh= ' ' do
  52. Read (InputFile, TempCh);
  53. if TempCh<> Ch then
  54. raise ECharNotFoundInFile.Create (Ch);
  55. LastChar:= ' ';
  56. end;
  57. function ExtractValue (var Value: String): String;
  58. var
  59. i: Integer;
  60. Flag: Byte;
  61. Ch: Char;
  62. begin
  63. Value:= Trim (Value);
  64. Result:= '';
  65. if Length (Value)<> 0 then
  66. begin
  67. if Value [1]= '"' then
  68. begin
  69. Delete (Value, 1, 1);
  70. Flag:= 0;
  71. for i:= 1 to Length (Value)- 1 do
  72. begin
  73. Ch:= Value [i];
  74. if Ch= '\' then
  75. begin
  76. Inc (Flag);
  77. if Flag= 2 then
  78. Flag:= 0;
  79. end
  80. else
  81. Flag:= 0;
  82. if Ch= '"' then
  83. begin
  84. Delete (Value, 1, i);
  85. Break;
  86. end;
  87. if Flag<> 1 then
  88. Result:= Result+ Ch;
  89. end;
  90. end
  91. else
  92. begin
  93. Flag:= 0;
  94. for i:= 1 to Length (Value)- 1 do
  95. begin
  96. Ch:= Value [i];
  97. if Ch= '\' then
  98. begin
  99. Inc (Flag);
  100. if Flag= 2 then
  101. Flag:= 0;
  102. end
  103. else
  104. Flag:= 0;
  105. if Ch= ' ' then
  106. begin
  107. Delete (Value, 1, i);
  108. Break;
  109. end;
  110. Result:= Result+ Ch;
  111. end;
  112. end;
  113. end;
  114. end;
  115. function ExtractValue (var InputFile: TCharFile): String;
  116. var
  117. Flag: Byte;
  118. Ch: Char;
  119. begin
  120. Result:= '';
  121. Ch:= LastChar;;
  122. while Ch= ' ' do
  123. Read (InputFile, Ch);
  124. if Ch= '"' then
  125. begin
  126. Flag:= 0;
  127. while True do
  128. begin
  129. Read (InputFile, Ch);
  130. if Ch= '\' then
  131. begin
  132. Inc (Flag);
  133. if Flag= 2 then
  134. Flag:= 0;
  135. end
  136. else
  137. Flag:= 0;
  138. if Ch= '"' then
  139. begin
  140. LastChar:= ' ';
  141. Break;
  142. end;
  143. if Flag<> 1 then
  144. Result:= Result+ Ch;
  145. end;
  146. end
  147. else
  148. begin
  149. Flag:= 0;
  150. while True do
  151. begin
  152. Read (InputFile, Ch);
  153. if Ch= '\' then
  154. begin
  155. Inc (Flag);
  156. if Flag= 2 then
  157. Flag:= 0;
  158. end
  159. else
  160. Flag:= 0;
  161. if Ch= ' ' then
  162. begin
  163. LastChar:= ' ';
  164. Break;
  165. end;
  166. Result:= Result+ Ch;
  167. end;
  168. end;
  169. end;
  170. function ExtractName (var InputFile: TCharFile): String;
  171. var
  172. Flag: Byte;
  173. Ch: Char;
  174. begin
  175. Ch:= LastChar;;
  176. while Ch= ' ' do
  177. Read (InputFile, Ch);
  178. Result:= Ch;
  179. Flag:= 0;
  180. while True do
  181. begin
  182. Read (InputFile, Ch);
  183. if Ch= '\' then
  184. begin
  185. Inc (Flag);
  186. if Flag= 2 then
  187. Flag:= 0;
  188. end
  189. else
  190. Flag:= 0;
  191. if Ch= '=' then
  192. begin
  193. LastChar:= '=';
  194. Break;
  195. end
  196. else if Ch= ' ' then
  197. begin
  198. LastChar:= ' ';
  199. Break;
  200. end;
  201. if Flag<> 1 then
  202. Result:= Result+ Ch;
  203. end;
  204. end;
  205. function ReadNextToken (var InputFile: TCharFile): String;
  206. var
  207. Ch: Char;
  208. begin
  209. Ch:= LastChar;
  210. Result:= '';
  211. while Ch= ' ' do
  212. Read (InputFile, Ch);
  213. while Ch<> ' ' do
  214. begin
  215. Result:= Result+ Ch;
  216. Read (InputFile, Ch);
  217. end;
  218. LastChar:= ' ';
  219. end;
  220. { TAttribute }
  221. constructor TAttribute.Create;
  222. begin
  223. inherited Create;
  224. end;
  225. constructor TAttribute.Create (AttName, AttValue: WideString);
  226. begin
  227. inherited Create;
  228. FName:= AttName;
  229. FValue:= AttValue;
  230. if Length (FValue)<> 0 then
  231. if (FValue [1]= '"') and (FValue [Length (FValue)]= '"') then
  232. begin
  233. Delete (FValue, 1, 1);
  234. Delete (FValue, Length (FValue), 1);
  235. end;
  236. end;
  237. function TAttribute.ToString: WideString;
  238. begin
  239. Result:= FName+ '="'+ FValue+ '"';
  240. //Remove invalid characters
  241. end;
  242. { TAttributeCollection }
  243. function TAttributeCollection.GetAttribute(Index: Integer): TAttribute;
  244. begin
  245. Result:= Member [Index] as TAttribute;
  246. end;
  247. function TAttributeCollection.GetAttributeByName (AttrName: String): TAttribute;
  248. var
  249. i: Integer;
  250. begin
  251. AttrName:= UpperCase (AttrName);
  252. for i:= 0 to Size- 1 do
  253. if UpperCase (Attribute [i].Name)= AttrName then
  254. begin
  255. Result:= Attribute [i];
  256. Exit;
  257. end;
  258. raise EAttributeNotFound.Create (AttrName+ Self.ToString);
  259. end;
  260. constructor TAttributeCollection.Create;
  261. begin
  262. inherited;
  263. end;
  264. function TAttributeCollection.ToString: String;
  265. var
  266. i: Integer;
  267. Ptr: ^TAttribute;
  268. begin
  269. Result:= '';
  270. if 0< Size then
  271. begin
  272. Ptr:= @FMembers [0];
  273. for i:= 0 to Size- 1 do
  274. begin
  275. Result:= Result+ Ptr^.ToString+ ' ';
  276. Inc (Ptr);
  277. end;
  278. end;
  279. end;
  280. procedure TAttributeCollection.Add (NewAttribute: TAttribute);
  281. begin
  282. inherited;
  283. end;
  284. procedure TAttributeCollection.SaveToFile (var OutputFile: TextFile);
  285. var
  286. i: Integer;
  287. begin
  288. for i:= 1 to Size- 1 do
  289. Write (OutputFile, Attribute [i].ToString, ' ');
  290. end;
  291. { EAttributeNotFound }
  292. constructor EAttributeNotFound.Create (Msg: String);
  293. begin
  294. inherited Create ('Attribute '+ Msg+ ' not found!');
  295. end;
  296. { ECharNotFound }
  297. constructor ECharNotFoundInFile.Create(Ch: Char);
  298. begin
  299. inherited Create ('Char '+ Ch+ ' not found in File!');
  300. end;
  301. end.