Simpatico  v1.10
XmlXmlTag.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "XmlXmlTag.h"
9 #include <util/misc/ioUtil.h>
10 
11 namespace Util
12 {
13 
15  : endBracket_(false)
16  {}
17 
19  {}
20 
21  bool XmlXmlTag::match(const std::string& line, int begin)
22  {
23  // Check label
24  if (!matchLabel(line, begin)) return false;
25 
26  // Find attributes
27  XmlAttribute attribute;
28  bool version = false;
29  bool encoding = false;
30  while (matchAttribute(attribute)) {
31  if (attribute.label() != "version") {
32  if (version) return false;
33  version = true;
34  } else
35  if (attribute.label() != "encoding") {
36  if (encoding) return false;
37  encoding = true;
38  } else {
39  return false;
40  }
41  }
42  if (!version) return false;
43  if (!encoding) return false;
44 
45  // Check for end bracket
46  if (!endBracket()) {
47  return false;
48  }
49  return true;
50  }
51 
52  bool XmlXmlTag::matchLabel(const std::string& line, int begin)
53  {
54  label_ = "";
55  endBracket_ = false;
56  setString(line, begin);
57 
58  // Skip leading white space
59  if (isEnd()) return false;
60  skip();
61  if (isEnd()) return false;
62 
63  // Read opening <? bracket
64  if (c() != '<') {
65  return false;
66  }
67  next();
68  if (c() != '?') {
69  return false;
70  }
71 
72  // Read "xml" label
73  int beginLabel, endLabel;
74  next();
75  skip();
76  if (isEnd()) return false;
77  beginLabel = cursor();
78  while (c() != ' ') {
79  next();
80  if (isEnd()) return false;
81  }
82  endLabel = cursor();
83  label_ = string().substr(beginLabel, endLabel - beginLabel);
84  if (label_ != "xml") {
85  return false;
86  } else {
87  return true;
88  }
89 
90  }
91 
92  bool XmlXmlTag::matchAttribute(XmlAttribute& attribute)
93  {
94  skip();
95  if (isEnd()) return false;
96  if (c() == '?') {
97  next();
98  if (isEnd()) return false;
99  if (c() == '>') {
100  endBracket_ = true;
101  }
102  return false;
103  } else {
104  return attribute.match(*this);
105  }
106  }
107 
108 }
XmlXmlTag()
Constructor.
Definition: XmlXmlTag.cpp:14
~XmlXmlTag()
Destructor.
Definition: XmlXmlTag.cpp:18
void setString(const std::string &string, int cursor=0)
Initialize string and cursor.
Definition: XmlBase.cpp:32
Parser for an XML attribute.
Definition: XmlAttribute.h:23
int cursor() const
Return the index of the current character.
Definition: XmlBase.h:124
const std::string & label()
Return label string.
Definition: XmlAttribute.h:53
bool match(const std::string &string, int begin)
Attempt to match entire xml tag.
Definition: XmlXmlTag.cpp:21
void next()
Advance to the next character.
Definition: XmlBase.h:104
Utility classes for scientific computation.
Definition: accumulators.mod:1
bool isEnd() const
Has the cursor reached the end of the string?
Definition: XmlBase.h:136
const std::string & string() const
Return the associated string.
Definition: XmlBase.h:118
int c() const
Return the current character.
Definition: XmlBase.h:130
bool match(const std::string &string, int begin)
Return true if an attribute is found, false otherwise.
void skip()
Skip leading white space, if any.
Definition: XmlBase.h:88