XML.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. #include "XML.h"
  2. using namespace Framework;
  3. using namespace XML;
  4. // Erstellt ein XML Element
  5. // string: entweder der name des Elements oder ein XML Text der geparsed werden
  6. // soll
  7. Element::Element(Text string)
  8. : Element(string, 0)
  9. {}
  10. // Erstellt ein XML Element
  11. // string: entweder der name des Elements oder ein XML Text der geparsed werden
  12. // soll zParent: Ein Zeiger auf das eltern element (ohne erhöhten reference
  13. // Counter)
  14. Element::Element(Text string, Element* zParent)
  15. : ReferenceCounter()
  16. {
  17. children = new RCArray<Element>();
  18. attributes = new RCArray<Text>();
  19. attributeValues = new RCArray<Text>();
  20. text = new Text();
  21. name = new Text();
  22. string.removeWhitespaceAfter(0);
  23. string.removeWhitespaceBefore(string.getLength());
  24. setText(string);
  25. if (string[0] == '<' && string[string.getLength() - 1] == '>')
  26. {
  27. string.removeWhitespaceAfter(1);
  28. string.removeWhitespaceBefore(string.getLength() - 1);
  29. int nameEnd = 0;
  30. for (int i = 1; i < string.getLength(); i++)
  31. {
  32. if ((string[i] < 'a' || string[i] > 'z')
  33. && (string[i] < 'A' || string[i] > 'Z')
  34. && (string[i] < '0' || string[i] > '9') && string[i] != '-')
  35. {
  36. nameEnd = i;
  37. break;
  38. }
  39. }
  40. name->setText(string.getTeilText(1, nameEnd));
  41. if (string.hatAt(
  42. string.getLength() - 1 - name->getLength(), name->getText())
  43. || string[string.getLength() - 2] == '/')
  44. {
  45. string.removeWhitespaceAfter(nameEnd);
  46. // parse attributes
  47. int start = nameEnd;
  48. while (string[nameEnd] != '>' && string[nameEnd] != '/')
  49. {
  50. for (int i = nameEnd + 1; i < string.getLength(); i++)
  51. {
  52. if ((string[i] < 'a' || string[i] > 'z')
  53. && (string[i] < 'A' || string[i] > 'Z')
  54. && (string[i] < '0' || string[i] > '9')
  55. && string[i] != '-')
  56. {
  57. nameEnd = i;
  58. break;
  59. }
  60. }
  61. Text* attrName = string.getTeilText(start, nameEnd);
  62. string.removeWhitespaceAfter(nameEnd);
  63. if (string[nameEnd] == '=')
  64. {
  65. string.removeWhitespaceAfter(nameEnd + 1);
  66. Text value = "";
  67. if (string[nameEnd + 1] == '"')
  68. {
  69. bool esc = 0;
  70. start = nameEnd + 2;
  71. for (int i = nameEnd + 2; string[i]; i++)
  72. {
  73. if (string[i] == '\\')
  74. esc = !esc;
  75. else
  76. {
  77. if (string[i] == '"' && !esc)
  78. {
  79. nameEnd = i + 1;
  80. break;
  81. }
  82. esc = 0;
  83. }
  84. }
  85. value.setText(string.getTeilText(start, nameEnd - 1));
  86. value.ersetzen("\\\"", "\"");
  87. }
  88. if (string[nameEnd + 1] == '\'')
  89. {
  90. bool esc = 0;
  91. start = nameEnd + 2;
  92. for (int i = nameEnd + 2; string[i]; i++)
  93. {
  94. if (string[i] == '\\')
  95. esc = !esc;
  96. else
  97. {
  98. if (string[i] == '\'' && !esc)
  99. {
  100. nameEnd = i + 1;
  101. break;
  102. }
  103. esc = 0;
  104. }
  105. }
  106. value.setText(string.getTeilText(start, nameEnd - 1));
  107. value.ersetzen("\\'", "'");
  108. }
  109. setAttribute(attrName->getText(), value);
  110. }
  111. else
  112. setAttribute(attrName->getText(), "");
  113. attrName->release();
  114. string.removeWhitespaceAfter(nameEnd);
  115. start = nameEnd;
  116. }
  117. if (string[string.getLength() - 2] != '/')
  118. {
  119. string.removeWhitespaceBefore(
  120. string.getLength() - 1 - name->getLength());
  121. if (string[string.getLength() - 2 - name->getLength()] == '/')
  122. {
  123. string.removeWhitespaceBefore(
  124. string.getLength() - 2 - name->getLength());
  125. if (string[string.getLength() - 3 - name->getLength()]
  126. == '<')
  127. {
  128. text->setText(string.getTeilText(nameEnd + 1,
  129. string.getLength() - 3 - name->getLength()));
  130. // parse children
  131. text->removeWhitespaceAfter(0);
  132. text->removeWhitespaceBefore(text->getLength());
  133. if (text->getText()[0] == '<'
  134. && text->getText()[text->getLength() - 1] == '>')
  135. {
  136. int start = 0;
  137. int lastStart = -1;
  138. while (start < text->getLength())
  139. {
  140. if (lastStart == start) break;
  141. lastStart = start;
  142. bool esc = 0;
  143. bool inString1 = 0;
  144. bool inString2 = 0;
  145. int poc = 0;
  146. bool lastSlash = 0;
  147. bool lastOpen = 0;
  148. bool openSlash = 0;
  149. for (int i = 0; text->getText()[i]; i++)
  150. {
  151. switch (text->getText()[i])
  152. {
  153. case '\\':
  154. esc = !esc;
  155. lastSlash = 0;
  156. lastOpen = 0;
  157. break;
  158. case '"':
  159. if (!esc && !inString2)
  160. inString1 = !inString1;
  161. esc = 0;
  162. lastSlash = 0;
  163. lastOpen = 0;
  164. break;
  165. case '\'':
  166. if (!esc && !inString1)
  167. inString2 = !inString2;
  168. esc = 0;
  169. lastSlash = 0;
  170. lastOpen = 0;
  171. break;
  172. case '<':
  173. if (!inString1 && !inString2)
  174. lastOpen = 1;
  175. esc = 0;
  176. lastSlash = 0;
  177. break;
  178. case '/':
  179. lastSlash = 0;
  180. if (!inString1 && !inString2)
  181. {
  182. lastSlash = 1;
  183. if (lastOpen) openSlash = 1;
  184. }
  185. esc = 0;
  186. lastOpen = 0;
  187. break;
  188. case '>':
  189. if (!inString1 && !inString2)
  190. {
  191. if (openSlash)
  192. poc--;
  193. else if (!lastSlash)
  194. poc++;
  195. if (poc == 0)
  196. {
  197. Text* str = text->getTeilText(
  198. start, i + 1);
  199. addChild(new Element(
  200. str->getText(), this));
  201. str->release();
  202. start = i + 1;
  203. }
  204. }
  205. esc = 0;
  206. lastSlash = 0;
  207. openSlash = 0;
  208. break;
  209. default:
  210. esc = 0;
  211. if (text->getText()[i] != ' '
  212. && text->getText()[i] != '\t'
  213. && text->getText()[i] != '\r'
  214. && text->getText()[i] != '\n')
  215. {
  216. lastSlash = 0;
  217. lastOpen = 0;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. else
  227. text->setText("");
  228. }
  229. }
  230. parent = zParent;
  231. }
  232. Element::~Element()
  233. {
  234. children->release();
  235. attributes->release();
  236. attributeValues->release();
  237. text->release();
  238. name->release();
  239. }
  240. // ändert ein attribut oder fügt eines hinzu
  241. // attribut: Der Name des Attributes
  242. // value: Der Wert des Attributes
  243. void Element::setAttribute(Text attribut, Text value)
  244. {
  245. for (auto i = attributes->begin(), j = attributeValues->begin(); i && j;
  246. i++, j++)
  247. {
  248. if (i->istGleich(attribut))
  249. {
  250. j->setText(value);
  251. return;
  252. }
  253. }
  254. attributes->add(new Text(attribut));
  255. attributeValues->add(new Text(value));
  256. }
  257. // entfernt ein attribut
  258. // attribut: Der Name des Attributes
  259. void Element::removeAttribute(Text attribut)
  260. {
  261. for (int i = 0; i < attributes->getEintragAnzahl(); i++)
  262. {
  263. if (attributes->z(i)->istGleich(attribut))
  264. {
  265. attributes->remove(i);
  266. attributeValues->remove(i);
  267. i--;
  268. }
  269. }
  270. }
  271. // fügt ein child hinzu
  272. // child: Das neue Child Element
  273. void Element::addChild(Element* child)
  274. {
  275. child->parent = this;
  276. children->add(child);
  277. }
  278. // fügt ein child hinzu
  279. // child: Das neue Child Element
  280. void Element::addChildAtFront(Element* child)
  281. {
  282. child->parent = this;
  283. children->add(child, 0);
  284. }
  285. // entfernt ein child
  286. // zChild: das zu entfernende Child
  287. void Element::removeChild(Element* child)
  288. {
  289. for (int i = 0; i < children->getEintragAnzahl(); i++)
  290. {
  291. if (children->z(i) == child)
  292. {
  293. children->remove(i);
  294. i--;
  295. }
  296. }
  297. child->release();
  298. }
  299. // entfernt das i-te child
  300. // i: der Index des childs (bei 0 beginnend)
  301. void Element::removeChild(int i)
  302. {
  303. children->remove(i);
  304. }
  305. // entfernt alle childs
  306. void Element::removeAllChilds()
  307. {
  308. children->leeren();
  309. }
  310. // entfernt eine Liste mit childs
  311. // childs: alle Childs die entfernt werden sollen
  312. void Element::removeChilds(RCArray<Element>* childs)
  313. {
  314. for (auto i : *childs)
  315. removeChild(dynamic_cast<XML::Element*>(i->getThis()));
  316. childs->release();
  317. }
  318. // entfernt dieses Element vom Eltern element
  319. void Element::remove()
  320. {
  321. if (parent) parent->removeChild(dynamic_cast<XML::Element*>(getThis()));
  322. }
  323. // setzt den Text in dem Element falls es keine childs gibt
  324. // text: dert Text
  325. void Element::setText(Text text)
  326. {
  327. this->text->setText(text);
  328. }
  329. // gibt den Text im Element zurück
  330. Text Element::getText() const
  331. {
  332. return text->getText();
  333. }
  334. // gibt die Anzahl der Childs zurück
  335. int Element::getChildCount() const
  336. {
  337. return children->getEintragAnzahl();
  338. }
  339. // gibt das i-te child zurück
  340. Element* Element::getChild(int i) const
  341. {
  342. return children->get(i);
  343. }
  344. // gibt das i-te child zurück (ohne erhöhten reference Counter)
  345. Element* Element::zChild(int i) const
  346. {
  347. return children->z(i);
  348. }
  349. // gibt das parent element zurück
  350. Element* Element::getParent() const
  351. {
  352. return parent ? dynamic_cast<Element*>(parent->getThis()) : 0;
  353. }
  354. // gibt das parent element zurück (ohne erhöhten reference Counter)
  355. Element* Element::zParent() const
  356. {
  357. return parent;
  358. }
  359. // gibt einen iterator zurück mit dem durch alle childs iteriert werden kann
  360. Iterator<Element*> Element::getChilds() const
  361. {
  362. return children->begin();
  363. }
  364. // gibt einen selector zurück der alle childs beinhaltet
  365. Editor Element::selectChildren() const
  366. {
  367. return Editor(dynamic_cast<RCArray<XML::Element>*>(children->getThis()));
  368. }
  369. // gibt eine Liste mit childs zurück, die einen bestimmten Namen haben
  370. // name: der name der Childs
  371. Editor Element::selectChildsByName(Text name) const
  372. {
  373. RCArray<Element>* tmp = new RCArray<Element>();
  374. for (auto i : *children)
  375. {
  376. if (i->getName().istGleich(name))
  377. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  378. }
  379. return Editor(tmp);
  380. }
  381. // gibt eine Liste mit childs zurück, die ein bestimmtes Attribut haben
  382. // attribute: der name des Attributes
  383. Editor Element::selectChildsByAttribute(Text attribute) const
  384. {
  385. RCArray<Element>* tmp = new RCArray<Element>();
  386. for (auto i : *children)
  387. {
  388. if (i->hasAttribute(attribute))
  389. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  390. }
  391. return Editor(tmp);
  392. }
  393. // gibt eine Liste mit childs zurück, die ein bestimmtes Attribut mit einem
  394. // bestimmten wert haben
  395. // attribute: der name des Attributes
  396. // value: der Wert des Attributes
  397. Editor Element::selectChildsByAttribute(Text attribute, Text value) const
  398. {
  399. RCArray<Element>* tmp = new RCArray<Element>();
  400. for (auto i : *children)
  401. {
  402. if (i->hasAttribute(attribute)
  403. && i->getAttributeValue(attribute).istGleich(value))
  404. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  405. }
  406. return Editor(tmp);
  407. }
  408. // gibt 1 zurück, falls ein Attribut Name existiert, 0 sonnst
  409. bool Element::hasAttribute(Text name) const
  410. {
  411. for (auto i : *attributes)
  412. {
  413. if (i->istGleich(name)) return 1;
  414. }
  415. return 0;
  416. }
  417. // gibt die Anzahl der Attribute zurück
  418. int Element::getAttributeCount() const
  419. {
  420. return attributes->getEintragAnzahl();
  421. }
  422. // gibt den Namen des i-ten Attributes zurück
  423. Text Element::getAttributeName(int i) const
  424. {
  425. return attributes->z(i)->getText();
  426. }
  427. // gibt den Wert des i-ten Attributes zurück
  428. Text Element::getAttributeValue(int i) const
  429. {
  430. return attributeValues->z(i)->getText();
  431. }
  432. // gibt den Wert eines Attributes zurück
  433. // attribut: Der Name des Attributes
  434. Text Element::getAttributeValue(Text attribut) const
  435. {
  436. for (auto i = attributes->begin(), j = attributeValues->begin(); i && j;
  437. i++, j++)
  438. {
  439. if (i->istGleich(attribut)) return j->getText();
  440. }
  441. return "";
  442. }
  443. // gibt einen iterator zurück mit dem durch alle Attribut Namen iteriert werden
  444. // kann
  445. Iterator<Text*> Element::getAttributeNames() const
  446. {
  447. return attributes->begin();
  448. }
  449. // gibt einen iterator zurück mit dem durch alle Attribut Werte iteriert werden
  450. // kann
  451. Iterator<Text*> Element::getAttributeValues() const
  452. {
  453. return attributeValues->begin();
  454. }
  455. // gibt den Namen des Elementes zurück zurück
  456. Text Element::getName() const
  457. {
  458. return name->getText();
  459. }
  460. // erzeugt einen XML Text der dieses Element und alle childs beinhaltet
  461. Text Element::toString() const
  462. {
  463. Text ret = "<";
  464. ret += name->getText();
  465. if (attributes->getEintragAnzahl()) ret += " ";
  466. for (auto i = attributes->begin(), j = attributeValues->begin(); i && j;
  467. i++, j++)
  468. {
  469. ret += i->getText();
  470. if (j->getLength())
  471. {
  472. if (j->hat('"'))
  473. {
  474. ret += "='";
  475. Text txt = j->getText();
  476. txt.ersetzen("'", "\\'");
  477. ret += txt;
  478. ret += "'";
  479. }
  480. else
  481. {
  482. ret += "=\"";
  483. Text txt = j->getText();
  484. txt.ersetzen("\"", "\\\"");
  485. ret += txt;
  486. ret += "\"";
  487. }
  488. }
  489. if (i.hasNext()) ret += " ";
  490. }
  491. if (children->getEintragAnzahl() || text->getLength())
  492. {
  493. ret += ">";
  494. if (children->getEintragAnzahl())
  495. {
  496. for (auto i : *children)
  497. ret += i->toString();
  498. }
  499. else
  500. ret += text->getText();
  501. ret += "</";
  502. ret += name->getText();
  503. ret += ">";
  504. }
  505. else
  506. ret += "/>";
  507. return ret;
  508. }
  509. // Erzeugt eine Kopie ohne referenzen auf dieses objekt
  510. Element* Element::dublicate() const
  511. {
  512. return new Element(toString());
  513. }
  514. // Erzeugt einen neuen XML Editor mit einer Liste von Objekten die editiert
  515. // werden sollen
  516. Editor::Editor(RCArray<Element>* elements)
  517. : ReferenceCounter()
  518. {
  519. this->elements = new RCArray<Element>();
  520. for (auto i : *elements)
  521. this->elements->add(dynamic_cast<XML::Element*>(i->getThis()));
  522. elements->release();
  523. }
  524. Editor::Editor(const Editor& e)
  525. : Editor(dynamic_cast<RCArray<XML::Element>*>(e.elements->getThis()))
  526. {}
  527. Editor::~Editor()
  528. {
  529. elements->release();
  530. }
  531. // ändert ein attribut oder fügt eines hinzu (auf allen elementen in der Liste)
  532. // attribut: Der Name des Attributes
  533. // value: Der Wert des Attributes
  534. void Editor::setAttribute(Text attribut, Text value)
  535. {
  536. for (auto i : *elements)
  537. i->setAttribute(attribut, value);
  538. }
  539. // entfernt ein attribut (auf allen elementen in der Liste)
  540. // attribut: Der Name des Attributes
  541. void Editor::removeAttribute(Text attribut)
  542. {
  543. for (auto i : *elements)
  544. i->removeAttribute(attribut);
  545. }
  546. // fügt ein child hinzu (auf allen elementen in der Liste)
  547. // child: Das neue Child Element
  548. void Editor::addChild(Element* child)
  549. {
  550. for (auto i : *elements)
  551. i->addChild(child->dublicate());
  552. child->release();
  553. }
  554. // entfernt ein child (auf allen elementen in der Liste)
  555. // zChild: das zu entfernende Child
  556. void Editor::removeChild(Element* child)
  557. {
  558. for (auto i : *elements)
  559. i->removeChild(dynamic_cast<XML::Element*>(child->getThis()));
  560. child->release();
  561. }
  562. // entfernt das i-te child (auf allen elementen in der Liste)
  563. // i: der Index des childs (bei 0 beginnend)
  564. void Editor::removeChild(int i)
  565. {
  566. for (auto j : *elements)
  567. j->removeChild(i);
  568. }
  569. // entfernt alle childs (auf allen elementen in der Liste)
  570. void Editor::removeAllChilds()
  571. {
  572. for (auto i : *elements)
  573. i->removeAllChilds();
  574. }
  575. // entfernt eine Liste mit childs (auf allen elementen in der Liste)
  576. // childs: alle Childs die entfernt werden sollen
  577. void Editor::removeChilds(RCArray<Element>* childs)
  578. {
  579. for (auto i : *elements)
  580. i->removeChilds(
  581. dynamic_cast<RCArray<XML::Element>*>(childs->getThis()));
  582. childs->release();
  583. }
  584. // entfernt dieses Element vom Eltern element (auf allen elementen in der Liste)
  585. void Editor::remove()
  586. {
  587. for (auto i : *elements)
  588. i->remove();
  589. }
  590. // setzt den Text in dem Element falls es keine childs gibt (auf allen elementen
  591. // in der Liste)
  592. // text: dert Text
  593. void Editor::setText(Text text)
  594. {
  595. for (auto i : *elements)
  596. i->setText(text);
  597. }
  598. // Gibt ein Iterator durch alle Elemente zurück
  599. Iterator<Element*> Editor::begin()
  600. {
  601. return elements->begin();
  602. }
  603. //! Gibt das ende des iterators zurück
  604. Iterator<Element*> Editor::end()
  605. {
  606. return elements->end();
  607. }
  608. // gibt einen selector zurück der alle childs beinhaltet
  609. Editor Editor::selectChildren() const
  610. {
  611. RCArray<Element>* list = new RCArray<Element>();
  612. for (auto i : *elements)
  613. {
  614. for (Element* j : i->selectChildren())
  615. {
  616. list->add(dynamic_cast<XML::Element*>(j->getThis()));
  617. }
  618. }
  619. return Editor(list);
  620. }
  621. // gibt einen selector zurück der alle parents beinhaltet
  622. Editor Editor::selectParents() const
  623. {
  624. RCArray<Element>* list = new RCArray<Element>();
  625. for (auto i : *elements)
  626. {
  627. if (i->parent)
  628. list->add(dynamic_cast<XML::Element*>(i->parent->getThis()));
  629. }
  630. return Editor(list);
  631. }
  632. // gibt eine Liste mit elementen zurück, die einen bestimmten Namen haben
  633. // name: der name der Childs
  634. Editor Editor::whereNameEquals(Text name) const
  635. {
  636. RCArray<Element>* list = new RCArray<Element>();
  637. for (auto i : *elements)
  638. {
  639. if (i->getName().istGleich(name))
  640. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  641. }
  642. return Editor(list);
  643. }
  644. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  645. // name: der name des childs
  646. Editor Editor::whereChildWithNameExists(Text name) const
  647. {
  648. RCArray<Element>* list = new RCArray<Element>();
  649. for (auto i : *elements)
  650. {
  651. if (i->selectChildsByName(name).elements->getEintragAnzahl())
  652. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  653. }
  654. return Editor(list);
  655. }
  656. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  657. // attribute: der name des attributes
  658. Editor Editor::whereChildWithAttributeExists(Text attribute) const
  659. {
  660. RCArray<Element>* list = new RCArray<Element>();
  661. for (auto i : *elements)
  662. {
  663. if (i->selectChildsByAttribute(attribute).elements->getEintragAnzahl())
  664. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  665. }
  666. return Editor(list);
  667. }
  668. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  669. // attribute: der name des attributes
  670. // value: der Wert des Attributes
  671. Editor Editor::whereChildWithAttributeExists(Text attribute, Text value) const
  672. {
  673. RCArray<Element>* list = new RCArray<Element>();
  674. for (auto i : *elements)
  675. {
  676. if (i->selectChildsByAttribute(attribute, value)
  677. .elements->getEintragAnzahl())
  678. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  679. }
  680. return Editor(list);
  681. }
  682. // gibt eine Liste mit elementen zurück, die ein bestimmtes Attribut haben
  683. // attribute: der name des Attributes
  684. Editor Editor::whereAttributeExists(Text attribute) const
  685. {
  686. RCArray<Element>* list = new RCArray<Element>();
  687. for (auto i : *elements)
  688. {
  689. if (i->hasAttribute(attribute))
  690. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  691. }
  692. return Editor(list);
  693. }
  694. // gibt eine Liste mit elementen zurück, die ein bestimmtes Attribut mit einem
  695. // bestimmten wert haben
  696. // attribute: der name des Attributes
  697. // value: der Wert des Attributes
  698. Editor Editor::whereAttributeEquals(Text attribute, Text value) const
  699. {
  700. RCArray<Element>* list = new RCArray<Element>();
  701. for (auto i : *elements)
  702. {
  703. if (i->hasAttribute(attribute)
  704. && i->getAttributeValue(attribute).istGleich(value))
  705. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  706. }
  707. return Editor(list);
  708. }
  709. // Gibt einen Editor zurück welcher nurnoch die Elemente enthält die nicht in e
  710. // sind
  711. // e: Ein Editor mit elementen die nicht enthalten sein sollen
  712. Editor Editor::without(Editor e) const
  713. {
  714. RCArray<Element>* list = new RCArray<Element>();
  715. for (auto i : *elements)
  716. {
  717. bool found = 0;
  718. for (auto j : *e.elements)
  719. found |= i == j;
  720. if (!found) list->add(dynamic_cast<XML::Element*>(i->getThis()));
  721. }
  722. return Editor(list);
  723. }
  724. // Ruft eine funktion für jedes Element auf
  725. // f: die funktion (nimmt als argument ein Element objekt ohne erhöhten
  726. // reference Counter)
  727. void Editor::forEach(std::function<void(Element*)> f) const
  728. {
  729. for (auto i : *elements)
  730. f(i);
  731. }
  732. //! gibt 1 zurück, wenn mindestens ein Element gefunden wurde
  733. bool Editor::exists() const
  734. {
  735. return elements->getEintragAnzahl() > 0;
  736. }
  737. //! gibt die anzahl der ausgewählten elemente zurück
  738. int Editor::getSize() const
  739. {
  740. return elements->getEintragAnzahl();
  741. }