XML.cpp 26 KB

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