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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
43 #include "Teuchos_StrUtils.hpp"
44 #include "Teuchos_Assert.hpp"
45 
46 using namespace Teuchos;
47 
49  : root_(), current_(), path_()
50 {
51  current_ = root_;
52 }
53 
54 void TreeBuildingXMLHandler::characters(const std::string& chars)
55 {
56  TEUCHOS_TEST_FOR_EXCEPTION(current_.isEmpty(), std::logic_error,
57  "TreeBuildingXMLHandler::trying to add content to an empty node");
58 
59  size_t k = current_.numContentLines();
60  // a new line indicates the start of a new ContentLine. Only add it if current line is not empty.
61  if(chars.compare("\n")==0) {
62  if((k>0) && (current_.getContentLine(k-1).length()>0))
63  current_.addContent("");
64  } else {
65  // If no contentLine exists to append create one, else add to the last contentLine
66  // Do not add white characters at the start of a new line
67  if(k==0) {
68  if(!StrUtils::isWhite(chars))
70  } else {
71  if((!StrUtils::isWhite(chars)) || (current_.getContentLine(k-1).length()>0))
72  current_.appendContentLine(k-1,StrUtils::fixUnprintableCharacters(chars));
73  }
74  }
75 }
76 
77 void TreeBuildingXMLHandler::startElement(const std::string& tag,
78  const Map& attributes)
79 {
80  XMLObject parent;
81 
82  if (current_.isEmpty())
83  {
84  root_ = XMLObject("root");
85  current_ = root_;
86  }
87  parent = current_;
88  path_.push(current_);
89  current_ = XMLObject(tag);
90  parent.addChild(current_);
91 
92  for (Map::const_iterator i=attributes.begin(); i != attributes.end(); ++i)
93  {
94  const std::string& key = (*i).first;
95  const std::string& val = (*i).second;
96  current_.addAttribute(key, val);
97  }
98 }
99 
100 int TreeBuildingXMLHandler::endElement(const std::string& tag)
101 {
102  int error = 0;
103  if (path_.size() > 0)
104  {
105  if (current_.getTag() != tag) {
106  error = 1; // error: tags must be balanced
107  }
108  // remove empty contentLines at the end.
109  size_t k = current_.numContentLines();
110  while( (k>0) && (current_.getContentLine(--k).length() == 0))
111  current_.removeContentLine(k);
112 
113  current_ = path_.top();
114  path_.pop();
115  }
116  else
117  {
118  error = 1; // error: cannot end element that wasn't started
119  }
120  return error;
121 }
122 
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.