2020-09-17 23:30:25 +08:00
|
|
|
|
#ifndef HGL_UTIL_XML_ELEMENT_PARSE_CREATER_INCLUDE
|
2020-08-21 21:11:07 +08:00
|
|
|
|
#define HGL_UTIL_XML_ELEMENT_PARSE_CREATER_INCLUDE
|
|
|
|
|
|
|
|
|
|
#include<hgl/util/xml/ElementParse.h>
|
2020-09-03 15:58:38 +08:00
|
|
|
|
#include<hgl/type/String.h>
|
2020-08-21 21:11:07 +08:00
|
|
|
|
#include<hgl/type/Stack.h>
|
|
|
|
|
#include<hgl/type/Map.h>
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace xml
|
|
|
|
|
{
|
|
|
|
|
class ElementCreater
|
|
|
|
|
{
|
|
|
|
|
friend class ElementParseCreater;
|
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
U8String element_name;
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
2020-09-17 23:30:25 +08:00
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
Map<U8String,ElementCreater *> ecs_map;
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
virtual ElementCreater *GetSubElementCreater(const U8String &sub_name);
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
const U8String &GetElementName()const{return element_name;}
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
ElementCreater(const U8String &en){element_name=en;}
|
2020-08-21 21:11:07 +08:00
|
|
|
|
virtual ~ElementCreater()=default;
|
|
|
|
|
|
2020-08-25 21:47:05 +08:00
|
|
|
|
bool Registry(ElementCreater *ec);
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2020-09-04 18:07:00 +08:00
|
|
|
|
virtual bool Init (){return true;} ///<初始化节点
|
|
|
|
|
virtual void Attr (const u8char *flag,const u8char *info){} ///<节点属性
|
|
|
|
|
virtual bool Start (){return true;} ///<开始内部数据解晰
|
|
|
|
|
virtual void CharData (const u8char *str,const int str_length){} ///<文本数据
|
|
|
|
|
virtual void End (){} ///<节点结束
|
2020-08-21 21:11:07 +08:00
|
|
|
|
};//class ElementCreater
|
2020-09-04 18:07:00 +08:00
|
|
|
|
|
2020-09-11 15:41:20 +08:00
|
|
|
|
class ElementAttribute:public ElementCreater
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
U8String null_string;
|
2020-09-11 15:41:20 +08:00
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
Map<U8String,U8String> attrs_list;
|
2020-09-11 15:41:20 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
ElementAttribute(const U8String &en):ElementCreater(en){}
|
2020-09-11 15:41:20 +08:00
|
|
|
|
virtual ~ElementAttribute()=default;
|
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
const U8String operator[](const U8String &flag)
|
2020-09-11 15:41:20 +08:00
|
|
|
|
{
|
2024-12-24 22:41:09 +08:00
|
|
|
|
U8String key=flag.ToLowerCase();
|
|
|
|
|
U8String info;
|
2020-09-11 15:41:20 +08:00
|
|
|
|
|
2020-09-18 19:49:20 +08:00
|
|
|
|
if(attrs_list.Get(key,info))
|
2020-09-11 15:41:20 +08:00
|
|
|
|
return info;
|
|
|
|
|
else
|
|
|
|
|
return null_string;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 19:49:20 +08:00
|
|
|
|
#define TO_CONV(TYPE,FN,SFN,DEFAULT_VALUE) \
|
2024-12-24 22:41:09 +08:00
|
|
|
|
const TYPE FN (const U8String &flag,const TYPE default_value=DEFAULT_VALUE) \
|
2020-09-18 19:49:20 +08:00
|
|
|
|
{ \
|
2024-12-24 22:41:09 +08:00
|
|
|
|
const U8String key=flag.ToLowerCase(); \
|
2020-09-18 19:49:20 +08:00
|
|
|
|
\
|
2024-12-24 22:41:09 +08:00
|
|
|
|
U8String info; \
|
2020-09-18 19:49:20 +08:00
|
|
|
|
\
|
|
|
|
|
if(!attrs_list.Get(key,info)) \
|
|
|
|
|
return default_value; \
|
|
|
|
|
\
|
|
|
|
|
TYPE result; \
|
|
|
|
|
\
|
|
|
|
|
return info.SFN(result)?result:default_value; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TO_CONV(uint, ToUInteger, ToUint, 0)
|
|
|
|
|
TO_CONV( int, ToInteger, ToInt, 0)
|
|
|
|
|
TO_CONV(float, ToFloat, ToFloat, 0)
|
|
|
|
|
TO_CONV(double, ToDouble, ToFloat, 0)
|
|
|
|
|
TO_CONV(bool, ToBool, ToBool, false)
|
|
|
|
|
|
|
|
|
|
#undef TO_CONV
|
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
const U8String ToString (const U8String &flag){return operator[](flag);}
|
|
|
|
|
const int ToSerial (const u8char **list,const U8String &flag,const int default_value)
|
2020-09-18 19:49:20 +08:00
|
|
|
|
{
|
2024-12-24 22:41:09 +08:00
|
|
|
|
U8String key=flag.ToLowerCase();
|
|
|
|
|
U8String info;
|
2020-09-18 19:49:20 +08:00
|
|
|
|
|
|
|
|
|
if(!attrs_list.Get(key,info))
|
|
|
|
|
return default_value;
|
|
|
|
|
|
2024-12-22 00:16:32 +08:00
|
|
|
|
int result=find_str_in_array(list,info.c_str());
|
2020-09-18 19:49:20 +08:00
|
|
|
|
|
|
|
|
|
if(result==-1)return default_value;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T> const T ToEnum(const u8char **str_list,const u8char *str,const T &default_enum)
|
|
|
|
|
{
|
|
|
|
|
return T(this->ToSerial(str_list,str,(int)default_enum));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 15:41:20 +08:00
|
|
|
|
public:
|
2020-09-17 23:30:25 +08:00
|
|
|
|
|
2020-09-16 22:03:08 +08:00
|
|
|
|
virtual bool Init () override
|
|
|
|
|
{
|
2023-08-14 20:02:15 +08:00
|
|
|
|
attrs_list.Clear();
|
2020-09-16 22:03:08 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-09-11 15:41:20 +08:00
|
|
|
|
|
|
|
|
|
virtual void Attr (const u8char *flag,const u8char *info) override///<节点属性
|
|
|
|
|
{
|
2024-12-24 22:41:09 +08:00
|
|
|
|
U8String key=flag;
|
2020-09-18 19:49:20 +08:00
|
|
|
|
|
|
|
|
|
key.LowerCase();
|
|
|
|
|
|
|
|
|
|
attrs_list.Add(key,info);
|
2020-09-11 15:41:20 +08:00
|
|
|
|
}
|
|
|
|
|
};//class ElementAttribute:public ElementCreater
|
|
|
|
|
|
2020-08-21 21:11:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* Creater模式XML节点解析器
|
|
|
|
|
*/
|
|
|
|
|
class ElementParseCreater:public ElementParse
|
|
|
|
|
{
|
|
|
|
|
Stack<ElementCreater *> ecs_stack;
|
2020-09-17 23:30:25 +08:00
|
|
|
|
|
2024-12-24 22:41:09 +08:00
|
|
|
|
Map<U8String,ElementCreater *> ecs_map;
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
ElementCreater *cur_ec;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2020-08-31 19:09:12 +08:00
|
|
|
|
ElementParseCreater(ElementCreater *root_ec)
|
2020-08-21 21:11:07 +08:00
|
|
|
|
{
|
2020-08-31 19:09:12 +08:00
|
|
|
|
cur_ec=nullptr;
|
|
|
|
|
|
|
|
|
|
Registry(root_ec);
|
2020-08-21 21:11:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~ElementParseCreater()=default;
|
|
|
|
|
|
2020-08-31 19:09:12 +08:00
|
|
|
|
bool Registry (ElementCreater *ec);
|
|
|
|
|
|
2020-08-21 21:11:07 +08:00
|
|
|
|
public:
|
2020-09-17 23:30:25 +08:00
|
|
|
|
|
2020-09-04 18:07:00 +08:00
|
|
|
|
virtual bool Init (const u8char *element_name) override;
|
|
|
|
|
virtual void Attr (const u8char *flag,const u8char *info) override;
|
|
|
|
|
virtual bool Start () override;
|
|
|
|
|
virtual void CharData (const u8char *str,const int str_length) override;
|
|
|
|
|
virtual void End (const u8char *element_name) override;
|
2020-08-21 21:11:07 +08:00
|
|
|
|
};//class ElementParseCreater:public ElementParse
|
|
|
|
|
}//namespace xml
|
|
|
|
|
}//namespace hgl
|
|
|
|
|
#endif//HGL_UTIL_XML_ELEMENT_PARSE_CREATER_INCLUDE
|