PhoenixInkscape  2.0.0
Generate multiple png files with svg inkscape files
pxml_utils.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
8 #define ALLOWED_BALISE_CHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:?!-"
9 
11 #define ALLOWED_ATTRIBUTE_CHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:-"
12 
13 #include "pxml_utils.h"
14 
16 
20 PFileParser pxml_setXmlParser(const PString & fileContent, bool isSvg){
21  PFileParser parser;
22  parser.setSeparator("<>\"=");
23  if(isSvg){
24  parser.setWhiteSpace("");
25  }else{
26  parser.setWhiteSpace("\t\n ");
27  }
28  parser.setFileContent(fileContent);
29  return parser;
30 }
31 
33 
38 bool pxml_parserFile(PXml & xml, const PPath & fileName, bool isSvg){
39  return pxml_parserContent(xml, fileName.loadFileContent(), isSvg);
40 }
41 
43 
48 bool pxml_parserContent(PXml & xml, const PString & fileContent, bool isSvg){
49  PFileParser parser(pxml_setXmlParser(fileContent, isSvg));
50  xml.setName("root");
51 
52  return pxml_parserXmlContent(xml, parser, true);
53 
54 // return pxml_parserVecXml(xml.getVecChild(), parser, isSvg);
55 }
56 
58 
62 bool pxml_isAttributeEnd(PXml & parent, PFileParser & parser){
63  if(parser.isMatch("/>")){
64  parent.setIsCompact(true);
65  return true;
66  }else if(parser.isMatch(">")){
67  parent.setIsCompact(false);
68  return true;
69  }else{
70  return false;
71  }
72 }
73 
75 
79 bool pxml_parserXmlAttribute(PXml & parent, PFileParser & parser){
80  parser.setWhiteSpace(" \t\n");
81  while(!pxml_isAttributeEnd(parent, parser) && !parser.isEndOfFile()){
82  PString attributeName(parser.getStrComposedOf(ALLOWED_ATTRIBUTE_CHAR));
83  if(attributeName == ""){
84  std::cerr << "pxml_parserXmlAttribute : error at : " << parser.getLocation() << std::endl;
85  std::cerr << "\tcannot parse the attributes name as '"<<parser.getNextToken()<<"'" << std::endl;
86  return false;
87  }
88  if(!parser.isMatch("=")){
89  std::cerr << "pxml_parserXmlAttribute : error at : " << parser.getLocation() << std::endl;
90  std::cerr << "\texpected '=' after attribute '"<<attributeName<<"'" << std::endl;
91  return false;
92  }
93  if(!parser.isMatch("\"")){
94  std::cerr << "pxml_parserXmlAttribute : error at : " << parser.getLocation() << std::endl;
95  std::cerr << "\texpected '\"' after attribute '"<<attributeName<<"='" << std::endl;
96  return false;
97  }
98  PString attributeValue(parser.getUntilKeyWithoutPatern("\""));
99  PXmlAttr tmpAttribute;
100  tmpAttribute.setName(attributeName);
101  tmpAttribute.setValue(attributeValue);
102  parent.getVecAttr().push_back(tmpAttribute);
103  }
104  parser.setWhiteSpace("");
105  return true;
106 }
107 
108 
110 
115 bool pxml_parserXmlContent(PXml & parent, PFileParser & parser, bool isMainBalise){
116  parser.setSeparator("<>\"=");
117  parser.setWhiteSpace("");
118  PString balisePartialEnd("/" + parent.getName() + ">");
119  PString baliseEnd("<" + balisePartialEnd);
120  PXml text;
121  text.setIsText(true);
122  while(!parser.isMatch(baliseEnd) && !parser.isEndOfFile()){
123  PString textString(parser.getUntilKeyWithoutPatern("<"));
124  if(textString != ""){ //If the next token is not a < it is a text
125  text.getValue() += textString;
126  if(parent.getVecChild().size() == 0lu){
127  if(parser.isMatch(balisePartialEnd)){
128  parent.setValue(text.getValue());
129  return true;
130  }else{
131  parent.getVecChild().push_back(text);
132  }
133  }else{
134  parent.getVecChild().push_back(text);
135  }
136  text.setValue(""); //Reset value for next one
137  }
138  if(parser.isEndOfFile()){
139  if(isMainBalise){
140  return true;
141  }else{
142  std::cerr << "pxml_parserXmlContent : error at : " << parser.getLocation() << std::endl;
143  std::cerr << "\tunexpected end of file. We are supposed to be in the balise '"<<parent.getName()<<"'" << std::endl;
144  return false;
145  }
146  }
147  if(parser.isMatch(balisePartialEnd)){ //We find the end of the balise
148  return true;
149  }else{ //Or it can be another balise
150  PString childBaliseName(parser.getStrComposedOf(ALLOWED_BALISE_CHAR));
151  if(childBaliseName != ""){ //We find a new balise
152  PXml xml;
153  xml.setName(childBaliseName);
154  if(childBaliseName == "!--"){
155  parser.getUntilKeyWithoutPatern("-->"); //Skip the comment
156  }else if(childBaliseName == "?xml"){
157  parser.getUntilKeyWithoutPatern("?>"); //Skip the svg balise
158  }else{
159  if(!pxml_parserXmlAttribute(xml, parser)){ //Let's parse the attribute
160  std::cerr << "pxml_parserXmlContent : error at : " << parser.getLocation() << std::endl;
161  std::cerr << "\tcannot parse the attributes of balise '"<<childBaliseName<<"'" << std::endl;
162  return false;
163  }
164  if(!xml.getIsCompact()){
165  //Let's parse the content
166  if(!pxml_parserXmlContent(xml, parser, false)){
167  std::cerr << "pxml_parserXmlContent : error at : " << parser.getLocation() << std::endl;
168  std::cerr << "\tcannot parse balise '"<<childBaliseName<<"'" << std::endl;
169  return false;
170  }
171  }
172  parent.getVecChild().push_back(xml);
173  }
174  }else if(parser.isMatch("/")){
175  std::cerr << "pxml_parserXmlContent : in balise '"<<parent.getName()<<"' unexpected '</' at : " << parser.getLocation() << std::endl;
176  return false;
177  }else{ //It was just a text <
178  text.getValue() += "<";
179  }
180  }
181  }
182  return true;
183 }
184 
185 
186 
187 
188 
189 
190 
192 
197 bool pxml_getVecChildIfExist(PVecXml & vecMatch, const PXml & xml, const PString & childName){
198  bool isFound(false);
199  const PVecXml & vecChild = xml.getVecChild();
200  for(PVecXml::const_iterator it(vecChild.begin()); it != vecChild.end(); ++it){
201  if(it->getName() == childName){
202  vecMatch.push_back(*it);
203  isFound = true;
204  }
205  }
206  return isFound;
207 }
208 
210 
215 bool pxml_getChildIfExist(PXml & match, const PXml & xml, const PString & childName){
216  bool isSearched(true);
217  const PVecXml & vecChild = xml.getVecChild();
218  PVecXml::const_iterator it(vecChild.begin());
219  while(it != vecChild.end() && isSearched){
220  if(it->getName() == childName){
221  match = *it;
222  isSearched = false;
223  }
224  ++it;
225  }
226  return !isSearched;
227 }
228 
230 
234 PXml * pxml_getChildPtr(PXml & xml, const PString & childName){
235  PXml * out = NULL;
236  PVecXml & vecChild = xml.getVecChild();
237  for(PVecXml::iterator it(vecChild.begin()); it != vecChild.end() && out == NULL; ++it){
238  if(it->getName() == childName){
239  out = &(*it);
240  }
241  }
242  return out;
243 }
244 
246 
251 bool pxml_getAttrIfExist(PXmlAttr & attr, const PXml & xml, const PString & attrName){
252  bool isSearched(true);
253  const PVecXmlAttr & vecAttr = xml.getVecAttr();
254  PVecXmlAttr::const_iterator it(vecAttr.begin());
255  while(it != vecAttr.end() && isSearched){
256  if(it->getName() == attrName){
257  attr = *it;
258  isSearched = false;
259  }
260  ++it;
261  }
262  return !isSearched;
263 }
264 
266 
270 void pxml_setAttr(PXml & xml, const PString & nameAttr, const PString & valueAttr){
271  bool isSearched(true);
272  PVecXmlAttr & vecAttr = xml.getVecAttr();
273  PVecXmlAttr::iterator it(vecAttr.begin());
274  while(it != vecAttr.end() && isSearched){
275  if(it->getName() == nameAttr){
276  it->setValue(valueAttr);
277  isSearched = false;
278  }
279  ++it;
280  }
281 }
282 
284 
288 PXml pxml_eraseVecChild(const PXml & xml, const PString & childName){
289  PXml out;
290  out.setName(xml.getName());
291  out.setValue(xml.getValue());
292  out.setVecAttr(xml.getVecAttr());
293  const PVecXml & vecXml = xml.getVecChild();
294  for(PVecXml::const_iterator it(vecXml.begin()); it != vecXml.end(); ++it){
295  if(it->getName() != childName){
296  out.getVecChild().push_back(*it);
297  }
298  }
299  return out;
300 }
301 
303 
308 bool pxml_saveFile(const PPath & fileName, const PXml & xml, bool isSvg){
309  PString body(pxml_baliseStr(xml, isSvg));
310  return fileName.saveFileContent(body);
311 }
312 
314 
318 PString pxml_baliseStr(const PXml & xml, bool isSvg){
319  PString body(""), name(xml.getName());
320  PString baseXmlNewLine("\n");
321  if(isSvg){
322  baseXmlNewLine = "";
323  }
324  if(xml.getIsText()){
325  body += xml.getValue();
326  }else{
327  if(name != ""){
328  body += "<";
329  body += name;
330  body += pxml_vecAttrStr(xml.getVecAttr(), isSvg);
331 
332  if(name == "?xml"){body += " ?>\n";}
333  else if(name == "!--"){body += " -->\n";}
334  else if(xml.getIsCompact()){
335  body += " />"+baseXmlNewLine;
336  }else{
337  body += ">"+baseXmlNewLine;
338  body += pxml_vecXmlStr(xml.getVecChild(), isSvg);
339  body += xml.getValue();
340  body += "</"+ name + ">" + baseXmlNewLine;
341  }
342  }else{
343 
344  body += pxml_vecXmlStr(xml.getVecChild(), isSvg);
345  }
346  }
347  return body;
348 }
349 
351 
355 PString pxml_vecXmlStr(const PVecXml & vecXml, bool isSvg){
356  PString body("");
357  for(PVecXml::const_iterator it(vecXml.begin()); it != vecXml.end(); ++it){
358  body += pxml_baliseStr(*it, isSvg);
359  }
360  return body;
361 }
362 
364 
368 PString pxml_attrStr(const PXmlAttr & xmlAttr, bool isSvg){
369  PString body(" ");
370  body += xmlAttr.getName() + "=\"" + xmlAttr.getValue() + "\"";
371  if(isSvg){
372  body += "\n\t";
373  }
374  return body;
375 }
376 
378 
382 PString pxml_vecAttrStr(const PVecXmlAttr & vecXmlAttr, bool isSvg){
383  if(vecXmlAttr.size() == 0lu){return "";}
384  PString body("");
385  for(PVecXmlAttr::const_iterator it(vecXmlAttr.begin()); it != vecXmlAttr.end(); ++it){
386  body += pxml_attrStr(*it, isSvg);
387  }
388  return body;
389 }
390 
392 
396  const PVecXml & vecXml = xml.getVecChild();
397  if(vecXml.size() != 0lu){
398  return pxml_vecXmlStr(vecXml);
399  }else{
400  return xml.getValue();
401  }
402 }
403 
404 
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
void setSeparator(const PString &separator)
Initialise la liste des caractères séparateurs.
Definition: PFileParser.cpp:43
PString getNextToken()
Get the next token.
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
PString getStrComposedOf(const PString &charset)
Get string composed of the characters in the string charset.
void setWhiteSpace(const PString &whiteSpace)
Initialise la liste des caractères blancs.
Definition: PFileParser.cpp:35
bool isMatch(const PString &patern)
Says if the patern match with the current caracters of the PFileParser.
PLocation getLocation() const
Fonction qui renvoie la PLocation du PFileParser.
void setFileContent(const PString &fileContent)
Set the file content.
Definition: PFileParser.cpp:50
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
Path of a directory or a file.
Definition: PPath.h:17
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
PString loadFileContent() const
Get the file content in a PString.
Definition: PPath.cpp:382
Extends the std::string.
Definition: PString.h:16
Attribute from xml.
Definition: PXml.h:30
const PString & getName() const
Get the variable p_name.
Definition: PXml.cpp:68
void setName(const PString &name)
Set the variable p_name, of type 'PString'.
Definition: PXml.cpp:54
const PString & getValue() const
Get the variable p_value.
Definition: PXml.cpp:82
void setValue(const PString &value)
Set the variable p_value, of type 'PString'.
Definition: PXml.cpp:61
Class used to parse xml.
Definition: PXml.h:54
bool getIsText() const
Get the variable p_isText.
Definition: PXml.cpp:236
void setVecAttr(const std ::vector< PXmlAttr > &vecAttr)
Set the variable p_vecAttr, of type 'std ::vector<PXmlAttr>'.
Definition: PXml.cpp:180
void setValue(const PString &value)
Set the variable p_value, of type 'PString'.
Definition: PXml.cpp:166
const std ::vector< PXml > & getVecChild() const
Get the variable p_vecChild.
Definition: PXml.cpp:264
void setIsText(bool isText)
Set the variable p_isText, of type 'bool'.
Definition: PXml.cpp:173
void setIsCompact(bool isCompact)
Set the variable p_isCompact, of type 'bool'.
Definition: PXml.cpp:159
const PString & getValue() const
Get the variable p_value.
Definition: PXml.cpp:222
bool getIsCompact() const
Get the variable p_isCompact.
Definition: PXml.cpp:208
const std ::vector< PXmlAttr > & getVecAttr() const
Get the variable p_vecAttr.
Definition: PXml.cpp:250
void setName(const PString &name)
Set the variable p_name, of type 'PString'.
Definition: PXml.cpp:152
const PString & getName() const
Get the variable p_name.
Definition: PXml.cpp:194
bool pxml_isAttributeEnd(PXml &parent, PFileParser &parser)
Say if it is the end of the attribute definition of the current balise.
Definition: pxml_utils.cpp:62
PString pxml_attrStr(const PXmlAttr &xmlAttr, bool isSvg)
Convert attribute in string.
Definition: pxml_utils.cpp:368
bool pxml_getVecChildIfExist(PVecXml &vecMatch, const PXml &xml, const PString &childName)
Get the vector of childs with given name if exist.
Definition: pxml_utils.cpp:197
PXml pxml_eraseVecChild(const PXml &xml, const PString &childName)
Erase the childs of the current xml if it has childName as name.
Definition: pxml_utils.cpp:288
bool pxml_getAttrIfExist(PXmlAttr &attr, const PXml &xml, const PString &attrName)
Get the attribute with given name if exist.
Definition: pxml_utils.cpp:251
PString pxml_vecAttrStr(const PVecXmlAttr &vecXmlAttr, bool isSvg)
Convert attributes in string.
Definition: pxml_utils.cpp:382
bool pxml_saveFile(const PPath &fileName, const PXml &xml, bool isSvg)
Save a xml in a file.
Definition: pxml_utils.cpp:308
bool pxml_parserXmlAttribute(PXml &parent, PFileParser &parser)
Parse the attribute of a xml balise.
Definition: pxml_utils.cpp:79
#define ALLOWED_BALISE_CHAR
List of allowed char as balise name.
Definition: pxml_utils.cpp:8
PString pxml_baliseStr(const PXml &xml, bool isSvg)
Convert xml in string.
Definition: pxml_utils.cpp:318
void pxml_setAttr(PXml &xml, const PString &nameAttr, const PString &valueAttr)
Set a value to an attribute.
Definition: pxml_utils.cpp:270
PString pxml_getFullContent(const PXml &xml)
Get the content of the PXml (children or value)
Definition: pxml_utils.cpp:395
PFileParser pxml_setXmlParser(const PString &fileContent, bool isSvg)
Set the PFileParser for xml.
Definition: pxml_utils.cpp:20
bool pxml_parserFile(PXml &xml, const PPath &fileName, bool isSvg)
Parse a PXml with a file.
Definition: pxml_utils.cpp:38
bool pxml_getChildIfExist(PXml &match, const PXml &xml, const PString &childName)
Get the child with given name if exist.
Definition: pxml_utils.cpp:215
PXml * pxml_getChildPtr(PXml &xml, const PString &childName)
Get the child with given name if exist.
Definition: pxml_utils.cpp:234
#define ALLOWED_ATTRIBUTE_CHAR
List of allowed char as attribute name.
Definition: pxml_utils.cpp:11
bool pxml_parserXmlContent(PXml &parent, PFileParser &parser, bool isMainBalise)
Parse the content of an xml balise.
Definition: pxml_utils.cpp:115
PString pxml_vecXmlStr(const PVecXml &vecXml, bool isSvg)
Convert a vecto of xml in string.
Definition: pxml_utils.cpp:355
bool pxml_parserContent(PXml &xml, const PString &fileContent, bool isSvg)
Parse a PXml with a file content.
Definition: pxml_utils.cpp:48
std::vector< PXmlAttr > PVecXmlAttr
Vector of PXml.
Definition: pxml_utils.h:16
std::vector< PXml > PVecXml
Vector of PXml.
Definition: pxml_utils.h:14