NCL Composer  0.1.5
 All Classes Functions Variables Pages
ProjectReader.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 "modules/ProjectReader.h"
11 
12 namespace composer {
13  namespace core {
14 
15 ProjectReader::ProjectReader()
16 {
17 
18 }
19 
20 ProjectReader::~ProjectReader()
21 {
22 
23 }
24 
25 Project *ProjectReader::readFile(QString location)
26 {
27  QFile file(location);
28  bool error = false;
29  project = NULL;
30 
31  if(!file.open(QIODevice::ReadOnly))
32  {
33  qDebug() << "ERROR: Could not open the file " << location;
34  error = true;
35  }
36 
37  QByteArray data = file.readAll();
38  QString content(qUncompress(data));
39  /* READING MODEL */
40  QString startCpModelStr = "#COMPOSER_MODEL#\n";
41  QString endCpModelStr = "\n#END_COMPOSER_MODEL#";
42 
43  int startCpModel = content.indexOf(startCpModelStr)+startCpModelStr.size();
44  int endCpModel = content.indexOf(endCpModelStr);
45 
46  project = new Project();
47  project->setLocation(location);
48 
49  if(content != "")
50  {
51  QString modelStr = content.mid(startCpModel, endCpModel-startCpModel);
52  parseModelString(modelStr);
53  }
54  /* FINISH READING MODEL */
55 
56  /* READING PLUGIN DATA */
57  QString pluginsData = content.mid(endCpModel + endCpModelStr.size());
58  QString startPluginDataStr = "#COMPOSER_PLUGIN_DATA ";
59  QString endPluginDataStr = "\n#END_COMPOSER_PLUGIN_DATA#";
60  int pos = 0;
61 
62  while(pos >= 0 && pos < pluginsData.size())
63  {
64  int startPluginData = pluginsData.indexOf(startPluginDataStr, pos);
65  int endStartPluginData =
66  pluginsData.indexOf("#\n", startPluginData
67  + startPluginDataStr.size());
68 
69  QString pluginID = pluginsData.mid(
70  startPluginData + startPluginDataStr.size(),
71  endStartPluginData- (startPluginData + startPluginDataStr.size())
72  );
73 
74  int endPluginData = pluginsData.indexOf( endPluginDataStr,
75  startPluginData);
76 
77  // +2 and -2 because of the previously #\n
78  QString data = pluginsData.mid( endStartPluginData+2,
79  endPluginData-endStartPluginData-2);
80 
81  pos = endPluginData + endPluginDataStr.size() + 1;
82 
83  project->setPluginData(pluginID, data.toAscii());
84  }
85  /* FINISH READING MODEL */
86 
87  if(error)
88  qDebug() << "ERROR: File is corrupted " << location;
89 
90  file.close();
91 
92  return project;
93 }
94 
95 bool ProjectReader::parseModelString(const QString &str)
96 {
97  QXmlInputSource inputSource;
98  inputSource.setData(str);
99 
100  QXmlSimpleReader reader;
101  reader.setContentHandler(this);
102  reader.setErrorHandler(this);
103 
104  return reader.parse(inputSource);
105 }
106 
107 bool ProjectReader::startElement( const QString &namespaceURI,
108  const QString &localName,
109  const QString &qName,
110  const QXmlAttributes &attributes)
111 {
112  QMap<QString,QString> atts;
113  QString parentId = project->getUniqueId();
114  QString uniqueId = "";
115 
116  Entity *parentEntity = NULL;
117  if (qName != "document")
118  {
119  lockStack.lock();
120  parentEntity = elementStack.top();
121  lockStack.unlock();
122  }
123 
124  for (int i=0; i < attributes.count(); i++)
125  {
126  if(attributes.qName(i) != "uniqueEntityId")
127  atts[attributes.qName(i)] = attributes.value(i);
128  else
129  uniqueId = attributes.value(i);
130  }
131 
132  Entity *entity = NULL;
133  if(qName != "document" && parentEntity != NULL)
134  {
135  if(uniqueId == "")
136  qDebug() << "trying to add an entity whithout an uniqueId";
137  else
138  {
139  entity = new Entity(uniqueId, qName, atts, project);
140  project->addEntity(entity, parentEntity->getUniqueId());
141  }
142  }
143  else
144  entity = project;
145 
146  lockStack.lock();
147  elementStack.push(entity);
148  lockStack.unlock();
149  return true;
150 }
151 
152 bool ProjectReader::endElement( const QString &namespaceURI,
153  const QString &localName,
154  const QString &qName)
155 {
156  lockStack.lock();
157  if(elementStack.size())
158  elementStack.pop();
159  lockStack.unlock();
160 
161  return true;
162 }
163 
164 bool ProjectReader::characters(const QString &str)
165 {
166  return true;
167 }
168 
169 bool ProjectReader::fatalError(const QXmlParseException &exception)
170 {
171  qDebug() << "Fatal error on line" << exception.lineNumber()
172  << ", column" << exception.columnNumber() << ":"
173  << exception.message();
174  return false;
175 }
176 
177 } } //end namespace