Fréchet View  1.6.0
A Tool for Exploring Fréchet Distance Algorithms
inputreader.cpp
Go to the documentation of this file.
1 
2 #include <inputreader.h>
3 
4 #ifdef HAVE_QUAZIP
5 #include <quazip.h>
6 #endif
7 #include <QXmlQuery>
8 #include <QXmlResultItems>
9 #include <QDebug>
10 #include <QBuffer>
11 #include <QXmlSerializer>
12 #include <QXmlFormatter>
13 #include <QAbstractXmlNodeModel>
14 
15 //#include <QScriptEngine>
16 
17 using namespace frechet;
18 using namespace input;
19 
21  results.clear();
22  error_message.clear();
23  is_svg = is_ipe = is_kml = is_script = false;
24 }
25 
26 void InputReader::readAll(const DataPath &path)
27 {
28  QString fileSuffix = path.getFile().suffix().toLower();
29  error_message = "";
30  if (InputReader::isZipFile(fileSuffix)) {
31  error_message = "zip not yet supported";
32 #ifdef HAVE_QUAZIP
33  // navigate into zip file
34  QuaZip zip(path.getFile().absoluteFilePath());
35  zip.open(QuaZip::Mode::mdUnzip);
36  zip.setCurrentFile(path.getArchFile().absolutePath());
37  // TODO if path is empty, and there is exactly one zip file, use it !
38  readAll(zip.getIoDevice(), path.getArchFile().suffix().toLower(), path.getSelector());
39 #endif
40  }
41  else
42  if (InputReader::isGzipFile(fileSuffix)) {
43  // uncompress gzip file
44  error_message = "gzip not yet supported";
45  }
46  else {
47  QString filePath = path.getFile().absoluteFilePath();
48  QFile input(filePath);
49  if(! input.open(QIODevice::ReadOnly))
50  qDebug() << input.errorString() << "\n";
51 
52  readAll(&input, fileSuffix, path.getSelector());
53  }
54 }
55 
56 void InputReader::readAll(QIODevice* device, QString fileSuffix, QString queryString)
57 {
58  if (isXmlFile(fileSuffix))
59  {
60  readXml(device,fileSuffix,queryString);
61  }
62  else if (isScriptFile(fileSuffix))
63  {
64  QTextStream stream(device);
65  QString program = stream.readAll();
66  device->close();
67 
68  is_script = true;
69  readScript(program);
70  }
71  else if (isCsvFile(fileSuffix))
72  {
73  // read CSV file
74  error_message = "csv not yet supported";
75  }
76 }
77 
78 void InputReader::readXml(QIODevice* device, QString fileSuffix, QString queryString)
79 {
80  // navigate into xml file
81  // make a XPath query
82  QXmlQuery query;
83  QXmlResultItems xmlitems;
84 #ifdef DEBUG_XML
85  XmlMessageHandler handler;
86  query.setMessageHandler(&handler);
87 #endif
88  if(! query.setFocus(device))
89  throw "invalid IO device\n";
90 
91  is_svg = isSvgFile(fileSuffix);
92  is_ipe = isIpeFile(fileSuffix);
93 
94  if (queryString.isEmpty()) {
95  if (is_svg)
96  queryString = DataPath::SVG_QUERY;
97  else if (is_ipe)
98  queryString = DataPath::IPE_QUERY;
99  else
100  throw "query string for type "+fileSuffix+" expected";
101  }
102 
103  query.setQuery(queryString);
104  if(! query.isValid())
105  throw "invalid query\n";
106  // if xmlPath is empty, use default paths for known file types (ipe,svg,kml)
107  query.evaluateTo(&xmlitems);
108 
109  // parse text data to polygons
110  for(QXmlItem item=xmlitems.next(); ! item.isNull(); item=xmlitems.next()) {
111  if (xmlitems.hasError())
112  throw "Error\n";
113 
114  if (item.isAtomicValue()) {
115  QVariant value = item.toAtomicValue();
116  if (is_ipe)
117  results.append(readIpePath(value.toString()));
118  else
119  results.append(readSvgPath(value.toString()));
120  // else ?
121  //qDebug() << item.toAtomicValue() << "\n";
122  }
123  else if (item.isNode()) {
124  QXmlNodeModelIndex nmi = item.toNodeModelIndex();
125  //qDebug() << nmi << "\n";
126  QTransform tf = readTransformationMatrix(item, query.namePool());
127  QString text = nmi.model()->stringValue(nmi);
128  if (is_ipe)
129  results.append(readIpePath(text,tf));
130  else
131  results.append(readSvgPath(text,tf));
132  //qDebug() << text << "\n";
133  // TODO ipe: <path matrix="2x3"> transformation matrix
134  }
135  }
136 }
137 
138 QTransform InputReader::readTransformationMatrix(QXmlItem item, QXmlNamePool namepool)
139 {
140  // IMPORTANT
141  // name pool must be inherited from parent query !!!!!
142  QXmlQuery query(namepool);
143 
144  query.setFocus(item);
145  query.setQuery("@matrix/string()");
146  if(! query.isValid())
147  throw "invalid query\n";
148 
149  QString value;
150  query.evaluateTo(&value);
151 
152  QStringList values = value.split(' ');
153  if (values.length()==6)
154  return QTransform(
155  values[0].toDouble(), values[1].toDouble(),
156  values[2].toDouble(), values[3].toDouble(),
157  values[4].toDouble(), values[5].toDouble());
158  else
159  return QTransform();
160 }
161 
162 Path InputReader::readSvgPath(QString pathData, QTransform tf)
163 {
164  Path path = Path::svgPath(pathData);
165  path.map(tf);
166  return path;
167 }
168 
169 Path InputReader::readIpePath(QString pathData, QTransform tf)
170 {
171  Path path = Path::ipePath(pathData);
172  path.map(tf);
173  return path;
174 }
175 
176 bool InputReader::isZipFile(QString suffix) {
177  return (suffix=="zip") || (suffix=="kmz");
178 }
179 
180 bool InputReader::isGzipFile(QString suffix) {
181  return suffix=="gz" || suffix=="svgz";
182 }
183 
184 bool InputReader::isXmlFile(QString suffix) {
185  return (suffix=="xml") || (suffix=="svg") || (suffix=="ipe") || (suffix=="kml");
186 }
187 
188 bool InputReader::isCsvFile(QString suffix) {
189  return (suffix=="csv");
190 }
191 
192 bool InputReader::isSvgFile(QString suffix) {
193  return (suffix=="svg") || (suffix=="svgz");
194 }
195 
196 bool InputReader::isIpeFile(QString suffix) {
197  return (suffix=="ipe");
198 }
199 
200 bool InputReader::isScriptFile(QString suffix) {
201  return (suffix=="js") || (suffix=="jscript");
202 }
203 
205  const QString &description,
206  const QUrl&,
207  const QSourceLocation&)
208 {
209  //qDebug() << description << "\n";
210 }
211  // namespace frechet
QFileInfo getArchFile() const
Definition: datapath.h:49
handles XML parse errors; used for debugging only
Definition: inputreader.h:113
static bool isXmlFile(QString suffix)
location of input data in an XML file
Definition: datapath.h:24
Path readSvgPath(QString text, QTransform tf=QTransform())
read a polygonal curve from an SVG string
static Path svgPath(QString str, bool forceAbsolute=false)
static constructor from SVG input string
Definition: path.cpp:33
global definitions for all algorithms.
void readAll(const DataPath &path)
read polygonal curves from a file
Definition: inputreader.cpp:26
static const QString IPE_QUERY
default xml path for ipe files
Definition: datapath.h:39
static Path ipePath(QString str)
static constructor from IPE input string
Definition: path.cpp:40
bool readScript(QString program)
read a JavaScript file
Definition: scriptinput.cpp:12
bool is_svg
is it an SVG file?
Definition: inputreader.h:39
Path * map(QTransform tf)
apply a transformation to all vertexes
Definition: path.cpp:121
QTransform readTransformationMatrix(QXmlItem item, QXmlNamePool namepool)
read a transformation matrix from XML
static bool isSvgFile(QString suffix)
static bool isGzipFile(QString suffix)
static bool isZipFile(QString suffix)
static bool isScriptFile(QString suffix)
QString getSelector() const
Definition: datapath.h:51
bool is_kml
not implemented
Definition: inputreader.h:41
Represents a polygonal curve.
Definition: path.h:30
QFileInfo getFile() const
Definition: datapath.h:47
void handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation)
QVector< Path > results
result values; expected to contain 2 result
Definition: inputreader.h:36
bool is_ipe
is it an IPE file?
Definition: inputreader.h:40
static const QString SVG_QUERY
default xml path for svg files (= all path elements and the attribute "d")
Definition: datapath.h:37
QString error_message
parse error
Definition: inputreader.h:38
bool is_script
is it a JavaScript file
Definition: inputreader.h:42
Path readIpePath(QString text, QTransform tf=QTransform())
read a polygonal curve from an IPE string
static bool isIpeFile(QString suffix)
void clear()
clear results
Definition: inputreader.cpp:20
void readXml(QIODevice *device, QString fileSuffix, QString xmlPath)
read polygonal curves from an XML file
Definition: inputreader.cpp:78
static bool isCsvFile(QString suffix)