summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/htmlparse.cpp
blob: acde2e35847e678fec7eeb61ab22e066081c6f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2004 by Michael Buesch                                  *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

#include "htmlparse.h"
#include "pwmexception.h"

#include <dom/html_document.h>
#include <dom/html_element.h>
#include <dom/html_misc.h>


/** enable/disable HTML-parser debugging (0/1) */
#define HTMLPARSE_DEBUG		1


#if !defined(PWM_DEBUG) || HTMLPARSE_DEBUG == 0
# undef printDebug
# define printDebug(x)	do {} while (0)
#endif

HtmlParse::HtmlParse()
{
}

HtmlParse::~HtmlParse()
{
}

bool HtmlParse::parseHtmlComment(const QString &dta,
				  HtmlComment *ret)
{
	PWM_ASSERT(ret);
	ret->clear();
	DOM::HTMLDocument curDoc(static_cast<KHTMLView *>(0));
	curDoc.open();
	curDoc.write(dta);
	curDoc.close();
	DOM::HTMLElement body(curDoc.body());
	DOM::HTMLCollection children(body.children());

	unsigned long i, numCh = children.length();
	if (numCh != 1) {
		/* we currently support only one global table */
		printDebug("HtmlParse::parseHtmlComment(): global children cnt != 1");
		return false;
	}
	DOM::DOMString nodeName;
	DOM::Node curNode;
	for (i = 0; i < numCh; ++i) {
		curNode = children.item(i);
		nodeName = curNode.nodeName();
		if (nodeName == "table") {
			if (!parseCommentGlobTbl(curNode, ret))
				return false;
		} else {
			// We don't support something else than tables, yet.
			printDebug("HtmlParse::parseHtmlComment(): unknown node");
		}
	}
	return true;
}

bool HtmlParse::parseCommentGlobTbl(const DOM::Node &node,
				    HtmlComment *ret)
{
	PWM_ASSERT(node.nodeName() == "table");
	DOM::Node bodyNode(node.firstChild());
	PWM_ASSERT(bodyNode.nodeName() == "tbody");
	DOM::Node curNode(bodyNode.firstChild());
	DOM::DOMString nodeName;
	ret->clear();
	while (!curNode.isNull()) {
		nodeName = curNode.nodeName();
		if (nodeName == "tr") {
			if (!parseCommentGlobTblRow(curNode, ret))
				return false;
		} else {
			printDebug("HtmlParse::parseCommentGlobTbl(): node unknown");
		}
		curNode = curNode.nextSibling();
	}
	return true;
}

bool HtmlParse::parseCommentGlobTblRow(const DOM::Node &node,
				       HtmlComment *ret)
{
	DOM::Node curNode(node.firstChild()), child;
	DOM::DOMString nodeName;
	HtmlComment::SubTable subTbl;
	while (!curNode.isNull()) {
		nodeName = curNode.nodeName();
		if (nodeName == "th") {
			// global title
			ret->setTitle(curNode.firstChild().nodeValue().string());
		} else if (nodeName == "td") {
			child = curNode.firstChild();
			if (child.nodeName() == "table" &&
			    child.firstChild().nodeName() == "tbody") {
				// we have a sub-table
				if (!parseCommentSubTbl(child.firstChild(), &subTbl))
					return false;
				ret->addSubtable(subTbl);
			} else {
				printDebug("HtmlParse::parseCommentGlobTblRow(): subelement unknown");
			}
		} else {
			printDebug("HtmlParse::parseCommentGlobTblRow(): node unknown");
		}
		curNode = curNode.nextSibling();
	}
	return true;
}

bool HtmlParse::parseCommentSubTbl(const DOM::Node &node,
				   HtmlComment::SubTable *ret)
{
	PWM_ASSERT(node.nodeName() == "tbody");
	DOM::Node curNode(node.firstChild());
	DOM::DOMString nodeName;
	ret->clear();
	while (!curNode.isNull()) {
		nodeName = curNode.nodeName();
		if (nodeName == "tr") {
			if (!parseCommentSubTblRow(curNode, ret))
				return false;
		} else {
			printDebug("HtmlParse::parseCommentSubTbl(): node unknown");
		}
		curNode = curNode.nextSibling();
	}
	return true;
}

bool HtmlParse::parseCommentSubTblRow(const DOM::Node &node,
				      HtmlComment::SubTable *ret)
{
	DOM::Node curNode(node.firstChild()), child;
	DOM::DOMString nodeName;
	pair<QString, QString> curEntr;
	while (!curNode.isNull()) {
		nodeName = curNode.nodeName();
		if (nodeName == "th") {
			// sub title
			ret->setTitle(curNode.firstChild().nodeValue().string());
		} else if (nodeName == "td") {
			child = curNode.firstChild();
			if (child.nodeName() == "#text") {
				if (!parseCommentSubTblEntry(curNode, &curEntr))
					return false;
				ret->addEntry(curEntr);
				return true;
			} else {
				printDebug("HtmlParse::parseCommentSubTblRow(): subelement unknown");
			}
		} else {
			printDebug("HtmlParse::parseCommentGlobTblRow(): node unknown");
		}
		curNode = curNode.nextSibling();
	}
	return true;
}

bool HtmlParse::parseCommentSubTblEntry(const DOM::Node &node,
					pair<QString, QString> *ret)
{
	DOM::Node curChild(node);
	if (curChild.isNull())
		return false;
	ret->first = curChild.firstChild().nodeValue().string();
	curChild = curChild.nextSibling();
	if (curChild.isNull())
		return false;
	ret->second = curChild.firstChild().nodeValue().string();
	return true;
}