Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
11#include "Teuchos_StrUtils.hpp"
12#include "Teuchos_Assert.hpp"
13
14using namespace Teuchos;
15
17 : root_(), current_(), path_()
18{
19 current_ = root_;
20}
21
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) {
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
46 const Map& attributes)
47{
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
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
A std::string utilities class for Teuchos.
Defines a class for assembling an XMLObject from XML input.
Smart reference counting pointer class for automatic garbage collection.
static std::string fixUnprintableCharacters(const std::string &str)
Convert unprintable non-null characters to whitespace.
static bool isWhite(const std::string &str)
Returns true if a std::string consists entirely of whitespace.
void characters(const std::string &chars)
Process character data.
void startElement(const std::string &tag, const Map &attributes)
Receive notification of the start of an element.
int endElement(const std::string &tag)
Receive notification of the end of an element.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
const std::string & getTag() const
Return the tag of the current node.
int numContentLines() const
Return the number of lines of character content stored in this node.
void addContent(const std::string &contentLine)
Add a line of character content.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
const std::string & getContentLine(int i) const
Return the i-th line of character content stored in this node.
bool isEmpty() const
Find out if a node is empty.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...