comparison framework/SVFileReader.cpp @ 140:9ccaa8fd9b9f

* Add code to identify (usually) the type of an XML file that contains either SV session or layer data, and use it to permit loading files with plain .xml extension that contain complete uncompression sessions
author Chris Cannam
date Thu, 20 Nov 2008 10:59:14 +0000
parents e2aec1708a2c
children 99afa5fa8d30
comparison
equal deleted inserted replaced
139:2cf711ed89e2 140:9ccaa8fd9b9f
1368 1368
1369 SVFileReaderPaneCallback::~SVFileReaderPaneCallback() 1369 SVFileReaderPaneCallback::~SVFileReaderPaneCallback()
1370 { 1370 {
1371 } 1371 }
1372 1372
1373
1374 class SVFileIdentifier : public QXmlDefaultHandler
1375 {
1376 public:
1377 SVFileIdentifier() :
1378 m_inSv(false),
1379 m_inData(false),
1380 m_type(SVFileReader::UnknownFileType)
1381 { }
1382 virtual ~SVFileIdentifier() { }
1383
1384 void parse(QXmlInputSource &source) {
1385 QXmlSimpleReader reader;
1386 reader.setContentHandler(this);
1387 reader.setErrorHandler(this);
1388 reader.parse(source);
1389 }
1390
1391 SVFileReader::FileType getType() const { return m_type; }
1392
1393 virtual bool startElement(const QString &,
1394 const QString &,
1395 const QString &qName,
1396 const QXmlAttributes& atts)
1397 {
1398 QString name = qName.toLower();
1399
1400 // SV session files have an sv element containing a data
1401 // element containing a model element with mainModel="true".
1402
1403 // If the sv element is present but the rest does not satisfy,
1404 // then it's (probably) an SV layer file.
1405
1406 // Otherwise, it's of unknown type.
1407
1408 if (name == "sv") {
1409 m_inSv = true;
1410 if (m_type == SVFileReader::UnknownFileType) {
1411 m_type = SVFileReader::SVLayerFile;
1412 }
1413 return true;
1414 } else if (name == "data") {
1415 if (!m_inSv) return true;
1416 m_inData = true;
1417 } else if (name == "model") {
1418 if (!m_inData) return true;
1419 if (atts.value("mainModel").trimmed() == "true") {
1420 if (m_type == SVFileReader::SVLayerFile) {
1421 m_type = SVFileReader::SVSessionFile;
1422 return false; // done
1423 }
1424 }
1425 }
1426 return true;
1427 }
1428
1429 virtual bool endElement(const QString &,
1430 const QString &,
1431 const QString &qName)
1432 {
1433 QString name = qName.toLower();
1434
1435 if (name == "sv") {
1436 if (m_inSv) {
1437 m_inSv = false;
1438 return false; // done
1439 }
1440 } else if (name == "data") {
1441 if (m_inData) {
1442 m_inData = false;
1443 return false; // also done, nothing after the first
1444 // data element is of use here
1445 }
1446 }
1447 return true;
1448 }
1449
1450 private:
1451 bool m_inSv;
1452 bool m_inData;
1453 SVFileReader::FileType m_type;
1454 };
1455
1456
1457 SVFileReader::FileType
1458 SVFileReader::identifyXmlFile(QString path)
1459 {
1460 QFile file(path);
1461 SVFileIdentifier identifier;
1462 QXmlInputSource source(&file);
1463 identifier.parse(source);
1464 return identifier.getType();
1465 }
1466
1467
1468