PSCF v1.1
XmlStartTag.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 "XmlStartTag.h"
9#include <util/misc/ioUtil.h>
10
11namespace Util
12{
13
15 : endBracket_(false)
16 {}
17
19 {}
20
21 bool XmlStartTag::matchLabel(const std::string& line, int begin)
22 {
23 label_ = "";
24 endBracket_ = false;
25 setString(line, begin);
26
27 // Skip leading white space
28 if (isEnd()) return false;
29 skip();
30 if (isEnd()) return false;
31
32 // Read opening bracket
33 if (c() != '<') {
34 //std::cout << "Missing opening bracket" << std::endl;
35 return false;
36 }
37
38 // Read label
39 int beginLabel, endLabel;
40 next();
41 if (isEnd()) return false;
42 if (c() == '/') return false; // Indicates an end tag
43 skip();
44 if (isEnd()) return false;
45 beginLabel = cursor();
46 while (c() != ' ' && c() != '>') {
47 next();
48 if (isEnd()) return false;
49 }
50 endLabel = cursor();
51 label_ = string().substr(beginLabel, endLabel - beginLabel);
52 if (c() == '>') {
53 endBracket_ = true;
54 }
55 return true;
56 }
57
58 void XmlStartTag::matchLabel(const std::string expected,
59 const std::string& line, int begin)
60 {
61 if (!matchLabel(line, begin)) {
62 UTIL_THROW("Unable to match start tag label");
63 }
64 if (label() != expected) {
65 Log::file() << "found = " << label() << std::endl;
66 Log::file() << "expected = " << expected << std::endl;
67 UTIL_THROW("Incorrect start tag label");
68 }
69 }
70
72 {
73 skip();
74 if (isEnd()) return false;
75 if (c() == '>') {
76 endBracket_ = true;
77 return false;
78 } else if (c() == '/') {
79 next();
80 if (isEnd()) return false;
81 if (c() == '>') {
82 endBracket_ = true;
83 }
84 return false;
85 } else {
86 return attribute.match(*this);
87 }
88 }
89
90 /*
91 * Throw exception if now end bracket was found.
92 */
94 {
95 if (!endBracket()) {
96 Log::file() << "line = " << string() << std::endl;
97 UTIL_THROW("Missing end bracket");
98 }
99 }
100
101}
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
Parser for an XML attribute.
Definition: XmlAttribute.h:24
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
const std::string & string() const
Return the associated string.
Definition: XmlBase.h:118
void next()
Advance to the next character.
Definition: XmlBase.h:104
int cursor() const
Return the index of the current character.
Definition: XmlBase.h:124
void setString(const std::string &string, int cursor=0)
Initialize string and cursor.
Definition: XmlBase.cpp:32
int c() const
Return the current character.
Definition: XmlBase.h:130
bool isEnd() const
Has the cursor reached the end of the string?
Definition: XmlBase.h:136
bool matchAttribute(XmlAttribute &attribute)
Attempt to match an attribute.
Definition: XmlStartTag.cpp:71
~XmlStartTag()
Destructor.
Definition: XmlStartTag.cpp:18
bool endBracket()
True if a closing bracket was found.
Definition: XmlStartTag.h:96
const std::string label()
Label string.
Definition: XmlStartTag.h:90
XmlStartTag()
Constructor.
Definition: XmlStartTag.cpp:14
bool matchLabel(const std::string &string, int begin)
Match opening bracket and any label.
Definition: XmlStartTag.cpp:21
void finish()
Check if end bracket was found.
Definition: XmlStartTag.cpp:93
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1