/src/yolk-syndication-dom_builder.adb

http://github.com/ThomasLocke/yolk · Ada · 902 lines · 689 code · 125 blank · 88 comment · 32 complexity · db1317569458d766cba1cbd727cea09c MD5 · raw file

  1. -------------------------------------------------------------------------------
  2. -- --
  3. -- Copyright (C) 2010-, Thomas ¸cke --
  4. -- --
  5. -- This library is free software; you can redistribute it and/or modify --
  6. -- it under terms of the GNU General Public License as published by the --
  7. -- Free Software Foundation; either version 3, or (at your option) any --
  8. -- later version. This library is distributed in the hope that it will be --
  9. -- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of --
  10. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
  11. -- --
  12. -- As a special exception under Section 7 of GPL version 3, you are --
  13. -- granted additional permissions described in the GCC Runtime Library --
  14. -- Exception, version 3.1, as published by the Free Software Foundation. --
  15. -- --
  16. -- You should have received a copy of the GNU General Public License and --
  17. -- a copy of the GCC Runtime Library Exception along with this program; --
  18. -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
  19. -- <http://www.gnu.org/licenses/>. --
  20. -- --
  21. -------------------------------------------------------------------------------
  22. with Ada.Calendar.Formatting;
  23. with Ada.Strings.Fixed;
  24. with DOM.Core.Documents;
  25. with DOM.Core.Elements;
  26. with DOM.Core.Nodes;
  27. with DOM.Readers;
  28. with Input_Sources.Strings;
  29. with Sax.Readers;
  30. with Unicode.CES.Utf8;
  31. package body Yolk.Syndication.DOM_Builder is
  32. -----------------------
  33. -- Atom_Date_Image --
  34. -----------------------
  35. function Atom_Date_Image
  36. (Time_Stamp : in Ada.Calendar.Time)
  37. return String
  38. is
  39. use Ada.Calendar.Formatting;
  40. Atom_Time : String (1 .. 20);
  41. begin
  42. Atom_Time (1 .. 19) := Image (Date => Time_Stamp,
  43. Include_Time_Fraction => False);
  44. Atom_Time (11) := 'T';
  45. Atom_Time (20) := 'Z';
  46. return Atom_Time;
  47. end Atom_Date_Image;
  48. -----------------
  49. -- Attribute --
  50. -----------------
  51. procedure Attribute
  52. (Elem : in DOM.Core.Node;
  53. Name : in String;
  54. Value : in String)
  55. is
  56. use DOM.Core.Elements;
  57. begin
  58. if Value /= "" then
  59. Set_Attribute (Elem => Elem,
  60. Name => Name,
  61. Value => Value);
  62. end if;
  63. end Attribute;
  64. --------------------------------
  65. -- Create_Category_Elements --
  66. --------------------------------
  67. procedure Create_Category_Elements
  68. (Doc : in DOM.Core.Document;
  69. List : in Category_List.List;
  70. Parent : in DOM.Core.Node)
  71. is
  72. use DOM.Core;
  73. use DOM.Core.Documents;
  74. use DOM.Core.Elements;
  75. use DOM.Core.Nodes;
  76. Category_Node : Node;
  77. begin
  78. for A_Category of List loop
  79. Category_Node := Append_Child
  80. (N => Parent,
  81. New_Child => Create_Element (Doc => Doc,
  82. Tag_Name => "category"));
  83. Set_Attribute (Elem => Category_Node,
  84. Name => "term",
  85. Value => To_String (A_Category.Term));
  86. Attribute (Elem => Category_Node,
  87. Name => "xml:base",
  88. Value => To_String (A_Category.Common.Base_URI));
  89. Attribute (Elem => Category_Node,
  90. Name => "xml:lang",
  91. Value => To_String (A_Category.Common.Language));
  92. Attribute (Elem => Category_Node,
  93. Name => "label",
  94. Value => To_String (A_Category.Label));
  95. Attribute (Elem => Category_Node,
  96. Name => "scheme",
  97. Value => To_String (A_Category.Scheme));
  98. end loop;
  99. end Create_Category_Elements;
  100. ------------------------------
  101. -- Create_Content_Element --
  102. ------------------------------
  103. procedure Create_Content_Element
  104. (Doc : in DOM.Core.Document;
  105. Entry_Content : in Atom_Entry_Content;
  106. Parent : in DOM.Core.Node)
  107. is
  108. use DOM.Core;
  109. use DOM.Core.Documents;
  110. use DOM.Core.Nodes;
  111. Content_Node : Node;
  112. begin
  113. case Entry_Content.Content_Kind is
  114. when Text | Html | Xhtml =>
  115. Create_Text_Construct
  116. (Common => Entry_Content.Common,
  117. Data => To_String (Entry_Content.Content),
  118. Doc => Doc,
  119. Elem_Name => "content",
  120. Parent => Parent,
  121. Text_Kind => Entry_Content.Content_Kind);
  122. when others =>
  123. Content_Node := Append_Child
  124. (N => Parent,
  125. New_Child => Create_Element (Doc => Doc,
  126. Tag_Name => "content"));
  127. Attribute (Elem => Content_Node,
  128. Name => "xml:base",
  129. Value => To_String (Entry_Content.Common.Base_URI));
  130. Attribute (Elem => Content_Node,
  131. Name => "xml:lang",
  132. Value => To_String (Entry_Content.Common.Language));
  133. Attribute (Elem => Content_Node,
  134. Name => "type",
  135. Value => To_String (Entry_Content.Mime_Type));
  136. if Entry_Content.Source /= Null_Unbounded_String then
  137. Attribute (Elem => Content_Node,
  138. Name => "src",
  139. Value => To_String (Entry_Content.Source));
  140. else
  141. Content_Node := Append_Child
  142. (N => Content_Node,
  143. New_Child => Create_Text_Node
  144. (Doc => Doc,
  145. Data => To_String (Entry_Content.Content)));
  146. end if;
  147. end case;
  148. end Create_Content_Element;
  149. -----------------------------
  150. -- Create_Entry_Elements --
  151. -----------------------------
  152. procedure Create_Entry_Elements
  153. (Doc : in DOM.Core.Document;
  154. Entries : in Entry_List.List;
  155. Parent : in DOM.Core.Node)
  156. is
  157. use DOM.Core;
  158. use DOM.Core.Documents;
  159. use DOM.Core.Nodes;
  160. Entry_Node : Node;
  161. begin
  162. for An_Entry of Entries loop
  163. Entry_Node := Append_Child
  164. (N => Parent,
  165. New_Child => Create_Element (Doc => Doc,
  166. Tag_Name => "entry"));
  167. Attribute (Elem => Entry_Node,
  168. Name => "xml:base",
  169. Value => To_String (An_Entry.Common.Base_URI));
  170. Attribute (Elem => Entry_Node,
  171. Name => "xml:lang",
  172. Value => To_String (An_Entry.Common.Language));
  173. -- entry:author elements
  174. Create_Person_Elements (Doc => Doc,
  175. Elem_Name => "author",
  176. List => An_Entry.Authors,
  177. Parent => Entry_Node);
  178. -- entry:category elements
  179. Create_Category_Elements (Doc => Doc,
  180. List => An_Entry.Categories,
  181. Parent => Entry_Node);
  182. -- entry:content element
  183. if An_Entry.Content.Content /= Null_Unbounded_String then
  184. Create_Content_Element (Doc => Doc,
  185. Entry_Content => An_Entry.Content,
  186. Parent => Entry_Node);
  187. end if;
  188. -- entry:contributor elements
  189. Create_Person_Elements (Doc => Doc,
  190. Elem_Name => "contributor",
  191. List => An_Entry.Contributors,
  192. Parent => Entry_Node);
  193. -- entry:id element
  194. if An_Entry.Id.URI /= Null_Unbounded_String then
  195. Create_Generic_Element (Common => An_Entry.Id.Common,
  196. Data => To_String (An_Entry.Id.URI),
  197. Doc => Doc,
  198. Elem_Name => "id",
  199. Parent => Entry_Node);
  200. end if;
  201. -- entry:link elements
  202. Create_Link_Elements (Doc => Doc,
  203. List => An_Entry.Links,
  204. Parent => Entry_Node);
  205. -- entry:published element
  206. if An_Entry.Updated.Is_Set then
  207. Create_Generic_Element
  208. (Common => An_Entry.Published.Common,
  209. Data => Atom_Date_Image
  210. (Time_Stamp => An_Entry.Published.Time_Stamp),
  211. Doc => Doc,
  212. Elem_Name => "published",
  213. Parent => Entry_Node);
  214. end if;
  215. -- entry:rights
  216. if An_Entry.Rights.Text_Content /= Null_Unbounded_String then
  217. Create_Text_Construct
  218. (Common => An_Entry.Rights.Common,
  219. Data => To_String (An_Entry.Rights.Text_Content),
  220. Doc => Doc,
  221. Elem_Name => "rights",
  222. Parent => Entry_Node,
  223. Text_Kind => An_Entry.Rights.Text_Kind);
  224. end if;
  225. -- entry:source element
  226. if An_Entry.Source /= Null_Atom_Entry_Source then
  227. Create_Entry_Source_Element (Doc => Doc,
  228. Source => An_Entry.Source,
  229. Parent => Entry_Node);
  230. end if;
  231. -- entry:summary element
  232. if An_Entry.Summary.Text_Content /= Null_Unbounded_String then
  233. Create_Text_Construct
  234. (Common => An_Entry.Summary.Common,
  235. Data => To_String (An_Entry.Summary.Text_Content),
  236. Doc => Doc,
  237. Elem_Name => "summary",
  238. Parent => Entry_Node,
  239. Text_Kind => An_Entry.Summary.Text_Kind);
  240. end if;
  241. -- entry:title element
  242. if An_Entry.Title.Text_Content /= Null_Unbounded_String then
  243. Create_Text_Construct
  244. (Common => An_Entry.Title.Common,
  245. Data => To_String (An_Entry.Title.Text_Content),
  246. Doc => Doc,
  247. Elem_Name => "title",
  248. Parent => Entry_Node,
  249. Text_Kind => An_Entry.Title.Text_Kind);
  250. end if;
  251. -- entry:updated element
  252. if An_Entry.Updated.Is_Set then
  253. Create_Generic_Element
  254. (Common => An_Entry.Common,
  255. Data => Atom_Date_Image
  256. (Time_Stamp => An_Entry.Updated.Time_Stamp),
  257. Doc => Doc,
  258. Elem_Name => "updated",
  259. Parent => Entry_Node);
  260. end if;
  261. end loop;
  262. end Create_Entry_Elements;
  263. -----------------------------------
  264. -- Create_Entry_Source_Element --
  265. -----------------------------------
  266. procedure Create_Entry_Source_Element
  267. (Doc : in DOM.Core.Document;
  268. Source : in Atom_Entry_Source;
  269. Parent : in DOM.Core.Node)
  270. is
  271. use DOM.Core;
  272. use DOM.Core.Documents;
  273. use DOM.Core.Nodes;
  274. Source_Node : Node;
  275. begin
  276. Source_Node := Append_Child
  277. (N => Parent,
  278. New_Child => Create_Element (Doc => Doc,
  279. Tag_Name => "source"));
  280. Attribute (Elem => Source_Node,
  281. Name => "xml:base",
  282. Value => To_String (Source.Common.Base_URI));
  283. Attribute (Elem => Source_Node,
  284. Name => "xml:lang",
  285. Value => To_String (Source.Common.Language));
  286. Create_Person_Elements (Doc => Doc,
  287. Elem_Name => "author",
  288. List => Source.Authors,
  289. Parent => Source_Node);
  290. Create_Category_Elements (Doc => Doc,
  291. List => Source.Categories,
  292. Parent => Source_Node);
  293. Create_Person_Elements (Doc => Doc,
  294. Elem_Name => "contributor",
  295. List => Source.Contributors,
  296. Parent => Source_Node);
  297. Create_Generator_Element (A_Generator => Source.Generator,
  298. Doc => Doc,
  299. Parent => Source_Node);
  300. if Source.Icon.URI /= Null_Unbounded_String then
  301. Create_Generic_Element (Common => Source.Icon.Common,
  302. Data => To_String (Source.Icon.URI),
  303. Doc => Doc,
  304. Elem_Name => "icon",
  305. Parent => Source_Node);
  306. end if;
  307. if Source.Id.URI /= Null_Unbounded_String then
  308. Create_Generic_Element (Common => Source.Id.Common,
  309. Data => To_String (Source.Id.URI),
  310. Doc => Doc,
  311. Elem_Name => "id",
  312. Parent => Source_Node);
  313. end if;
  314. Create_Link_Elements (Doc => Doc,
  315. List => Source.Links,
  316. Parent => Source_Node);
  317. if Source.Logo.URI /= Null_Unbounded_String then
  318. Create_Generic_Element (Common => Source.Logo.Common,
  319. Data => To_String (Source.Logo.URI),
  320. Doc => Doc,
  321. Elem_Name => "logo",
  322. Parent => Source_Node);
  323. end if;
  324. if Source.Rights.Text_Content /= Null_Unbounded_String then
  325. Create_Text_Construct
  326. (Common => Source.Rights.Common,
  327. Data => To_String (Source.Rights.Text_Content),
  328. Doc => Doc,
  329. Elem_Name => "rights",
  330. Parent => Source_Node,
  331. Text_Kind => Source.Rights.Text_Kind);
  332. end if;
  333. if Source.Subtitle.Text_Content /= Null_Unbounded_String then
  334. Create_Text_Construct
  335. (Common => Source.Subtitle.Common,
  336. Data => To_String (Source.Subtitle.Text_Content),
  337. Doc => Doc,
  338. Elem_Name => "subtitle",
  339. Parent => Source_Node,
  340. Text_Kind => Source.Subtitle.Text_Kind);
  341. end if;
  342. if Source.Title.Text_Content /= Null_Unbounded_String then
  343. Create_Text_Construct
  344. (Common => Source.Title.Common,
  345. Data => To_String (Source.Title.Text_Content),
  346. Doc => Doc,
  347. Elem_Name => "title",
  348. Parent => Source_Node,
  349. Text_Kind => Source.Title.Text_Kind);
  350. end if;
  351. if Source.Updated.Is_Set then
  352. Create_Generic_Element
  353. (Common => Source.Updated.Common,
  354. Data =>
  355. Atom_Date_Image (Time_Stamp => Source.Updated.Time_Stamp),
  356. Doc => Doc,
  357. Elem_Name => "updated",
  358. Parent => Source_Node);
  359. end if;
  360. end Create_Entry_Source_Element;
  361. ---------------------------
  362. -- Create_Feed_Element --
  363. ---------------------------
  364. procedure Create_Feed_Element
  365. (Authors : in Person_List.List;
  366. Categories : in Category_List.List;
  367. Common : in Atom_Common;
  368. Contributors : in Person_List.List;
  369. Doc : in DOM.Core.Document;
  370. Entries : in Entry_List.List;
  371. Generator : in Atom_Generator;
  372. Icon : in Atom_Icon;
  373. Id : in Atom_Id;
  374. Links : in Link_List.List;
  375. Logo : in Atom_Logo;
  376. Rights : in Atom_Text;
  377. Subtitle : in Atom_Text;
  378. Title : in Atom_Text;
  379. Updated : in Atom_Date)
  380. is
  381. use DOM.Core;
  382. use DOM.Core.Documents;
  383. use DOM.Core.Elements;
  384. use DOM.Core.Nodes;
  385. Feed_Node : Node;
  386. begin
  387. -- feed element
  388. Feed_Node := Append_Child
  389. (N => Doc,
  390. New_Child => Create_Element (Doc => Doc,
  391. Tag_Name => "feed"));
  392. Set_Attribute (Elem => Feed_Node,
  393. Name => "xmlns",
  394. Value => XMLNS);
  395. Attribute (Elem => Feed_Node,
  396. Name => "xml:base",
  397. Value => To_String (Common.Base_URI));
  398. Attribute (Elem => Feed_Node,
  399. Name => "xml:lang",
  400. Value => To_String (Common.Language));
  401. -- feed:author elements
  402. Create_Person_Elements (Doc => Doc,
  403. Elem_Name => "author",
  404. List => Authors,
  405. Parent => Feed_Node);
  406. -- feed:category elements
  407. Create_Category_Elements (Doc => Doc,
  408. List => Categories,
  409. Parent => Feed_Node);
  410. -- feed:contributor elements
  411. Create_Person_Elements (Doc => Doc,
  412. Elem_Name => "contributor",
  413. List => Contributors,
  414. Parent => Feed_Node);
  415. -- feed:generator element
  416. Create_Generator_Element (A_Generator => Generator,
  417. Doc => Doc,
  418. Parent => Feed_Node);
  419. -- feed:icon element
  420. if Icon.URI /= Null_Unbounded_String then
  421. Create_Generic_Element (Common => Icon.Common,
  422. Data => To_String (Icon.URI),
  423. Doc => Doc,
  424. Elem_Name => "icon",
  425. Parent => Feed_Node);
  426. end if;
  427. -- feed:id element
  428. if Id.URI /= Null_Unbounded_String then
  429. Create_Generic_Element (Common => Id.Common,
  430. Data => To_String (Id.URI),
  431. Doc => Doc,
  432. Elem_Name => "id",
  433. Parent => Feed_Node);
  434. end if;
  435. -- feed:link elements
  436. Create_Link_Elements (Doc => Doc,
  437. List => Links,
  438. Parent => Feed_Node);
  439. -- feed:logo
  440. if Logo.URI /= Null_Unbounded_String then
  441. Create_Generic_Element (Common => Logo.Common,
  442. Data => To_String (Logo.URI),
  443. Doc => Doc,
  444. Elem_Name => "logo",
  445. Parent => Feed_Node);
  446. end if;
  447. -- feed:rights
  448. if Rights.Text_Content /= Null_Unbounded_String then
  449. Create_Text_Construct (Common => Rights.Common,
  450. Data => To_String (Rights.Text_Content),
  451. Doc => Doc,
  452. Elem_Name => "rights",
  453. Parent => Feed_Node,
  454. Text_Kind => Rights.Text_Kind);
  455. end if;
  456. -- feed:subtitle
  457. if Subtitle.Text_Content /= Null_Unbounded_String then
  458. Create_Text_Construct (Common => Subtitle.Common,
  459. Data => To_String (Subtitle.Text_Content),
  460. Doc => Doc,
  461. Elem_Name => "subtitle",
  462. Parent => Feed_Node,
  463. Text_Kind => Subtitle.Text_Kind);
  464. end if;
  465. -- feed:title element
  466. if Title.Text_Content /= Null_Unbounded_String then
  467. Create_Text_Construct (Common => Title.Common,
  468. Data => To_String (Title.Text_Content),
  469. Doc => Doc,
  470. Elem_Name => "title",
  471. Parent => Feed_Node,
  472. Text_Kind => Title.Text_Kind);
  473. end if;
  474. -- feed:updated element
  475. if Updated.Is_Set then
  476. Create_Generic_Element
  477. (Common => Updated.Common,
  478. Data => Atom_Date_Image (Time_Stamp => Updated.Time_Stamp),
  479. Doc => Doc,
  480. Elem_Name => "updated",
  481. Parent => Feed_Node);
  482. end if;
  483. -- feed:entry elements
  484. Create_Entry_Elements (Doc => Doc,
  485. Entries => Entries,
  486. Parent => Feed_Node);
  487. end Create_Feed_Element;
  488. --------------------------------
  489. -- Create_Generator_Element --
  490. --------------------------------
  491. procedure Create_Generator_Element
  492. (A_Generator : in Atom_Generator;
  493. Doc : in DOM.Core.Document;
  494. Parent : in DOM.Core.Node)
  495. is
  496. use DOM.Core;
  497. use DOM.Core.Documents;
  498. use DOM.Core.Nodes;
  499. Generator_Node : Node;
  500. begin
  501. if A_Generator.Agent /= Null_Unbounded_String then
  502. Generator_Node := Append_Child
  503. (N => Parent,
  504. New_Child => Create_Element (Doc => Doc,
  505. Tag_Name => "generator"));
  506. Attribute (Elem => Generator_Node,
  507. Name => "xml:base",
  508. Value => To_String (A_Generator.Common.Base_URI));
  509. Attribute (Elem => Generator_Node,
  510. Name => "xml:lang",
  511. Value => To_String (A_Generator.Common.Language));
  512. Attribute (Elem => Generator_Node,
  513. Name => "uri",
  514. Value => To_String (A_Generator.URI));
  515. Attribute (Elem => Generator_Node,
  516. Name => "version",
  517. Value => To_String (A_Generator.Version));
  518. Generator_Node := Append_Child
  519. (N => Generator_Node,
  520. New_Child => Create_Text_Node
  521. (Doc => Doc,
  522. Data => To_String (A_Generator.Agent)));
  523. end if;
  524. end Create_Generator_Element;
  525. ------------------------------
  526. -- Create_Generic_Element --
  527. ------------------------------
  528. procedure Create_Generic_Element
  529. (Common : in Atom_Common;
  530. Data : in String;
  531. Doc : in DOM.Core.Document;
  532. Elem_Name : in String;
  533. Parent : in DOM.Core.Node)
  534. is
  535. use DOM.Core;
  536. use DOM.Core.Documents;
  537. use DOM.Core.Nodes;
  538. Elem_Node : Node;
  539. begin
  540. Elem_Node := Append_Child
  541. (N => Parent,
  542. New_Child => Create_Element (Doc => Doc,
  543. Tag_Name => Elem_Name));
  544. Attribute (Elem => Elem_Node,
  545. Name => "xml:base",
  546. Value => To_String (Common.Base_URI));
  547. Attribute (Elem => Elem_Node,
  548. Name => "xml:lang",
  549. Value => To_String (Common.Language));
  550. Elem_Node := Append_Child
  551. (N => Elem_Node,
  552. New_Child => Create_Text_Node (Doc => Doc,
  553. Data => Data));
  554. pragma Unreferenced (Elem_Node);
  555. -- We need this because XML/Ada have no Append_Child procedures,
  556. -- which obviously is annoying as hell.
  557. end Create_Generic_Element;
  558. ----------------------------
  559. -- Create_Link_Elements --
  560. ----------------------------
  561. procedure Create_Link_Elements
  562. (Doc : in DOM.Core.Document;
  563. List : in Link_List.List;
  564. Parent : in DOM.Core.Node)
  565. is
  566. use Ada.Strings;
  567. use DOM.Core;
  568. use DOM.Core.Documents;
  569. use DOM.Core.Elements;
  570. use DOM.Core.Nodes;
  571. Link_Node : Node;
  572. begin
  573. for A_Link of List loop
  574. Link_Node := Append_Child
  575. (N => Parent,
  576. New_Child => Create_Element (Doc => Doc,
  577. Tag_Name => "link"));
  578. Attribute (Elem => Link_Node,
  579. Name => "xml:base",
  580. Value => To_String (A_Link.Common.Base_URI));
  581. Attribute (Elem => Link_Node,
  582. Name => "xml:lang",
  583. Value => To_String (A_Link.Common.Language));
  584. case A_Link.Rel is
  585. when Alternate =>
  586. Set_Attribute (Elem => Link_Node,
  587. Name => "rel",
  588. Value => "alternate");
  589. when Related =>
  590. Set_Attribute (Elem => Link_Node,
  591. Name => "rel",
  592. Value => "related");
  593. when Self =>
  594. Set_Attribute (Elem => Link_Node,
  595. Name => "rel",
  596. Value => "self");
  597. when Enclosure =>
  598. Set_Attribute (Elem => Link_Node,
  599. Name => "rel",
  600. Value => "enclosure");
  601. when Via =>
  602. Set_Attribute (Elem => Link_Node,
  603. Name => "rel",
  604. Value => "via");
  605. end case;
  606. Set_Attribute (Elem => Link_Node,
  607. Name => "href",
  608. Value => To_String (A_Link.Href));
  609. Attribute (Elem => Link_Node,
  610. Name => "hreflang",
  611. Value => To_String (A_Link.Hreflang));
  612. if A_Link.Length > 0 then
  613. Set_Attribute
  614. (Elem => Link_Node,
  615. Name => "length",
  616. Value => Fixed.Trim
  617. (Source => Natural'Image (A_Link.Length),
  618. Side => Left));
  619. end if;
  620. Attribute (Elem => Link_Node,
  621. Name => "type",
  622. Value => To_String (A_Link.Mime_Type));
  623. Attribute (Elem => Link_Node,
  624. Name => "title",
  625. Value => To_String (A_Link.Title));
  626. end loop;
  627. end Create_Link_Elements;
  628. -------------------------------
  629. -- Create_Node_From_String --
  630. -------------------------------
  631. function Create_Node_From_String
  632. (XML_String : in String)
  633. return DOM.Core.Node
  634. is
  635. use DOM.Core;
  636. use DOM.Core.Nodes;
  637. use DOM.Readers;
  638. use Input_Sources.Strings;
  639. use Sax.Readers;
  640. Input : String_Input;
  641. Reader : Tree_Reader;
  642. begin
  643. return Fragment : Node do
  644. Open (Str => XML_String,
  645. Encoding => Unicode.CES.Utf8.Utf8_Encoding,
  646. Input => Input);
  647. Set_Feature (Parser => Reader,
  648. Name => Validation_Feature,
  649. Value => False);
  650. Set_Feature (Parser => Reader,
  651. Name => Namespace_Feature,
  652. Value => False);
  653. Parse (Parser => Reader,
  654. Input => Input);
  655. Close (Input => Input);
  656. Fragment := Get_Tree (Read => Reader);
  657. exception
  658. when others =>
  659. raise Not_Valid_XML with XML_String;
  660. end return;
  661. end Create_Node_From_String;
  662. ------------------------------
  663. -- Create_Person_Elements --
  664. ------------------------------
  665. procedure Create_Person_Elements
  666. (Doc : in DOM.Core.Document;
  667. Elem_Name : in String;
  668. List : in Person_List.List;
  669. Parent : in DOM.Core.Node)
  670. is
  671. use DOM.Core;
  672. use DOM.Core.Documents;
  673. use DOM.Core.Nodes;
  674. Elem_Node : Node;
  675. Person_Node : Node;
  676. begin
  677. for A_Person of List loop
  678. Person_Node := Append_Child
  679. (N => Parent,
  680. New_Child => Create_Element (Doc => Doc,
  681. Tag_Name => Elem_Name));
  682. Attribute (Elem => Person_Node,
  683. Name => "xml:base",
  684. Value => To_String (A_Person.Common.Base_URI));
  685. Attribute (Elem => Person_Node,
  686. Name => "xml:lang",
  687. Value => To_String (A_Person.Common.Language));
  688. Elem_Node := Append_Child
  689. (N => Person_Node,
  690. New_Child => Create_Element (Doc => Doc,
  691. Tag_Name => "name"));
  692. Elem_Node := Append_Child
  693. (N => Elem_Node,
  694. New_Child => Create_Text_Node (Doc => Doc,
  695. Data => To_String (A_Person.Name)));
  696. if A_Person.Email /= Null_Unbounded_String then
  697. Elem_Node := Append_Child
  698. (N => Person_Node,
  699. New_Child => Create_Element (Doc => Doc,
  700. Tag_Name => "email"));
  701. Elem_Node := Append_Child
  702. (N => Elem_Node,
  703. New_Child => Create_Text_Node
  704. (Doc => Doc,
  705. Data => To_String (A_Person.Email)));
  706. end if;
  707. if A_Person.URI /= Null_Unbounded_String then
  708. Elem_Node := Append_Child
  709. (N => Person_Node,
  710. New_Child => Create_Element (Doc => Doc,
  711. Tag_Name => "uri"));
  712. Elem_Node := Append_Child
  713. (N => Elem_Node,
  714. New_Child => Create_Text_Node
  715. (Doc => Doc,
  716. Data => To_String (A_Person.URI)));
  717. end if;
  718. end loop;
  719. end Create_Person_Elements;
  720. -----------------------------
  721. -- Create_Text_Construct --
  722. -----------------------------
  723. procedure Create_Text_Construct
  724. (Common : in Atom_Common;
  725. Data : in String;
  726. Doc : in DOM.Core.Document;
  727. Elem_Name : in String;
  728. Parent : in DOM.Core.Node;
  729. Text_Kind : in Text_Kinds)
  730. is
  731. use DOM.Core;
  732. use DOM.Core.Documents;
  733. use DOM.Core.Elements;
  734. use DOM.Core.Nodes;
  735. Elem_Node : Node;
  736. begin
  737. Elem_Node := Append_Child
  738. (N => Parent,
  739. New_Child => Create_Element (Doc => Doc,
  740. Tag_Name => Elem_Name));
  741. Attribute (Elem => Elem_Node,
  742. Name => "xml:base",
  743. Value => To_String (Common.Base_URI));
  744. Attribute (Elem => Elem_Node,
  745. Name => "xml:lang",
  746. Value => To_String (Common.Language));
  747. case Text_Kind is
  748. when Text =>
  749. Set_Attribute (Elem => Elem_Node,
  750. Name => "type",
  751. Value => "text");
  752. Elem_Node := Append_Child
  753. (N => Elem_Node,
  754. New_Child => Create_Text_Node
  755. (Doc => Doc,
  756. Data => Data));
  757. when Html =>
  758. Set_Attribute (Elem => Elem_Node,
  759. Name => "type",
  760. Value => "html");
  761. Elem_Node := Append_Child
  762. (N => Elem_Node,
  763. New_Child => Create_Text_Node
  764. (Doc => Doc,
  765. Data => Data));
  766. when Xhtml =>
  767. Set_Attribute (Elem => Elem_Node,
  768. Name => "type",
  769. Value => "xhtml");
  770. Elem_Node := Append_Child
  771. (N => Elem_Node,
  772. New_Child => Create_Node_From_String
  773. (XML_String =>
  774. "<div " & DIVNS & ">" & Data & "</div>"));
  775. end case;
  776. end Create_Text_Construct;
  777. end Yolk.Syndication.DOM_Builder;