SVN_SILENT made messages (.desktop file)
[kdeaccessibility.git] / kttsd / filters / xhtml2ssml / xhtml2ssml.cpp
blobe8c981f8d20f84e1d38919b9af5dedc1df37a707
3 /****************************************************************************
4 XHTMLToSSMLParser class
6 Parses a piece of XHTML markup and converts into SSML.
7 -------------------
8 Copyright:
9 (C) 2004 by Paul Giannaros <ceruleanblaze@gmail.com>
10 -------------------
11 Original author: Paul Giannaros <ceruleanblaze@gmail.com>
12 ******************************************************************************/
14 /***************************************************************************
15 * *
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. *
19 * *
20 ***************************************************************************/
22 #include <QtXml>
23 #include <QtCore/QFile>
24 #include <QtCore/QTextStream>
26 #include <iostream>
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
38 return false;
40 QTextStream stream(&file);
41 // File parsing.
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.
46 if(!linestatus)
47 return false;
48 /// Maybe call processEvents() to prevent GUI blockages?
50 return true;
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];
64 if(toelement)
65 m_output.append(XMLElement::fromQString(toelement).startTag());
66 return true;
69 bool XHTMLToSSMLParser::endElement(const QString &, const QString &, const QString &qName) {
70 QString fromelement = qName;
71 QString toelement = m_xhtml2ssml[fromelement];
72 if(toelement)
73 m_output.append(XMLElement::fromQString(toelement).endTag());
74 return true;
77 bool XHTMLToSSMLParser::characters(const QString &characters) {
78 m_output.append(characters);
79 return true;
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) {
92 // comments
93 if(line.trimmed().startsWith('#')) {
94 return true;
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)
101 return false;
102 m_xhtml2ssml[keyvalue[0]] = keyvalue[1];
103 return true;