1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
26 #include <osl/diagnose.h>
28 #include <com/sun/star/xml/dom/DOMException.hpp>
29 #include <com/sun/star/xml/dom/events/XMutationEvent.hpp>
31 #include "document.hxx"
33 using namespace css::uno
;
34 using namespace css::xml::dom
;
35 using namespace css::xml::dom::events
;
39 CAttr::CAttr(CDocument
const& rDocument
, ::osl::Mutex
const& rMutex
,
40 xmlAttrPtr
const pAttr
)
41 : CAttr_Base(rDocument
, rMutex
,
42 NodeType_ATTRIBUTE_NODE
, reinterpret_cast<xmlNodePtr
>(pAttr
))
47 xmlNsPtr
CAttr::GetNamespace(xmlNodePtr
const pNode
)
49 if (!m_pNamespace
.get()) {
52 xmlChar
const*const pUri(reinterpret_cast<xmlChar
const*>(
53 m_pNamespace
->first
.getStr()));
54 xmlChar
const*const pPrefix(reinterpret_cast<xmlChar
const*>(
55 m_pNamespace
->second
.getStr()));
56 xmlNsPtr pNs
= xmlSearchNs(pNode
->doc
, pNode
, pPrefix
);
57 if (pNs
&& (0 != xmlStrcmp(pNs
->href
, pUri
))) {
60 pNs
= xmlNewNs(pNode
, pUri
, pPrefix
);
64 pNs
= xmlSearchNsByHref(pNode
->doc
, pNode
, pUri
);
65 // if (!pNs) hmm... now what? throw?
67 SAL_WARN("unoxml", "CAttr: cannot create namespace");
72 bool CAttr::IsChildTypeAllowed(NodeType
const nodeType
)
75 case NodeType_TEXT_NODE
:
76 case NodeType_ENTITY_REFERENCE_NODE
:
83 OUString SAL_CALL
CAttr::getNodeName()
87 OUString SAL_CALL
CAttr::getNodeValue()
91 OUString SAL_CALL
CAttr::getLocalName()
98 Returns the name of this attribute.
100 OUString SAL_CALL
CAttr::getName()
102 ::osl::MutexGuard
const g(m_rMutex
);
104 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
107 OUString
const aName(reinterpret_cast<char const *>(m_aAttrPtr
->name
),
108 strlen(reinterpret_cast<char const *>(m_aAttrPtr
->name
)), RTL_TEXTENCODING_UTF8
);
113 The Element node this attribute is attached to or null if this
114 attribute is not in use.
116 Reference
< XElement
> SAL_CALL
CAttr::getOwnerElement()
118 ::osl::MutexGuard
const g(m_rMutex
);
120 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
123 if (nullptr == m_aAttrPtr
->parent
) {
126 Reference
< XElement
> const xRet(
127 static_cast< XNode
* >(GetOwnerDocument().GetCNode(
128 m_aAttrPtr
->parent
).get()),
134 If this attribute was explicitly given a value in the original
135 document, this is true; otherwise, it is false.
137 sal_Bool SAL_CALL
CAttr::getSpecified()
139 // FIXME if this DOM implementation supported DTDs it would need
140 // to check that this attribute is not default or something
145 On retrieval, the value of the attribute is returned as a string.
147 OUString SAL_CALL
CAttr::getValue()
149 ::osl::MutexGuard
const g(m_rMutex
);
151 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
154 if (nullptr == m_aAttrPtr
->children
) {
157 char const*const pContent((m_aAttrPtr
->children
)
158 ? reinterpret_cast<char const*>(m_aAttrPtr
->children
->content
)
160 OUString
const ret(pContent
, strlen(pContent
), RTL_TEXTENCODING_UTF8
);
165 Sets the value of the attribute from a string.
167 void SAL_CALL
CAttr::setValue(const OUString
& value
)
169 ::osl::ClearableMutexGuard
guard(m_rMutex
);
171 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
175 // remember old value (for mutation event)
176 OUString sOldValue
= getValue();
178 OString o1
= OUStringToOString(value
, RTL_TEXTENCODING_UTF8
);
179 xmlChar
const * pValue
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
180 // this does not work if the attribute was created anew
181 // xmlNodePtr pNode = m_aAttrPtr->parent;
182 // xmlSetProp(pNode, m_aAttrPtr->name, pValue);
183 std::shared_ptr
<xmlChar
const> const buffer(
184 xmlEncodeEntitiesReentrant(m_aAttrPtr
->doc
, pValue
), xmlFree
);
185 xmlFreeNodeList(m_aAttrPtr
->children
);
186 m_aAttrPtr
->children
=
187 xmlStringGetNodeList(m_aAttrPtr
->doc
, buffer
.get());
188 xmlNodePtr tmp
= m_aAttrPtr
->children
;
189 while (tmp
!= nullptr) {
190 tmp
->parent
= m_aNodePtr
;
191 tmp
->doc
= m_aAttrPtr
->doc
;
192 if (tmp
->next
== nullptr)
193 m_aNodePtr
->last
= tmp
;
197 // dispatch DOM events to signal change in attribute value
198 // dispatch DomAttrModified + DOMSubtreeModified
199 OUString
sEventName( "DOMAttrModified" );
200 Reference
< XDocumentEvent
> docevent(getOwnerDocument(), UNO_QUERY
);
201 Reference
< XMutationEvent
> event(docevent
->createEvent(sEventName
),UNO_QUERY
);
202 event
->initMutationEvent(
203 sEventName
, true, false,
204 Reference
<XNode
>( static_cast<XAttr
*>( this ) ),
205 sOldValue
, value
, getName(), AttrChangeType_MODIFICATION
);
207 guard
.clear(); // release mutex before calling event handlers
209 dispatchEvent(event
);
210 dispatchSubtreeModified();
213 void SAL_CALL
CAttr::setPrefix(const OUString
& prefix
)
215 ::osl::MutexGuard
const g(m_rMutex
);
217 if (!m_aNodePtr
) { return; }
219 if (m_pNamespace
.get()) {
220 OSL_ASSERT(!m_aNodePtr
->parent
);
221 m_pNamespace
->second
=
222 OUStringToOString(prefix
, RTL_TEXTENCODING_UTF8
);
224 CNode::setPrefix(prefix
);
228 OUString SAL_CALL
CAttr::getPrefix()
230 ::osl::MutexGuard
const g(m_rMutex
);
232 if (!m_aNodePtr
) { return OUString(); }
234 if (m_pNamespace
.get()) {
235 OSL_ASSERT(!m_aNodePtr
->parent
);
236 OUString
const ret(OStringToOUString(
237 m_pNamespace
->second
, RTL_TEXTENCODING_UTF8
));
240 return CNode::getPrefix();
244 OUString SAL_CALL
CAttr::getNamespaceURI()
246 ::osl::MutexGuard
const g(m_rMutex
);
248 if (!m_aNodePtr
) { return OUString(); }
250 if (m_pNamespace
.get()) {
251 OSL_ASSERT(!m_aNodePtr
->parent
);
252 OUString
const ret(OStringToOUString(
253 m_pNamespace
->first
, RTL_TEXTENCODING_UTF8
));
256 return CNode::getNamespaceURI();
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */