NCL Composer  0.1.5
 All Classes Functions Variables Pages
NCLDocumentParser.cpp
1 /* Copyright (c) 2011 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include "NCLDocumentParser.h"
11 
12 namespace composer {
13 namespace language {
14 
15 NCLDocumentParser::NCLDocumentParser(Project *project)
16 {
17  this->project = project;
18 }
19 
20 NCLDocumentParser::~NCLDocumentParser()
21 {
22 
23 }
24 
26 {
27  return "NCLDocumentParser";
28 }
29 
31 {
32  QString uri = project->getLocation();
33 
34  QFile *file = new QFile(uri, this);
35  if (!file->open(QIODevice::ReadWrite)) {
36  qDebug() << "DocumentParser::parseDocument"
37  << tr("Could not open file %1\n").arg(uri);
38  return false;
39  }
40 
41  file->close();
42 
43  QXmlInputSource inputSource(file);
44  QXmlSimpleReader reader;
45  reader.setContentHandler(this);
46  reader.setErrorHandler(this);
47 
48  return reader.parse(inputSource);
49 }
50 
51 bool NCLDocumentParser::parseContent(const QString &str)
52 {
53  QXmlInputSource inputSource;
54  inputSource.setData(str);
55 
56  QXmlSimpleReader reader;
57  reader.setContentHandler(this);
58  reader.setErrorHandler(this);
59 
60  return reader.parse(inputSource);
61 }
62 
63 bool NCLDocumentParser::startElement(const QString &,
64  const QString &,
65  const QString &qName,
66  const QXmlAttributes &attributes)
67 {
68  QMap<QString,QString> atts;
69  QString parentId = project->getUniqueId();
70 
71  if (qName != "ncl")
72  {
73  if(elementStack.size())
74  {
75  lockStack.lock();
76  Entity *parentEntity = elementStack.top();
77  lockStack.unlock();
78  parentId = parentEntity->getUniqueId();
79  }
80  }
81 
82  for (int i=0 ;i < attributes.count(); i++)
83  atts[attributes.qName(i)] = attributes.value(i);
84 
85  emit addEntity(qName, parentId, atts, false);
86 
87  return true;
88 }
89 
90 bool NCLDocumentParser::endElement(const QString &namespaceURI,
91  const QString &localName,
92  const QString &qName)
93 {
94  lockStack.lock();
95  if(elementStack.size())
96  elementStack.pop();
97  lockStack.unlock();
98 
99  return true;
100 }
101 
102 bool NCLDocumentParser::characters(const QString &)
103 {
104  return true;
105 }
106 
107 bool NCLDocumentParser::endDocument()
108 {
109  emit parseFinished();
110  return true;
111 }
112 
113 bool NCLDocumentParser::fatalError(const QXmlParseException &exception)
114 {
115  qDebug() << "Fatal error on line" << exception.lineNumber()
116  << ", column" << exception.columnNumber() << ":"
117  << exception.message();
118 
119  return false;
120 }
121 
122 void NCLDocumentParser::onEntityAdded(QString , Entity *entity)
123 {
124  lockStack.lock();
125  elementStack.push(entity);
126  lockStack.unlock();
127 }
128 
129 void NCLDocumentParser::onEntityAddError(QString error)
130 {
131  qDebug() << "NCLDocumentParser::onEntityAddError(" << error << ")";
132 }
133 
134 } } //end namespace