1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _XMLDOCUMENTWRAPPER_XMLSECIMPL_HXX
25 #define _XMLDOCUMENTWRAPPER_XMLSECIMPL_HXX
26 
27 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
28 #include <com/sun/star/xml/csax/XCompressedDocumentHandler.hpp>
29 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeper.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <cppuhelper/implbase4.hxx>
32 
33 #include "saxhelper.hxx"
34 //#include "libxml/parserInternals.h"
35 //#include "libxslt/xslt.h"
36 
37 #define NODEPOSITION_NORMAL        1
38 #define NODEPOSITION_STARTELEMENT  2
39 #define NODEPOSITION_ENDELEMENT    3
40 
41 #include <libxml/tree.h>
42 
43 class XMLDocumentWrapper_XmlSecImpl : public cppu::WeakImplHelper4
44 <
45 	com::sun::star::xml::wrapper::XXMLDocumentWrapper,
46 	com::sun::star::xml::sax::XDocumentHandler,
47 	com::sun::star::xml::csax::XCompressedDocumentHandler,
48 	com::sun::star::lang::XServiceInfo
49 >
50 /****** XMLDocumentWrapper_XmlSecImpl.hxx/CLASS XMLDocumentWrapper_XmlSecImpl *
51  *
52  *   NAME
53  *	XMLDocumentWrapper_XmlSecImpl -- Class to manipulate a libxml2
54  *	document
55  *
56  *   FUNCTION
57  *	Converts SAX events into a libxml2 document, converts the document back
58  *	into SAX event stream, and manipulate nodes in the document.
59  *
60  *   HISTORY
61  *	05.01.2004 -	Interface supported: XXMLDocumentWrapper,
62  * 			XDocumentHandler, XCompressedDocumentHandler,
63  *			XServiceInfo
64  *
65  *   AUTHOR
66  *	Michael Mi
67  *	Email: michael.mi@sun.com
68  ******************************************************************************/
69 {
70 private:
71 	/* the sax helper */
72 	SAXHelper saxHelper;
73 
74 	/* the document used to convert SAX events to */
75 	xmlDocPtr m_pDocument;
76 
77 	/* the root element */
78 	xmlNodePtr m_pRootElement;
79 
80 	/*
81 	 * the current active element. The next incoming SAX event will be
82 	 * appended to this element
83 	 */
84 	xmlNodePtr m_pCurrentElement;
85 
86 	/*
87 	 * This variable is used when converting the document or part of it into
88 	 * SAX events. See getNextSAXEvent method.
89 	 */
90 	sal_Int32 m_nCurrentPosition;
91 
92 	/*
93 	 * used for recursive deletion. See recursiveDelete method
94 	 */
95 	xmlNodePtr m_pStopAtNode;
96 	xmlNodePtr m_pCurrentReservedNode;
97 	com::sun::star::uno::Sequence< com::sun::star::uno::Reference<
98 		com::sun::star::xml::wrapper::XXMLElementWrapper > > m_aReservedNodes;
99 	sal_Int32 m_nReservedNodeIndex;
100 
101 private:
102 	void getNextSAXEvent();
103 
104 	void sendStartElement(
105 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xHandler,
106 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xHandler2,
107 		const xmlNodePtr pNode) const
108 		throw (com::sun::star::xml::sax::SAXException);
109 
110 	void sendEndElement(
111 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xHandler,
112 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xHandler2,
113 		const xmlNodePtr pNode) const
114 		throw (com::sun::star::xml::sax::SAXException);
115 
116 	void sendNode(
117 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xHandler,
118 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xHandler2,
119 		const xmlNodePtr pNode) const
120 		throw (com::sun::star::xml::sax::SAXException);
121 
122 	rtl::OString getNodeQName(const xmlNodePtr pNode) const;
123 
124 	sal_Int32 recursiveDelete( const xmlNodePtr pNode);
125 
126 	void getNextReservedNode();
127 
128 	void removeNode( const xmlNodePtr pNode) const;
129 
130 	xmlNodePtr checkElement(
131 		const com::sun::star::uno::Reference<
132 			com::sun::star::xml::wrapper::XXMLElementWrapper >& xXMLElement) const;
133 
134 	void buildIDAttr( xmlNodePtr pNode ) const;
135 	void rebuildIDLink( xmlNodePtr pNode ) const;
136 
137 public:
138 	XMLDocumentWrapper_XmlSecImpl();
139 	virtual ~XMLDocumentWrapper_XmlSecImpl();
140 
141 	/* com::sun::star::xml::wrapper::XXMLDocumentWrapper */
142 	virtual com::sun::star::uno::Reference<
143 		com::sun::star::xml::wrapper::XXMLElementWrapper > SAL_CALL getCurrentElement(  )
144 		throw (com::sun::star::uno::RuntimeException);
145 
146 	virtual void SAL_CALL setCurrentElement( const com::sun::star::uno::Reference<
147 		com::sun::star::xml::wrapper::XXMLElementWrapper >& element )
148 		throw (com::sun::star::uno::RuntimeException);
149 
150 	virtual void SAL_CALL removeCurrentElement(  )
151 		throw (com::sun::star::uno::RuntimeException);
152 
153 	virtual sal_Bool SAL_CALL isCurrent( const com::sun::star::uno::Reference<
154 		com::sun::star::xml::wrapper::XXMLElementWrapper >& node )
155 		throw (com::sun::star::uno::RuntimeException);
156 
157 	virtual sal_Bool SAL_CALL isCurrentElementEmpty(  )
158 		throw (com::sun::star::uno::RuntimeException);
159 
160 	virtual rtl::OUString SAL_CALL getNodeName( const com::sun::star::uno::Reference<
161 		com::sun::star::xml::wrapper::XXMLElementWrapper >& node )
162 		throw (com::sun::star::uno::RuntimeException);
163 
164 	virtual void SAL_CALL clearUselessData(
165 		const com::sun::star::uno::Reference<
166 			com::sun::star::xml::wrapper::XXMLElementWrapper >& node,
167 		const com::sun::star::uno::Sequence< com::sun::star::uno::Reference<
168 			com::sun::star::xml::wrapper::XXMLElementWrapper > >& reservedDescendants,
169 		const com::sun::star::uno::Reference<
170 			com::sun::star::xml::wrapper::XXMLElementWrapper >& stopAtNode )
171 		throw (com::sun::star::uno::RuntimeException);
172 
173 	virtual void SAL_CALL collapse( const com::sun::star::uno::Reference<
174 		com::sun::star::xml::wrapper::XXMLElementWrapper >& node )
175 		throw (com::sun::star::uno::RuntimeException);
176 
177 	virtual void SAL_CALL generateSAXEvents(
178 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& handler,
179 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xEventKeeperHandler,
180 		const com::sun::star::uno::Reference<
181 			com::sun::star::xml::wrapper::XXMLElementWrapper >& startNode,
182 		const com::sun::star::uno::Reference<
183 			com::sun::star::xml::wrapper::XXMLElementWrapper >& endNode )
184 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
185 
186 	virtual void SAL_CALL getTree(
187 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& handler )
188 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
189 
190 	virtual void SAL_CALL rebuildIDLink(
191 		const com::sun::star::uno::Reference< com::sun::star::xml::wrapper::XXMLElementWrapper >& node )
192 		throw (com::sun::star::uno::RuntimeException);
193 
194 	/* com::sun::star::xml::sax::XDocumentHandler */
195 	virtual void SAL_CALL startDocument(  )
196 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
197 
198 	virtual void SAL_CALL endDocument(  )
199 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
200 
201 	virtual void SAL_CALL startElement(
202 		const rtl::OUString& aName,
203 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttribs )
204 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
205 
206 	virtual void SAL_CALL endElement( const rtl::OUString& aName )
207 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
208 
209 	virtual void SAL_CALL characters( const rtl::OUString& aChars )
210 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
211 
212 	virtual void SAL_CALL ignorableWhitespace( const rtl::OUString& aWhitespaces )
213 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
214 
215 	virtual void SAL_CALL processingInstruction( const rtl::OUString& aTarget, const rtl::OUString& aData )
216 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
217 
218 	virtual void SAL_CALL setDocumentLocator( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator )
219 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
220 
221 	/* com::sun::star::xml::csax::XCompressedDocumentHandler */
222 	virtual void SAL_CALL _startDocument(  )
223 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
224 
225 	virtual void SAL_CALL _endDocument(  )
226 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
227 
228 	virtual void SAL_CALL _startElement(
229 		const rtl::OUString& aName,
230 		const com::sun::star::uno::Sequence<
231 			com::sun::star::xml::csax::XMLAttribute >& aAttributes )
232 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
233 
234 	virtual void SAL_CALL _endElement( const rtl::OUString& aName )
235 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
236 
237 	virtual void SAL_CALL _characters( const rtl::OUString& aChars )
238 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
239 
240 	virtual void SAL_CALL _ignorableWhitespace( const rtl::OUString& aWhitespaces )
241 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
242 
243 	virtual void SAL_CALL _processingInstruction( const rtl::OUString& aTarget, const rtl::OUString& aData )
244 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
245 
246 	virtual void SAL_CALL _setDocumentLocator(
247 		sal_Int32 columnNumber,
248 		sal_Int32 lineNumber,
249 		const rtl::OUString& publicId,
250 		const rtl::OUString& systemId )
251 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
252 
253 	/* com::sun::star::lang::XServiceInfo */
254 	virtual rtl::OUString SAL_CALL getImplementationName(  )
255 		throw (com::sun::star::uno::RuntimeException);
256 
257 	virtual sal_Bool SAL_CALL supportsService( const rtl::OUString& ServiceName )
258 		throw (com::sun::star::uno::RuntimeException);
259 
260 	virtual com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames(  )
261 		throw (com::sun::star::uno::RuntimeException);
262 };
263 
264 rtl::OUString XMLDocumentWrapper_XmlSecImpl_getImplementationName()
265 	throw ( com::sun::star::uno::RuntimeException );
266 
267 sal_Bool SAL_CALL XMLDocumentWrapper_XmlSecImpl_supportsService( const rtl::OUString& ServiceName )
268 	throw ( com::sun::star::uno::RuntimeException );
269 
270 com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL
271 	XMLDocumentWrapper_XmlSecImpl_getSupportedServiceNames(  )
272 	throw ( com::sun::star::uno::RuntimeException );
273 
274 com::sun::star::uno::Reference< com::sun::star::uno::XInterface >
275 SAL_CALL XMLDocumentWrapper_XmlSecImpl_createInstance(
276 	const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > & rSMgr)
277 	throw ( com::sun::star::uno::Exception );
278 
279 #endif
280 
281