Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
14 using namespace Teuchos;
15 
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))
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 
const std::string & getTag() const
Return the tag of the current node.
void characters(const std::string &chars)
Process character data.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void addChild(const XMLObject &child)
Add a child node to the node.
Defines a class for assembling an XMLObject from XML input.
int endElement(const std::string &tag)
Receive notification of the end of an element.
bool isEmpty() const
Find out if a node is empty.
A std::string utilities class for Teuchos.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
void startElement(const std::string &tag, const Map &attributes)
Receive notification of the start of an element.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
static bool isWhite(const std::string &str)
Returns true if a std::string consists entirely of whitespace.
static std::string fixUnprintableCharacters(const std::string &str)
Convert unprintable non-null characters to whitespace.
void addContent(const std::string &contentLine)
Add a line of character content.
int numContentLines() const
Return the number of lines of character content stored in this node.
const std::string & getContentLine(int i) const
Return the i-th line of character content stored in this node.