XML.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. // Erstellt ein XML Element
  19. // string: entweder der name des Elements oder ein XML Text der geparsed werden
  20. // soll zParent: Ein Zeiger auf das eltern element (ohne erhöhten 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.hatAt(
  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.ersetzen("\\\"", "\"");
  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.ersetzen("\\'", "'");
  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. // ändert ein attribut oder fügt eines hinzu
  254. // attribut: Der Name des Attributes
  255. // value: Der Wert des Attributes
  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->istGleich(attribut))
  262. {
  263. j->setText(value);
  264. return;
  265. }
  266. }
  267. attributes->add(new Text(attribut));
  268. attributeValues->add(new Text(value));
  269. }
  270. // entfernt ein attribut
  271. // attribut: Der Name des Attributes
  272. void Element::removeAttribute(Text attribut)
  273. {
  274. for (int i = 0; i < attributes->getEintragAnzahl(); i++)
  275. {
  276. if (attributes->z(i)->istGleich(attribut))
  277. {
  278. attributes->remove(i);
  279. attributeValues->remove(i);
  280. i--;
  281. }
  282. }
  283. }
  284. // fügt ein child hinzu
  285. // child: Das neue Child Element
  286. void Element::addChild(Element* child)
  287. {
  288. child->parent = this;
  289. children->add(child);
  290. }
  291. // fügt ein child hinzu
  292. // child: Das neue Child Element
  293. void Element::addChildAtFront(Element* child)
  294. {
  295. child->parent = this;
  296. children->add(child, 0);
  297. }
  298. // entfernt ein child
  299. // zChild: das zu entfernende Child
  300. void Element::removeChild(Element* child)
  301. {
  302. for (int i = 0; i < children->getEintragAnzahl(); i++)
  303. {
  304. if (children->z(i) == child)
  305. {
  306. children->remove(i);
  307. i--;
  308. }
  309. }
  310. child->release();
  311. }
  312. // entfernt das i-te child
  313. // i: der Index des childs (bei 0 beginnend)
  314. void Element::removeChild(int i)
  315. {
  316. children->remove(i);
  317. }
  318. // entfernt alle childs
  319. void Element::removeAllChilds()
  320. {
  321. children->leeren();
  322. }
  323. // entfernt eine Liste mit childs
  324. // childs: alle Childs die entfernt werden sollen
  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. // entfernt dieses Element vom Eltern element
  332. void Element::remove()
  333. {
  334. if (parent) parent->removeChild(dynamic_cast<XML::Element*>(getThis()));
  335. }
  336. // setzt den Text in dem Element falls es keine childs gibt
  337. // text: dert Text
  338. void Element::setText(Text text)
  339. {
  340. this->text->setText(text);
  341. }
  342. // gibt den Text im Element zurück
  343. Text Element::getText() const
  344. {
  345. return text->getText();
  346. }
  347. // gibt die Anzahl der Childs zurück
  348. int Element::getChildCount() const
  349. {
  350. return children->getEintragAnzahl();
  351. }
  352. int Framework::XML::Element::getChildIndex(Element* zChild) const
  353. {
  354. return children->indexOf(zChild);
  355. }
  356. // gibt das i-te child zurück
  357. Element* Element::getChild(int i) const
  358. {
  359. return children->get(i);
  360. }
  361. // gibt das i-te child zurück (ohne erhöhten reference Counter)
  362. Element* Element::zChild(int i) const
  363. {
  364. return children->z(i);
  365. }
  366. // gibt das parent element zurück
  367. Element* Element::getParent() const
  368. {
  369. return parent ? dynamic_cast<Element*>(parent->getThis()) : 0;
  370. }
  371. // gibt das parent element zurück (ohne erhöhten reference Counter)
  372. Element* Element::zParent() const
  373. {
  374. return parent;
  375. }
  376. // gibt einen iterator zurück mit dem durch alle childs iteriert werden kann
  377. ArrayIterator<Element*> Element::getChilds() const
  378. {
  379. return children->begin();
  380. }
  381. //! gibt einen Editor für dieses Element zurück
  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. // gibt einen selector zurück der alle childs beinhaltet
  389. Editor Element::selectChildren() const
  390. {
  391. return Editor(dynamic_cast<RCArray<XML::Element>*>(children->getThis()));
  392. }
  393. // gibt eine Liste mit childs zurück, die einen bestimmten Namen haben
  394. // name: der name der Childs
  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().istGleich(name))
  401. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  402. }
  403. return Editor(tmp);
  404. }
  405. // gibt eine Liste mit childs zurück, die ein bestimmtes Attribut haben
  406. // attribute: der name des Attributes
  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. // gibt eine Liste mit childs zurück, die ein bestimmtes Attribut mit einem
  418. // bestimmten wert haben
  419. // attribute: der name des Attributes
  420. // value: der Wert des Attributes
  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).istGleich(value))
  428. tmp->add(dynamic_cast<XML::Element*>(i->getThis()));
  429. }
  430. return Editor(tmp);
  431. }
  432. // gibt 1 zurück, falls ein Attribut Name existiert, 0 sonnst
  433. bool Element::hasAttribute(Text name) const
  434. {
  435. for (auto i : *attributes)
  436. {
  437. if (i->istGleich(name)) return 1;
  438. }
  439. return 0;
  440. }
  441. // gibt die Anzahl der Attribute zurück
  442. int Element::getAttributeCount() const
  443. {
  444. return attributes->getEintragAnzahl();
  445. }
  446. // gibt den Namen des i-ten Attributes zurück
  447. Text Element::getAttributeName(int i) const
  448. {
  449. return attributes->z(i)->getText();
  450. }
  451. // gibt den Wert des i-ten Attributes zurück
  452. Text Element::getAttributeValue(int i) const
  453. {
  454. return attributeValues->z(i)->getText();
  455. }
  456. // gibt den Wert eines Attributes zurück
  457. // attribut: Der Name des Attributes
  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->istGleich(attribut)) return *j.val();
  464. }
  465. return Text::EMPTY;
  466. }
  467. // gibt einen iterator zurück mit dem durch alle Attribut Namen iteriert werden
  468. // kann
  469. ArrayIterator<Text*> Element::getAttributeNames() const
  470. {
  471. return attributes->begin();
  472. }
  473. // gibt einen iterator zurück mit dem durch alle Attribut Werte iteriert werden
  474. // kann
  475. ArrayIterator<Text*> Element::getAttributeValues() const
  476. {
  477. return attributeValues->begin();
  478. }
  479. void Framework::XML::Element::setName(Text name)
  480. {
  481. this->name->setText(name);
  482. }
  483. // gibt den Namen des Elementes zurück zurück
  484. Text Element::getName() const
  485. {
  486. return name->getText();
  487. }
  488. // erzeugt einen XML Text der dieses Element und alle childs beinhaltet
  489. Text Element::toString() const
  490. {
  491. Text ret = "<";
  492. ret += name->getText();
  493. if (attributes->getEintragAnzahl()) ret += " ";
  494. for (auto i = attributes->begin(), j = attributeValues->begin(); i && j;
  495. i++, j++)
  496. {
  497. ret += i->getText();
  498. if (j->hat('"'))
  499. {
  500. ret += "='";
  501. Text txt = j->getText();
  502. txt.ersetzen("'", "\\'");
  503. ret += txt;
  504. ret += "'";
  505. }
  506. else
  507. {
  508. ret += "=\"";
  509. Text txt = j->getText();
  510. txt.ersetzen("\"", "\\\"");
  511. ret += txt;
  512. ret += "\"";
  513. }
  514. if (i.hasNext()) ret += " ";
  515. }
  516. if (children->getEintragAnzahl() || text->getLength())
  517. {
  518. ret += ">";
  519. if (children->getEintragAnzahl())
  520. {
  521. for (auto i : *children)
  522. ret += i->toString();
  523. }
  524. else
  525. ret += text->getText();
  526. ret += "</";
  527. ret += name->getText();
  528. ret += ">";
  529. }
  530. else
  531. ret += "/>";
  532. return ret;
  533. }
  534. // Erzeugt eine Kopie ohne referenzen auf dieses objekt
  535. Element* Element::dublicate() const
  536. {
  537. return new Element(toString());
  538. }
  539. // Erzeugt einen neuen XML Editor mit einer Liste von Objekten die editiert
  540. // werden sollen
  541. Editor::Editor(RCArray<Element>* elements)
  542. : ReferenceCounter()
  543. {
  544. this->elements = elements;
  545. }
  546. Editor::Editor(const Editor& e)
  547. : Editor(dynamic_cast<RCArray<XML::Element>*>(e.elements->getThis()))
  548. {}
  549. Editor::~Editor()
  550. {
  551. elements->release();
  552. }
  553. Maybe<RCPointer<Element>> Framework::XML::Editor::getFirstElement() const
  554. {
  555. if (this->elements->getEintragAnzahl() > 0)
  556. {
  557. return Maybe<RCPointer<Element>>::of(
  558. RCPointer<Element>::of(this->elements->get(0)));
  559. }
  560. return Maybe<RCPointer<Element>>::empty();
  561. }
  562. // ändert ein attribut oder fügt eines hinzu (auf allen elementen in der Liste)
  563. // attribut: Der Name des Attributes
  564. // value: Der Wert des Attributes
  565. void Editor::setAttribute(Text attribut, Text value)
  566. {
  567. for (auto i : *elements)
  568. i->setAttribute(attribut, value);
  569. }
  570. // entfernt ein attribut (auf allen elementen in der Liste)
  571. // attribut: Der Name des Attributes
  572. void Editor::removeAttribute(Text attribut)
  573. {
  574. for (auto i : *elements)
  575. i->removeAttribute(attribut);
  576. }
  577. // fügt ein child hinzu (auf allen elementen in der Liste)
  578. // child: Das neue Child Element
  579. void Editor::addChild(Element* child)
  580. {
  581. for (auto i : *elements)
  582. i->addChild(child->dublicate());
  583. child->release();
  584. }
  585. // entfernt ein child (auf allen elementen in der Liste)
  586. // zChild: das zu entfernende Child
  587. void Editor::removeChild(Element* child)
  588. {
  589. for (auto i : *elements)
  590. i->removeChild(dynamic_cast<XML::Element*>(child->getThis()));
  591. child->release();
  592. }
  593. // entfernt das i-te child (auf allen elementen in der Liste)
  594. // i: der Index des childs (bei 0 beginnend)
  595. void Editor::removeChild(int i)
  596. {
  597. for (auto j : *elements)
  598. j->removeChild(i);
  599. }
  600. // entfernt alle childs (auf allen elementen in der Liste)
  601. void Editor::removeAllChilds()
  602. {
  603. for (auto i : *elements)
  604. i->removeAllChilds();
  605. }
  606. // entfernt eine Liste mit childs (auf allen elementen in der Liste)
  607. // childs: alle Childs die entfernt werden sollen
  608. void Editor::removeChilds(RCArray<Element>* childs)
  609. {
  610. for (auto i : *elements)
  611. i->removeChilds(
  612. dynamic_cast<RCArray<XML::Element>*>(childs->getThis()));
  613. childs->release();
  614. }
  615. // entfernt dieses Element vom Eltern element (auf allen elementen in der Liste)
  616. void Editor::remove()
  617. {
  618. for (auto i : *elements)
  619. i->remove();
  620. }
  621. // setzt den Text in dem Element falls es keine childs gibt (auf allen elementen
  622. // in der Liste)
  623. // text: dert Text
  624. void Editor::setText(Text text)
  625. {
  626. for (auto i : *elements)
  627. i->setText(text);
  628. }
  629. // Gibt ein Iterator durch alle Elemente zurück
  630. ArrayIterator<Element*> Editor::begin()
  631. {
  632. return elements->begin();
  633. }
  634. //! Gibt das ende des iterators zurück
  635. ArrayIterator<Element*> Editor::end()
  636. {
  637. return elements->end();
  638. }
  639. void Editor::selectAllElements(RCArray<Element>* zResult)
  640. {
  641. for (auto i : *elements)
  642. {
  643. zResult->add(dynamic_cast<XML::Element*>(i->getThis()));
  644. i->selectChildren().selectAllElements(zResult);
  645. }
  646. }
  647. //! Gibt einen selector zurück der alle elemente beinhaltet die in diesem
  648. //! selector vorkommen und rekursiv alle Kinder der elemente Enthält
  649. Editor Editor::selectAllElements()
  650. {
  651. RCArray<Element>* list = new RCArray<Element>();
  652. selectAllElements(list);
  653. return Editor(list);
  654. }
  655. // gibt einen selector zurück der alle childs beinhaltet
  656. Editor Editor::selectChildren() const
  657. {
  658. RCArray<Element>* list = new RCArray<Element>();
  659. for (auto i : *elements)
  660. {
  661. for (Element* j : i->selectChildren())
  662. {
  663. list->add(dynamic_cast<XML::Element*>(j->getThis()));
  664. }
  665. }
  666. return Editor(list);
  667. }
  668. // gibt einen selector zurück der alle parents beinhaltet
  669. Editor Editor::selectParents() const
  670. {
  671. RCArray<Element>* list = new RCArray<Element>();
  672. for (auto i : *elements)
  673. {
  674. if (i->parent)
  675. list->add(dynamic_cast<XML::Element*>(i->parent->getThis()));
  676. }
  677. return Editor(list);
  678. }
  679. // gibt eine Liste mit elementen zurück, die einen bestimmten Namen haben
  680. // name: der name der Childs
  681. Editor Editor::whereNameEquals(Text name) const
  682. {
  683. RCArray<Element>* list = new RCArray<Element>();
  684. for (auto i : *elements)
  685. {
  686. if (i->getName().istGleich(name))
  687. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  688. }
  689. return Editor(list);
  690. }
  691. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  692. // name: der name des childs
  693. Editor Editor::whereChildWithNameExists(Text name) const
  694. {
  695. RCArray<Element>* list = new RCArray<Element>();
  696. for (auto i : *elements)
  697. {
  698. if (i->selectChildsByName(name).elements->getEintragAnzahl())
  699. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  700. }
  701. return Editor(list);
  702. }
  703. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  704. // attribute: der name des attributes
  705. Editor Editor::whereChildWithAttributeExists(Text attribute) const
  706. {
  707. RCArray<Element>* list = new RCArray<Element>();
  708. for (auto i : *elements)
  709. {
  710. if (i->selectChildsByAttribute(attribute).elements->getEintragAnzahl())
  711. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  712. }
  713. return Editor(list);
  714. }
  715. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  716. // attribute: der name des attributes
  717. // value: der Wert des Attributes
  718. Editor Editor::whereChildWithAttributeExists(Text attribute, Text value) const
  719. {
  720. RCArray<Element>* list = new RCArray<Element>();
  721. for (auto i : *elements)
  722. {
  723. if (i->selectChildsByAttribute(attribute, value)
  724. .elements->getEintragAnzahl())
  725. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  726. }
  727. return Editor(list);
  728. }
  729. // gibt eine Liste mit elementen zurück, die ein bestimmtes Attribut haben
  730. // attribute: der name des Attributes
  731. Editor Editor::whereAttributeExists(Text attribute) const
  732. {
  733. RCArray<Element>* list = new RCArray<Element>();
  734. for (auto i : *elements)
  735. {
  736. if (i->hasAttribute(attribute))
  737. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  738. }
  739. return Editor(list);
  740. }
  741. // gibt eine Liste mit elementen zurück, die ein bestimmtes Attribut mit einem
  742. // bestimmten wert haben
  743. // attribute: der name des Attributes
  744. // value: der Wert des Attributes
  745. Editor Editor::whereAttributeEquals(Text attribute, Text value) const
  746. {
  747. RCArray<Element>* list = new RCArray<Element>();
  748. for (auto i : *elements)
  749. {
  750. if (i->hasAttribute(attribute)
  751. && i->getAttributeValue(attribute).istGleich(value))
  752. list->add(dynamic_cast<XML::Element*>(i->getThis()));
  753. }
  754. return Editor(list);
  755. }
  756. // Gibt einen Editor zurück welcher nurnoch die Elemente enthält die nicht in e
  757. // sind
  758. // e: Ein Editor mit elementen die nicht enthalten sein sollen
  759. Editor Editor::without(Editor e) const
  760. {
  761. RCArray<Element>* list = new RCArray<Element>();
  762. for (auto i : *elements)
  763. {
  764. bool found = 0;
  765. for (auto j : *e.elements)
  766. found |= i == j;
  767. if (!found) list->add(dynamic_cast<XML::Element*>(i->getThis()));
  768. }
  769. return Editor(list);
  770. }
  771. // Ruft eine funktion für jedes Element auf
  772. // f: die funktion (nimmt als argument ein Element objekt ohne erhöhten
  773. // reference Counter)
  774. void Editor::forEach(std::function<void(Element*)> f) const
  775. {
  776. for (auto i : *elements)
  777. f(i);
  778. }
  779. //! gibt 1 zurück, wenn mindestens ein Element gefunden wurde
  780. bool Editor::exists() const
  781. {
  782. return elements->getEintragAnzahl() > 0;
  783. }
  784. //! gibt die anzahl der ausgewählten elemente zurück
  785. int Editor::getSize() const
  786. {
  787. return elements->getEintragAnzahl();
  788. }
  789. DLLEXPORT Editor& Framework::XML::Editor::operator=(const Editor& e)
  790. {
  791. if (this != &e)
  792. {
  793. this->elements->leeren();
  794. for (auto i : *e.elements)
  795. this->elements->add(dynamic_cast<XML::Element*>(i->getThis()));
  796. }
  797. return *this;
  798. }