Teuchos - Trilinos Tools Package
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
parameterlist
src
Teuchos_TreeBuildingXMLHandler.cpp
1
// @HEADER
2
// *****************************************************************************
3
// Teuchos: Common Tools Package
4
//
5
// Copyright 2004 NTESS and the Teuchos contributors.
6
// SPDX-License-Identifier: BSD-3-Clause
7
// *****************************************************************************
8
// @HEADER
9
10
#include "
Teuchos_TreeBuildingXMLHandler.hpp
"
11
#include "
Teuchos_StrUtils.hpp
"
12
#include "Teuchos_Assert.hpp"
13
14
using namespace
Teuchos
;
15
16
TreeBuildingXMLHandler::TreeBuildingXMLHandler
()
17
: root_(), current_(), path_()
18
{
19
current_ = root_;
20
}
21
22
void
TreeBuildingXMLHandler::characters
(
const
std::string& chars)
23
{
24
TEUCHOS_TEST_FOR_EXCEPTION
(current_.isEmpty(), std::logic_error,
25
"TreeBuildingXMLHandler::trying to add content to an empty node"
);
26
27
size_t
k = current_.numContentLines();
28
// a new line indicates the start of a new ContentLine. Only add it if current line is not empty.
29
if
(chars.compare(
"\n"
)==0) {
30
if
((k>0) && (current_.getContentLine(k-1).length()>0))
31
current_.addContent(
""
);
32
}
else
{
33
// If no contentLine exists to append create one, else add to the last contentLine
34
// Do not add white characters at the start of a new line
35
if
(k==0) {
36
if
(!
StrUtils::isWhite
(chars))
37
current_.addContent(
StrUtils::fixUnprintableCharacters
(chars));
38
}
else
{
39
if
((!
StrUtils::isWhite
(chars)) || (current_.getContentLine(k-1).length()>0))
40
current_.appendContentLine(k-1,
StrUtils::fixUnprintableCharacters
(chars));
41
}
42
}
43
}
44
45
void
TreeBuildingXMLHandler::startElement
(
const
std::string& tag,
46
const
Map& attributes)
47
{
48
XMLObject
parent;
49
50
if
(current_.isEmpty())
51
{
52
root_ =
XMLObject
(
"root"
);
53
current_ = root_;
54
}
55
parent = current_;
56
path_.push(current_);
57
current_ =
XMLObject
(tag);
58
parent.
addChild
(current_);
59
60
for
(Map::const_iterator i=attributes.begin(); i != attributes.end(); ++i)
61
{
62
const
std::string& key = (*i).first;
63
const
std::string& val = (*i).second;
64
current_.addAttribute(key, val);
65
}
66
}
67
68
int
TreeBuildingXMLHandler::endElement
(
const
std::string& tag)
69
{
70
int
error = 0;
71
if
(path_.size() > 0)
72
{
73
if
(current_.getTag() != tag) {
74
error = 1;
// error: tags must be balanced
75
}
76
// remove empty contentLines at the end.
77
size_t
k = current_.numContentLines();
78
while
( (k>0) && (current_.getContentLine(--k).length() == 0))
79
current_.removeContentLine(k);
80
81
current_ = path_.top();
82
path_.pop();
83
}
84
else
85
{
86
error = 1;
// error: cannot end element that wasn't started
87
}
88
return
error;
89
}
90
Teuchos_StrUtils.hpp
A std::string utilities class for Teuchos.
Teuchos_TreeBuildingXMLHandler.hpp
Defines a class for assembling an XMLObject from XML input.
Teuchos::StrUtils::fixUnprintableCharacters
static std::string fixUnprintableCharacters(const std::string &str)
Convert unprintable non-null characters to whitespace.
Definition
Teuchos_StrUtils.cpp:324
Teuchos::StrUtils::isWhite
static bool isWhite(const std::string &str)
Returns true if a std::string consists entirely of whitespace.
Definition
Teuchos_StrUtils.cpp:310
Teuchos::TreeBuildingXMLHandler::characters
void characters(const std::string &chars)
Process character data.
Definition
Teuchos_TreeBuildingXMLHandler.cpp:22
Teuchos::TreeBuildingXMLHandler::startElement
void startElement(const std::string &tag, const Map &attributes)
Receive notification of the start of an element.
Definition
Teuchos_TreeBuildingXMLHandler.cpp:45
Teuchos::TreeBuildingXMLHandler::TreeBuildingXMLHandler
TreeBuildingXMLHandler()
Empty constructor.
Definition
Teuchos_TreeBuildingXMLHandler.cpp:16
Teuchos::TreeBuildingXMLHandler::endElement
int endElement(const std::string &tag)
Receive notification of the end of an element.
Definition
Teuchos_TreeBuildingXMLHandler.cpp:68
Teuchos::XMLObject
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
Definition
Teuchos_XMLObject.hpp:30
Teuchos::XMLObject::addChild
void addChild(const XMLObject &child)
Add a child node to the node.
Definition
Teuchos_XMLObject.cpp:223
TEUCHOS_TEST_FOR_EXCEPTION
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Definition
Teuchos_TestForException.hpp:146
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Generated by
1.17.0