LCOV - code coverage report
Current view: top level - libreoffice/unoxml/source/dom - elementlist.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 55 0.0 %
Date: 2012-12-17 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "elementlist.hxx"
      21             : 
      22             : #include <string.h>
      23             : 
      24             : #include <element.hxx>
      25             : #include <document.hxx>
      26             : 
      27             : 
      28             : namespace DOM
      29             : {
      30             : 
      31           0 :     static xmlChar* lcl_initXmlString(::rtl::OUString const& rString)
      32             :     {
      33             :         ::rtl::OString const os =
      34           0 :             ::rtl::OUStringToOString(rString, RTL_TEXTENCODING_UTF8);
      35           0 :         xmlChar *const pRet = new xmlChar[os.getLength() + 1];
      36           0 :         strcpy(reinterpret_cast<char*>(pRet), os.getStr());
      37           0 :         return pRet;
      38             :     }
      39             : 
      40           0 :     CElementList::CElementList(::rtl::Reference<CElement> const& pElement,
      41             :             ::osl::Mutex & rMutex,
      42             :             OUString const& rName, OUString const*const pURI)
      43             :         : m_pElement(pElement)
      44             :         , m_rMutex(rMutex)
      45             :         , m_pName(lcl_initXmlString(rName))
      46             :         , m_pURI((pURI) ? lcl_initXmlString(*pURI) : 0)
      47           0 :         , m_bRebuild(true)
      48             :     {
      49           0 :         if (m_pElement.is()) {
      50           0 :             registerListener(*m_pElement);
      51             :         }
      52           0 :     }
      53             : 
      54           0 :     void CElementList::registerListener(CElement & rElement)
      55             :     {
      56             :         try {
      57             :             Reference< XEventTarget > const xTarget(
      58           0 :                     static_cast<XElement*>(& rElement), UNO_QUERY_THROW);
      59           0 :             sal_Bool capture = sal_False;
      60           0 :             xTarget->addEventListener("DOMSubtreeModified",
      61           0 :                     Reference< XEventListener >(this), capture);
      62           0 :         } catch (const Exception &e){
      63           0 :             OString aMsg("Exception caught while registering NodeList as listener:\n");
      64           0 :             aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
      65           0 :             OSL_FAIL(aMsg.getStr());
      66             :         }
      67           0 :     }
      68             : 
      69           0 :     void CElementList::buildlist(xmlNodePtr pNode, sal_Bool start)
      70             :     {
      71             :         // bail out if no rebuild is needed
      72           0 :         if (start) {
      73           0 :             if (!m_bRebuild)
      74             :             {
      75           0 :                 return;
      76             :             } else {
      77           0 :                 m_nodevector.erase(m_nodevector.begin(), m_nodevector.end());
      78           0 :                 m_bRebuild = false; // don't rebuild until tree is mutated
      79             :             }
      80             :         }
      81             : 
      82           0 :         while (pNode != NULL )
      83             :         {
      84           0 :             if (pNode->type == XML_ELEMENT_NODE &&
      85           0 :                 (strcmp((char*)pNode->name, (char*)m_pName.get()) == 0))
      86             :             {
      87           0 :                 if (!m_pURI) {
      88           0 :                     m_nodevector.push_back(pNode);
      89             :                 } else {
      90           0 :                     if (pNode->ns != NULL && (0 ==
      91           0 :                          strcmp((char*)pNode->ns->href, (char*)m_pURI.get())))
      92             :                     {
      93           0 :                         m_nodevector.push_back(pNode);
      94             :                     }
      95             :                 }
      96             :             }
      97           0 :             if (pNode->children != NULL) buildlist(pNode->children, sal_False);
      98             : 
      99           0 :             if (!start) pNode = pNode->next;
     100           0 :             else break; // fold back
     101             :         }
     102             :     }
     103             : 
     104             :     /**
     105             :     The number of nodes in the list.
     106             :     */
     107           0 :     sal_Int32 SAL_CALL CElementList::getLength() throw (RuntimeException)
     108             :     {
     109           0 :         ::osl::MutexGuard const g(m_rMutex);
     110             : 
     111           0 :         if (!m_pElement.is()) { return 0; }
     112             : 
     113             :         // this has to be 'live'
     114           0 :         buildlist(m_pElement->GetNodePtr());
     115           0 :         return m_nodevector.size();
     116             :     }
     117             :     /**
     118             :     Returns the indexth item in the collection.
     119             :     */
     120           0 :     Reference< XNode > SAL_CALL CElementList::item(sal_Int32 index)
     121             :         throw (RuntimeException)
     122             :     {
     123           0 :         if (index < 0) throw RuntimeException();
     124             : 
     125           0 :         ::osl::MutexGuard const g(m_rMutex);
     126             : 
     127           0 :         if (!m_pElement.is()) { return 0; }
     128             : 
     129           0 :         buildlist(m_pElement->GetNodePtr());
     130           0 :         if (m_nodevector.size() <= static_cast<size_t>(index)) {
     131           0 :             throw RuntimeException();
     132             :         }
     133             :         Reference< XNode > const xRet(
     134           0 :             m_pElement->GetOwnerDocument().GetCNode(m_nodevector[index]).get());
     135           0 :         return xRet;
     136             :     }
     137             : 
     138             :     // tree mutations can change the list
     139           0 :     void SAL_CALL CElementList::handleEvent(Reference< XEvent > const&)
     140             :         throw (RuntimeException)
     141             :     {
     142           0 :         ::osl::MutexGuard const g(m_rMutex);
     143             : 
     144           0 :         m_bRebuild = true;
     145           0 :     }
     146             : }
     147             : 
     148             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10