XML.cpp 26 KB

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