https://issues.shibboleth.net/jira/browse/CPPXT-90
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractSimpleElement.cpp
index 920aa3b..5782e3b 100644 (file)
@@ -1,36 +1,88 @@
-/*\r
- *  Copyright 2001-2006 Internet2\r
- * \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-/**\r
- * AbstractSimpleElement.cpp\r
- * \r
- * Extension of AbstractXMLObject that implements simple elements \r
- */\r
-\r
-#include "internal.h"\r
-#include "AbstractSimpleElement.h"\r
-\r
-using namespace xmltooling;\r
-using namespace std;\r
-\r
-// shared "empty" list of children for childless objects\r
-\r
-list<XMLObject*> AbstractSimpleElement::m_no_children;\r
-\r
-void AbstractSimpleElement::removeChild(XMLObject* child)\r
-{\r
-    throw XMLObjectException("Cannot remove child from a childless object.");\r
-}\r
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
+ */
+
+/**
+ * AbstractSimpleElement.cpp
+ * 
+ * Extension of AbstractXMLObject that implements simple elements.
+ */
+
+#include "internal.h"
+#include "AbstractSimpleElement.h"
+
+#include <xercesc/util/XMLChar.hpp>
+
+using namespace xmltooling;
+using xercesc::XMLString;
+using xercesc::XMLChar1_0;
+using namespace std;
+
+// shared "empty" list of children for childless objects
+
+list<XMLObject*> AbstractSimpleElement::m_no_children;
+
+AbstractSimpleElement::AbstractSimpleElement() : m_value(nullptr)
+{
+}
+
+AbstractSimpleElement::AbstractSimpleElement(const AbstractSimpleElement& src)
+    : AbstractXMLObject(src), m_value(XMLString::replicate(src.m_value))
+{
+}
+
+AbstractSimpleElement::~AbstractSimpleElement()
+{
+    XMLString::release(&m_value);
+}
+
+bool AbstractSimpleElement::hasChildren() const
+{
+    return false;
+}
+
+const list<XMLObject*>& AbstractSimpleElement::getOrderedChildren() const
+{
+    return m_no_children;
+}
+
+void AbstractSimpleElement::removeChild(XMLObject* child)
+{
+    throw XMLObjectException("Cannot remove child from a childless object.");
+}
+
+const XMLCh* AbstractSimpleElement::getTextContent(unsigned int position) const
+{
+    return (position==0) ? m_value : nullptr;
+}
+
+void AbstractSimpleElement::setTextContent(const XMLCh* value, unsigned int position)
+{
+    if (position > 0)
+        throw XMLObjectException("Cannot set text content in simple element at position > 0.");
+
+    // We overwrite the "one" piece of Text content if:
+    //  - the new value is null
+    //  - there is no existing value
+    //  - the old value is all whitespace
+    // If there's a non-whitespace value set, we leave it alone unless we're clearing it with a null.
+
+    if (!value || !m_value || XMLChar1_0::isAllSpaces(m_value, XMLString::stringLen(m_value)))
+        m_value=prepareForAssignment(m_value, value);
+}