}

Delphi.int.ru — Портал программистов

Вход Регистрация | Забыли пароль?

Просмотр кода

Идентификатор: be2f81f2 Описание: Код загружен: 26 июня 2012, 16:51 (min@y™)

  1. unit uXmlUtils;
  2.  
  3. interface
  4.  
  5. uses
  6. SysUtils, Graphics, ECXMLParser;
  7.  
  8. // --------------- Добавление информации в XML -----------------
  9. procedure XmlA
  10. procedure XmlAddStringParam(ANode: TXMLItem; const Name, Value: string);
  11. procedure XmlAddIntegerParam(ANode: TXMLItem; const Name: string; const Value: Integer);
  12. procedure XmlAddCardinalParam(ANode: TXMLItem; const Name: string; const Value: Cardinal);
  13. procedure XmlAddColorParam(ANode: TXMLItem; const Name: string; const Value: TColor);
  14. procedure XmlAddBoolParam(ANode: TXMLItem; const Name: string; const Value: Boolean);
  15. procedure XmlAddTimeParam(ANode: TXMLItem; const Name: string; const Value: TDateTime);
  16. procedure XmlAddDateParam(ANode: TXMLItem; const Name: string; const Value: TDateTime);
  17. procedure XmlAddDateTimeParam(ANode: TXMLItem; const Name: string; const Value: TDateTime);
  18. procedure XmlAddFloatParam(ANode: TXMLItem; const Name: string; const Value: Extended);
  19. procedure XmlAddFontStyleParam(ANode: TXMLItem; const Name: string; const Value: TFontStyles);
  20.  
  21. //иск и извлечение информации из XML -----------
  22. function XmlFindParam(ANode: TXMLIt
  23. function XmlFindParam(ANode: TXMLItem; const Name: string): Integer;
  24. function XmlFindNodeIndex(AParentNode: TXMLItem; const Name: string): Integer;
  25. function XmlFindNode(AParentNode: TXMLItem; const Name: string): TXMLItem;
  26.  
  27. function XmlGetStringParam(ANode: TXMLItem; const Name, Default: string): string;
  28. function XmlGetIntegerParam(ANode: TXMLItem; const Name: string; const Default: Integer): Integer;
  29. function XmlGetCardinalParam(ANode: TXMLItem; const Name: string; const Default: Cardinal): Cardinal;
  30. function XmlGetColorParam(ANode: TXMLItem; const Name: string; const Default: TColor): TColor;
  31. function XmlGetBoolParam(ANode: TXMLItem; const Name: string; const Default: Boolean): Boolean;
  32. function XmlGetTimeParam(ANode: TXMLItem; const Name: string; Default: TDateTime): TDateTime;
  33. function XmlGetDateParam(ANode: TXMLItem; const Name: string; Default: TDateTime): TDateTime;
  34. function XmlGetDateTimeParam(ANode: TXMLItem; const Name: string; Default: TDateTime): TDateTime;
  35. function XmlGetFloatParam(ANode: TXMLItem; const Name: string; Default: Extended): Extended;
  36. function XmlGetFontStyleParam(ANode: TXMLItem; const Name: string; Default: TFontStyles): TFontStyles;
  37.  
  38. //----------------
  39. function FindNamedItem(AParentNode: TXMLItem;
  40. function FindNamedItem(AParentNode: TXMLItem; const Name: string; const FromIndex: Integer = 0): TXMLItem;
  41. function ReplaceIllegalSymbols(const S: string): string;
  42.  
  43. implementation
  44.  
  45. //-----------------
  46.  
  47. // Преобразование стилей шрифта в число
  48. function FontStyle2Int(const AFontStyle: TF
  49.  
  50. //tyles): Integer;
  51. var
  52. Style: TFontStyle;
  53. begin
  54. Result:= 0;
  55.  
  56. for Style:= Low(TFontStyle) to High(
  57. var
  58. Style: TFontStyle;
  59. begin
  60. Result:= 0;
  61.  
  62. for Style:= Low(TFontStyle) to High(TFontStyle) do
  63. if Style in AFontStyle
  64. then Result:= Result or (1 shl Integer(Style));
  65. end;
  66.  
  67. //yle: TFontStyle;
  68. begin
  69. Result:= [];
  70.  
  71. for Style:= Low(TFontStyle) to High(TFontStyle) do
  72. if iFontStyle and (1 shl Inte
  73. begin
  74. Result:= [];
  75.  
  76. for Style:= Low(TFontStyle) to High(TFontStyle) do
  77. if iFontStyle and (1 shl Integer(Style)) <> 0
  78. then Result:= Result + [Style];
  79. end;
  80.  
  81. //e: string);
  82. begin
  83. ANode.Params.Add(Name + '=' + Value);
  84. end;
  85.  
  86. procedure XmlAddIntegerParam(ANode: TXMLItem; const Name: string; const
  87. begin
  88. ANode.Params.Add(Name + '=' + Value);
  89. end;
  90.  
  91. procedure XmlAddIntegerParam(ANode: TXMLItem; const Name: string; const Value: Integer);
  92. begin
  93. ANode.Params.Add(Name + '=' + IntToStr(Value));
  94. end;
  95.  
  96. procedure XmlAddCardinalParam(ANode: TXMLItem; const Name: string; const Value: Cardinal);
  97. begin
  98. ANode.Params.Add(Name + '=' + IntToHex(Value, 8));
  99. end;
  100.  
  101. procedure XmlAddColorParam(ANode: TXMLItem; const Name: string; const Value: TColor);
  102. begin
  103. //e: TXMLItem; const Name: string; const Value: Boolean);
  104. begin
  105. if Value
  106. then XmlAddStringParam(ANode, Name, '1')
  107. else XmlAddStringParam(ANode, Name, '0');
  108. end;
  109.  
  110. procedure XmlAddTimeParam(ANode:
  111. begin
  112. if Value
  113. then XmlAddStringParam(ANode, Name, '1')
  114. else XmlAddStringParam(ANode, Name, '0');
  115. end;
  116.  
  117. procedure XmlAddTimeParam(ANode: TXMLItem; const Name: string; const Value: TDateTime);
  118. begin
  119. ANode.Params.Add(Name + '=' + TimeToStr(Value));
  120. end;
  121.  
  122. procedure XmlAddDateParam(ANode: TXMLItem; const Name: string; const Value: TDateTime);
  123. begin
  124. ANode.Params.Add(Name + '=' + DateToStr(Value));
  125. end;
  126.  
  127. procedure XmlAddDateTimeParam(ANode: TXMLItem; const Name: string; const Value: TDateTime);
  128. begin
  129. ANode.Params.Add(Name + '=' + DateTimeToStr(Value));
  130. end;
  131.  
  132. procedure XmlAddFloatParam(ANode: TXMLItem; const Name: string; const Value: Extended);
  133. begin
  134. ANode.Params.Add(Name + '=' + FloatToStr(Value));
  135. end;
  136.  
  137. procedure XmlAddFontStyleParam(ANode: TXMLItem; const Name: string; const Value: TFontStyles);
  138. begin
  139. ANode.Params.Add(Name + '=' + IntToStr(FontStyle2Int(Value)));
  140. end;
  141.  
  142. //Name(Name);
  143. end;
  144.  
  145. function XmlFindNodeIndex(AParentNode: TXMLItem; const Name: string): Integer;
  146. begin
  147. Result:= AParentNode.IndexOfName(Name);
  148. end;
  149.  
  150. function XmlFindNode(AParentN
  151. end;
  152.  
  153. function XmlFindNodeIndex(AParentNode: TXMLItem; const Name: string): Integer;
  154. begin
  155. Result:= AParentNode.IndexOfName(Name);
  156. end;
  157.  
  158. function XmlFindNode(AParentNode: TXMLItem; const Name: string): TXMLItem;
  159. var
  160. Index: Integer;
  161. begin
  162. Result:= nil;
  163. Index:= AParentNode.IndexOfName(Name);
  164. if Index <> -1
  165. then Result:= AParentNode.SubItems[Index];
  166. end;
  167.  
  168. function XmlGetStringParam(ANode: TXMLItem; const Name, Default: string): string;
  169. var
  170. Index: Integer;
  171. begin
  172. Index:= XmlFindParam(ANode, Name);
  173. if Index <> -1
  174. then Result:= ANode.Params.ValueFromIndex[Index]
  175. else Result:= Default;
  176.  
  177. if Result = '""'
  178. then Result:= ''; //= XmlFindParam(ANode, Name);
  179.  
  180. if Index = -1
  181. then Result:= Default
  182. else if not TryStrToInt(ANode.Params.ValueFromIndex[Index], Result)
  183. then Result:= Default;
  184. end;
  185.  
  186. function XmlGetCardinalParam(ANode: TXMLIt
  187.  
  188. if Index = -1
  189. then Result:= Default
  190. else if not TryStrToInt(ANode.Params.ValueFromIndex[Index], Result)
  191. then Result:= Default;
  192. end;
  193.  
  194. function XmlGetCardinalParam(ANode: TXMLItem; const Name: string; const Default: Cardinal): Cardinal;
  195. var
  196. Index: Integer;
  197. begin
  198. //))
  199. then Result:= Default;
  200. end;
  201.  
  202. function XmlGetColorParam(ANode: TXMLItem; const Name: string; const Default: TColor): TColor;
  203. var
  204. Index: Integer;
  205. begin
  206. // Чтение параметра типа Param="11BBCC" (шестнадцатеричного)
  207. Index:= XmlFindPara
  208. then Result:= Default;
  209. end;
  210.  
  211. function XmlGetColorParam(ANode: TXMLItem; const Name: string; const Default: TColor): TColor;
  212. var
  213. Index: Integer;
  214. begin
  215. //:= Default;
  216. end;
  217.  
  218. function XmlGetBoolParam(ANode: TXMLItem; const Name: string; const Default: Boolean): Boolean;
  219. var
  220. Index: Integer;
  221. begin
  222. Index:= XmlFindParam(ANode, Name);
  223.  
  224. if Index = -1
  225. then Result:= Default
  226. else Result:= XmlGetStringParam(A
  227. end;
  228.  
  229. function XmlGetBoolParam(ANode: TXMLItem; const Name: string; const Default: Boolean): Boolean;
  230. var
  231. Index: Integer;
  232. begin
  233. Index:= XmlFindParam(ANode, Name);
  234.  
  235. if Index = -1
  236. then Result:= Default
  237. else Result:= XmlGetStringParam(ANode, Name, '0') = '1';
  238. end;
  239.  
  240. function XmlGetTimeParam(ANode: TXMLItem; const Name: string; Default: TDateTime): TDateTime;
  241. var
  242. Index: Integer;
  243. begin
  244. Index:= XmlFindParam(ANode, Name);
  245.  
  246. if Index = -1
  247. then Result:= Default
  248. else if not TryStrToTime(ANode.Params.ValueFromIndex[Index], Result)
  249. then Result:= Default;
  250. end;
  251.  
  252. function XmlGetDateParam(ANode: TXMLItem; const Name: string; Default: TDateTime): TDateTime;
  253. var
  254. Index: Integer;
  255. begin
  256. Index:= XmlFindParam(ANode, Name);
  257.  
  258. if Index = -1
  259. then Result:= Default
  260. else if not TryStrToDate(ANode.Params.ValueFromIndex[Index], Result)
  261. then Result:= Default;
  262. end;
  263.  
  264. function XmlGetDateTimeParam(ANode: TXMLItem; const Name: string; Default: TDateTime): TDateTime;
  265. var
  266. Index: Integer;
  267. begin
  268. Index:= XmlFindParam(ANode, Name);
  269.  
  270. if Index = -1
  271. then Result:= Default
  272. else if not TryStrToDateTime(ANode.Params.ValueFromIndex[Index], Result)
  273. then Result:= Default;
  274. end;
  275.  
  276. function XmlGetFloatParam(ANode: TXMLItem; const Name: string; Default: Extended): Extended;
  277. var
  278. Index: Integer;
  279. begin
  280. Index:= XmlFindParam(ANode, Name);
  281.  
  282. if Index = -1
  283. then Result:= Default
  284. else if not TryStrToFloat(ANode.Params.ValueFromIndex[Index], Result)
  285. then Result:= Default;
  286. end;
  287.  
  288. function XmlGetFontStyleParam(ANode: TXMLItem; const Name: string; Default: TFontStyles): TFontStyles;
  289. var
  290. Index, Temp: Integer;
  291. begin
  292. Index:= XmlFindParam(ANode, Name);
  293.  
  294. if Index = -1
  295. then Result:= Default
  296. else if TryStrToInt(ANode.Params.ValueFromIndex[Index], Temp)
  297. then Result:= Int2FontStyle(Temp)
  298. else Result:= Default;
  299. end;
  300.  
  301. //Index:= FromIndex to AParentNode.Count - 1 do
  302. if AParentNode.SubItems[Index].Name = Name
  303. then begin
  304. Result:= AParentNode.SubItems[Index];
  305. Break;
  306. end;
  307. end;
  308.  
  309. // Замена неподходящих для XML-имён символов на "_"
  310. function ReplaceIllegalSymbols(const S: string): string;
  311. const
  312. AllowedSymbols: set of Char = ['0'..'9',
  313. if AParentNode.SubItems[Index].Name = Name
  314. then begin
  315. Result:= AParentNode.SubItems[Index];
  316. Break;
  317. end;
  318. end;
  319.  
  320. //t) do
  321. if not (Result[Index] in AllowedSymbols)
  322. then Result[Index]:= '_';
  323. end;
  324.  
  325.  
  326. end.</code>
  327.  

Ссылка на данный код:

На главную страницу сервиса обмена кодом »