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 <attributesmap.hxx>
21 :
22 : #include <string.h>
23 :
24 : #include <element.hxx>
25 : #include <document.hxx>
26 :
27 : using namespace css::uno;
28 : using namespace css::xml::dom;
29 :
30 : namespace DOM
31 : {
32 41891 : CAttributesMap::CAttributesMap(::rtl::Reference<CElement> const& pElement,
33 : ::osl::Mutex & rMutex)
34 : : m_pElement(pElement)
35 41891 : , m_rMutex(rMutex)
36 : {
37 41891 : }
38 :
39 : /**
40 : The number of nodes in this map.
41 : */
42 14750 : sal_Int32 SAL_CALL CAttributesMap::getLength() throw (RuntimeException, std::exception)
43 : {
44 14750 : ::osl::MutexGuard const g(m_rMutex);
45 :
46 14750 : sal_Int32 count = 0;
47 14750 : xmlNodePtr pNode = m_pElement->GetNodePtr();
48 14750 : if (pNode != NULL)
49 : {
50 14750 : xmlAttrPtr cur = pNode->properties;
51 71144 : while (cur != NULL)
52 : {
53 41644 : count++;
54 41644 : cur = cur->next;
55 : }
56 : }
57 14750 : return count;
58 : }
59 :
60 : /**
61 : Retrieves a node specified by local name
62 : */
63 : Reference< XNode > SAL_CALL
64 56092 : CAttributesMap::getNamedItem(OUString const& name) throw (RuntimeException, std::exception)
65 : {
66 56092 : ::osl::MutexGuard const g(m_rMutex);
67 :
68 56092 : Reference< XNode > aNode;
69 56092 : xmlNodePtr pNode = m_pElement->GetNodePtr();
70 56092 : if (pNode != NULL)
71 : {
72 56092 : OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
73 56092 : xmlChar const * xName = reinterpret_cast<xmlChar const *>(o1.getStr());
74 56092 : xmlAttrPtr cur = pNode->properties;
75 162494 : while (cur != NULL)
76 : {
77 106402 : if( strcmp(reinterpret_cast<char const *>(xName), reinterpret_cast<char const *>(cur->name)) == 0)
78 : {
79 112184 : aNode = Reference< XNode >(
80 112184 : m_pElement->GetOwnerDocument().GetCNode(
81 224368 : reinterpret_cast<xmlNodePtr>(cur)).get() );
82 56092 : break;
83 : }
84 50310 : cur = cur->next;
85 56092 : }
86 : }
87 56092 : return aNode;
88 : }
89 :
90 : /**
91 : Retrieves a node specified by local name and namespace URI.
92 : */
93 : Reference< XNode > SAL_CALL
94 3 : CAttributesMap::getNamedItemNS(
95 : OUString const& namespaceURI, OUString const& localName)
96 : throw (RuntimeException, std::exception)
97 : {
98 3 : ::osl::MutexGuard const g(m_rMutex);
99 :
100 3 : Reference< XNode > aNode;
101 3 : xmlNodePtr pNode = m_pElement->GetNodePtr();
102 3 : if (pNode != NULL)
103 : {
104 3 : OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
105 3 : xmlChar const * xName = reinterpret_cast<xmlChar const *>(o1.getStr());
106 6 : OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
107 : xmlChar const*const xNs =
108 3 : reinterpret_cast<xmlChar const*>(o2.getStr());
109 3 : xmlNsPtr const pNs = xmlSearchNsByHref(pNode->doc, pNode, xNs);
110 3 : xmlAttrPtr cur = pNode->properties;
111 8 : while (cur != NULL && pNs != NULL)
112 : {
113 10 : if( strcmp(reinterpret_cast<char const *>(xName), reinterpret_cast<char const *>(cur->name)) == 0 &&
114 5 : cur->ns == pNs)
115 : {
116 6 : aNode = Reference< XNode >(
117 6 : m_pElement->GetOwnerDocument().GetCNode(
118 12 : reinterpret_cast<xmlNodePtr>(cur)).get() );
119 3 : break;
120 : }
121 2 : cur = cur->next;
122 3 : }
123 : }
124 3 : return aNode;
125 : }
126 :
127 : /**
128 : Returns the indexth item in the map.
129 : */
130 : Reference< XNode > SAL_CALL
131 10204 : CAttributesMap::item(sal_Int32 index) throw (RuntimeException, std::exception)
132 : {
133 10204 : ::osl::MutexGuard const g(m_rMutex);
134 :
135 10204 : Reference< XNode > aNode;
136 10204 : xmlNodePtr pNode = m_pElement->GetNodePtr();
137 10204 : if (pNode != NULL)
138 : {
139 10204 : xmlAttrPtr cur = pNode->properties;
140 10204 : sal_Int32 count = 0;
141 31028 : while (cur != NULL)
142 : {
143 20823 : if (count == index)
144 : {
145 20406 : aNode = Reference< XNode >(
146 20406 : m_pElement->GetOwnerDocument().GetCNode(
147 40812 : reinterpret_cast<xmlNodePtr>(cur)).get() );
148 10203 : break;
149 : }
150 10620 : count++;
151 10620 : cur = cur->next;
152 : }
153 : }
154 10204 : return aNode;
155 : }
156 :
157 : /**
158 : Removes a node specified by name.
159 : */
160 : Reference< XNode > SAL_CALL
161 1 : CAttributesMap::removeNamedItem(OUString const& name)
162 : throw (DOMException, RuntimeException, std::exception)
163 : {
164 : // no MutexGuard needed: m_pElement is const
165 1 : Reference< XAttr > const xAttr(m_pElement->getAttributeNode(name));
166 1 : if (!xAttr.is()) {
167 : throw DOMException(
168 : "CAttributesMap::removeNamedItem: no such attribute",
169 : static_cast<OWeakObject*>(this),
170 0 : DOMExceptionType_NOT_FOUND_ERR);
171 : }
172 : Reference< XNode > const xRet(
173 1 : m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
174 1 : return xRet;
175 : }
176 :
177 : /**
178 : // Removes a node specified by local name and namespace URI.
179 : */
180 : Reference< XNode > SAL_CALL
181 1 : CAttributesMap::removeNamedItemNS(
182 : OUString const& namespaceURI, OUString const& localName)
183 : throw (DOMException, RuntimeException, std::exception)
184 : {
185 : // no MutexGuard needed: m_pElement is const
186 : Reference< XAttr > const xAttr(
187 1 : m_pElement->getAttributeNodeNS(namespaceURI, localName));
188 1 : if (!xAttr.is()) {
189 : throw DOMException(
190 : "CAttributesMap::removeNamedItemNS: no such attribute",
191 : static_cast<OWeakObject*>(this),
192 0 : DOMExceptionType_NOT_FOUND_ERR);
193 : }
194 : Reference< XNode > const xRet(
195 1 : m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
196 1 : return xRet;
197 : }
198 :
199 : /**
200 : // Adds a node using its nodeName attribute.
201 : */
202 : Reference< XNode > SAL_CALL
203 1 : CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
204 : throw (DOMException, RuntimeException, std::exception)
205 : {
206 1 : Reference< XAttr > const xAttr(xNode, UNO_QUERY);
207 1 : if (!xNode.is()) {
208 : throw DOMException(
209 : "CAttributesMap::setNamedItem: XAttr argument expected",
210 : static_cast<OWeakObject*>(this),
211 0 : DOMExceptionType_HIERARCHY_REQUEST_ERR);
212 : }
213 : // no MutexGuard needed: m_pElement is const
214 : Reference< XNode > const xRet(
215 1 : m_pElement->setAttributeNode(xAttr), UNO_QUERY);
216 1 : return xRet;
217 : }
218 :
219 : /**
220 : Adds a node using its namespaceURI and localName.
221 : */
222 : Reference< XNode > SAL_CALL
223 1 : CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
224 : throw (DOMException, RuntimeException, std::exception)
225 : {
226 1 : Reference< XAttr > const xAttr(xNode, UNO_QUERY);
227 1 : if (!xNode.is()) {
228 : throw DOMException(
229 : "CAttributesMap::setNamedItemNS: XAttr argument expected",
230 : static_cast<OWeakObject*>(this),
231 0 : DOMExceptionType_HIERARCHY_REQUEST_ERR);
232 : }
233 : // no MutexGuard needed: m_pElement is const
234 : Reference< XNode > const xRet(
235 1 : m_pElement->setAttributeNodeNS(xAttr), UNO_QUERY);
236 1 : return xRet;
237 : }
238 : }
239 :
240 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|