3 /****************************************************************************
4 XHTMLToSSMLParser class
6 Parses a piece of XHTML markup and converts into SSML.
9 (C) 2004 by Paul Giannaros <ceruleanblaze@gmail.com>
11 Original author: Paul Giannaros <ceruleanblaze@gmail.com>
12 ******************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published by *
18 * the Free Software Foundation; version 2 of the License. *
20 ***************************************************************************/
23 #include <QtCore/QFile>
24 #include <QtCore/QTextStream>
28 #include "xmlelement.h"
29 #include "xhtml2ssml.h"
31 /// Document parsing begin. Init stuff here.
32 bool XHTMLToSSMLParser::startDocument() {
33 /// Read the file which maps xhtml tags -> ssml tags. Look at the file for more information.
34 QFile
file("tagmappingrc");
35 if(!file
.open(QIODevice::ReadOnly
)) {
36 std::cerr
<< "Could not read config file 'tagmappingrc'. Please check that it exists and is readable.\n";
37 // Kill further parsing
40 QTextStream
stream(&file
);
42 bool linestatus
= true;
43 while(!stream
.atEnd()) {
44 linestatus
= readFileConfigEntry(stream
.readLine());
45 // If there's some syntactical error in the file then return false.
48 /// Maybe call processEvents() to prevent GUI blockages?
53 bool XHTMLToSSMLParser::startElement(const QString
&, const QString
&, const QString
&qName
, const QXmlAttributes
&atts
) {
54 QString attributes
= "";
55 if(atts
.length() > 0) {
56 const int attsLength
= atts
.length();
57 for(int i
= 0; i
< attsLength
; ++i
)
58 attributes
+= " " + atts
.qName(i
) + "=\"" + atts
.value(i
) + "\"";
60 QString fromelement
= qName
+ attributes
;
61 // If this element is one of the keys that was specified in the configuration file, get what it should be converted to and
62 // append to the output string.
63 QString toelement
= m_xhtml2ssml
[fromelement
];
65 m_output
.append(XMLElement::fromQString(toelement
).startTag());
69 bool XHTMLToSSMLParser::endElement(const QString
&, const QString
&, const QString
&qName
) {
70 QString fromelement
= qName
;
71 QString toelement
= m_xhtml2ssml
[fromelement
];
73 m_output
.append(XMLElement::fromQString(toelement
).endTag());
77 bool XHTMLToSSMLParser::characters(const QString
&characters
) {
78 m_output
.append(characters
);
83 QString
XHTMLToSSMLParser::convertedText() {
84 return m_output
.simplified();
87 /// Parse a line from the configuration file which maps xhtml : ssml equivalent.
88 /// It makes entries in the m_xhtml2ssml map accordingly.
89 /// @param line A line from a file to parse
90 /// @returns true if the syntax of the line was okay and the parsing succeeded - false otherwise.
91 bool XHTMLToSSMLParser::readFileConfigEntry(const QString
&line
) {
93 if(line
.trimmed().startsWith('#')) {
96 // break into QStringList
97 // the second parameter to split is the string, with all space simplified and all space around the : removed, i.e
98 // "something : somethingelse" -> "something:somethingelse"
99 QStringList keyvalue
= QString( ":").replace(":").split( ":", line
.simplified().replace(" :", ":"));
100 if(keyvalue
.count() != 2)
102 m_xhtml2ssml
[keyvalue
[0]] = keyvalue
[1];