add new sigc++2 directory
[ardour.git] / libs / pbd / tests / xpath.cc
1 #include "assert.h"
2 #include <iostream>
3
4 #include "pbd/xml++.h"
5
6 using namespace std;
7
8 int main()
9 {
10         cout << "Test 1: RosegardenPatchFile.xml: Find all banks in the file" << endl;
11         XMLTree  doc("./RosegardenPatchFile.xml");
12         // "//bank" gives as last element an empty element libxml bug????
13         boost::shared_ptr<XMLSharedNodeList> result = doc.root()->find("//bank[@name]");
14         
15         cout << "Found " << result->size() << " banks" << endl;
16         assert(result->size() == 8);
17         int counter = 1;
18         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
19                 assert((*i)->name() == "bank");
20                 assert((*i)->property("name"));
21                 cout << "Found bank number " << counter++ << " with name: " << (*i)->property("name")->value() << endl;
22                 for(XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
23                         cout << "\t found program " << (*j)->property("id")->value() << 
24                                 " with name: " << (*j)->property("name")->value() << endl;
25                 }
26         }
27         
28         cout << endl << endl << "Test 2: RosegardenPatchFile.xml: Find all programs whose program name contains 'Latin'" << endl;
29         
30         result = doc.root()->find("/rosegarden-data/studio/device/bank/program[contains(@name, 'Latin')]");
31         assert(result->size() == 5);
32         
33         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
34                 cout << "\t found program " << (*i)->property("id")->value() << 
35                         " with name: " << (*i)->property("name")->value() << endl;
36         }
37
38         cout << endl << endl << "Test 3: TestSession.ardour: find all Sources where captured-for contains the string 'Guitar'" << endl;
39         
40         // We have to allocate a new document here, or we get segfaults
41         XMLTree doc2("./TestSession.ardour");
42         result = doc2.root()->find("/Session/Sources/Source[contains(@captured-for, 'Guitar')]");
43         assert(result->size() == 16);
44         
45         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
46                 cout << "\t found source '" << (*i)->property("name")->value() << 
47                         "' with id: " << (*i)->property("id")->value() << endl;
48         }
49         
50         cout << endl << endl << "Test 4: TestSession.ardour: Find all elements with an 'id' and 'name' attribute" << endl;
51         
52         result = doc2.root()->find("//*[@id and @name]");
53         
54         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
55                 assert((*i)->property("id"));
56                 assert((*i)->property("name"));
57                 cout << "\t found element '" << (*i)->name() << 
58                         "' with id: "  << (*i)->property("id")->value() << 
59                         "' and name: " << (*i)->property("name")->value() << endl;
60         }
61         
62         cout << endl << endl << "Test 5: ProtoolsPatchFile.midnam: Get Banks and Patches for 'Name Set 1'" << endl;
63         
64         // We have to allocate a new document here, or we get segfaults
65         XMLTree doc3("./ProtoolsPatchFile.midnam");
66         result = doc3.root()->find("/MIDINameDocument/MasterDeviceNames/ChannelNameSet[@Name='Name Set 1']/PatchBank");
67         assert(result->size() == 16);
68         
69         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
70                 cout << "\t found Patchbank " << (*i)->property("Name")->value() << endl;
71                 boost::shared_ptr<XMLSharedNodeList> patches = (*i)->find("//Patch[@Name]");
72                 for(XMLSharedNodeList::const_iterator p = patches->begin(); p != patches->end(); ++p) {
73                         cout << "\t\t found patch number " << (*p)->property("Number")->value() 
74                              << " with name: " << (*p)->property("Name")->value()  << endl;
75                 }
76         }
77
78         cout << endl << endl << "Test 5: ProtoolsPatchFile.midnam: Find attribute nodes" << endl;
79         result = doc3.root()->find("//@Value");
80         
81         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
82                 boost::shared_ptr<XMLNode> node = (*i);
83                 cout << "\t found attribute node: " << node->name()  
84                      << " value: " << node->attribute_value() << endl;
85         }       
86         
87         cout << endl << endl << "Test 6: ProtoolsPatchFile.midnam: Find available channels on 'Name Set 1'" << endl;
88         result = doc3.root()->find(
89                 "//ChannelNameSet[@Name = 'Name Set 1']//AvailableChannel[@Available = 'true']/@Channel");
90         
91         assert(result->size() == 15);
92         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
93                 boost::shared_ptr<XMLNode> node = (*i);
94                 cout << "\t found available Channel: " << node->name()  
95                      << " value: " << node->attribute_value() << endl;
96         }       
97         
98 }