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