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