NCL Composer  0.1.5
 All Classes Functions Variables Pages
NCLParser.cpp
1 /*
2  * Copyright 2011 TeleMidia/PUC-Rio.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 #include "NCLParser.h"
19 
20 NCLParser::NCLParser(NCLTreeWidget *tree)
21 {
22  treeWidget = tree;
23  currentItem = 0;
24 }
25 
26 bool NCLParser::startElement(const QString & /* namespaceURI */,
27  const QString & /*localName */,
28  const QString &qName,
29  const QXmlAttributes &attributes)
30 {
31  QMap <QString, QString> attrs;
32  for(int i = 0; i < attributes.length(); i++) {
33  attrs[attributes.localName(i)] = attributes.value(i);
34  }
35 
36  if (currentItem) {
37  currentItem = treeWidget->addElement(currentItem,
38  currentItem->childCount(),
39  qName,
40  attributes.value("id"),
41  attrs,
42  locator->lineNumber(),
43  locator->columnNumber());
44  } else {
45  currentItem = treeWidget->addElement(0,
46  -1,
47  qName,
48  attributes.value("id"),
49  attrs,
50  locator->lineNumber(),
51  locator->columnNumber());
52  }
53  //TODO: send a signal to all interested plugins.
54  return true;
55 }
56 
57 bool NCLParser::characters(const QString &str)
58 {
59  currentText += str;
60  return true;
61 }
62 
63 bool NCLParser::endElement(const QString & /* namespaceURI */,
64  const QString & /* localName */,
65  const QString & /* qName */)
66 {
67  currentItem = currentItem->parent();
68  return true;
69 }
70 
71 bool NCLParser::fatalError(const QXmlParseException &exception)
72 {
73  if(currentItem != NULL) {
74  currentItem->setIcon(0, QIcon(":/images/error-icon-16.png"));
75  currentItem->setTextColor(0, QColor("#FF0000"));
76 // currentItem->setBackgroundColor(0, QColor("#FF0000"));
77  }
78 
79  qDebug() << QObject::tr("Parse error at line %1, column "
80  "%2:\n%3.")
81  .arg(exception.lineNumber())
82  .arg(exception.columnNumber())
83  .arg(exception.message());
84 
85  emit fatalErrorFound ( exception.message(),
86  QString("FILENAME"),
87  exception.lineNumber(),
88  exception.columnNumber(),
89  0);
90 
91  return false;
92 }
93 
94 void NCLParser::setDocumentLocator(QXmlLocator *locator)
95 {
96  this->locator = locator;
97 }