2010-10-12 15:56:41 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-21 14:30:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2004-11-16 11:18:40 +00:00
|
|
|
|
2011-01-19 20:27:24 +01:00
|
|
|
#include <attributesmap.hxx>
|
|
|
|
|
2008-02-04 12:55:57 +00:00
|
|
|
#include <string.h>
|
2004-11-16 11:18:40 +00:00
|
|
|
|
2011-01-19 20:27:24 +01:00
|
|
|
#include <element.hxx>
|
2011-01-19 20:27:25 +01:00
|
|
|
#include <document.hxx>
|
2011-01-19 20:27:24 +01:00
|
|
|
|
2014-05-21 12:18:36 +02:00
|
|
|
using namespace css::uno;
|
|
|
|
using namespace css::xml::dom;
|
2011-01-19 20:27:24 +01:00
|
|
|
|
2004-11-16 11:18:40 +00:00
|
|
|
namespace DOM
|
|
|
|
{
|
2011-01-19 20:27:26 +01:00
|
|
|
CAttributesMap::CAttributesMap(::rtl::Reference<CElement> const& pElement,
|
|
|
|
::osl::Mutex & rMutex)
|
2011-01-19 20:27:24 +01:00
|
|
|
: m_pElement(pElement)
|
2011-01-19 20:27:26 +01:00
|
|
|
, m_rMutex(rMutex)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The number of nodes in this map.
|
|
|
|
*/
|
2014-02-25 21:31:58 +01:00
|
|
|
sal_Int32 SAL_CALL CAttributesMap::getLength() throw (RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-01-19 20:27:26 +01:00
|
|
|
::osl::MutexGuard const g(m_rMutex);
|
|
|
|
|
2004-11-16 11:18:40 +00:00
|
|
|
sal_Int32 count = 0;
|
2011-01-19 20:27:27 +01:00
|
|
|
xmlNodePtr pNode = m_pElement->GetNodePtr();
|
2015-11-10 10:28:02 +01:00
|
|
|
if (pNode != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
|
|
|
xmlAttrPtr cur = pNode->properties;
|
2015-11-10 10:28:02 +01:00
|
|
|
while (cur != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Retrieves a node specified by local name
|
|
|
|
*/
|
2011-01-19 20:27:24 +01:00
|
|
|
Reference< XNode > SAL_CALL
|
2014-02-25 21:31:58 +01:00
|
|
|
CAttributesMap::getNamedItem(OUString const& name) throw (RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-01-19 20:27:26 +01:00
|
|
|
::osl::MutexGuard const g(m_rMutex);
|
|
|
|
|
2004-11-16 11:18:40 +00:00
|
|
|
Reference< XNode > aNode;
|
2011-01-19 20:27:27 +01:00
|
|
|
xmlNodePtr pNode = m_pElement->GetNodePtr();
|
2015-11-10 10:28:02 +01:00
|
|
|
if (pNode != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
|
|
|
OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
|
2015-01-18 21:41:32 +01:00
|
|
|
xmlChar const * xName = reinterpret_cast<xmlChar const *>(o1.getStr());
|
2004-11-16 11:18:40 +00:00
|
|
|
xmlAttrPtr cur = pNode->properties;
|
2015-11-10 10:28:02 +01:00
|
|
|
while (cur != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2015-01-18 21:41:32 +01:00
|
|
|
if( strcmp(reinterpret_cast<char const *>(xName), reinterpret_cast<char const *>(cur->name)) == 0)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2015-11-05 12:42:10 +02:00
|
|
|
aNode.set( m_pElement->GetOwnerDocument().GetCNode(
|
|
|
|
reinterpret_cast<xmlNodePtr>(cur)).get() );
|
2004-11-16 11:18:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Retrieves a node specified by local name and namespace URI.
|
|
|
|
*/
|
2011-01-19 20:27:24 +01:00
|
|
|
Reference< XNode > SAL_CALL
|
|
|
|
CAttributesMap::getNamedItemNS(
|
|
|
|
OUString const& namespaceURI, OUString const& localName)
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-01-19 20:27:26 +01:00
|
|
|
::osl::MutexGuard const g(m_rMutex);
|
|
|
|
|
2004-11-16 11:18:40 +00:00
|
|
|
Reference< XNode > aNode;
|
2011-01-19 20:27:27 +01:00
|
|
|
xmlNodePtr pNode = m_pElement->GetNodePtr();
|
2015-11-10 10:28:02 +01:00
|
|
|
if (pNode != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
|
|
|
OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
|
2015-01-18 21:41:32 +01:00
|
|
|
xmlChar const * xName = reinterpret_cast<xmlChar const *>(o1.getStr());
|
2004-11-16 11:18:40 +00:00
|
|
|
OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
|
2011-02-10 16:45:03 +01:00
|
|
|
xmlChar const*const xNs =
|
|
|
|
reinterpret_cast<xmlChar const*>(o2.getStr());
|
|
|
|
xmlNsPtr const pNs = xmlSearchNsByHref(pNode->doc, pNode, xNs);
|
2004-11-16 11:18:40 +00:00
|
|
|
xmlAttrPtr cur = pNode->properties;
|
2015-11-10 10:28:02 +01:00
|
|
|
while (cur != nullptr && pNs != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2015-01-18 21:41:32 +01:00
|
|
|
if( strcmp(reinterpret_cast<char const *>(xName), reinterpret_cast<char const *>(cur->name)) == 0 &&
|
2004-11-16 11:18:40 +00:00
|
|
|
cur->ns == pNs)
|
|
|
|
{
|
2015-11-05 12:42:10 +02:00
|
|
|
aNode.set( m_pElement->GetOwnerDocument().GetCNode(
|
|
|
|
reinterpret_cast<xmlNodePtr>(cur)).get() );
|
2004-11-16 11:18:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the indexth item in the map.
|
|
|
|
*/
|
2011-01-19 20:27:24 +01:00
|
|
|
Reference< XNode > SAL_CALL
|
2014-02-25 21:31:58 +01:00
|
|
|
CAttributesMap::item(sal_Int32 index) throw (RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-01-19 20:27:26 +01:00
|
|
|
::osl::MutexGuard const g(m_rMutex);
|
|
|
|
|
2004-11-16 11:18:40 +00:00
|
|
|
Reference< XNode > aNode;
|
2011-01-19 20:27:27 +01:00
|
|
|
xmlNodePtr pNode = m_pElement->GetNodePtr();
|
2015-11-10 10:28:02 +01:00
|
|
|
if (pNode != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
|
|
|
xmlAttrPtr cur = pNode->properties;
|
|
|
|
sal_Int32 count = 0;
|
2015-11-10 10:28:02 +01:00
|
|
|
while (cur != nullptr)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
|
|
|
if (count == index)
|
|
|
|
{
|
2015-11-05 12:42:10 +02:00
|
|
|
aNode.set( m_pElement->GetOwnerDocument().GetCNode(
|
2011-01-19 20:27:15 +01:00
|
|
|
reinterpret_cast<xmlNodePtr>(cur)).get() );
|
2004-11-16 11:18:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Removes a node specified by name.
|
|
|
|
*/
|
2011-01-19 20:27:24 +01:00
|
|
|
Reference< XNode > SAL_CALL
|
|
|
|
CAttributesMap::removeNamedItem(OUString const& name)
|
2014-04-08 10:44:35 +01:00
|
|
|
throw (DOMException, RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-02-10 16:45:04 +01:00
|
|
|
// no MutexGuard needed: m_pElement is const
|
|
|
|
Reference< XAttr > const xAttr(m_pElement->getAttributeNode(name));
|
|
|
|
if (!xAttr.is()) {
|
2014-05-26 16:59:44 +02:00
|
|
|
throw DOMException(
|
|
|
|
"CAttributesMap::removeNamedItem: no such attribute",
|
2011-02-10 16:45:04 +01:00
|
|
|
static_cast<OWeakObject*>(this),
|
|
|
|
DOMExceptionType_NOT_FOUND_ERR);
|
2004-11-16 11:18:40 +00:00
|
|
|
}
|
2011-02-10 16:45:04 +01:00
|
|
|
Reference< XNode > const xRet(
|
|
|
|
m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
|
|
|
|
return xRet;
|
2004-11-16 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
// Removes a node specified by local name and namespace URI.
|
|
|
|
*/
|
2011-01-19 20:27:24 +01:00
|
|
|
Reference< XNode > SAL_CALL
|
|
|
|
CAttributesMap::removeNamedItemNS(
|
|
|
|
OUString const& namespaceURI, OUString const& localName)
|
2014-04-08 10:44:35 +01:00
|
|
|
throw (DOMException, RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-02-10 16:45:04 +01:00
|
|
|
// no MutexGuard needed: m_pElement is const
|
|
|
|
Reference< XAttr > const xAttr(
|
|
|
|
m_pElement->getAttributeNodeNS(namespaceURI, localName));
|
|
|
|
if (!xAttr.is()) {
|
2014-05-26 16:59:44 +02:00
|
|
|
throw DOMException(
|
|
|
|
"CAttributesMap::removeNamedItemNS: no such attribute",
|
2011-02-10 16:45:04 +01:00
|
|
|
static_cast<OWeakObject*>(this),
|
|
|
|
DOMExceptionType_NOT_FOUND_ERR);
|
2004-11-16 11:18:40 +00:00
|
|
|
}
|
2011-02-10 16:45:04 +01:00
|
|
|
Reference< XNode > const xRet(
|
|
|
|
m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
|
|
|
|
return xRet;
|
2004-11-16 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
// Adds a node using its nodeName attribute.
|
|
|
|
*/
|
2011-01-19 20:27:24 +01:00
|
|
|
Reference< XNode > SAL_CALL
|
2011-02-10 16:45:03 +01:00
|
|
|
CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
|
2014-04-08 10:44:35 +01:00
|
|
|
throw (DOMException, RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-02-10 16:45:03 +01:00
|
|
|
Reference< XAttr > const xAttr(xNode, UNO_QUERY);
|
|
|
|
if (!xNode.is()) {
|
2014-05-26 16:59:44 +02:00
|
|
|
throw DOMException(
|
|
|
|
"CAttributesMap::setNamedItem: XAttr argument expected",
|
2011-02-10 16:45:03 +01:00
|
|
|
static_cast<OWeakObject*>(this),
|
|
|
|
DOMExceptionType_HIERARCHY_REQUEST_ERR);
|
|
|
|
}
|
|
|
|
// no MutexGuard needed: m_pElement is const
|
|
|
|
Reference< XNode > const xRet(
|
|
|
|
m_pElement->setAttributeNode(xAttr), UNO_QUERY);
|
|
|
|
return xRet;
|
2004-11-16 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Adds a node using its namespaceURI and localName.
|
|
|
|
*/
|
2011-01-19 20:27:24 +01:00
|
|
|
Reference< XNode > SAL_CALL
|
2011-02-10 16:45:03 +01:00
|
|
|
CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
|
2014-04-08 10:44:35 +01:00
|
|
|
throw (DOMException, RuntimeException, std::exception)
|
2004-11-16 11:18:40 +00:00
|
|
|
{
|
2011-02-10 16:45:03 +01:00
|
|
|
Reference< XAttr > const xAttr(xNode, UNO_QUERY);
|
|
|
|
if (!xNode.is()) {
|
2014-05-26 16:59:44 +02:00
|
|
|
throw DOMException(
|
|
|
|
"CAttributesMap::setNamedItemNS: XAttr argument expected",
|
2011-02-10 16:45:03 +01:00
|
|
|
static_cast<OWeakObject*>(this),
|
|
|
|
DOMExceptionType_HIERARCHY_REQUEST_ERR);
|
|
|
|
}
|
|
|
|
// no MutexGuard needed: m_pElement is const
|
|
|
|
Reference< XNode > const xRet(
|
|
|
|
m_pElement->setAttributeNodeNS(xAttr), UNO_QUERY);
|
|
|
|
return xRet;
|
2004-11-16 11:18:40 +00:00
|
|
|
}
|
2006-06-19 23:43:33 +00:00
|
|
|
}
|
2010-10-12 15:56:41 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|