• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeaccessibility API Reference
  • KDE Home
  • Contact Us
 

jovie

  • sources
  • kde-4.12
  • kdeaccessibility
  • jovie
  • libkttsd
utils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Class of utility functions.
3  -------------------
4  Copyright : (C) 2004 Paul Giannaros <ceruleanblaze@gmail.com>
5  -------------------
6  Original author: Paul Giannaros <ceruleanblaze@gmail.com>
7  Current Maintainer: Paul Giannaros <ceruleanblaze@gmail.com>
8  ****************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; version 2 of the License or *
15  * (at your option) version 3. *
16  * *
17  ***************************************************************************/
18 
19 #include "utils.h"
20 
21 #include <kdebug.h>
22 #include <kcombobox.h>
23 
24 KttsUtils::KttsUtils() {
25 }
26 
27 
28 KttsUtils::~KttsUtils() {
29 }
30 
37 bool KttsUtils::hasRootElement(const QString &xmldoc, const QString &elementName) {
38  // Strip all whitespace and go from there.
39  QString doc = xmldoc.simplified();
40  // Take off the <?xml...?> if it exists
41  if(doc.startsWith(QLatin1String("<?xml"))) {
42  // Look for ?> and strip everything off from there to the start - effectively removing
43  // <?xml...?>
44  int xmlStatementEnd = doc.indexOf(QLatin1String( "?>" ));
45  if(xmlStatementEnd == -1) {
46  kDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
47  return false;
48  }
49  xmlStatementEnd += 2; // len '?>' == 2
50  doc = doc.right(doc.length() - xmlStatementEnd);
51  }
52  // Take off leading comments, if they exist.
53  while(doc.startsWith(QLatin1String("<!--")) || doc.startsWith(QLatin1String(" <!--"))) {
54  int commentStatementEnd = doc.indexOf(QLatin1String( "-->" ));
55  if(commentStatementEnd == -1) {
56  kDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
57  return false;
58  }
59  commentStatementEnd += 3; // len '>' == 2
60  doc = doc.right(doc.length() - commentStatementEnd);
61  }
62  // Take off the doctype statement if it exists.
63  while(doc.startsWith(QLatin1String("<!DOCTYPE")) || doc.startsWith(QLatin1String(" <!DOCTYPE"))) {
64  int doctypeStatementEnd = doc.indexOf(QLatin1Char( '>' ));
65  if(doctypeStatementEnd == -1) {
66  kDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
67  return false;
68  }
69  doctypeStatementEnd += 1; // len '>' == 2
70  doc = doc.right(doc.length() - doctypeStatementEnd);
71  }
72  // We should (hopefully) be left with the root element.
73  return (doc.startsWith(QString(QLatin1Char( '<' ) + elementName)) || doc.startsWith(QString(QLatin1String( " <" ) + elementName)));
74 }
75 
76 bool KttsUtils::hasDoctype(const QString &xmldoc, const QString &name) {
77  // Strip all whitespace and go from there.
78  QString doc = xmldoc.trimmed();
79  // Take off the <?xml...?> if it exists
80  if(doc.startsWith(QLatin1String("<?xml"))) {
81  // Look for ?> and strip everything off from there to the start - effectively removing
82  // <?xml...?>
83  int xmlStatementEnd = doc.indexOf(QLatin1String( "?>" ));
84  if(xmlStatementEnd == -1) {
85  kDebug() << "KttsUtils::hasDoctype: Bad XML file syntax\n";
86  return false;
87  }
88  xmlStatementEnd += 2; // len '?>' == 2
89  doc = doc.right(doc.length() - xmlStatementEnd);
90  doc = doc.trimmed();
91  }
92  // Take off leading comments, if they exist.
93  while(doc.startsWith(QLatin1String("<!--"))) {
94  int commentStatementEnd = doc.indexOf(QLatin1String( "-->" ));
95  if(commentStatementEnd == -1) {
96  kDebug() << "KttsUtils::hasDoctype: Bad XML file syntax\n";
97  return false;
98  }
99  commentStatementEnd += 3; // len '>' == 2
100  doc = doc.right(doc.length() - commentStatementEnd);
101  doc = doc.trimmed();
102  }
103  // Match the doctype statement if it exists.
104  // kDebug() << "KttsUtils::hasDoctype: searching " << doc.left(20) << "... for " << "<!DOCTYPE " << name;
105  return (doc.startsWith(QString(QLatin1String( "<!DOCTYPE " ) + name)));
106 }
107 
112 /*static*/ void KttsUtils::setCbItemFromText(KComboBox* cb, const QString& text)
113 {
114  const int itemCount = cb->count();
115  for (int ndx = 0; ndx < itemCount; ++ndx)
116  {
117  if (cb->itemText(ndx) == text)
118  {
119  cb->setCurrentIndex(ndx);
120  return;
121  }
122  }
123 }
124 
KttsUtils::hasRootElement
static bool hasRootElement(const QString &xmldoc, const QString &elementName)
Check if an XML document has a certain root element.
Definition: utils.cpp:37
KttsUtils::hasDoctype
static bool hasDoctype(const QString &xmldoc, const QString &name)
Check if an XML document has a certain DOCTYPE.
Definition: utils.cpp:76
KttsUtils::setCbItemFromText
static void setCbItemFromText(KComboBox *cb, const QString &text)
Sets the current item in the given combobox to the item with the given text.
Definition: utils.cpp:112
utils.h
KttsUtils::~KttsUtils
~KttsUtils()
Destructor.
Definition: utils.cpp:28
KttsUtils::KttsUtils
KttsUtils()
Constructor.
Definition: utils.cpp:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:32:25 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

jovie

Skip menu "jovie"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeaccessibility API Reference

Skip menu "kdeaccessibility API Reference"
  • jovie

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal