/*************************************************************************** * * * 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. * * * ***************************************************************************/ /*************************************************************************** * copyright (C) 2004 by Ulf Schenk * This file is originaly based on version 1.0.1 of pwmanager * and was modified to run on embedded devices that run microkde * * $Id$ **************************************************************************/ #include "htmlgen.h" #include "pwmexception.h" #include /** enable/disable HTML-generator debugging (0/1) */ #define HTMLGEN_DEBUG 0 #define HTML_DOCTYPE_HDR "\n" #define HTML_PWM_HDR "" #define HTML_COMMENT_HDR "" #define HTML_COMMENTVER_HDR "" #define HTML_STYLESHEET_DUMMY "@STYLESHEET@" #define HTML_GLOBTBL_CLASS "\"globtable\"" #define HTML_GLOBTITLE_CLASS "\"globtitle\"" #define HTML_SUBTBL_CLASS "\"subtable\"" #define HTML_SUBTITLE_CLASS "\"subtitle\"" #define HTML_ENTRY_CLASS "\"entry\"" #define HTML_VALUE_CLASS "\"value\"" #define PATH_COMMENTSTYLE_CSS "pwmanager/html/htmlcomment_style.css" #if defined(PWM_DEBUG) && HTMLGEN_DEBUG != 0 #define HTML_ENDL "\n" #else // defined(PWM_DEBUG) && ... #define HTML_ENDL "" #endif // defined(PWM_DEBUG) && ... HtmlGen::HtmlGen() { useSSDummy = true; } HtmlGen::~HtmlGen() { } QString HtmlGen::escapeHtmlText(const QString &str) { QString ret; unsigned int len = str.length(), i; char c; for (i = 0; i < len; ++i) { c = str.at(i).toLatin1(); switch (c) { case '<': ret.append("<"); break; case '>': ret.append(">"); break; case '&': ret.append("&"); break; case '\"': ret.append("""); break; case 'ä': ret.append("ä"); break; case 'Ä': ret.append("Ä"); break; case 'ü': ret.append("ü"); break; case 'Ü': ret.append("Ü"); break; case 'ö': ret.append("ö"); break; case 'Ö': ret.append("Ö"); break; case 'ß': ret.append("ß"); break; case '¿': ret.append("€"); break; default: ret.append(c); } } return ret; } bool HtmlGen::isHtml(const QString &dta) { int ret; ret = dta.find("", 0, false); if (ret == -1) return false; ret = dta.find("", ret, false); if (ret == -1) return false; return true; } QString HtmlGen::getStyleSheetHtml() { QString ret; ret = "" HTML_ENDL; return ret; } bool HtmlGen::replaceSSDummy(QString *doc) { int beginPos = doc->find(HTML_STYLESHEET_DUMMY); if (beginPos == -1) { printDebug("HtmlGen::replaceSSDummy(): not found"); return false; } *doc = doc->replace(beginPos, strlen(HTML_STYLESHEET_DUMMY), getStyleSheetHtml()); return true; } QString HtmlGen::genHtmlComment(const HtmlComment *dta) { QString ret(HTML_DOCTYPE_HDR HTML_PWM_HDR HTML_ENDL HTML_COMMENT_HDR HTML_ENDL HTML_COMMENTVER_HDR HTML_ENDL); ret += "" HTML_ENDL; if (!appendCommentHeader(&ret)) return ""; if (!appendCommentBody(&ret, dta)) return ""; ret += "" HTML_ENDL; #if defined(PWM_DEBUG) && HTMLGEN_DEBUG != 0 printDebug(""); cout << ret << endl; printDebug(""); #endif // DEBUG return ret; } bool HtmlGen::appendCommentHeader(QString *str) { *str += "" HTML_ENDL; if (useSSDummy) { *str += HTML_STYLESHEET_DUMMY HTML_ENDL; } else { QString ssLine(getStyleSheetHtml()); if (ssLine.isEmpty()) return false; *str += ssLine; } *str += "" HTML_ENDL; return true; } bool HtmlGen::appendCommentBody(QString *str, const HtmlComment *dta) { *str += "" HTML_ENDL; if (!appendCommentGlobTbl(str, dta)) return false; *str += "" HTML_ENDL; return true; } bool HtmlGen::appendCommentGlobTbl(QString *str, const HtmlComment *dta) { *str += "" HTML_ENDL; *str += "" HTML_ENDL; const vector *subTbls = dta->getSubTableList(); vector::const_iterator i = subTbls->begin(), end = subTbls->end(); while (i != end) { *str += "" HTML_ENDL; } *str += "
"; *str += escapeHtmlText(dta->getTitle()); *str += "
" HTML_ENDL; if (!appendCommentSubTbl(str, &(*i))) return false; ++i; *str += "
" HTML_ENDL; return true; } bool HtmlGen::appendCommentSubTbl(QString *str, const HtmlComment::SubTable *dta) { *str += "" HTML_ENDL; *str += "" HTML_ENDL; const vector< pair > *entries = dta->getEntryList(); vector< pair >::const_iterator i = entries->begin(), end = entries->end(); while (i != end) { *str += "" HTML_ENDL; if (!appendCommentSubTblEntry(str, &(*i))) return false; *str += "" HTML_ENDL; ++i; } *str += "
"; *str += escapeHtmlText(dta->getTitle()); *str += "
" HTML_ENDL; return true; } bool HtmlGen::appendCommentSubTblEntry(QString *str, const pair *dta) { *str += ""; *str += escapeHtmlText(dta->first); *str += "" HTML_ENDL; *str += ""; *str += escapeHtmlText(dta->second); *str += "" HTML_ENDL; return true; }