1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*
21  * XSEC
22  *
23  * XSECNameSpaceHolder := Container class for holding and managing the name space stack
24  *						  Used when running through a DOM document
25  *
26  * $Id: XSECNameSpaceExpander.hpp 1817224 2017-12-05 19:27:33Z scantor $
27  *
28  */
29 
30 #ifndef XSECNAMESPACEEXPANDER_HEADER
31 #define XSECNAMESPACEEXPANDER_HEADER
32 
33 // XSEC Includes
34 #include <xsec/framework/XSECDefs.hpp>
35 #include <xsec/utils/XSECSafeBuffer.hpp>
36 #include <xsec/utils/XSECSafeBufferFormatter.hpp>
37 
38 // Xerces Includes
39 XSEC_DECLARE_XERCES_CLASS(DOMDocument);
40 XSEC_DECLARE_XERCES_CLASS(DOMNode);
41 XSEC_DECLARE_XERCES_CLASS(DOMElement);
42 
43 #include <vector>
44 
45 // --------------------------------------------------------------------------------
46 //           Structure Definition for the nodes within the list of nodes
47 // --------------------------------------------------------------------------------
48 
49 struct XSECNameSpaceEntry {
50 
51 	// Variables
52 	safeBuffer									m_name;			// The name for this name space
53 	XERCES_CPP_NAMESPACE_QUALIFIER DOMElement	* mp_node;		// The Element Node owner
54 	XERCES_CPP_NAMESPACE_QUALIFIER DOMNode		* mp_att;		// The added attribute node
55 
56 };
57 
58 // --------------------------------------------------------------------------------
59 //           Class definition for the list
60 // --------------------------------------------------------------------------------
61 
62 /**
63  * @ingroup pubsig
64  */
65 /*\@{*/
66 
67 /**
68  * @brief Class to "expand" name spaces
69  *
70  * For most things, a DOM model interoperates well with XPath.  Unfortunately,
71  * name-spaces are the one main problem.  In particular, the XPath spec
72  * states that every element node has an attribute node for its own
73  * namespaces, and one for namespaces above that are in scope.
74  *
75  * In the DOM scheme of things, a namespace is only available in the node in
76  * which it is defined.  Normally this is not a problem, you can just just
77  * refer backwards until you find the namespace you need.  However, for XPath
78  * expressions that select namespace nodes, we need to actually promulgate
79  * the name-spaces down to every node where they are visible so that the XPath
80  * selection will work properly.
81  *
82  * This is important for Canonicalisation of the found nodes, but we cannot
83  * do this only in the canonicaliser as it does not internally understand how
84  * to do DSIG style XPath.  So the XPath is done externally, and the
85  * resultant node set (including any selected "Expanded" attribute nodes).
86  * are passed in.
87  *
88  * The expander therefore handles the propogation of the namespace nodes, and
89  * removes the propogated nodes when it goes out of scope (or when
90  * deleteAddedNamespaces() is called).
91  *
92  */
93 
94 
95 class XSEC_EXPORT XSECNameSpaceExpander {
96 
97 
98 #if defined(XALAN_NO_NAMESPACES)
99 	typedef vector<XSECNameSpaceEntry *>			NameSpaceEntryListVectorType;
100 #else
101 	typedef std::vector<XSECNameSpaceEntry *>		NameSpaceEntryListVectorType;
102 #endif
103 
104 
105 public:
106 
107     /** @name Constructors and Destructors */
108     //@{
109 
110     /**
111 	 * \brief Main constructure
112 	 *
113 	 * Use this constructor to expand namespaces through an entire document.
114 	 *
115 	 * @param d The DOM document to be expanded.
116 	 */
117 
118 	XSECNameSpaceExpander(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *d);			// Constructor
119 
120     /**
121 	 * \brief Fragment constructor
122 	 *
123 	 * Use this constructor to expand namespaces in a given fragment only.
124 	 * @note The fragment does not need to be rooted in an actual document.
125 	 *
126 	 * @param f The starting element of the fragment to be expanded.
127 	 */
128 
129 	XSECNameSpaceExpander(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *f);		    // frag Constructor
130 
131 	~XSECNameSpaceExpander();						// Default destructor
132 
133 	//@}
134 
135 	// Operate
136 
137 	/**
138 	 * \brief Expand namespaces.
139 	 *
140 	 * Perform the expansion operation and create a list of all added nodes.
141 	 */
142 
143 	void expandNameSpaces();
144 
145 	/**
146 	 * \brief Collapse name-spaces
147 	 *
148 	 * Delete all namespaces added in exandNameSpaces() (using the list that
149 	 * was created at that time
150 	 */
151 
152 	void deleteAddedNamespaces();
153 
154 	// Check if a node is an added node
155 	bool nodeWasAdded(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *n) const;
156 
157 private:  // Functions
158 
159 	XSECNameSpaceExpander();					// No default constructor
160 	void recurse(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *n);
161 
162 	// data
163 
164 	NameSpaceEntryListVectorType	m_lst;			// List of added name spaces
165 	XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument
166 									* mp_doc;		// The owner document
167 	XERCES_CPP_NAMESPACE_QUALIFIER DOMElement
168 									* mp_fragment;  // If we are doing a fragment
169 	bool							m_expanded;		// Have we expanded already?
170 	XSECSafeBufferFormatter			* mp_formatter;
171 
172 };
173 
174 #endif /* XSECNAMESPACEEXPANDER_HEADER */
175 
176