lxpath_node and lxpath_node_set

This commit is contained in:
d-led 2012-10-03 12:58:34 +02:00
parent 9d169706de
commit 44e2d0a334
3 changed files with 189 additions and 82 deletions

View File

@ -14,4 +14,12 @@ local query1=doc:select_nodes('Project/PropertyGroup')
local query2=node:select_nodes('PropertyGroup')
assert(query1)
assert(query2)
print(query1.type,pugi.xpath_node_set.type_sorted)
print(query1.type,pugi.xpath_node_set.type_sorted)
query1:sort(true)
print(query1.type,pugi.xpath_node_set.type_sorted)
print(query1.size,query2.size)
n=query1.size
for i=0,n-1 do
node=query1:get(i):node()
print(node.valid)
end

View File

@ -9,15 +9,87 @@
namespace pugi {
namespace lua {
class lxml_xpath_node_set {
class lxpath_node;
class lxpath_node_set;
///////////////////////
class lxml_parse_result {
public:
lxml_xpath_node_set(pugi::xpath_node_set& s):node_set(s) { }
lxml_xpath_node_set() { }
lxml_parse_result(pugi::xml_parse_result& r);
lxml_parse_result();
public:
std::string description() const;
bool valid() const;
private:
pugi::xml_parse_result res;
};
///////////////
class lxml_node {
public:
lxml_node(pugi::xml_node const& n);
lxml_node();
public:
bool valid() const;
RefCountedPtr<lxml_node> child(char const* name);
std::string name() const;
RefCountedPtr<lxpath_node_set> select_nodes(char const* query);
private:
pugi::xml_node node;
};
///////////////////
class lxml_document {
public:
RefCountedPtr<lxml_parse_result> load_file(char const* path);
RefCountedPtr<lxml_node> child(char const* name);
RefCountedPtr<lxpath_node_set> select_nodes(char const* query);
bool valid() const;
private:
pugi::xml_document doc;
};
/////////////////
class lxpath_node {
public:
lxpath_node(pugi::xpath_node const& n);
lxpath_node();
bool valid() const;
RefCountedPtr<lxml_node> node() const;
private:
pugi::xpath_node _node;
};
/////////////////////
class lxpath_node_set {
public:
lxpath_node_set(pugi::xpath_node_set& s);
lxpath_node_set();
public:
int _type() const {
return node_set.type();
}
int type() const;
size_t size() const;
// todo: think if 1..size is a better convention for lua
RefCountedPtr<lxpath_node> get(size_t i);
void sort(bool reverse);
public:
// enums
@ -28,87 +100,105 @@ namespace pugi {
pugi::xpath_node_set node_set;
};
class lxml_parse_result {
public:
lxml_parse_result(pugi::xml_parse_result& r):res(r) { }
lxml_parse_result() { }
}
}
public:
std::string description() const {
return res.description();
namespace pugi {
namespace lua {
///////////////////////
lxml_parse_result::lxml_parse_result(pugi::xml_parse_result& r):res(r) { }
lxml_parse_result::lxml_parse_result() { }
std::string lxml_parse_result::description() const {
return res.description();
}
bool lxml_parse_result::valid() const {
return (bool)res;
}
///////////////
lxml_node::lxml_node(pugi::xml_node const& n):node(n){}
lxml_node::lxml_node() { }
bool lxml_node::valid() const {
return (bool)node;
}
RefCountedPtr<lxml_node> lxml_node::child(char const* name) {
return RefCountedPtr<lxml_node>(new lxml_node(node.child(name)));
}
std::string lxml_node::name() const {
return node.name();
}
RefCountedPtr<lxpath_node_set> lxml_node::select_nodes(char const* query) {
try {
return RefCountedPtr<lxpath_node_set>(new lxpath_node_set(node.select_nodes(query)));
} catch (pugi::xpath_exception const& e) {
std::cerr<<"Error: "<<e.what()<<std::endl;
return RefCountedPtr<lxpath_node_set>(new lxpath_node_set());
}
}
bool valid() const {
return (bool)res;
///////////////////
RefCountedPtr<lxml_parse_result> lxml_document::load_file(char const* path) {
return RefCountedPtr<lxml_parse_result>(new lxml_parse_result(doc.load_file(path)));
}
RefCountedPtr<lxml_node> lxml_document::child(char const* name) {
return RefCountedPtr<lxml_node>(new lxml_node(doc.child(name)));
}
RefCountedPtr<lxpath_node_set> lxml_document::select_nodes(char const* query) {
try {
return RefCountedPtr<lxpath_node_set>(new lxpath_node_set(doc.select_nodes(query)));
} catch (pugi::xpath_exception const& e) {
std::cerr<<"Error: "<<e.what()<<std::endl;
return RefCountedPtr<lxpath_node_set>(new lxpath_node_set());
}
}
private:
pugi::xml_parse_result res;
};
bool lxml_document::valid() const {
return (bool)doc;
}
//int const lxml_xpath_node_set::type_unsorted=pugi::xpath_node_set::type_unsorted;
//int const lxml_xpath_node_set::type_sorted=pugi::xpath_node_set::type_sorted;
//int const lxml_xpath_node_set::type_sorted_reverse=pugi::xpath_node_set::type_sorted_reverse;
//////////////////////
lxpath_node::lxpath_node(pugi::xpath_node const& n):_node(n){}
lxpath_node::lxpath_node() { }
class lxml_node {
public:
lxml_node(pugi::xml_node& n):node(n){}
lxml_node() { }
bool lxpath_node::valid() const {
return (bool)_node;
}
public:
bool valid() const {
return (bool)node;
}
RefCountedPtr<lxml_node> lxpath_node::node() const {
return RefCountedPtr<lxml_node>(new lxml_node(_node.node()));
}
RefCountedPtr<lxml_node> child(char const* name) {
return RefCountedPtr<lxml_node>(new lxml_node(node.child(name)));
}
std::string name() const {
return node.name();
}
/////////////////////
lxpath_node_set::lxpath_node_set(pugi::xpath_node_set& s):node_set(s) { }
lxpath_node_set::lxpath_node_set() { }
RefCountedPtr<lxml_xpath_node_set> select_nodes(char const* query) {
try {
return RefCountedPtr<lxml_xpath_node_set>(new lxml_xpath_node_set(node.select_nodes(query)));
} catch (pugi::xpath_exception const& e) {
std::cerr<<"Error: "<<e.what()<<std::endl;
return RefCountedPtr<lxml_xpath_node_set>(new lxml_xpath_node_set());
}
}
int lxpath_node_set::type() const {
return node_set.type();
}
private:
pugi::xml_node node;
};
size_t lxpath_node_set::size() const {
return node_set.size();
}
class lxml_document {
public:
RefCountedPtr<lxml_parse_result> load_file(char const* path) {
return RefCountedPtr<lxml_parse_result>(new lxml_parse_result(doc.load_file(path)));
}
// redundant, but defined due to composition up to now
RefCountedPtr<lxml_node> child(char const* name) {
return RefCountedPtr<lxml_node>(new lxml_node(doc.child(name)));
}
RefCountedPtr<lxml_xpath_node_set> select_nodes(char const* query) {
try {
return RefCountedPtr<lxml_xpath_node_set>(new lxml_xpath_node_set(doc.select_nodes(query)));
} catch (pugi::xpath_exception const& e) {
std::cerr<<"Error: "<<e.what()<<std::endl;
return RefCountedPtr<lxml_xpath_node_set>(new lxml_xpath_node_set());
}
}
bool valid() const {
return (bool)doc;
}
private:
pugi::xml_document doc;
};
RefCountedPtr<lxpath_node> lxpath_node_set::get(size_t i) {
return RefCountedPtr<lxpath_node>(new lxpath_node(node_set[i]));
}
void lxpath_node_set::sort(bool reverse) {
node_set.sort(reverse);
}
}
}
@ -117,12 +207,21 @@ void register_pugilua (lua_State* L) {
luabridge::getGlobalNamespace(L)
.beginNamespace("pugi")
.beginClass<lxml_xpath_node_set>("xpath_node_set")
.beginClass<lxpath_node>("xpath_node")
.addConstructor<void (*)()>()
.addProperty("type",&lxml_xpath_node_set::_type)
.addStaticProperty("type_unsorted",&lxml_xpath_node_set::type_unsorted)
.addStaticProperty("type_sorted",&lxml_xpath_node_set::type_sorted)
.addStaticProperty("type_sorted_reverse",&lxml_xpath_node_set::type_sorted_reverse)
.addProperty("valid",&lxpath_node::valid)
.addFunction("node",&lxpath_node::node)
.endClass()
.beginClass<lxpath_node_set>("xpath_node_set")
.addConstructor<void (*)()>()
.addProperty("type",&lxpath_node_set::type)
.addProperty("size",&lxpath_node_set::size)
.addStaticProperty("type_unsorted",&lxpath_node_set::type_unsorted)
.addStaticProperty("type_sorted",&lxpath_node_set::type_sorted)
.addStaticProperty("type_sorted_reverse",&lxpath_node_set::type_sorted_reverse)
.addFunction("get",&lxpath_node_set::get)
.addFunction("sort",&lxpath_node_set::sort)
.endClass()
.beginClass<lxml_parse_result>("xml_parse_result")

View File

@ -15,15 +15,15 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\pugilua\pugilua.cpp">
<ClCompile Include="..\..\contrib\pugilua\pugilua.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\pugilua\pugilua_lib.cpp">
<ClCompile Include="..\..\contrib\pugilua\pugilua_lib.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\pugilua\pugilua_lib.h">
<ClInclude Include="..\..\contrib\pugilua\pugilua_lib.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>