1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.
*/
/*
* XSEC
*
* XSECXMLNSStack := NS Stack for simple canonicalisation
*
* $Id$
*
*/
// XSEC
#include <xsec/framework/XSECDefs.hpp>
#include <xsec/framework/XSECError.hpp>
#include <xsec/canon/XSECXMLNSStack.hpp>
#include "../utils/XSECDOMUtils.hpp"
// Xerces
XERCES_CPP_NAMESPACE_USE
// --------------------------------------------------------------------------------
// Holder structures
// --------------------------------------------------------------------------------
typedef struct XSECNSHolderStruct {
XERCES_CPP_NAMESPACE_QUALIFIER DOMNode * mp_ns; // Actual NS attribute
XERCES_CPP_NAMESPACE_QUALIFIER DOMNode * mp_owner; // Owner Element
struct XSECNSHolderStruct * mp_hides; // WHat does this NS hide?
struct XSECNSHolderStruct * mp_next; // Next in list
XERCES_CPP_NAMESPACE_QUALIFIER DOMNode * mp_printed; // Node at which it was printed
bool m_isDefault; // Is this a default NS?
} XSECNSHolder;
typedef struct XSECNSElementStruct {
XERCES_CPP_NAMESPACE_QUALIFIER DOMNode * mp_elt; // Element
struct XSECNSHolderStruct * mp_firstNS; // WHat does this NS hide?
} XSECNSElement;
// --------------------------------------------------------------------------------
// Construct/Destruct
// --------------------------------------------------------------------------------
XSECXMLNSStack::XSECXMLNSStack() {
}
XSECXMLNSStack::~XSECXMLNSStack() {
// Need to iterate through the Stack to
XSECNSElement * t;
XSECNSHolder * u, *v;
while (m_elements.size() > 0) {
t = m_elements.top();
u = t->mp_firstNS;
while (u != NULL) {
v = u->mp_next;
delete u;
u = v;
}
delete t;
m_elements.pop();
}
}
// --------------------------------------------------------------------------------
// Stack Functions
// --------------------------------------------------------------------------------
void XSECXMLNSStack::pushElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode * elt) {
XSECNSElement * t;
XSECnew(t, XSECNSElement);
t->mp_elt = elt;
t->mp_firstNS = NULL;
m_elements.push(t);
}
void XSECXMLNSStack::popElement() {
// First - are there any namespaces to remove from the currently visible list?
XSECNSHolder * t, *u;
XSECNSElement * e = m_elements.top();
// Iterate through the current namespaces vector
XSECNSHolderVectorType::iterator it = m_currentNS.begin();
while (it != m_currentNS.end()) {
t = *it;
if (t->mp_owner == e->mp_elt) {
// Need to delete this
m_currentNS.erase(it);
if (t->mp_hides != NULL) {
m_currentNS.push_back(t->mp_hides);
}
// TODO - Fix this, it is naieve and slow
it = m_currentNS.begin();
}
else {
if (t->mp_printed == e->mp_elt)
t->mp_printed = NULL;
it++;
}
}
// OK - Now we delete the NS nodes
t = e->mp_firstNS;
while (t != NULL) {
u = t->mp_next;
delete t;
t = u;
}
// Now delete the element holder itself
m_elements.pop();
delete e;
}
// --------------------------------------------------------------------------------
// NS Addition
// --------------------------------------------------------------------------------
void XSECXMLNSStack::addNamespace(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode * ns) {
// Create the new entry for this node
XSECNSHolder * t, *u;
XSECnew(t, XSECNSHolder);
t->mp_hides = NULL;
t->mp_next = NULL;
t->mp_ns = ns;
t->mp_owner = (m_elements.top())->mp_elt;
t->mp_printed = NULL;
t->m_isDefault = strEquals(ns->getNodeName(), DSIGConstants::s_unicodeStrXmlns);
// Does this hide something in the current namespace list?
XSECNSHolderVectorType::iterator it = m_currentNS.begin();
while (it != m_currentNS.end()) {
u = (*it);
if (strEquals(u->mp_ns->getNodeName(), ns->getNodeName())) {
// This hides a current NS entry
t->mp_hides = u;
m_currentNS.erase(it);
it = m_currentNS.end();
}
else
it++;
}
// Now push it onto the namespace vector
m_currentNS.push_back(t);
// Add me to the current element's namespaces
XSECNSElement * e = m_elements.top();
t->mp_next = e->mp_firstNS;
e->mp_firstNS = t;
}
void XSECXMLNSStack::printNamespace(DOMNode * ns, DOMNode * elt) {
XSECNSHolder * t;
XSECNSHolderVectorType::iterator it = m_currentNS.begin();
while (it != m_currentNS.end()) {
t = (*it);
// Fix for bug#47353, go ahead and track printing of default namespaces.
if (t->mp_ns == ns) { // && t->m_isDefault == false) {
t->mp_printed = elt;
break;
}
it++;
}
}
// --------------------------------------------------------------------------------
// NS Searching
// --------------------------------------------------------------------------------
DOMNode * XSECXMLNSStack::getFirstNamespace(void) {
m_currentNSIterator = m_currentNS.begin();
while (m_currentNSIterator != m_currentNS.end() &&
(*m_currentNSIterator)->mp_printed != NULL)
m_currentNSIterator++;
if (m_currentNSIterator != m_currentNS.end())
return (*m_currentNSIterator)->mp_ns;
return NULL;
}
DOMNode * XSECXMLNSStack::getNextNamespace(void) {
if (m_currentNSIterator == m_currentNS.end())
return NULL;
m_currentNSIterator++;
while (m_currentNSIterator != m_currentNS.end() &&
(*m_currentNSIterator)->mp_printed != NULL)
m_currentNSIterator++;
if (m_currentNSIterator == m_currentNS.end())
return NULL;
return (*m_currentNSIterator)->mp_ns;
}
// Fix for bug#47353, explicit check for non-empty default NS decl.
bool XSECXMLNSStack::isNonEmptyDefaultNS(void) {
for (XSECNSHolderVectorType::iterator it = m_currentNS.begin(); it != m_currentNS.end(); ++it) {
if ((*it)->m_isDefault) {
const XMLCh* val = (*it)->mp_ns->getNodeValue();
if (val && *val)
return true;
}
}
return false;
}
|