TextTest.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #include "TextTest.h"
16 
17 #include "base/Debug.h"
18 #include "base/StringBits.h"
19 
20 #include <QFile>
21 #include <QXmlInputSource>
22 
23 bool
25 {
26  // Return true if the document can be opened and contains some
27  // sort of text, either UTF-8 (so it could be Turtle) or another
28  // encoding that is recognised as XML
29 
30  if (!source.isAvailable()) {
31  SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Failed to retrieve document from " << source.getLocation() << endl;
32  return false;
33  }
34 
35  QFile file(source.getLocalFilename());
36  if (!file.open(QFile::ReadOnly)) {
37  SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Failed to open local file from " << source.getLocalFilename() << endl;
38  return false;
39  }
40 
41  QByteArray bytes = file.read(200);
42 
43  if (StringBits::isValidUtf8(std::string(bytes.data()), true)) {
44  SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Document appears to be UTF-8" << endl;
45  return true; // good enough to be worth trying to parse
46  }
47 
48  QXmlInputSource xmlSource;
49  xmlSource.setData(bytes); // guesses text encoding
50 
51  if (xmlSource.data().startsWith("<?xml")) {
52  SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Document appears to be XML" << endl;
53  return true;
54  }
55 
56  SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Document is not UTF-8 and is not XML, rejecting" << endl;
57  return false;
58 }
static bool isValidUtf8(const std::string &bytes, bool isTruncated)
Return true if the given byte array contains a valid UTF-8 sequence, false if not.
Definition: StringBits.cpp:189
QString getLocation() const
Return the location filename or URL as passed to the constructor.
Definition: FileSource.cpp:616
QString getLocalFilename() const
Return the name of the local file this FileSource refers to.
Definition: FileSource.cpp:622
static bool isApparentTextDocument(FileSource)
Return true if the source appears to point to a text format of some kind (could be CSV...
Definition: TextTest.cpp:24
FileSource is a class used to refer to the contents of a file that may be either local or at a remote...
Definition: FileSource.h:59
#define SVDEBUG
Definition: Debug.h:106
bool isAvailable()
Return true if the file or remote URL exists.
Definition: FileSource.cpp:544