1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: node.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 #include <rtl/ref.hxx>
35 #include <rtl/string.hxx>
36 #include <rtl/ustring.hxx>
37 #include <sal/types.h>
38 #include <sax/fastattribs.hxx>
39 #include <cppuhelper/implbase1.hxx>
40 #include <cppuhelper/implbase2.hxx>
41 #include <com/sun/star/uno/Reference.h>
42 #include <com/sun/star/uno/Exception.hpp>
43 #include <com/sun/star/xml/dom/XNode.hpp>
44 #include <com/sun/star/xml/dom/XNodeList.hpp>
45 #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
46 #include <com/sun/star/xml/dom/NodeType.hpp>
47 #include <com/sun/star/uno/Sequence.h>
48 #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
49 #include <com/sun/star/xml/dom/events/XDocumentEvent.hpp>
50 #include <com/sun/star/xml/dom/events/XEvent.hpp>
51 #include <com/sun/star/xml/dom/events/XMutationEvent.hpp>
52 #include <com/sun/star/xml/dom/events/XUIEvent.hpp>
53 #include <com/sun/star/xml/dom/events/XMouseEvent.hpp>
54 #include <com/sun/star/xml/dom/DOMException.hpp>
55 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
56 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
57 #include <libxml/tree.h>
62 using ::rtl::OUString
;
64 using namespace sax_fastparser
;
65 using namespace com::sun::star::uno
;
66 using namespace com::sun::star::xml::sax
;
67 using namespace com::sun::star::xml::dom
;
68 using namespace com::sun::star::xml::dom::events
;
74 Context( const Reference
< XFastDocumentHandler
>& i_xHandler
,
75 const Reference
< XFastTokenHandler
>& i_xTokenHandler
) :
76 maNamespaces( 1, std::vector
<Namespace
>() ),
78 mxAttribList(new FastAttributeList(i_xTokenHandler
)),
79 mxCurrentHandler(i_xHandler
, UNO_QUERY_THROW
),
80 mxDocHandler(i_xHandler
),
81 mxTokenHandler(i_xTokenHandler
)
88 OUString maNamespaceURL
;
90 const OString
& getPrefix() const { return maPrefix
; }
93 typedef std::vector
< std::vector
<Namespace
> > NamespaceVectorType
;
94 typedef std::hash_map
< OUString
,
96 rtl::OUStringHash
> NamespaceMapType
;
98 /// outer vector: xml context; inner vector: current NS
99 NamespaceVectorType maNamespaces
;
100 NamespaceMapType maNamespaceMap
;
101 ::rtl::Reference
<FastAttributeList
> mxAttribList
;
102 Reference
<XFastContextHandler
> mxCurrentHandler
;
103 Reference
<XFastDocumentHandler
> mxDocHandler
;
104 Reference
<XFastTokenHandler
> mxTokenHandler
;
107 void pushContext(Context
& io_rContext
);
108 void popContext(Context
& io_rContext
);
110 sal_Int32
getTokenWithPrefix( const Context
& rContext
, const sal_Char
* xPrefix
, const sal_Char
* xName
);
111 sal_Int32
getToken( const Context
& rContext
, const sal_Char
* xName
);
113 /// add namespaces on this node to context
114 void addNamespaces(Context
& io_rContext
, xmlNodePtr pNode
);
117 typedef std::map
< const xmlNodePtr
, CNode
* > nodemap_t
;
120 class CNode
: public cppu::WeakImplHelper2
< XNode
, XEventTarget
>
122 friend class CDocument
;
123 friend class CElement
;
124 friend class CAttributesMap
;
125 friend class CChildList
;
126 friend class CElementList
;
127 friend class CEntitiesMap
;
128 friend class CNotationsMap
;
130 static nodemap_t theNodeMap
;
133 NodeType m_aNodeType
;
134 xmlNodePtr m_aNodePtr
;
136 Reference
< XDocument
> m_rDocument
;
138 // for initialization by classes derived through ImplInheritanceHelper
140 void init_node(const xmlNodePtr aNode
);
142 void dispatchSubtreeModified();
148 // get a representaion for a libxml node
149 static CNode
* get(const xmlNodePtr aNode
, sal_Bool bCreate
= sal_True
);
150 // remove a wrapper instance
151 static void remove(const xmlNodePtr aNode
);
153 // get the libxml node implementation
154 static xmlNodePtr
getNodePtr(const Reference
< XNode
>& aNode
);
156 //static Sequence< sal_Int8 >
158 // recursively create SAX events
159 virtual void SAL_CALL
saxify(
160 const Reference
< XDocumentHandler
>& i_xHandler
);
162 // recursively create SAX events
163 virtual void SAL_CALL
fastSaxify( Context
& io_rContext
);
165 // ---- DOM interfaces
168 Adds the node newChild to the end of the list of children of this node.
170 virtual Reference
< XNode
> SAL_CALL
appendChild(const Reference
< XNode
>& newChild
)
171 throw (RuntimeException
, DOMException
);
174 Returns a duplicate of this node, i.e., serves as a generic copy
175 constructor for nodes.
177 virtual Reference
< XNode
> SAL_CALL
cloneNode(sal_Bool deep
)
178 throw (RuntimeException
);
181 A NamedNodeMap containing the attributes of this node (if it is an Element)
184 virtual Reference
< XNamedNodeMap
> SAL_CALL
getAttributes()
185 throw (RuntimeException
);
188 A NodeList that contains all children of this node.
190 virtual Reference
< XNodeList
> SAL_CALL
getChildNodes()
191 throw (RuntimeException
);
194 The first child of this node.
196 virtual Reference
< XNode
> SAL_CALL
getFirstChild()
197 throw (RuntimeException
);
200 The last child of this node.
202 virtual Reference
< XNode
> SAL_CALL
getLastChild()
203 throw (RuntimeException
);
206 Returns the local part of the qualified name of this node.
208 virtual OUString SAL_CALL
getLocalName()
209 throw (RuntimeException
);
212 The namespace URI of this node, or null if it is unspecified.
214 virtual OUString SAL_CALL
getNamespaceURI()
215 throw (RuntimeException
);
218 The node immediately following this node.
220 virtual Reference
< XNode
> SAL_CALL
getNextSibling()
221 throw (RuntimeException
);
224 The name of this node, depending on its type; see the table above.
225 -- virtual implemented by actual node types
227 virtual OUString SAL_CALL
getNodeName()
228 throw (RuntimeException
);
231 A code representing the type of the underlying object, as defined above.
233 virtual NodeType SAL_CALL
getNodeType()
234 throw (RuntimeException
);
237 The value of this node, depending on its type; see the table above.
238 -- virtual implemented by actual node types
240 virtual OUString SAL_CALL
getNodeValue()
241 throw (RuntimeException
);
244 The Document object associated with this node.
246 virtual Reference
< XDocument
> SAL_CALL
getOwnerDocument()
247 throw (RuntimeException
);
250 The parent of this node.
252 virtual Reference
< XNode
> SAL_CALL
getParentNode()
253 throw (RuntimeException
);
256 The namespace prefix of this node, or null if it is unspecified.
258 virtual OUString SAL_CALL
getPrefix()
259 throw (RuntimeException
);
262 The node immediately preceding this node.
264 virtual Reference
< XNode
> SAL_CALL
getPreviousSibling()
265 throw (RuntimeException
);
268 Returns whether this node (if it is an element) has any attributes.
270 virtual sal_Bool SAL_CALL
hasAttributes()
271 throw (RuntimeException
);
274 Returns whether this node has any children.
276 virtual sal_Bool SAL_CALL
hasChildNodes()
277 throw (RuntimeException
);
280 Inserts the node newChild before the existing child node refChild.
282 virtual Reference
< XNode
> SAL_CALL
insertBefore(
283 const Reference
< XNode
>& newChild
, const Reference
< XNode
>& refChild
)
284 throw (RuntimeException
, DOMException
);
287 Tests whether the DOM implementation implements a specific feature and
288 that feature is supported by this node.
290 virtual sal_Bool SAL_CALL
isSupported(const OUString
& feature
, const OUString
& ver
)
291 throw (RuntimeException
);
294 Puts all Text nodes in the full depth of the sub-tree underneath this
295 Node, including attribute nodes, into a "normal" form where only structure
296 (e.g., elements, comments, processing instructions, CDATA sections, and
297 entity references) separates Text nodes, i.e., there are neither adjacent
298 Text nodes nor empty Text nodes.
300 virtual void SAL_CALL
normalize()
301 throw (RuntimeException
);
304 Removes the child node indicated by oldChild from the list of children,
307 virtual Reference
< XNode
> SAL_CALL
removeChild(const Reference
< XNode
>& oldChild
)
308 throw (RuntimeException
, DOMException
);
311 Replaces the child node oldChild with newChild in the list of children,
312 and returns the oldChild node.
314 virtual Reference
< XNode
> SAL_CALL
replaceChild(
315 const Reference
< XNode
>& newChild
, const Reference
< XNode
>& oldChild
)
316 throw (RuntimeException
, DOMException
);
319 The value of this node, depending on its type; see the table above.
321 virtual void SAL_CALL
setNodeValue(const OUString
& nodeValue
)
322 throw (RuntimeException
, DOMException
);
325 The namespace prefix of this node, or null if it is unspecified.
327 virtual void SAL_CALL
setPrefix(const OUString
& prefix
)
328 throw (RuntimeException
, DOMException
);
332 virtual void SAL_CALL
addEventListener(const OUString
& eventType
,
333 const Reference
< XEventListener
>& listener
,
335 throw (RuntimeException
);
337 virtual void SAL_CALL
removeEventListener(const OUString
& eventType
,
338 const Reference
< XEventListener
>& listener
,
340 throw (RuntimeException
);
342 virtual sal_Bool SAL_CALL
dispatchEvent(const Reference
< XEvent
>& evt
)
343 throw(RuntimeException
, EventException
);
347 /// eliminate redundant namespace declarations
348 void _nscleanup(const xmlNodePtr aNode
, const xmlNodePtr aParent
);