3dea6874582a70530e4e4a38850bea8e5b35d09b
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractSimpleElement.cpp
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * AbstractSimpleElement.cpp
19  * 
20  * Extension of AbstractXMLObject that implements simple elements.
21  */
22
23 #include "internal.h"
24 #include "AbstractSimpleElement.h"
25
26 #include <xercesc/util/XMLChar.hpp>
27
28 using namespace xmltooling;
29 using xercesc::XMLString;
30 using xercesc::XMLChar1_0;
31 using namespace std;
32
33 // shared "empty" list of children for childless objects
34
35 list<XMLObject*> AbstractSimpleElement::m_no_children;
36
37 AbstractSimpleElement::AbstractSimpleElement() : m_value(nullptr)
38 {
39 }
40
41 AbstractSimpleElement::AbstractSimpleElement(const AbstractSimpleElement& src)
42     : AbstractXMLObject(src), m_value(XMLString::replicate(src.m_value))
43 {
44 }
45
46 AbstractSimpleElement::~AbstractSimpleElement()
47 {
48     XMLString::release(&m_value);
49 }
50
51 bool AbstractSimpleElement::hasChildren() const
52 {
53     return false;
54 }
55
56 const list<XMLObject*>& AbstractSimpleElement::getOrderedChildren() const
57 {
58     return m_no_children;
59 }
60
61 void AbstractSimpleElement::removeChild(XMLObject* child)
62 {
63     throw XMLObjectException("Cannot remove child from a childless object.");
64 }
65
66 const XMLCh* AbstractSimpleElement::getTextContent(unsigned int position) const
67 {
68     return (position==0) ? m_value : nullptr;
69 }
70
71 void AbstractSimpleElement::setTextContent(const XMLCh* value, unsigned int position)
72 {
73     if (position > 0)
74         throw XMLObjectException("Cannot set text content in simple element at position > 0.");
75
76     // We overwrite the "one" piece of Text content if:
77     //  - the new value is null
78     //  - there is no existing value
79     //  - the old value is all whitespace
80     // If there's a non-whitespace value set, we leave it alone unless we're clearing it with a null.
81
82     if (!value || !m_value || XMLChar1_0::isAllSpaces(m_value, XMLString::stringLen(m_value)))
83         m_value=prepareForAssignment(m_value, value);
84 }