XML.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. #include "XML.h"
  2. using namespace Framework;
  3. using namespace XML;
  4. Framework::XML::Element::Element()
  5. : ReferenceCounter(),
  6. children(new RCArray<Element>()),
  7. attributes(new RCArray<Text>()),
  8. attributeValues(new RCArray<Text>()),
  9. name(new Text()),
  10. text(new Text()),
  11. parent(0)
  12. {}
  13. // Creates an XML Element
  14. // string: either the name of the element or an XML text to be parsed
  15. Element::Element(Text string)
  16. : Element(string, 0)
  17. {}
  18. // Creates an XML Element
  19. // string: either the name of the element or an XML text to be parsed
  20. // zParent: A pointer to the parent element (without increased reference
  21. // counter)
  22. Element::Element(Text string, Element* zParent)
  23. : Element()
  24. {
  25. string.removeWhitespaceAfter(0);
  26. string.removeWhitespaceBefore(string.getLength());
  27. setText(string);
  28. if (string[0] == '<' && string[string.getLength() - 1] == '>')
  29. {
  30. string.removeWhitespaceAfter(1);
  31. string.removeWhitespaceBefore(string.getLength() - 1);
  32. int nameEnd = 0;
  33. for (int i = 1; i < string.getLength(); i++)
  34. {
  35. if ((string[i] < 'a' || string[i] > 'z')
  36. && (string[i] < 'A' || string[i] > 'Z')
  37. && (string[i] < '0' || string[i] > '9') && string[i] != '-'
  38. && string[i] != '_' && string[i] != '.')
  39. {
  40. nameEnd = i;
  41. break;
  42. }
  43. }
  44. Text* tmp = string.getTeilText(1, nameEnd);
  45. name->setText(*tmp);
  46. tmp->release();
  47. if (string.hasAt(
  48. string.getLength() - 1 - name->getLength(), name->getText())
  49. || string[string.getLength() - 2] == '/')
  50. {
  51. string.removeWhitespaceAfter(nameEnd);
  52. // parse attributes
  53. int start = nameEnd;
  54. while (string[nameEnd] != '>' && string[nameEnd] != '/')
  55. {
  56. for (int i = nameEnd + 1; i < string.getLength(); i++)
  57. {
  58. if ((string[i] < 'a' || string[i] > 'z')
  59. && (string[i] < 'A' || string[i] > 'Z')
  60. && (string[i] < '0' || string[i] > '9')
  61. && string[i] != '-' && string[i] != '_'
  62. && string[i] != '.')
  63. {
  64. nameEnd = i;
  65. break;
  66. }
  67. }
  68. Text* attrName = string.getTeilText(start, nameEnd);
  69. string.removeWhitespaceAfter(nameEnd);
  70. if (string[nameEnd] == '=')
  71. {
  72. string.removeWhitespaceAfter(nameEnd + 1);
  73. Text value = "";
  74. if (string[nameEnd + 1] == '"')
  75. {
  76. bool esc = 0;
  77. start = nameEnd + 2;
  78. for (int i = nameEnd + 2; string[i]; i++)
  79. {
  80. if (string[i] == '\\')
  81. esc = !esc;
  82. else
  83. {
  84. if (string[i] == '"' && !esc)
  85. {
  86. nameEnd = i + 1;
  87. break;
  88. }
  89. esc = 0;
  90. }
  91. }
  92. tmp = string.getTeilText(start, nameEnd - 1);
  93. value.setText(*tmp);
  94. tmp->release();
  95. value.replace("\\\"", "\"");
  96. }
  97. if (string[nameEnd + 1] == '\'')
  98. {
  99. bool esc = 0;
  100. start = nameEnd + 2;
  101. for (int i = nameEnd + 2; string[i]; i++)
  102. {
  103. if (string[i] == '\\')
  104. esc = !esc;
  105. else
  106. {
  107. if (string[i] == '\'' && !esc)
  108. {
  109. nameEnd = i + 1;
  110. break;
  111. }
  112. esc = 0;
  113. }
  114. }
  115. tmp = string.getTeilText(start, nameEnd - 1);
  116. value.setText(*tmp);
  117. tmp->release();
  118. value.replace("\\'", "'");
  119. }
  120. setAttribute(attrName->getText(), value);
  121. }
  122. else
  123. setAttribute(attrName->getText(), "");
  124. attrName->release();
  125. string.removeWhitespaceAfter(nameEnd);
  126. start = nameEnd;
  127. }
  128. if (string[string.getLength() - 2] != '/')
  129. {
  130. string.removeWhitespaceBefore(
  131. string.getLength() - 1 - name->getLength());
  132. if (string[string.getLength() - 2 - name->getLength()] == '/')
  133. {
  134. string.removeWhitespaceBefore(
  135. string.getLength() - 2 - name->getLength());
  136. if (string[string.getLength() - 3 - name->getLength()]
  137. == '<')
  138. {
  139. tmp = string.getTeilText(nameEnd + 1,
  140. string.getLength() - 3 - name->getLength());
  141. text->setText(*tmp);
  142. tmp->release();
  143. // parse children
  144. text->removeWhitespaceAfter(0);
  145. text->removeWhitespaceBefore(text->getLength());
  146. if (text->getText()[0] == '<'
  147. && text->getText()[text->getLength() - 1] == '>')
  148. {
  149. int start = 0;
  150. int lastStart = -1;
  151. while (start < text->getLength())
  152. {
  153. if (lastStart == start) break;
  154. lastStart = start;
  155. bool esc = 0;
  156. bool inString1 = 0;
  157. bool inString2 = 0;
  158. int poc = 0;
  159. bool lastSlash = 0;
  160. bool lastOpen = 0;
  161. bool openSlash = 0;
  162. for (int i = 0; text->getText()[i]; i++)
  163. {
  164. switch (text->getText()[i])
  165. {
  166. case '\\':
  167. esc = !esc;
  168. lastSlash = 0;
  169. lastOpen = 0;
  170. break;
  171. case '"':
  172. if (!esc && !inString2)
  173. inString1 = !inString1;
  174. esc = 0;
  175. lastSlash = 0;
  176. lastOpen = 0;
  177. break;
  178. case '\'':
  179. if (!esc && !inString1)
  180. inString2 = !inString2;
  181. esc = 0;
  182. lastSlash = 0;
  183. lastOpen = 0;
  184. break;
  185. case '<':
  186. if (!inString1 && !inString2)
  187. lastOpen = 1;
  188. esc = 0;
  189. lastSlash = 0;
  190. break;
  191. case '/':
  192. lastSlash = 0;
  193. if (!inString1 && !inString2)
  194. {
  195. lastSlash = 1;
  196. if (lastOpen) openSlash = 1;
  197. }
  198. esc = 0;
  199. lastOpen = 0;
  200. break;
  201. case '>':
  202. if (!inString1 && !inString2)
  203. {
  204. if (openSlash)
  205. poc--;
  206. else if (!lastSlash)
  207. poc++;
  208. if (poc == 0)
  209. {
  210. Text* str = text->getTeilText(
  211. start, i + 1);
  212. addChild(new Element(
  213. str->getText(), this));
  214. str->release();
  215. start = i + 1;
  216. }
  217. }
  218. esc = 0;
  219. lastSlash = 0;
  220. openSlash = 0;
  221. break;
  222. default:
  223. esc = 0;
  224. if (text->getText()[i] != ' '
  225. && text->getText()[i] != '\t'
  226. && text->getText()[i] != '\r'
  227. && text->getText()[i] != '\n')
  228. {
  229. lastSlash = 0;
  230. lastOpen = 0;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. else
  240. text->setText("");
  241. }
  242. }
  243. parent = zParent;
  244. }
  245. Element::~Element()
  246. {
  247. children->release();
  248. attributes->release();
  249. attributeValues->release();
  250. text->release();
  251. name->release();
  252. }
  253. // Changes an attribute or adds a new one
  254. // attribut: The name of the attribute
  255. // value: The value of the attribute
  256. void Element::setAttribute(Text attribut, Text value)
  257. {
  258. for (auto i = attributes->begin(), j = attributeValues->begin(); i && j;
  259. i++, j++)
  260. {
  261. if (i->isEqual(attribut))
  262. {
  263. j->setText(value);
  264. return;
  265. }
  266. }
  267. attributes->add(new Text(attribut));
  268. attributeValues->add(new Text(value));
  269. }
  270. // Removes an attribute
  271. // attribut: The name of the attribute
  272. void Element::removeAttribute(Text attribut)
  273. {
  274. for (int i = 0; i < attributes->getEntryCount(); i++)
  275. {
  276. if (attributes->z(i)->isEqual(attribut))
  277. {
  278. attributes->remove(i);
  279. attributeValues->remove(i);
  280. i--;
  281. }
  282. }
  283. }
  284. // Adds a child
  285. // child: The new child element
  286. void Element::addChild(Element* child)
  287. {
  288. child->parent = this;
  289. children->add(child);
  290. }
  291. // Adds a child at the front
  292. // child: The new child element
  293. void Element::addChildAtFront(Element* child)
  294. {
  295. child->parent = this;
  296. children->add(child, 0);
  297. }
  298. // Removes a child
  299. // zChild: The child to remove
  300. void Element::removeChild(Element* child)
  301. {
  302. for (int i = 0; i < children->getEntryCount(); i++)
  303. {
  304. if (children->z(i) == child)
  305. {
  306. children->remove(i);
  307. i--;
  308. }
  309. }
  310. child->release();
  311. }
  312. // Removes the i-th child
  313. // i: The index of the child (starting at 0)
  314. void Element::removeChild(int i)
  315. {
  316. children->remove(i);
  317. }
  318. // Removes all children
  319. void Element::removeAllChilds()
  320. {
  321. children->clear();
  322. }
  323. // Removes a list of children
  324. // childs: All children to be removed
  325. void Element::removeChilds(RCArray<Element>* childs)
  326. {
  327. for (auto i : *childs)
  328. removeChild(dynamic_cast<XML::Element*>(i->getThis()));
  329. childs->release();
  330. }
  331. // Removes this element from the parent element
  332. void Element::remove()
  333. {
  334. if (parent) parent->removeChild(dynamic_cast<XML::Element*>(getThis()));
  335. }
  336. // Sets the text in the element if it has no children
  337. // text: The text
  338. void Element::setText(Text text)
  339. {
  340. this->text->setText(text);
  341. }
  342. // Returns the text in the element
  343. Text Element::getText() const
  344. {
  345. return text->getText();
  346. }
  347. // Returns the number of children
  348. int Element::getChildCount() const
  349. {
  350. return children->getEntryCount();
  351. }
  352. int Framework::XML::Element::getChildIndex(Element* zChild) const
  353. {
  354. return children->indexOf(zChild);
  355. }
  356. // Returns the i-th child
  357. Element* Element::getChild(int i) const
  358. {
  359. return children->get(i);
  360. }
  361. // Returns the i-th child (without increased reference counter)
  362. Element* Element::zChild(int i) const
  363. {
  364. return children->z(i);
  365. }
  366. // Returns the parent element
  367. Element* Element::getParent() const
  368. {
  369. return parent ? dynamic_cast<Element*>(parent->getThis()) : 0;
  370. }
  371. // Returns the parent element (without increased reference counter)
  372. Element* Element::zParent() const
  373. {
  374. return parent;
  375. }
  376. // Returns an iterator to iterate through all children
  377. ArrayIterator<Element*> Element::getChilds() const
  378. {
  379. return children->begin();
  380. }
  381. //! Returns an editor for this element
  382. Editor Element::select()
  383. {
  384. RCArray<Element>* tmp = new RCArray<Element>();
  385. tmp->add(dynamic_cast<XML::Element*>(getThis()));
  386. return Editor(tmp);
  387. }
  388. // Returns a selector containing all children
  389. Editor Element::selectChildren() const
  390. {
  391. return Editor(dynamic_cast<RCArray<XML::Element>*>(children->getThis()));
  392. }
  393. // Returns a list of children that have a specific name
  394. // name: The name of the children
  395. Editor Element::selectChildsByName(Text name) const
  396. {
  397. RCArray<Element>* tmp = new RCArray<Element>();
  398. for (auto i : *children)
  399. {
  400. if (i->getName().isEqual(name))
  401. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  402. }
  403. return Editor(tmp);
  404. }
  405. // Returns a list of children that have a specific attribute
  406. // attribute: The name of the attribute
  407. Editor Element::selectChildsByAttribute(Text attribute) const
  408. {
  409. RCArray<Element>* tmp = new RCArray<Element>();
  410. for (auto i : *children)
  411. {
  412. if (i->hasAttribute(attribute))
  413. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  414. }
  415. return Editor(tmp);
  416. }
  417. // Returns a list of children that have a specific attribute with a
  418. // specific value
  419. // attribute: The name of the attribute
  420. // value: The value of the attribute
  421. Editor Element::selectChildsByAttribute(Text attribute, Text value) const
  422. {
  423. RCArray<Element>* tmp = new RCArray<Element>();
  424. for (auto i : *children)
  425. {
  426. if (i->hasAttribute(attribute)
  427. && i->getAttributeValue(attribute).isEqual(value))
  428. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  429. }
  430. return Editor(tmp);
  431. }
  432. // Returns 1 if an attribute name exists, 0 otherwise
  433. bool Element::hasAttribute(Text name) const
  434. {
  435. for (auto i : *attributes)
  436. {
  437. if (i->isEqual(name)) return 1;
  438. }
  439. return 0;
  440. }
  441. // Returns the number of attributes
  442. int Element::getAttributeCount() const
  443. {
  444. return attributes->getEntryCount();
  445. }
  446. // Returns the name of the i-th attribute
  447. Text Element::getAttributeName(int i) const
  448. {
  449. return attributes->z(i)->getText();
  450. }
  451. // Returns the value of the i-th attribute
  452. Text Element::getAttributeValue(int i) const
  453. {
  454. return attributeValues->z(i)->getText();
  455. }
  456. // Returns the value of an attribute
  457. // attribut: The name of the attribute
  458. const Text& Element::getAttributeValue(const Text& attribut) const
  459. {
  460. for (auto i = attributes->begin(), j = attributeValues->begin(); i && j;
  461. i++, j++)
  462. {
  463. if (i->isEqual(attribut)) return *j.val();
  464. }
  465. return Text::EMPTY;
  466. }
  467. // Returns an iterator to iterate through all attribute names
  468. ArrayIterator<Text*> Element::getAttributeNames() const
  469. {
  470. return attributes->begin();
  471. }
  472. // Returns an iterator to iterate through all attribute values
  473. ArrayIterator<Text*> Element::getAttributeValues() const
  474. {
  475. return attributeValues->begin();
  476. }
  477. void Framework::XML::Element::setName(Text name)
  478. {
  479. this->name->setText(name);
  480. }
  481. // Returns the name of the element
  482. Text Element::getName() const
  483. {
  484. return name->getText();
  485. }
  486. // Generates an XML text containing this element and all children
  487. Text Element::toString() const
  488. {
  489. Text ret = "<";
  490. ret += name->getText();
  491. if (attributes->getEntryCount()) ret += " ";
  492. for (auto i = attributes->begin(), j = attributeValues->begin(); i && j;
  493. i++, j++)
  494. {
  495. ret += i->getText();
  496. if (j->has('"'))
  497. {
  498. ret += "='";
  499. Text txt = j->getText();
  500. txt.replace("'", "\\'");
  501. ret += txt;
  502. ret += "'";
  503. }
  504. else
  505. {
  506. ret += "=\"";
  507. Text txt = j->getText();
  508. txt.replace("\"", "\\\"");
  509. ret += txt;
  510. ret += "\"";
  511. }
  512. if (i.hasNext()) ret += " ";
  513. }
  514. if (children->getEntryCount() || text->getLength())
  515. {
  516. ret += ">";
  517. if (children->getEntryCount())
  518. {
  519. for (auto i : *children)
  520. ret += i->toString();
  521. }
  522. else
  523. ret += text->getText();
  524. ret += "</";
  525. ret += name->getText();
  526. ret += ">";
  527. }
  528. else
  529. ret += "/>";
  530. return ret;
  531. }
  532. // Creates a copy without references to this object
  533. Element* Element::dublicate() const
  534. {
  535. return new Element(toString());
  536. }
  537. // Creates a new XML Editor with a list of objects to be edited
  538. Editor::Editor(RCArray<Element>* elements)
  539. : ReferenceCounter()
  540. {
  541. this->elements = elements;
  542. }
  543. Editor::Editor(const Editor& e)
  544. : Editor(dynamic_cast<RCArray<XML::Element>*>(e.elements->getThis()))
  545. {}
  546. Editor::~Editor()
  547. {
  548. elements->release();
  549. }
  550. Maybe<RCPointer<Element>> Framework::XML::Editor::getFirstElement() const
  551. {
  552. if (this->elements->getEntryCount() > 0)
  553. {
  554. return Maybe<RCPointer<Element>>::of(
  555. RCPointer<Element>::of(this->elements->get(0)));
  556. }
  557. return Maybe<RCPointer<Element>>::empty();
  558. }
  559. // Changes an attribute or adds a new one (on all elements in the list)
  560. // attribut: The name of the attribute
  561. // value: The value of the attribute
  562. void Editor::setAttribute(Text attribut, Text value)
  563. {
  564. for (auto i : *elements)
  565. i->setAttribute(attribut, value);
  566. }
  567. // Removes an attribute (on all elements in the list)
  568. // attribut: The name of the attribute
  569. void Editor::removeAttribute(Text attribut)
  570. {
  571. for (auto i : *elements)
  572. i->removeAttribute(attribut);
  573. }
  574. // Adds a child (on all elements in the list)
  575. // child: The new child element
  576. void Editor::addChild(Element* child)
  577. {
  578. for (auto i : *elements)
  579. i->addChild(child->dublicate());
  580. child->release();
  581. }
  582. // Removes a child (on all elements in the list)
  583. // zChild: The child to remove
  584. void Editor::removeChild(Element* child)
  585. {
  586. for (auto i : *elements)
  587. i->removeChild(dynamic_cast<XML::Element*>(child->getThis()));
  588. child->release();
  589. }
  590. // Removes the i-th child (on all elements in the list)
  591. // i: The index of the child (starting at 0)
  592. void Editor::removeChild(int i)
  593. {
  594. for (auto j : *elements)
  595. j->removeChild(i);
  596. }
  597. // Removes all children (on all elements in the list)
  598. void Editor::removeAllChilds()
  599. {
  600. for (auto i : *elements)
  601. i->removeAllChilds();
  602. }
  603. // Removes a list of children (on all elements in the list)
  604. // childs: All children to be removed
  605. void Editor::removeChilds(RCArray<Element>* childs)
  606. {
  607. for (auto i : *elements)
  608. i->removeChilds(
  609. dynamic_cast<RCArray<XML::Element>*>(childs->getThis()));
  610. childs->release();
  611. }
  612. // Removes this element from the parent element (on all elements in the list)
  613. void Editor::remove()
  614. {
  615. for (auto i : *elements)
  616. i->remove();
  617. }
  618. // Sets the text in the element if it has no children (on all elements
  619. // in the list)
  620. // text: The text
  621. void Editor::setText(Text text)
  622. {
  623. for (auto i : *elements)
  624. i->setText(text);
  625. }
  626. // Returns an iterator through all elements
  627. ArrayIterator<Element*> Editor::begin()
  628. {
  629. return elements->begin();
  630. }
  631. //! Returns the end of the iterator
  632. ArrayIterator<Element*> Editor::end()
  633. {
  634. return elements->end();
  635. }
  636. void Editor::selectAllElements(RCArray<Element>* zResult)
  637. {
  638. for (auto i : *elements)
  639. {
  640. zResult->add(dynamic_cast<XML::Element*>(i->getThis()));
  641. i->selectChildren().selectAllElements(zResult);
  642. }
  643. }
  644. //! returns a selector that contains all elements in the selector and all
  645. //! children of these elements recursively
  646. Editor Editor::selectAllElements()
  647. {
  648. RCArray<Element>* list = new RCArray<Element>();
  649. selectAllElements(list);
  650. return Editor(list);
  651. }
  652. // returns a selector that contains all children
  653. Editor Editor::selectChildren() const
  654. {
  655. RCArray<Element>* list = new RCArray<Element>();
  656. for (auto i : *elements)
  657. {
  658. for (Element* j : i->selectChildren())
  659. {
  660. list->add(dynamic_cast<XML::Element*>(j->getThis()));
  661. }
  662. }
  663. return Editor(list);
  664. }
  665. // returns a selector that contains all parents
  666. Editor Editor::selectParents() const
  667. {
  668. RCArray<Element>* list = new RCArray<Element>();
  669. for (auto i : *elements)
  670. {
  671. if (i->parent)
  672. list->add(dynamic_cast<XML::Element*>(i->parent->getThis()));
  673. }
  674. return Editor(list);
  675. }
  676. // returns a list of elements that have a specific name
  677. // name: the name of the children
  678. Editor Editor::whereNameEquals(Text name) const
  679. {
  680. RCArray<Element>* list = new RCArray<Element>();
  681. for (auto i : *elements)
  682. {
  683. if (i->getName().isEqual(name))
  684. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  685. }
  686. return Editor(list);
  687. }
  688. // returns a list of elements that have a specific child
  689. // name: the name of the child
  690. Editor Editor::whereChildWithNameExists(Text name) const
  691. {
  692. RCArray<Element>* list = new RCArray<Element>();
  693. for (auto i : *elements)
  694. {
  695. if (i->selectChildsByName(name).elements->getEntryCount())
  696. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  697. }
  698. return Editor(list);
  699. }
  700. // returns a list of elements that have a specific child
  701. // attribute: the name of the attribute
  702. Editor Editor::whereChildWithAttributeExists(Text attribute) const
  703. {
  704. RCArray<Element>* list = new RCArray<Element>();
  705. for (auto i : *elements)
  706. {
  707. if (i->selectChildsByAttribute(attribute).elements->getEntryCount())
  708. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  709. }
  710. return Editor(list);
  711. }
  712. // returns a list of elements that have a specific child
  713. // attribute: the name of the attribute
  714. // value: the value of the attribute
  715. Editor Editor::whereChildWithAttributeExists(Text attribute, Text value) const
  716. {
  717. RCArray<Element>* list = new RCArray<Element>();
  718. for (auto i : *elements)
  719. {
  720. if (i->selectChildsByAttribute(attribute, value)
  721. .elements->getEntryCount())
  722. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  723. }
  724. return Editor(list);
  725. }
  726. // returns a list of elements that have a specific attribute
  727. // attribute: the name of the attribute
  728. Editor Editor::whereAttributeExists(Text attribute) const
  729. {
  730. RCArray<Element>* list = new RCArray<Element>();
  731. for (auto i : *elements)
  732. {
  733. if (i->hasAttribute(attribute))
  734. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  735. }
  736. return Editor(list);
  737. }
  738. // returns a list of elements that have a specific attribute with a
  739. // specific value
  740. // attribute: the name of the attribute
  741. // value: the value of the attribute
  742. Editor Editor::whereAttributeEquals(Text attribute, Text value) const
  743. {
  744. RCArray<Element>* list = new RCArray<Element>();
  745. for (auto i : *elements)
  746. {
  747. if (i->hasAttribute(attribute)
  748. && i->getAttributeValue(attribute).isEqual(value))
  749. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  750. }
  751. return Editor(list);
  752. }
  753. // Returns an editor that only contains the elements that are not in e
  754. // e: An editor with elements that should not be included
  755. Editor Editor::without(Editor e) const
  756. {
  757. RCArray<Element>* list = new RCArray<Element>();
  758. for (auto i : *elements)
  759. {
  760. bool found = 0;
  761. for (auto j : *e.elements)
  762. found |= i == j;
  763. if (!found) list->add(dynamic_cast<XML::Element*>(i->getThis()));
  764. }
  765. return Editor(list);
  766. }
  767. // calls a function for each element
  768. // f: the function (takes an Element object without increased reference
  769. // counter)
  770. void Editor::forEach(std::function<void(Element*)> f) const
  771. {
  772. for (auto i : *elements)
  773. f(i);
  774. }
  775. //! returns 1 if the editor contains at least one element, 0 otherwise
  776. bool Editor::exists() const
  777. {
  778. return elements->getEntryCount() > 0;
  779. }
  780. //! returns the number of elements in the editor
  781. int Editor::getSize() const
  782. {
  783. return elements->getEntryCount();
  784. }
  785. DLLEXPORT Editor& Framework::XML::Editor::operator=(const Editor& e)
  786. {
  787. if (this != &e)
  788. {
  789. this->elements->clear();
  790. for (auto i : *e.elements)
  791. this->elements->add(dynamic_cast<XML::Element*>(i->getThis()));
  792. }
  793. return *this;
  794. }