arcslibmakerparser.cpp
1 /*
2  name: tools/libmaker/arcslibmakerparser.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include "arcslibmakerparser.h"
30 #include <QFile>
31 #include <QTextStream>
32 #include <iostream>
33 
34 
35 ARCSLibMakerParser::ARCSLibMakerParser()
36 {
37 
38 }
39 
40 void ARCSLibMakerParser::clear()
41 {
42  headers.clear();
43  components.clear();
44  families.clear();
45  types.clear();
46  doc.clear();
47 }
48 
49 
50 bool ARCSLibMakerParser::parseFile()
51 {
52  if (fileName.isEmpty())
53  return false;
54 
55  QFile file(fileName);
56  if (!file.open(QIODevice::ReadOnly))
57  return false;
58 
59  QString error;
60  int line, col;
61 
62 
63 
64  if (!doc.setContent(&file, &error, &line, &col))
65  {
66  std::cerr << "[ARCS libmaker] Error in parsing xml file" << std::endl;
67  std::cerr << "" << "[" << line << "," << col << "] "
68  << qPrintable(error) << std::endl;
69  file.close();
70  return false;
71  }
72  file.close();
73 
74  parseHeaders();
75  parseComponents();
76  parseFamilies();
77  parseTypes();
78 
79  return true;
80 }
81 
82 
83 bool ARCSLibMakerParser::writeFile()
84 {
85  if (fileName.isEmpty())
86  return false;
87 
88  QFile data(fileName);
89 
90  if (data.open(QFile::WriteOnly | QFile::Truncate))
91  {
92  QTextStream out(&data);
93  doc.save(out, 4);
94  return true;
95  }
96 
97  return false;
98 }
99 
100 
101 void ARCSLibMakerParser::parseSection(QString secname,QStringList & strlist)
102 {
103  if (doc.isNull())
104  return ;
105 
106  QDomNodeList lst = doc.elementsByTagName(secname);
107 
108  for (unsigned int i = 0; i < lst.length(); i++)
109  {
110  QDomNode node = lst.item(i).attributes().namedItem("name");
111  if (node.isAttr())
112  strlist << node.nodeValue();
113  }
114 }
115 
116 
117 void ARCSLibMakerParser::parseTypes()
118 {
119  if (doc.isNull())
120  return ;
121 
122  QDomNodeList lst = doc.elementsByTagName("type");
123 
124  for (unsigned int i = 0; i < lst.length(); i++)
125  {
126  QDomNode nodeName = lst.item(i).attributes().namedItem("name");
127  QDomNode nodeWrapper = lst.item(i).attributes().namedItem("wrapper");
128  if (nodeName.isAttr() && nodeWrapper.isAttr())
129  {
130  types << ARCSTypeWrapper(nodeName.nodeValue(), nodeWrapper.nodeValue()) ;
131  std::cout << qPrintable(nodeName.nodeValue()) << ", " << qPrintable(nodeName.nodeValue()) << std::endl;
132  }
133  else
134  {
135  types << ARCSTypeWrapper(nodeName.nodeValue());
136  std::cout << qPrintable(nodeName.nodeValue()) << std::endl;
137  }
138 
139 
140  }
141 }