10 #include "modules/ProjectReader.h"
15 ProjectReader::ProjectReader()
20 ProjectReader::~ProjectReader()
25 Project *ProjectReader::readFile(QString location)
31 if(!file.open(QIODevice::ReadOnly))
33 qDebug() <<
"ERROR: Could not open the file " << location;
37 QByteArray data = file.readAll();
38 QString content(qUncompress(data));
40 QString startCpModelStr =
"#COMPOSER_MODEL#\n";
41 QString endCpModelStr =
"\n#END_COMPOSER_MODEL#";
43 int startCpModel = content.indexOf(startCpModelStr)+startCpModelStr.size();
44 int endCpModel = content.indexOf(endCpModelStr);
51 QString modelStr = content.mid(startCpModel, endCpModel-startCpModel);
52 parseModelString(modelStr);
57 QString pluginsData = content.mid(endCpModel + endCpModelStr.size());
58 QString startPluginDataStr =
"#COMPOSER_PLUGIN_DATA ";
59 QString endPluginDataStr =
"\n#END_COMPOSER_PLUGIN_DATA#";
62 while(pos >= 0 && pos < pluginsData.size())
64 int startPluginData = pluginsData.indexOf(startPluginDataStr, pos);
65 int endStartPluginData =
66 pluginsData.indexOf(
"#\n", startPluginData
67 + startPluginDataStr.size());
69 QString pluginID = pluginsData.mid(
70 startPluginData + startPluginDataStr.size(),
71 endStartPluginData- (startPluginData + startPluginDataStr.size())
74 int endPluginData = pluginsData.indexOf( endPluginDataStr,
78 QString data = pluginsData.mid( endStartPluginData+2,
79 endPluginData-endStartPluginData-2);
81 pos = endPluginData + endPluginDataStr.size() + 1;
88 qDebug() <<
"ERROR: File is corrupted " << location;
95 bool ProjectReader::parseModelString(
const QString &str)
97 QXmlInputSource inputSource;
98 inputSource.setData(str);
100 QXmlSimpleReader reader;
101 reader.setContentHandler(
this);
102 reader.setErrorHandler(
this);
104 return reader.parse(inputSource);
107 bool ProjectReader::startElement(
const QString &namespaceURI,
108 const QString &localName,
109 const QString &qName,
110 const QXmlAttributes &attributes)
112 QMap<QString,QString> atts;
113 QString parentId = project->getUniqueId();
114 QString uniqueId =
"";
116 Entity *parentEntity = NULL;
117 if (qName !=
"document")
120 parentEntity = elementStack.top();
124 for (
int i=0; i < attributes.count(); i++)
126 if(attributes.qName(i) !=
"uniqueEntityId")
127 atts[attributes.qName(i)] = attributes.value(i);
129 uniqueId = attributes.value(i);
133 if(qName !=
"document" && parentEntity != NULL)
136 qDebug() <<
"trying to add an entity whithout an uniqueId";
139 entity =
new Entity(uniqueId, qName, atts, project);
140 project->
addEntity(entity, parentEntity->getUniqueId());
147 elementStack.push(entity);
152 bool ProjectReader::endElement(
const QString &namespaceURI,
153 const QString &localName,
154 const QString &qName)
157 if(elementStack.size())
164 bool ProjectReader::characters(
const QString &str)
169 bool ProjectReader::fatalError(
const QXmlParseException &exception)
171 qDebug() <<
"Fatal error on line" << exception.lineNumber()
172 <<
", column" << exception.columnNumber() <<
":"
173 << exception.message();