1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef DOM_NODE_HXX 29*cdf0e10cSrcweir #define DOM_NODE_HXX 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <hash_map> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <libxml/tree.h> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <sal/types.h> 36*cdf0e10cSrcweir #include <rtl/ref.hxx> 37*cdf0e10cSrcweir #include <rtl/string.hxx> 38*cdf0e10cSrcweir #include <rtl/ustring.hxx> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx> 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir #include <sax/fastattribs.hxx> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h> 45*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.h> 46*cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp> 47*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp> 48*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNodeList.hpp> 49*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNamedNodeMap.hpp> 50*cdf0e10cSrcweir #include <com/sun/star/xml/dom/NodeType.hpp> 51*cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XEventTarget.hpp> 52*cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XEvent.hpp> 53*cdf0e10cSrcweir #include <com/sun/star/xml/dom/DOMException.hpp> 54*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp> 55*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp> 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir using ::rtl::OUString; 59*cdf0e10cSrcweir using ::rtl::OString; 60*cdf0e10cSrcweir using namespace sax_fastparser; 61*cdf0e10cSrcweir using namespace com::sun::star::uno; 62*cdf0e10cSrcweir using namespace com::sun::star::xml::sax; 63*cdf0e10cSrcweir using namespace com::sun::star::xml::dom; 64*cdf0e10cSrcweir using namespace com::sun::star::xml::dom::events; 65*cdf0e10cSrcweir using com::sun::star::lang::XUnoTunnel; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir namespace DOM 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir struct Context 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir Context( const Reference< XFastDocumentHandler >& i_xHandler, 73*cdf0e10cSrcweir const Reference< XFastTokenHandler >& i_xTokenHandler ) : 74*cdf0e10cSrcweir maNamespaces( 1, std::vector<Namespace>() ), 75*cdf0e10cSrcweir maNamespaceMap(101), 76*cdf0e10cSrcweir mxAttribList(new FastAttributeList(i_xTokenHandler)), 77*cdf0e10cSrcweir mxCurrentHandler(i_xHandler, UNO_QUERY_THROW), 78*cdf0e10cSrcweir mxDocHandler(i_xHandler), 79*cdf0e10cSrcweir mxTokenHandler(i_xTokenHandler) 80*cdf0e10cSrcweir {} 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir struct Namespace 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir OString maPrefix; 85*cdf0e10cSrcweir sal_Int32 mnToken; 86*cdf0e10cSrcweir OUString maNamespaceURL; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir const OString& getPrefix() const { return maPrefix; } 89*cdf0e10cSrcweir }; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir typedef std::vector< std::vector<Namespace> > NamespaceVectorType; 92*cdf0e10cSrcweir typedef std::hash_map< OUString, 93*cdf0e10cSrcweir sal_Int32, 94*cdf0e10cSrcweir rtl::OUStringHash > NamespaceMapType; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir /// outer vector: xml context; inner vector: current NS 97*cdf0e10cSrcweir NamespaceVectorType maNamespaces; 98*cdf0e10cSrcweir NamespaceMapType maNamespaceMap; 99*cdf0e10cSrcweir ::rtl::Reference<FastAttributeList> mxAttribList; 100*cdf0e10cSrcweir Reference<XFastContextHandler> mxCurrentHandler; 101*cdf0e10cSrcweir Reference<XFastDocumentHandler> mxDocHandler; 102*cdf0e10cSrcweir Reference<XFastTokenHandler> mxTokenHandler; 103*cdf0e10cSrcweir }; 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir void pushContext(Context& io_rContext); 106*cdf0e10cSrcweir void popContext(Context& io_rContext); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir sal_Int32 getTokenWithPrefix( const Context& rContext, const sal_Char* xPrefix, const sal_Char* xName ); 109*cdf0e10cSrcweir sal_Int32 getToken( const Context& rContext, const sal_Char* xName ); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir /// add namespaces on this node to context 112*cdf0e10cSrcweir void addNamespaces(Context& io_rContext, xmlNodePtr pNode); 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir class CDocument; 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir class CNode : public cppu::WeakImplHelper3< XNode, XUnoTunnel, XEventTarget > 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir friend class CDocument; 119*cdf0e10cSrcweir friend class CElement; 120*cdf0e10cSrcweir friend class CAttributesMap; 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir private: 123*cdf0e10cSrcweir bool m_bUnlinked; /// node has been removed from document 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir protected: 126*cdf0e10cSrcweir NodeType const m_aNodeType; 127*cdf0e10cSrcweir /// libxml node; NB: not const, because invalidate may reset it to 0! 128*cdf0e10cSrcweir xmlNodePtr m_aNodePtr; 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir ::rtl::Reference< CDocument > const m_xDocument; 131*cdf0e10cSrcweir ::osl::Mutex & m_rMutex; 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir // for initialization by classes derived through ImplInheritanceHelper 134*cdf0e10cSrcweir CNode(CDocument const& rDocument, ::osl::Mutex const& rMutex, 135*cdf0e10cSrcweir NodeType const& reNodeType, xmlNodePtr const& rpNode); 136*cdf0e10cSrcweir void invalidate(); 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir void dispatchSubtreeModified(); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir public: 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir virtual ~CNode(); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir static CNode * GetImplementation(::com::sun::star::uno::Reference< 145*cdf0e10cSrcweir ::com::sun::star::uno::XInterface> const& xNode); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir xmlNodePtr GetNodePtr() { return m_aNodePtr; } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir virtual CDocument & GetOwnerDocument(); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir // recursively create SAX events 152*cdf0e10cSrcweir virtual void saxify(const Reference< XDocumentHandler >& i_xHandler); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir // recursively create SAX events 155*cdf0e10cSrcweir virtual void fastSaxify( Context& io_rContext ); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir // constrains child relationship between nodes based on type 158*cdf0e10cSrcweir virtual bool IsChildTypeAllowed(NodeType const nodeType); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir // ---- DOM interfaces 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir /** 163*cdf0e10cSrcweir Adds the node newChild to the end of the list of children of this node. 164*cdf0e10cSrcweir */ 165*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL 166*cdf0e10cSrcweir appendChild(Reference< XNode > const& xNewChild) 167*cdf0e10cSrcweir throw (RuntimeException, DOMException); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir /** 170*cdf0e10cSrcweir Returns a duplicate of this node, i.e., serves as a generic copy 171*cdf0e10cSrcweir constructor for nodes. 172*cdf0e10cSrcweir */ 173*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) 174*cdf0e10cSrcweir throw (RuntimeException); 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir /** 177*cdf0e10cSrcweir A NamedNodeMap containing the attributes of this node 178*cdf0e10cSrcweir (if it is an Element) or null otherwise. 179*cdf0e10cSrcweir */ 180*cdf0e10cSrcweir virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() 181*cdf0e10cSrcweir throw (RuntimeException); 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir /** 184*cdf0e10cSrcweir A NodeList that contains all children of this node. 185*cdf0e10cSrcweir */ 186*cdf0e10cSrcweir virtual Reference< XNodeList > SAL_CALL getChildNodes() 187*cdf0e10cSrcweir throw (RuntimeException); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir /** 190*cdf0e10cSrcweir The first child of this node. 191*cdf0e10cSrcweir */ 192*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL getFirstChild() 193*cdf0e10cSrcweir throw (RuntimeException); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir /** 196*cdf0e10cSrcweir The last child of this node. 197*cdf0e10cSrcweir */ 198*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL getLastChild() 199*cdf0e10cSrcweir throw (RuntimeException); 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir /** 202*cdf0e10cSrcweir Returns the local part of the qualified name of this node. 203*cdf0e10cSrcweir */ 204*cdf0e10cSrcweir virtual OUString SAL_CALL getLocalName() 205*cdf0e10cSrcweir throw (RuntimeException); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir /** 208*cdf0e10cSrcweir The namespace URI of this node, or null if it is unspecified. 209*cdf0e10cSrcweir */ 210*cdf0e10cSrcweir virtual OUString SAL_CALL getNamespaceURI() 211*cdf0e10cSrcweir throw (RuntimeException); 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir /** 214*cdf0e10cSrcweir The node immediately following this node. 215*cdf0e10cSrcweir */ 216*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL getNextSibling() 217*cdf0e10cSrcweir throw (RuntimeException); 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir /** 220*cdf0e10cSrcweir The name of this node, depending on its type; see the table above. 221*cdf0e10cSrcweir -- virtual implemented by actual node types 222*cdf0e10cSrcweir */ 223*cdf0e10cSrcweir virtual OUString SAL_CALL getNodeName() 224*cdf0e10cSrcweir throw (RuntimeException); 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir /** 227*cdf0e10cSrcweir A code representing the type of the underlying object, as defined above. 228*cdf0e10cSrcweir */ 229*cdf0e10cSrcweir virtual NodeType SAL_CALL getNodeType() 230*cdf0e10cSrcweir throw (RuntimeException); 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir /** 233*cdf0e10cSrcweir The value of this node, depending on its type; see the table above. 234*cdf0e10cSrcweir -- virtual implemented by actual node types 235*cdf0e10cSrcweir */ 236*cdf0e10cSrcweir virtual OUString SAL_CALL getNodeValue() 237*cdf0e10cSrcweir throw (RuntimeException); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir /** 240*cdf0e10cSrcweir The Document object associated with this node. 241*cdf0e10cSrcweir */ 242*cdf0e10cSrcweir virtual Reference< XDocument > SAL_CALL getOwnerDocument() 243*cdf0e10cSrcweir throw (RuntimeException); 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir /** 246*cdf0e10cSrcweir The parent of this node. 247*cdf0e10cSrcweir */ 248*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL getParentNode() 249*cdf0e10cSrcweir throw (RuntimeException); 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir /** 252*cdf0e10cSrcweir The namespace prefix of this node, or null if it is unspecified. 253*cdf0e10cSrcweir */ 254*cdf0e10cSrcweir virtual OUString SAL_CALL getPrefix() 255*cdf0e10cSrcweir throw (RuntimeException); 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir /** 258*cdf0e10cSrcweir The node immediately preceding this node. 259*cdf0e10cSrcweir */ 260*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL getPreviousSibling() 261*cdf0e10cSrcweir throw (RuntimeException); 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir /** 264*cdf0e10cSrcweir Returns whether this node (if it is an element) has any attributes. 265*cdf0e10cSrcweir */ 266*cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasAttributes() 267*cdf0e10cSrcweir throw (RuntimeException); 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir /** 270*cdf0e10cSrcweir Returns whether this node has any children. 271*cdf0e10cSrcweir */ 272*cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasChildNodes() 273*cdf0e10cSrcweir throw (RuntimeException); 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir /** 276*cdf0e10cSrcweir Inserts the node newChild before the existing child node refChild. 277*cdf0e10cSrcweir */ 278*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL insertBefore( 279*cdf0e10cSrcweir const Reference< XNode >& newChild, const Reference< XNode >& refChild) 280*cdf0e10cSrcweir throw (RuntimeException, DOMException); 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir /** 283*cdf0e10cSrcweir Tests whether the DOM implementation implements a specific feature and 284*cdf0e10cSrcweir that feature is supported by this node. 285*cdf0e10cSrcweir */ 286*cdf0e10cSrcweir virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) 287*cdf0e10cSrcweir throw (RuntimeException); 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir /** 290*cdf0e10cSrcweir Puts all Text nodes in the full depth of the sub-tree underneath this 291*cdf0e10cSrcweir Node, including attribute nodes, into a "normal" form where only structure 292*cdf0e10cSrcweir (e.g., elements, comments, processing instructions, CDATA sections, and 293*cdf0e10cSrcweir entity references) separates Text nodes, i.e., there are neither adjacent 294*cdf0e10cSrcweir Text nodes nor empty Text nodes. 295*cdf0e10cSrcweir */ 296*cdf0e10cSrcweir virtual void SAL_CALL normalize() 297*cdf0e10cSrcweir throw (RuntimeException); 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir /** 300*cdf0e10cSrcweir Removes the child node indicated by oldChild from the list of children, 301*cdf0e10cSrcweir and returns it. 302*cdf0e10cSrcweir */ 303*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) 304*cdf0e10cSrcweir throw (RuntimeException, DOMException); 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir /** 307*cdf0e10cSrcweir Replaces the child node oldChild with newChild in the list of children, 308*cdf0e10cSrcweir and returns the oldChild node. 309*cdf0e10cSrcweir */ 310*cdf0e10cSrcweir virtual Reference< XNode > SAL_CALL replaceChild( 311*cdf0e10cSrcweir const Reference< XNode >& newChild, const Reference< XNode >& oldChild) 312*cdf0e10cSrcweir throw (RuntimeException, DOMException); 313*cdf0e10cSrcweir 314*cdf0e10cSrcweir /** 315*cdf0e10cSrcweir The value of this node, depending on its type; see the table above. 316*cdf0e10cSrcweir */ 317*cdf0e10cSrcweir virtual void SAL_CALL setNodeValue(const OUString& nodeValue) 318*cdf0e10cSrcweir throw (RuntimeException, DOMException); 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir /** 321*cdf0e10cSrcweir The namespace prefix of this node, or null if it is unspecified. 322*cdf0e10cSrcweir */ 323*cdf0e10cSrcweir virtual void SAL_CALL setPrefix(const OUString& prefix) 324*cdf0e10cSrcweir throw (RuntimeException, DOMException); 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir // --- XEventTarget 328*cdf0e10cSrcweir virtual void SAL_CALL addEventListener(const OUString& eventType, 329*cdf0e10cSrcweir const Reference< XEventListener >& listener, 330*cdf0e10cSrcweir sal_Bool useCapture) 331*cdf0e10cSrcweir throw (RuntimeException); 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir virtual void SAL_CALL removeEventListener(const OUString& eventType, 334*cdf0e10cSrcweir const Reference< XEventListener >& listener, 335*cdf0e10cSrcweir sal_Bool useCapture) 336*cdf0e10cSrcweir throw (RuntimeException); 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir virtual sal_Bool SAL_CALL dispatchEvent(const Reference< XEvent >& evt) 339*cdf0e10cSrcweir throw(RuntimeException, EventException); 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir // --- XUnoTunnel 342*cdf0e10cSrcweir virtual ::sal_Int64 SAL_CALL 343*cdf0e10cSrcweir getSomething(Sequence< ::sal_Int8 > const& rId) 344*cdf0e10cSrcweir throw (RuntimeException); 345*cdf0e10cSrcweir }; 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir /// eliminate redundant namespace declarations 348*cdf0e10cSrcweir void nscleanup(const xmlNodePtr aNode, const xmlNodePtr aParent); 349*cdf0e10cSrcweir } 350*cdf0e10cSrcweir 351*cdf0e10cSrcweir #endif 352