XML.cpp 24 KB

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