XML.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. : ReferenceCounter()
  14. {
  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' ) && string[ i ] != '-' )
  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' ) && string[ i ] != '-' )
  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. // fügt ein child hinzu
  261. // child: Das neue Child Element
  262. void Element::addChildAtFront( Element *child )
  263. {
  264. child->parent = this;
  265. children->add( child, 0 );
  266. }
  267. // entfernt ein child
  268. // zChild: das zu entfernende Child
  269. void Element::removeChild( Element *child )
  270. {
  271. for( int i = 0; i < children->getEintragAnzahl(); i++ )
  272. {
  273. if( children->z( i ) == child )
  274. {
  275. children->remove( i );
  276. i--;
  277. }
  278. }
  279. child->release();
  280. }
  281. // entfernt das i-te child
  282. // i: der Index des childs (bei 0 beginnend)
  283. void Element::removeChild( int i )
  284. {
  285. children->remove( i );
  286. }
  287. // entfernt alle childs
  288. void Element::removeAllChilds()
  289. {
  290. children->leeren();
  291. }
  292. // entfernt eine Liste mit childs
  293. // childs: alle Childs die entfernt werden sollen
  294. void Element::removeChilds( RCArray<Element> *childs )
  295. {
  296. for( auto i = childs->getIterator(); i; i++ )
  297. removeChild( (XML::Element *)i->getThis() );
  298. childs->release();
  299. }
  300. // entfernt dieses Element vom Eltern element
  301. void Element::remove()
  302. {
  303. if( parent )
  304. parent->removeChild( (XML::Element *)getThis() );
  305. }
  306. // setzt den Text in dem Element falls es keine childs gibt
  307. // text: dert Text
  308. void Element::setText( Text text )
  309. {
  310. this->text->setText( text );
  311. }
  312. // gibt den Text im Element zurück
  313. Text Element::getText() const
  314. {
  315. return text->getText();
  316. }
  317. // gibt die Anzahl der Childs zurück
  318. int Element::getChildCount() const
  319. {
  320. return children->getEintragAnzahl();
  321. }
  322. // gibt das i-te child zurück
  323. Element *Element::getChild( int i ) const
  324. {
  325. return children->get( i );
  326. }
  327. // gibt das i-te child zurück (ohne erhöhten reference Counter)
  328. Element *Element::zChild( int i ) const
  329. {
  330. return children->z( i );
  331. }
  332. // gibt das parent element zurück
  333. Element *Element::getParent() const
  334. {
  335. return parent ? (Element *)parent->getThis() : 0;
  336. }
  337. // gibt das parent element zurück (ohne erhöhten reference Counter)
  338. Element *Element::zParent() const
  339. {
  340. return parent;
  341. }
  342. // gibt einen iterator zurück mit dem durch alle childs iteriert werden kann
  343. Iterator< Element * > Element::getChilds() const
  344. {
  345. return children->getIterator();
  346. }
  347. // gibt einen selector zurück der alle childs beinhaltet
  348. Editor Element::selectChildren() const
  349. {
  350. return Editor( ( RCArray<XML::Element> * )children->getThis() );
  351. }
  352. // gibt eine Liste mit childs zurück, die einen bestimmten Namen haben
  353. // name: der name der Childs
  354. Editor Element::selectChildsByName( Text name ) const
  355. {
  356. RCArray< Element > *tmp = new RCArray< Element >();
  357. for( auto i = children->getIterator(); i; i++ )
  358. {
  359. if( i->getName().istGleich( name ) )
  360. tmp->add( (XML::Element *)i->getThis() );
  361. }
  362. return Editor( tmp );
  363. }
  364. // gibt eine Liste mit childs zurück, die ein bestimmtes Attribut haben
  365. // attribute: der name des Attributes
  366. Editor Element::selectChildsByAttribute( Text attribute ) const
  367. {
  368. RCArray< Element > *tmp = new RCArray< Element >();
  369. for( auto i = children->getIterator(); i; i++ )
  370. {
  371. if( i->hasAttribute( attribute ) )
  372. tmp->add( (XML::Element *)i->getThis() );
  373. }
  374. return Editor( tmp );
  375. }
  376. // gibt eine Liste mit childs zurück, die ein bestimmtes Attribut mit einem bestimmten wert haben
  377. // attribute: der name des Attributes
  378. // value: der Wert des Attributes
  379. Editor Element::selectChildsByAttribute( Text attribute, Text value ) const
  380. {
  381. RCArray< Element > *tmp = new RCArray< Element >();
  382. for( auto i = children->getIterator(); i; i++ )
  383. {
  384. if( i->hasAttribute( attribute ) && i->getAttributeValue( attribute ).istGleich( value ) )
  385. tmp->add( (XML::Element *)i->getThis() );
  386. }
  387. return Editor( tmp );
  388. }
  389. // gibt 1 zurück, falls ein Attribut Name existiert, 0 sonnst
  390. bool Element::hasAttribute( Text name ) const
  391. {
  392. for( auto i = attributes->getIterator(); i; i++ )
  393. {
  394. if( i->istGleich( name ) )
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. // gibt die Anzahl der Attribute zurück
  400. int Element::getAttributeCount() const
  401. {
  402. return attributes->getEintragAnzahl();
  403. }
  404. // gibt den Namen des i-ten Attributes zurück
  405. Text Element::getAttributeName( int i ) const
  406. {
  407. return attributes->z( i )->getText();
  408. }
  409. // gibt den Wert des i-ten Attributes zurück
  410. Text Element::getAttributeValue( int i ) const
  411. {
  412. return attributeValues->z( i )->getText();
  413. }
  414. // gibt den Wert eines Attributes zurück
  415. // attribut: Der Name des Attributes
  416. Text Element::getAttributeValue( Text attribut ) const
  417. {
  418. for( auto i = attributes->getIterator(), j = attributeValues->getIterator(); i && j; i++, j++ )
  419. {
  420. if( i->istGleich( attribut ) )
  421. return j->getText();
  422. }
  423. return "";
  424. }
  425. // gibt einen iterator zurück mit dem durch alle Attribut Namen iteriert werden kann
  426. Iterator< Text * > Element::getAttributeNames() const
  427. {
  428. return attributes->getIterator();
  429. }
  430. // gibt einen iterator zurück mit dem durch alle Attribut Werte iteriert werden kann
  431. Iterator< Text * > Element::getAttributeValues() const
  432. {
  433. return attributeValues->getIterator();
  434. }
  435. // gibt den Namen des Elementes zurück zurück
  436. Text Element::getName() const
  437. {
  438. return name->getText();
  439. }
  440. // erzeugt einen XML Text der dieses Element und alle childs beinhaltet
  441. Text Element::toString() const
  442. {
  443. Text ret = "<";
  444. ret += name->getText();
  445. if( attributes->getEintragAnzahl() )
  446. ret += " ";
  447. for( auto i = attributes->getIterator(), j = attributeValues->getIterator(); i && j; i++, j++ )
  448. {
  449. ret += i->getText();
  450. if( j->getLength() )
  451. {
  452. if( j->hat( '"' ) )
  453. {
  454. ret += "='";
  455. Text txt = j->getText();
  456. txt.ersetzen( "'", "\\'" );
  457. ret += txt;
  458. ret += "'";
  459. }
  460. else
  461. {
  462. ret += "=\"";
  463. Text txt = j->getText();
  464. txt.ersetzen( "\"", "\\\"" );
  465. ret += txt;
  466. ret += "\"";
  467. }
  468. }
  469. if( i.hasNext() )
  470. ret += " ";
  471. }
  472. if( children->getEintragAnzahl() || text->getLength() )
  473. {
  474. ret += ">";
  475. if( children->getEintragAnzahl() )
  476. {
  477. for( auto i = children->getIterator(); i; i++ )
  478. ret += i->toString();
  479. }
  480. else
  481. ret += text->getText();
  482. ret += "</";
  483. ret += name->getText();
  484. ret += ">";
  485. }
  486. else
  487. ret += "/>";
  488. return ret;
  489. }
  490. // Erzeugt eine Kopie ohne referenzen auf dieses objekt
  491. Element *Element::dublicate() const
  492. {
  493. return new Element( toString() );
  494. }
  495. // Erzeugt einen neuen XML Editor mit einer Liste von Objekten die editiert werden sollen
  496. Editor::Editor( RCArray< Element > *elements )
  497. : ReferenceCounter()
  498. {
  499. this->elements = new RCArray< Element >();
  500. for( auto i = elements->getIterator(); i; i++ )
  501. this->elements->add( (XML::Element *)i->getThis() );
  502. elements->release();
  503. }
  504. Editor::Editor( const Editor &e )
  505. : Editor( ( RCArray<XML::Element> * )e.elements->getThis() )
  506. {}
  507. Editor::~Editor()
  508. {
  509. elements->release();
  510. }
  511. // ändert ein attribut oder fügt eines hinzu (auf allen elementen in der Liste)
  512. // attribut: Der Name des Attributes
  513. // value: Der Wert des Attributes
  514. void Editor::setAttribute( Text attribut, Text value )
  515. {
  516. for( auto i = elements->getIterator(); i; i++ )
  517. i->setAttribute( attribut, value );
  518. }
  519. // entfernt ein attribut (auf allen elementen in der Liste)
  520. // attribut: Der Name des Attributes
  521. void Editor::removeAttribute( Text attribut )
  522. {
  523. for( auto i = elements->getIterator(); i; i++ )
  524. i->removeAttribute( attribut );
  525. }
  526. // fügt ein child hinzu (auf allen elementen in der Liste)
  527. // child: Das neue Child Element
  528. void Editor::addChild( Element *child )
  529. {
  530. for( auto i = elements->getIterator(); i; i++ )
  531. i->addChild( child->dublicate() );
  532. child->release();
  533. }
  534. // entfernt ein child (auf allen elementen in der Liste)
  535. // zChild: das zu entfernende Child
  536. void Editor::removeChild( Element *child )
  537. {
  538. for( auto i = elements->getIterator(); i; i++ )
  539. i->removeChild( (XML::Element *)child->getThis() );
  540. child->release();
  541. }
  542. // entfernt das i-te child (auf allen elementen in der Liste)
  543. // i: der Index des childs (bei 0 beginnend)
  544. void Editor::removeChild( int i )
  545. {
  546. for( auto j = elements->getIterator(); j; j++ )
  547. j->removeChild( i );
  548. }
  549. // entfernt alle childs (auf allen elementen in der Liste)
  550. void Editor::removeAllChilds()
  551. {
  552. for( auto i = elements->getIterator(); i; i++ )
  553. i->removeAllChilds();
  554. }
  555. // entfernt eine Liste mit childs (auf allen elementen in der Liste)
  556. // childs: alle Childs die entfernt werden sollen
  557. void Editor::removeChilds( RCArray<Element> *childs )
  558. {
  559. for( auto i = elements->getIterator(); i; i++ )
  560. i->removeChilds( ( RCArray<XML::Element> * )childs->getThis() );
  561. childs->release();
  562. }
  563. // entfernt dieses Element vom Eltern element (auf allen elementen in der Liste)
  564. void Editor::remove()
  565. {
  566. for( auto i = elements->getIterator(); i; i++ )
  567. i->remove();
  568. }
  569. // setzt den Text in dem Element falls es keine childs gibt (auf allen elementen in der Liste)
  570. // text: dert Text
  571. void Editor::setText( Text text )
  572. {
  573. for( auto i = elements->getIterator(); i; i++ )
  574. i->setText( text );
  575. }
  576. // Gibt ein Iterator durch alle Elemente zurück
  577. Iterator<Element *> Editor::getIterator()
  578. {
  579. return elements->getIterator();
  580. }
  581. // gibt einen selector zurück der alle childs beinhaltet
  582. Editor Editor::selectChildren() const
  583. {
  584. RCArray<Element> *list = new RCArray<Element>();
  585. for( auto i = elements->getIterator(); i; i++ )
  586. {
  587. for( auto j = i->selectChildren().getIterator(); j; j++ )
  588. {
  589. list->add( (XML::Element *)j->getThis() );
  590. }
  591. }
  592. return Editor( list );
  593. }
  594. // gibt einen selector zurück der alle parents beinhaltet
  595. Editor Editor::selectParents() const
  596. {
  597. RCArray<Element> *list = new RCArray<Element>();
  598. for( auto i = elements->getIterator(); i; i++ )
  599. {
  600. if( i->parent )
  601. list->add( (XML::Element *)i->parent->getThis() );
  602. }
  603. return Editor( list );
  604. }
  605. // gibt eine Liste mit elementen zurück, die einen bestimmten Namen haben
  606. // name: der name der Childs
  607. Editor Editor::whereNameEquals( Text name ) const
  608. {
  609. RCArray<Element> *list = new RCArray<Element>();
  610. for( auto i = elements->getIterator(); i; i++ )
  611. {
  612. if( i->getName().istGleich( name ) )
  613. list->add( (XML::Element *)i->getThis() );
  614. }
  615. return Editor( list );
  616. }
  617. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  618. // name: der name des childs
  619. Editor Editor::whereChildWithNameExists( Text name ) const
  620. {
  621. RCArray<Element> *list = new RCArray<Element>();
  622. for( auto i = elements->getIterator(); i; i++ )
  623. {
  624. if( i->selectChildsByName( name ).elements->getEintragAnzahl() )
  625. list->add( (XML::Element *)i->getThis() );
  626. }
  627. return Editor( list );
  628. }
  629. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  630. // attribute: der name des attributes
  631. Editor Editor::whereChildWithAttributeExists( Text attribute ) const
  632. {
  633. RCArray<Element> *list = new RCArray<Element>();
  634. for( auto i = elements->getIterator(); i; i++ )
  635. {
  636. if( i->selectChildsByAttribute( attribute ).elements->getEintragAnzahl() )
  637. list->add( (XML::Element *)i->getThis() );
  638. }
  639. return Editor( list );
  640. }
  641. // gibt eine Liste mit elementen zurück, die ein bestimmtes child haben
  642. // attribute: der name des attributes
  643. // value: der Wert des Attributes
  644. Editor Editor::whereChildWithAttributeExists( Text attribute, Text value ) const
  645. {
  646. RCArray<Element> *list = new RCArray<Element>();
  647. for( auto i = elements->getIterator(); i; i++ )
  648. {
  649. if( i->selectChildsByAttribute( attribute, value ).elements->getEintragAnzahl() )
  650. list->add( (XML::Element *)i->getThis() );
  651. }
  652. return Editor( list );
  653. }
  654. // gibt eine Liste mit elementen zurück, die ein bestimmtes Attribut haben
  655. // attribute: der name des Attributes
  656. Editor Editor::whereAttributeExists( Text attribute ) const
  657. {
  658. RCArray<Element> *list = new RCArray<Element>();
  659. for( auto i = elements->getIterator(); i; i++ )
  660. {
  661. if( i->hasAttribute( attribute ) )
  662. list->add( (XML::Element *)i->getThis() );
  663. }
  664. return Editor( list );
  665. }
  666. // gibt eine Liste mit elementen zurück, die ein bestimmtes Attribut mit einem bestimmten wert haben
  667. // attribute: der name des Attributes
  668. // value: der Wert des Attributes
  669. Editor Editor::whereAttributeEquals( Text attribute, Text value ) const
  670. {
  671. RCArray<Element> *list = new RCArray<Element>();
  672. for( auto i = elements->getIterator(); i; i++ )
  673. {
  674. if( i->hasAttribute( attribute ) && i->getAttributeValue( attribute ).istGleich( value ) )
  675. list->add( (XML::Element *)i->getThis() );
  676. }
  677. return Editor( list );
  678. }
  679. // Gibt einen Editor zurück welcher nurnoch die Elemente enthält die nicht in e sind
  680. // e: Ein Editor mit elementen die nicht enthalten sein sollen
  681. Editor Editor::without( Editor e ) const
  682. {
  683. RCArray<Element> *list = new RCArray<Element>();
  684. for( auto i = elements->getIterator(); i; i++ )
  685. {
  686. bool found = 0;
  687. for( auto j = e.elements->getIterator(); j; j++ )
  688. found |= i._ == j._;
  689. if( !found )
  690. list->add( (XML::Element *)i->getThis() );
  691. }
  692. return Editor( list );
  693. }
  694. // Ruft eine funktion für jedes Element auf
  695. // f: die funktion (nimmt als argument ein Element objekt ohne erhöhten reference Counter)
  696. void Editor::forEach( std::function< void( Element * ) > f ) const
  697. {
  698. for( auto i = elements->getIterator(); i; i++ )
  699. {
  700. f( i );
  701. }
  702. }