KConfig

KConfigXmlParser.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2003 Cornelius Schumacher <[email protected]>
5  SPDX-FileCopyrightText: 2003 Waldo Bastian <[email protected]>
6  SPDX-FileCopyrightText: 2003 Zack Rusin <[email protected]>
7  SPDX-FileCopyrightText: 2006 MichaĆ«l Larouche <[email protected]>
8  SPDX-FileCopyrightText: 2008 Allen Winter <[email protected]>
9  SPDX-FileCopyrightText: 2020 Tomaz Cananbrava <[email protected]>
10 
11  SPDX-License-Identifier: LGPL-2.0-or-later
12 */
13 
14 #include "KConfigXmlParser.h"
15 
16 #include <QDomAttr>
17 #include <QDomElement>
18 #include <QDomNode>
19 #include <QFile>
20 #include <QList>
21 #include <QStringList>
22 #include <QTextStream>
23 #include <iostream>
24 //TODO: Move preprocessDefault to Header / CPP implementation.
25 // it makes no sense for a parser to process those values and generate code.
26 
27 static void preProcessDefault(QString &defaultValue, const QString &name,
28  const QString &type,
29  const CfgEntry::Choices &choices,
30  QString &code, const KConfigParameters &cfg)
31 {
32  if (type == QLatin1String("String") && !defaultValue.isEmpty()) {
33  defaultValue = literalString(defaultValue);
34 
35  } else if (type == QLatin1String("Path") && !defaultValue.isEmpty()) {
36  defaultValue = literalString(defaultValue);
37  } else if (type == QLatin1String("Url") && !defaultValue.isEmpty()) {
38  // Use fromUserInput in order to support absolute paths and absolute urls, like KDE4's KUrl(QString) did.
39  defaultValue = QLatin1String("QUrl::fromUserInput( ") + literalString(defaultValue) + QLatin1Char(')');
40  } else if ((type == QLatin1String("UrlList") || type == QLatin1String("StringList") || type == QLatin1String("PathList")) && !defaultValue.isEmpty()) {
42  if (!code.isEmpty()) {
43  cpp << '\n';
44  }
45 
46  if (type == QLatin1String("UrlList")) {
47  cpp << " QList<QUrl> default" << name << ";\n";
48  } else {
49  cpp << " QStringList default" << name << ";\n";
50  }
51  const QStringList defaults = defaultValue.split(QLatin1Char(','));
53  for (it = defaults.constBegin(); it != defaults.constEnd(); ++it) {
54  cpp << " default" << name << ".append( ";
55  if (type == QLatin1String("UrlList")) {
56  cpp << "QUrl::fromUserInput(";
57  }
58  cpp << "QString::fromUtf8( \"" << *it << "\" ) ";
59  if (type == QLatin1String("UrlList")) {
60  cpp << ") ";
61  }
62  cpp << ");\n";
63  }
64  defaultValue = QLatin1String("default") + name;
65 
66  } else if (type == QLatin1String("Color") && !defaultValue.isEmpty()) {
68  QStringLiteral("\\d+,\\s*\\d+,\\s*\\d+(,\\s*\\d+)?")));
69 
70  if (colorRe.match(defaultValue).hasMatch()) {
71  defaultValue = QLatin1String("QColor( ") + defaultValue + QLatin1String(" )");
72  } else {
73  defaultValue = QLatin1String("QColor( \"") + defaultValue + QLatin1String("\" )");
74  }
75 
76  } else if (type == QLatin1String("Enum")) {
78  for (it = choices.choices.constBegin(); it != choices.choices.constEnd(); ++it) {
79  if ((*it).name == defaultValue) {
80  if (cfg.globalEnums && choices.name().isEmpty()) {
81  defaultValue.prepend(choices.prefix);
82  } else {
83  defaultValue.prepend(enumTypeQualifier(name, choices) + choices.prefix);
84  }
85  break;
86  }
87  }
88 
89  } else if (type == QLatin1String("IntList")) {
91  if (!code.isEmpty()) {
92  cpp << '\n';
93  }
94 
95  cpp << " QList<int> default" << name << ";\n";
96  if (!defaultValue.isEmpty()) {
97  const QStringList defaults = defaultValue.split(QLatin1Char(','));
99  for (it = defaults.constBegin(); it != defaults.constEnd(); ++it) {
100  cpp << " default" << name << ".append( " << *it << " );\n";
101  }
102  }
103  defaultValue = QLatin1String("default") + name;
104  }
105 }
106 
107 static QString dumpNode(const QDomNode &node)
108 {
109  QString msg;
111  node.save(s, 0);
112 
113  msg = msg.simplified();
114  if (msg.length() > 40) {
115  return msg.left(37) + QLatin1String("...");
116  }
117  return msg;
118 }
119 
120 void KConfigXmlParser::readParameterFromEntry(CfgEntry &readEntry, const QDomElement &e)
121 {
122  readEntry.param = e.attribute(QStringLiteral("name"));
123  readEntry.paramType = e.attribute(QStringLiteral("type"));
124 
125  if (readEntry.param.isEmpty()) {
126  std::cerr << "Parameter must have a name: " << qPrintable(dumpNode(e)) << std::endl;
127  exit (1);
128  }
129 
130  if (readEntry.paramType.isEmpty()) {
131  std::cerr << "Parameter must have a type: " << qPrintable(dumpNode(e)) << std::endl;
132  exit(1);
133  }
134 
135  if ((readEntry.paramType == QLatin1String("Int")) || (readEntry.paramType == QLatin1String("UInt"))) {
136  bool ok;
137  readEntry.paramMax = e.attribute(QStringLiteral("max")).toInt(&ok);
138  if (!ok) {
139  std::cerr << "Integer parameter must have a maximum (e.g. max=\"0\"): " << qPrintable(dumpNode(e)) << std::endl;
140  exit(1);
141  }
142  } else if (readEntry.paramType == QLatin1String("Enum")) {
143  for (QDomElement e2 = e.firstChildElement(); !e2.isNull(); e2 = e2.nextSiblingElement()) {
144  if (e2.tagName() == QLatin1String("values")) {
145  for (QDomElement e3 = e2.firstChildElement(); !e3.isNull(); e3 = e3.nextSiblingElement()) {
146  if (e3.tagName() == QLatin1String("value")) {
147  readEntry.paramValues.append(e3.text());
148  }
149  }
150  break;
151  }
152  }
153  if (readEntry.paramValues.isEmpty()) {
154  std::cerr << "No values specified for parameter '" << qPrintable(readEntry.param) << "'." << std::endl;
155  exit(1);
156  }
157  readEntry.paramMax = readEntry.paramValues.count() - 1;
158  } else {
159  std::cerr << "Parameter '" << qPrintable(readEntry.param) << "' has type " << qPrintable(readEntry.paramType)
160  << " but must be of type int, uint or Enum." << std::endl;
161  exit(1);
162  }
163 }
164 
165 bool KConfigXmlParser::hasDefaultCode(CfgEntry &readEntry, const QDomElement &element)
166 {
167  Q_UNUSED(readEntry)
168 
169  for (QDomElement e = element.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
170  if (e.attribute(QStringLiteral("param")).isEmpty()) {
171  if (e.attribute(QStringLiteral("code")) == QLatin1String("true")) {
172  return true;
173  }
174  }
175  }
176  return false;
177 }
178 
179 void KConfigXmlParser::readChoicesFromEntry(CfgEntry &readEntry, const QDomElement &e)
180 {
182  const auto choiceNameRegex = QRegularExpression(QStringLiteral("\\w+"));
183 
184  for (QDomElement e2 = e.firstChildElement(); !e2.isNull(); e2 = e2.nextSiblingElement()) {
185  if (e2.tagName() != QLatin1String("choice")) {
186  continue;
187  }
188  CfgEntry::Choice choice;
189  choice.name = e2.attribute(QStringLiteral("name"));
190  if (choice.name.isEmpty()) {
191  std::cerr << "Tag <choice> requires attribute 'name'." << std::endl;
192  } else if (!choiceNameRegex.match(choice.name).hasMatch()) {
193  std::cerr << "Tag <choice> attribute 'name' must be compatible with Enum naming. name was '" << qPrintable(choice.name) << "'. You can use attribute 'value' to pass any string as the choice value." << std::endl;
194  }
195  choice.val = e2.attribute(QStringLiteral("value"));
196  for (QDomElement e3 = e2.firstChildElement(); !e3.isNull(); e3 = e3.nextSiblingElement()) {
197  if (e3.tagName() == QLatin1String("label")) {
198  choice.label = e3.text();
199  choice.context = e3.attribute(QStringLiteral("context"));
200  }
201  if (e3.tagName() == QLatin1String("tooltip")) {
202  choice.toolTip = e3.text();
203  choice.context = e3.attribute(QStringLiteral("context"));
204  }
205  if (e3.tagName() == QLatin1String("whatsthis")) {
206  choice.whatsThis = e3.text();
207  choice.context = e3.attribute(QStringLiteral("context"));
208  }
209  }
210  chlist.append(choice);
211  }
212 
213  QString name = e.attribute(QStringLiteral("name"));
214  QString prefix = e.attribute(QStringLiteral("prefix"));
215 
216  readEntry.choices = CfgEntry::Choices(chlist, name, prefix);
217 }
218 
219 void KConfigXmlParser::readGroupElements(CfgEntry &readEntry, const QDomElement &element)
220 {
221  for (QDomElement e = element.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
222  QString tag = e.tagName();
223  if (tag == QLatin1String("label")) {
224  readEntry.label = e.text();
225  readEntry.labelContext = e.attribute(QStringLiteral("context"));
226  } else if (tag == QLatin1String("tooltip")) {
227  readEntry.toolTip = e.text();
228  readEntry.toolTipContext = e.attribute(QStringLiteral("context"));
229  } else if (tag == QLatin1String("whatsthis")) {
230  readEntry.whatsThis = e.text();
231  readEntry.whatsThisContext = e.attribute(QStringLiteral("context"));
232  } else if (tag == QLatin1String("min")) {
233  readEntry.min = e.text();
234  } else if (tag == QLatin1String("max")) {
235  readEntry.max = e.text();
236  } else if (tag == QLatin1String("code")) {
237  readEntry.code = e.text();
238  } else if (tag == QLatin1String("parameter")) {
239  readParameterFromEntry(readEntry, e);
240  } else if (tag == QLatin1String("default")) {
241  if (e.attribute(QStringLiteral("param")).isEmpty()) {
242  readEntry.defaultValue = e.text();
243  }
244  } else if (tag == QLatin1String("choices")) {
245  readChoicesFromEntry(readEntry, e);
246  } else if (tag == QLatin1String("emit")) {
247  Signal signal;
248  signal.name = e.attribute(QStringLiteral("signal"));
249  readEntry.signalList.append(signal);
250  }
251  }
252 }
253 
254 void KConfigXmlParser::createChangedSignal(CfgEntry &readEntry)
255 {
256  if (cfg.generateProperties && (cfg.allMutators || cfg.mutators.contains(readEntry.name))) {
257  Signal s;
258  s.name = changeSignalName(readEntry.name);
259  s.modify = true;
260  readEntry.signalList.append(s);
261  }
262 }
263 
264 void KConfigXmlParser::validateNameAndKey(CfgEntry &readEntry, const QDomElement &element)
265 {
266  bool nameIsEmpty = readEntry.name.isEmpty();
267  if (nameIsEmpty && readEntry.key.isEmpty()) {
268  std::cerr << "Entry must have a name or a key: " << qPrintable(dumpNode(element)) << std::endl;
269  exit (1);
270  }
271 
272  if (readEntry.key.isEmpty()) {
273  readEntry.key = readEntry.name;
274  }
275 
276  if (nameIsEmpty) {
277  readEntry.name = readEntry.key;
278  readEntry.name.remove(QLatin1Char(' '));
279  } else if (readEntry.name.contains(QLatin1Char(' '))) {
280  std::cout << "Entry '" << qPrintable(readEntry.name) << "' contains spaces! <name> elements can not contain spaces!" << std::endl;
281  readEntry.name.remove(QLatin1Char(' '));
282  }
283 
284  if (readEntry.name.contains(QStringLiteral("$("))) {
285  if (readEntry.param.isEmpty()) {
286  std::cerr << "Name may not be parameterized: " << qPrintable(readEntry.name) << std::endl;
287  exit (1);
288  }
289  } else {
290  if (!readEntry.param.isEmpty()) {
291  std::cerr << "Name must contain '$(" << qPrintable(readEntry.param) << ")': " << qPrintable(readEntry.name) << std::endl;
292  exit (1);
293  }
294  }
295 }
296 
297 void KConfigXmlParser::readParamDefaultValues(CfgEntry &readEntry, const QDomElement &element)
298 {
299  if (readEntry.param.isEmpty()) {
300  return;
301  }
302  // Adjust name
303  readEntry.paramName = readEntry.name;
304 
305  readEntry.name.remove(QStringLiteral("$(") + readEntry.param + QLatin1Char(')'));
306  // Lookup defaults for indexed entries
307  for (int i = 0; i <= readEntry.paramMax; i++) {
308  readEntry.paramDefaultValues.append(QString());
309  }
310 
311  for (QDomElement e = element.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
312  QString tag = e.tagName();
313  if (tag != QLatin1String("default")) {
314  continue;
315  }
316  QString index = e.attribute(QStringLiteral("param"));
317  if (index.isEmpty()) {
318  continue;
319  }
320 
321  bool ok;
322  int i = index.toInt(&ok);
323  if (!ok) {
324  i = readEntry.paramValues.indexOf(index);
325  if (i == -1) {
326  std::cerr << "Index '" << qPrintable(index) << "' for default value is unknown." << std::endl;
327  exit (1);
328  }
329  }
330 
331  if ((i < 0) || (i > readEntry.paramMax)) {
332  std::cerr << "Index '" << i << "' for default value is out of range [0, " << readEntry.paramMax << "]." << std::endl;
333  exit (1);
334  }
335 
336  QString tmpDefaultValue = e.text();
337 
338  if (e.attribute(QStringLiteral("code")) != QLatin1String("true")) {
339  preProcessDefault(tmpDefaultValue, readEntry.name, readEntry.type, readEntry.choices, readEntry.code, cfg);
340  }
341 
342  readEntry.paramDefaultValues[i] = tmpDefaultValue;
343  }
344 }
345 
346 CfgEntry *KConfigXmlParser::parseEntry(const QString &group, const QString &parentGroup, const QDomElement &element)
347 {
348  CfgEntry readEntry;
349  readEntry.type = element.attribute(QStringLiteral("type"));
350  readEntry.name = element.attribute(QStringLiteral("name"));
351  readEntry.key = element.attribute(QStringLiteral("key"));
352  readEntry.hidden = element.attribute(QStringLiteral("hidden")) == QLatin1String("true");;
353  readEntry.group = group;
354  readEntry.parentGroup = parentGroup;
355 
356  const bool nameIsEmpty = readEntry.name.isEmpty();
357 
358  readGroupElements(readEntry, element);
359 
360  validateNameAndKey(readEntry, element);
361 
362  if (readEntry.label.isEmpty()) {
363  readEntry.label = readEntry.key;
364  }
365 
366  if (readEntry.type.isEmpty()) {
367  readEntry.type = QStringLiteral("String"); // XXX : implicit type might be bad
368  }
369 
370  readParamDefaultValues(readEntry, element);
371 
372  if (!mValidNameRegexp.match(readEntry.name).hasMatch()) {
373  if (nameIsEmpty)
374  std::cerr << "The key '" << qPrintable(readEntry.key) << "' can not be used as name for the entry because "
375  "it is not a valid name. You need to specify a valid name for this entry." << std::endl;
376  else {
377  std::cerr << "The name '" << qPrintable(readEntry.name) << "' is not a valid name for an entry." << std::endl;
378  }
379  exit (1);
380  }
381 
382  if (mAllNames.contains(readEntry.name)) {
383  if (nameIsEmpty)
384  std::cerr << "The key '" << qPrintable(readEntry.key) << "' can not be used as name for the entry because "
385  "it does not result in a unique name. You need to specify a unique name for this entry." << std::endl;
386  else {
387  std::cerr << "The name '" << qPrintable(readEntry.name) << "' is not unique." << std::endl;
388  }
389  exit (1);
390  }
391 
392  mAllNames.append(readEntry.name);
393 
394  if (!hasDefaultCode(readEntry, element)) {
395  // TODO: Move all the options to CfgEntry.
396  preProcessDefault(readEntry.defaultValue, readEntry.name, readEntry.type, readEntry.choices, readEntry.code, cfg);
397  }
398 
399  // TODO: Try to Just return the CfgEntry we populated instead of
400  // creating another one to fill the code.
401  CfgEntry *result = new CfgEntry();
402  result->group = readEntry.group;
403  result->parentGroup = readEntry.parentGroup;
404  result->type = readEntry.type;
405  result->key = readEntry.key;
406  result->name = readEntry.name;
407  result->labelContext = readEntry.labelContext;
408  result->label = readEntry.label;
409  result->toolTipContext = readEntry.toolTipContext;
410  result->toolTip = readEntry.toolTip;
411  result->whatsThisContext = readEntry.whatsThisContext;
412  result->whatsThis = readEntry.whatsThis;
413  result->code = readEntry.code;
414  result->defaultValue = readEntry.defaultValue;
415  result->choices = readEntry.choices;
416  result->signalList = readEntry.signalList;
417  result->hidden = readEntry.hidden;
418 
419  if (!readEntry.param.isEmpty()) {
420  result->param = readEntry.param;
421  result->paramName = readEntry.paramName;
422  result->paramType = readEntry.paramType;
423  result->paramValues = readEntry.paramValues;
424  result->paramDefaultValues = readEntry.paramDefaultValues;
425  result->paramMax = readEntry.paramMax;
426  }
427  result->min = readEntry.min;
428  result->max = readEntry.max;
429  createChangedSignal(*result);
430 
431  return result;
432 }
433 
434 // TODO: Change the name of the config variable.
435 KConfigXmlParser::KConfigXmlParser(const KConfigParameters &cfg, const QString &inputFileName)
436  : cfg(cfg), mInputFileName(inputFileName)
437 {
438  mValidNameRegexp.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("[a-zA-Z_][a-zA-Z0-9_]*")));
439 }
440 
441 void KConfigXmlParser::start()
442 {
443  QFile input(mInputFileName);
444  QDomDocument doc;
445  QString errorMsg;
446  int errorRow;
447  int errorCol;
448  if (!doc.setContent(&input, &errorMsg, &errorRow, &errorCol)) {
449  std::cerr << "Unable to load document." << std::endl;
450  std::cerr << "Parse error in " << qPrintable(mInputFileName) << ", line " << errorRow << ", col " << errorCol << ": " << qPrintable(errorMsg) << std::endl;
451  exit (1);
452  }
453 
454  QDomElement cfgElement = doc.documentElement();
455  if (cfgElement.isNull()) {
456  std::cerr << "No document in kcfg file" << std::endl;
457  exit (1);
458  }
459 
460  for (QDomElement element = cfgElement.firstChildElement(); !element.isNull(); element = element.nextSiblingElement()) {
461  QString tag = element.tagName();
462 
463  if (tag == QLatin1String("include")) {
464  readIncludeTag(element);
465  } else if (tag == QLatin1String("kcfgfile")) {
466  readKcfgfileTag(element);
467  } else if (tag == QLatin1String("group")) {
468  readGroupTag(element);
469  } else if (tag == QLatin1String("signal")) {
470  readSignalTag(element);
471  }
472  }
473 }
474 
475 ParseResult KConfigXmlParser::getParseResult() const
476 {
477  return mParseResult;
478 }
479 
480 void KConfigXmlParser::readIncludeTag(const QDomElement &e)
481 {
482  QString includeFile = e.text();
483  if (!includeFile.isEmpty()) {
484  mParseResult.includes.append(includeFile);
485  }
486 }
487 
488 void KConfigXmlParser::readGroupTag(const QDomElement &e)
489 {
490  QString group = e.attribute(QStringLiteral("name"));
491  if (group.isEmpty()) {
492  std::cerr << "Group without name" << std::endl;
493  exit (1);
494  }
495 
496  const QString parentGroup = e.attribute(QStringLiteral("parentGroupName"));
497 
498  for (QDomElement e2 = e.firstChildElement(); !e2.isNull(); e2 = e2.nextSiblingElement()) {
499  if (e2.tagName() != QLatin1String("entry")) {
500  continue;
501  }
502  CfgEntry *entry = parseEntry(group, parentGroup, e2);
503  if (entry) {
504  mParseResult.entries.append(entry);
505  } else {
506  std::cerr << "Can not parse entry." << std::endl;
507  exit (1);
508  }
509  }
510 }
511 
512 void KConfigXmlParser::readKcfgfileTag(const QDomElement &e)
513 {
514  mParseResult.cfgFileName = e.attribute(QStringLiteral("name"));
515  mParseResult.cfgFileNameArg = e.attribute(QStringLiteral("arg")).toLower() == QLatin1String("true");
516  for (QDomElement e2 = e.firstChildElement(); !e2.isNull(); e2 = e2.nextSiblingElement()) {
517  if (e2.tagName() == QLatin1String("parameter")) {
518  Param p;
519  p.name = e2.attribute(QStringLiteral("name"));
520  p.type = e2.attribute(QStringLiteral("type"));
521  if (p.type.isEmpty()) {
522  p.type = QStringLiteral("String");
523  }
524  mParseResult.parameters.append(p);
525  }
526  }
527 }
528 
529 void KConfigXmlParser::readSignalTag(const QDomElement &e)
530 {
531  QString signalName = e.attribute(QStringLiteral("name"));
532  if (signalName.isEmpty()) {
533  std::cerr << "Signal without name." << std::endl;
534  exit (1);
535  }
536  Signal theSignal;
537  theSignal.name = signalName;
538 
539  for (QDomElement e2 = e.firstChildElement(); !e2.isNull(); e2 = e2.nextSiblingElement()) {
540  if (e2.tagName() == QLatin1String("argument")) {
541  Param argument;
542  argument.type = e2.attribute(QStringLiteral("type"));
543  if (argument.type.isEmpty()) {
544  std::cerr << "Signal argument without type." << std::endl;
545  exit (1);
546  }
547  argument.name= e2.text();
548  theSignal.arguments.append(argument);
549  } else if (e2.tagName() == QLatin1String("label")) {
550  theSignal.label = e2.text();
551  }
552  }
553 
554  mParseResult.signalList.append(theSignal);
555 }
KGuiItem defaults()
QString & append(QChar ch)
QString attribute(const QString &name, const QString &defValue) const const
QString & prepend(QChar ch)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QString simplified() const const
QDomElement nextSiblingElement(const QString &tagName) const const
QDomElement documentElement() const const
void append(const T &value)
QString text() const const
int toInt(bool *ok, int base) const const
QString anchoredPattern(const QString &expression)
bool isEmpty() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toLower() const const
bool isNull() const const
void save(QTextStream &stream, int indent, QDomNode::EncodingPolicy encodingPolicy) const const
QDomElement firstChildElement(const QString &tagName) const const
typedef ConstIterator
int length() const const
QString left(int n) const const
QString tagName() const const
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() const const
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
Configuration Compiler Configuration.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Oct 24 2020 22:53:32 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.