summaryrefslogtreecommitdiffstats
path: root/src/xml_to_data/xml_to_data.cpp
blob: 65dc4632ceea8bc45a85aee20dea1a842d13e21b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "xml_to_data.h"

#include <tqfile.h>
#include <tqtextstream.h>

TQDomElement XmlToData::findUniqueElement(TQDomElement tqparent, const TQString &tag,
                                         const TQString &attribute, const TQString &value) const
{
  TQDomElement element;
  TQDomNode child = tqparent.firstChild();
  while ( !child.isNull() ) {
    if ( child.nodeName()==tag && child.isElement()
         && (attribute.isEmpty() || child.toElement().attribute(attribute)==value) ) {
      if ( !element.isNull() ) qFatal(TQString("Duplicated element \"%1/%2\"").tqarg(tag).tqarg(value));
      element = child.toElement();
    }
    child = child.nextSibling();
  }
  return element;
}

void XmlToData::checkTagNames(TQDomElement element, const TQString &tag,
                              const TQStringList &names) const
{
  TQDomNodeList list = element.elementsByTagName(tag);
  for (uint i=0; i<uint(list.count()); i++) {
    if ( !list.item(i).isElement() ) continue;
    TQString name = list.item(i).toElement().attribute("name");
    if ( names.tqfind(name)==names.end() ) qFatal(TQString("Illegal name %1 for %2 element").tqarg(name).tqarg(tag));
  }
}

TQDomDocument XmlToData::parseFile(const TQString &filename) const
{
  qDebug("Parsing XML file \"%s\"...", filename.latin1());
  TQFile file(filename);
  if ( !file.open(IO_ReadOnly) ) qFatal("Cannot open file!");
  TQDomDocument doc;
  TQString error;
  int errorLine, errorColumn;
  if ( !doc.setContent(&file, false, &error, &errorLine, &errorColumn) )
    qFatal(TQString("Error parsing XML file (%1 at line %2, column %3)").tqarg(error).tqarg(errorLine).tqarg(errorColumn));
  return doc;
}

void XmlToData::warning(const TQString &message) const
{
  if ( currentDevice().isEmpty() ) ::qWarning("Warning: %s", message.latin1());
  else ::qWarning("Warning [%s]: %s", currentDevice().latin1(), message.latin1());
}
void XmlToData::qFatal(const TQString &message) const
{
  if ( currentDevice().isEmpty() ) ::qFatal("Fatal: %s", message.latin1());
  else ::qFatal("Fatal [%s]: %s", currentDevice().latin1(), message.latin1());
}

void XmlToData::process()
{
  parse();
  qDebug("Parsing XML successful.");
  output();
  qDebug("Output written.");
}