1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <attributesmap.hxx>
25 
26 #include <string.h>
27 
28 #include <element.hxx>
29 #include <document.hxx>
30 
31 
32 namespace DOM
33 {
CAttributesMap(::rtl::Reference<CElement> const & pElement,::osl::Mutex & rMutex)34     CAttributesMap::CAttributesMap(::rtl::Reference<CElement> const& pElement,
35                 ::osl::Mutex & rMutex)
36         : m_pElement(pElement)
37         , m_rMutex(rMutex)
38     {
39     }
40 
41     /**
42     The number of nodes in this map.
43     */
getLength()44     sal_Int32 SAL_CALL CAttributesMap::getLength() throw (RuntimeException)
45     {
46         ::osl::MutexGuard const g(m_rMutex);
47 
48         sal_Int32 count = 0;
49         xmlNodePtr pNode = m_pElement->GetNodePtr();
50         if (pNode != NULL)
51         {
52             xmlAttrPtr cur = pNode->properties;
53             while (cur != NULL)
54             {
55                 count++;
56                 cur = cur->next;
57             }
58         }
59         return count;
60     }
61 
62     /**
63     Retrieves a node specified by local name
64     */
65     Reference< XNode > SAL_CALL
getNamedItem(OUString const & name)66     CAttributesMap::getNamedItem(OUString const& name) throw (RuntimeException)
67     {
68         ::osl::MutexGuard const g(m_rMutex);
69 
70         Reference< XNode > aNode;
71         xmlNodePtr pNode = m_pElement->GetNodePtr();
72         if (pNode != NULL)
73         {
74             OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
75             xmlChar* xName = (xmlChar*)o1.getStr();
76             xmlAttrPtr cur = pNode->properties;
77             while (cur != NULL)
78             {
79                 if( strcmp((char*)xName, (char*)cur->name) == 0)
80                 {
81                     aNode = Reference< XNode >(
82                             m_pElement->GetOwnerDocument().GetCNode(
83                                 reinterpret_cast<xmlNodePtr>(cur)).get() );
84                     break;
85                 }
86                 cur = cur->next;
87             }
88         }
89         return aNode;
90     }
91 
92     /**
93     Retrieves a node specified by local name and namespace URI.
94     */
95     Reference< XNode > SAL_CALL
getNamedItemNS(OUString const & namespaceURI,OUString const & localName)96     CAttributesMap::getNamedItemNS(
97             OUString const& namespaceURI, OUString const& localName)
98     throw (RuntimeException)
99     {
100         ::osl::MutexGuard const g(m_rMutex);
101 
102         Reference< XNode > aNode;
103         xmlNodePtr pNode = m_pElement->GetNodePtr();
104         if (pNode != NULL)
105         {
106             OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
107             xmlChar* xName = (xmlChar*)o1.getStr();
108             OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
109             xmlChar const*const xNs =
110                 reinterpret_cast<xmlChar const*>(o2.getStr());
111             xmlNsPtr const pNs = xmlSearchNsByHref(pNode->doc, pNode, xNs);
112             xmlAttrPtr cur = pNode->properties;
113             while (cur != NULL && pNs != NULL)
114             {
115                 if( strcmp((char*)xName, (char*)cur->name) == 0 &&
116                     cur->ns == pNs)
117                 {
118                     aNode = Reference< XNode >(
119                             m_pElement->GetOwnerDocument().GetCNode(
120                                 reinterpret_cast<xmlNodePtr>(cur)).get() );
121                     break;
122                 }
123                 cur = cur->next;
124             }
125         }
126         return aNode;
127     }
128 
129     /**
130     Returns the indexth item in the map.
131     */
132     Reference< XNode > SAL_CALL
item(sal_Int32 index)133     CAttributesMap::item(sal_Int32 index) throw (RuntimeException)
134     {
135         ::osl::MutexGuard const g(m_rMutex);
136 
137         Reference< XNode > aNode;
138         xmlNodePtr pNode = m_pElement->GetNodePtr();
139         if (pNode != NULL)
140         {
141             xmlAttrPtr cur = pNode->properties;
142             sal_Int32 count = 0;
143             while (cur != NULL)
144             {
145                 if (count == index)
146                 {
147                     aNode = Reference< XNode >(
148                             m_pElement->GetOwnerDocument().GetCNode(
149                                 reinterpret_cast<xmlNodePtr>(cur)).get() );
150                     break;
151                 }
152                 count++;
153                 cur = cur->next;
154             }
155         }
156         return aNode;
157     }
158 
159     /**
160     Removes a node specified by name.
161     */
162     Reference< XNode > SAL_CALL
removeNamedItem(OUString const & name)163     CAttributesMap::removeNamedItem(OUString const& name)
164     throw (RuntimeException)
165     {
166         // no MutexGuard needed: m_pElement is const
167         Reference< XAttr > const xAttr(m_pElement->getAttributeNode(name));
168         if (!xAttr.is()) {
169             throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM(
170                     "CAttributesMap::removeNamedItem: no such attribute")),
171                 static_cast<OWeakObject*>(this),
172                 DOMExceptionType_NOT_FOUND_ERR);
173         }
174         Reference< XNode > const xRet(
175             m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
176         return xRet;
177     }
178 
179     /**
180     // Removes a node specified by local name and namespace URI.
181     */
182     Reference< XNode > SAL_CALL
removeNamedItemNS(OUString const & namespaceURI,OUString const & localName)183     CAttributesMap::removeNamedItemNS(
184             OUString const& namespaceURI, OUString const& localName)
185     throw (RuntimeException)
186     {
187         // no MutexGuard needed: m_pElement is const
188         Reference< XAttr > const xAttr(
189             m_pElement->getAttributeNodeNS(namespaceURI, localName));
190         if (!xAttr.is()) {
191             throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM(
192                     "CAttributesMap::removeNamedItemNS: no such attribute")),
193                 static_cast<OWeakObject*>(this),
194                 DOMExceptionType_NOT_FOUND_ERR);
195         }
196         Reference< XNode > const xRet(
197             m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
198         return xRet;
199     }
200 
201     /**
202     // Adds a node using its nodeName attribute.
203     */
204     Reference< XNode > SAL_CALL
setNamedItem(Reference<XNode> const & xNode)205     CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
206     throw (RuntimeException)
207     {
208         Reference< XAttr > const xAttr(xNode, UNO_QUERY);
209         if (!xNode.is()) {
210             throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM(
211                     "CAttributesMap::setNamedItem: XAttr argument expected")),
212                 static_cast<OWeakObject*>(this),
213                 DOMExceptionType_HIERARCHY_REQUEST_ERR);
214         }
215         // no MutexGuard needed: m_pElement is const
216         Reference< XNode > const xRet(
217             m_pElement->setAttributeNode(xAttr), UNO_QUERY);
218         return xRet;
219     }
220 
221     /**
222     Adds a node using its namespaceURI and localName.
223     */
224     Reference< XNode > SAL_CALL
setNamedItemNS(Reference<XNode> const & xNode)225     CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
226     throw (RuntimeException)
227     {
228         Reference< XAttr > const xAttr(xNode, UNO_QUERY);
229         if (!xNode.is()) {
230             throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM(
231                     "CAttributesMap::setNamedItemNS: XAttr argument expected")),
232                 static_cast<OWeakObject*>(this),
233                 DOMExceptionType_HIERARCHY_REQUEST_ERR);
234         }
235         // no MutexGuard needed: m_pElement is const
236         Reference< XNode > const xRet(
237             m_pElement->setAttributeNodeNS(xAttr), UNO_QUERY);
238         return xRet;
239     }
240 }
241