class CDocumentObject
{
protected:
static const char TAG_LT = '<';
static const char TAG_GT = '>';
static const char TAG_SLASH = '/';
static const char TAG_BSLASH = '\\';
static const char TAG_AND = '&';
typedef std::vector<CParserData> TDataVector;
typedef std::stack<CParserData> TDataStack;
struct TNodeData
{
size_t level;
CParserData tag;
CParserData value;
// CParserData end;
};
typedef std::deque<TNodeData> TNodeQueue;
public:
typedef std::stack<const CElementObject*> TElementStack;
public:
CDocumentObject();
virtual ~CDocumentObject();
int Load(const std::string& str);
const CElementObject* Root() const;
const CElementObject* FindFirstElement(const std::string& tag);
const CElementObject* FindNextElement();
const CElementObject* FindFirstElement(const CElementObject* element, const std::string& tag, TElementStack& tmpstack);
const CElementObject* FindNextElement(const CElementObject* element, const std::string& tag, TElementStack& tmpstack);
const CAttributeObject* FindAttribute(const CElementObject* element, const std::string& attr);
void Show(std::ostream& os) const;
protected:
int PreProcess(const std::string& str, std::string& html);
int PreParser(const std::string& html, TNodeQueue& vct);
int Parser(const std::string& html, TNodeQueue& que);
private:
int PreParserLT(const std::string& html, std::string::size_type& pos, CParserData& data);
int PushValueData(const CParserData& data, TDataStack& datastack) const;
int PushTagData(const std::string& html, const CParserData& data, TDataStack& datatstack, TNodeQueue& nodeque) const;
int CheckSpecialTag(const std::string& html, const CParserData& data) const;
int CheckTag(const std::string& html, const CParserData& tag, const CParserData& end) const;
CElementObject* MakeElement(const std::string& html, const TNodeData& node, CElementObject* parent, CElementObject* sibling) const;
void CDocumentObject::ShowElement(std::ostream& os, const CElementObject* e) const;
void FreeElement(CElementObject* root);
const CElementObject* FindElement(const CElementObject* root, const CElementObject* pe, const std::string& tag, TElementStack& stack);
private:
CElementObject* _root;
private:
std::string _findtag;
TElementStack _findstack;
}
;
先不关心性能和繁琐性,能写成这样已经累的我半死了。。。通过写这个功能,发现自己对于算法的设计不能一步到位,实现过程中总是有这样或哪样的地方没有考虑到,整个对象简直就是测试出来的。。。还好一条路不通,能马上换一条,还好敲字快,能马上再敲一边。。。