1 /**
2  * Copyright (c) 2021, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "config.h"
31 
32 #include "fmtlib/fmt/format.h"
33 
34 #include "xml_util.hh"
35 
36 namespace lnav {
37 namespace pugixml {
38 
get_actual_path(const pugi::xml_node & node)39 std::string get_actual_path(const pugi::xml_node &node)
40 {
41     std::string retval;
42     auto curr = node;
43 
44     while (curr) {
45         switch (curr.type()) {
46             case pugi::node_null:
47                 break;
48             case pugi::node_pcdata:
49                 retval += "text()";
50                 break;
51             default: {
52                 auto name = std::string(curr.name());
53 
54                 if (curr.previous_sibling(curr.name()) ||
55                     curr.next_sibling(curr.name())) {
56                     auto sibling = curr;
57                     int index = 0;
58 
59                     while (sibling) {
60                         index += 1;
61                         sibling = sibling.previous_sibling(curr.name());
62                     }
63 
64                     name = fmt::format("{}[{}]", name, index);
65                 }
66                 if (retval.empty()) {
67                     retval = name;
68                 } else {
69                     retval = fmt::format("{}/{}", name, retval);
70                 }
71                 break;
72             }
73         }
74         curr = curr.parent();
75     }
76 
77     return retval;
78 }
79 
80 }
81 }
82