summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/htmlparse.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/htmlparse.cpp') (more/less context) (show whitespace changes)
-rw-r--r--pwmanager/pwmanager/htmlparse.cpp184
1 files changed, 184 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/htmlparse.cpp b/pwmanager/pwmanager/htmlparse.cpp
new file mode 100644
index 0000000..acde2e3
--- a/dev/null
+++ b/pwmanager/pwmanager/htmlparse.cpp
@@ -0,0 +1,184 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. *
9 * *
10 ***************************************************************************/
11
12#include "htmlparse.h"
13#include "pwmexception.h"
14
15#include <dom/html_document.h>
16#include <dom/html_element.h>
17#include <dom/html_misc.h>
18
19
20/** enable/disable HTML-parser debugging (0/1) */
21 #define HTMLPARSE_DEBUG 1
22
23
24#if !defined(PWM_DEBUG) || HTMLPARSE_DEBUG == 0
25# undef printDebug
26 # define printDebug(x)do {} while (0)
27#endif
28
29HtmlParse::HtmlParse()
30{
31}
32
33HtmlParse::~HtmlParse()
34{
35}
36
37bool HtmlParse::parseHtmlComment(const QString &dta,
38 HtmlComment *ret)
39{
40 PWM_ASSERT(ret);
41 ret->clear();
42 DOM::HTMLDocument curDoc(static_cast<KHTMLView *>(0));
43 curDoc.open();
44 curDoc.write(dta);
45 curDoc.close();
46 DOM::HTMLElement body(curDoc.body());
47 DOM::HTMLCollection children(body.children());
48
49 unsigned long i, numCh = children.length();
50 if (numCh != 1) {
51 /* we currently support only one global table */
52 printDebug("HtmlParse::parseHtmlComment(): global children cnt != 1");
53 return false;
54 }
55 DOM::DOMString nodeName;
56 DOM::Node curNode;
57 for (i = 0; i < numCh; ++i) {
58 curNode = children.item(i);
59 nodeName = curNode.nodeName();
60 if (nodeName == "table") {
61 if (!parseCommentGlobTbl(curNode, ret))
62 return false;
63 } else {
64 // We don't support something else than tables, yet.
65 printDebug("HtmlParse::parseHtmlComment(): unknown node");
66 }
67 }
68 return true;
69}
70
71bool HtmlParse::parseCommentGlobTbl(const DOM::Node &node,
72 HtmlComment *ret)
73{
74 PWM_ASSERT(node.nodeName() == "table");
75 DOM::Node bodyNode(node.firstChild());
76 PWM_ASSERT(bodyNode.nodeName() == "tbody");
77 DOM::Node curNode(bodyNode.firstChild());
78 DOM::DOMString nodeName;
79 ret->clear();
80 while (!curNode.isNull()) {
81 nodeName = curNode.nodeName();
82 if (nodeName == "tr") {
83 if (!parseCommentGlobTblRow(curNode, ret))
84 return false;
85 } else {
86 printDebug("HtmlParse::parseCommentGlobTbl(): node unknown");
87 }
88 curNode = curNode.nextSibling();
89 }
90 return true;
91}
92
93bool HtmlParse::parseCommentGlobTblRow(const DOM::Node &node,
94 HtmlComment *ret)
95{
96 DOM::Node curNode(node.firstChild()), child;
97 DOM::DOMString nodeName;
98 HtmlComment::SubTable subTbl;
99 while (!curNode.isNull()) {
100 nodeName = curNode.nodeName();
101 if (nodeName == "th") {
102 // global title
103 ret->setTitle(curNode.firstChild().nodeValue().string());
104 } else if (nodeName == "td") {
105 child = curNode.firstChild();
106 if (child.nodeName() == "table" &&
107 child.firstChild().nodeName() == "tbody") {
108 // we have a sub-table
109 if (!parseCommentSubTbl(child.firstChild(), &subTbl))
110 return false;
111 ret->addSubtable(subTbl);
112 } else {
113 printDebug("HtmlParse::parseCommentGlobTblRow(): subelement unknown");
114 }
115 } else {
116 printDebug("HtmlParse::parseCommentGlobTblRow(): node unknown");
117 }
118 curNode = curNode.nextSibling();
119 }
120 return true;
121}
122
123bool HtmlParse::parseCommentSubTbl(const DOM::Node &node,
124 HtmlComment::SubTable *ret)
125{
126 PWM_ASSERT(node.nodeName() == "tbody");
127 DOM::Node curNode(node.firstChild());
128 DOM::DOMString nodeName;
129 ret->clear();
130 while (!curNode.isNull()) {
131 nodeName = curNode.nodeName();
132 if (nodeName == "tr") {
133 if (!parseCommentSubTblRow(curNode, ret))
134 return false;
135 } else {
136 printDebug("HtmlParse::parseCommentSubTbl(): node unknown");
137 }
138 curNode = curNode.nextSibling();
139 }
140 return true;
141}
142
143bool HtmlParse::parseCommentSubTblRow(const DOM::Node &node,
144 HtmlComment::SubTable *ret)
145{
146 DOM::Node curNode(node.firstChild()), child;
147 DOM::DOMString nodeName;
148 pair<QString, QString> curEntr;
149 while (!curNode.isNull()) {
150 nodeName = curNode.nodeName();
151 if (nodeName == "th") {
152 // sub title
153 ret->setTitle(curNode.firstChild().nodeValue().string());
154 } else if (nodeName == "td") {
155 child = curNode.firstChild();
156 if (child.nodeName() == "#text") {
157 if (!parseCommentSubTblEntry(curNode, &curEntr))
158 return false;
159 ret->addEntry(curEntr);
160 return true;
161 } else {
162 printDebug("HtmlParse::parseCommentSubTblRow(): subelement unknown");
163 }
164 } else {
165 printDebug("HtmlParse::parseCommentGlobTblRow(): node unknown");
166 }
167 curNode = curNode.nextSibling();
168 }
169 return true;
170}
171
172bool HtmlParse::parseCommentSubTblEntry(const DOM::Node &node,
173 pair<QString, QString> *ret)
174{
175 DOM::Node curChild(node);
176 if (curChild.isNull())
177 return false;
178 ret->first = curChild.firstChild().nodeValue().string();
179 curChild = curChild.nextSibling();
180 if (curChild.isNull())
181 return false;
182 ret->second = curChild.firstChild().nodeValue().string();
183 return true;
184}