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 .
25 #include <libxml/entities.h>
27 #include <osl/diagnose.h>
28 #include <sal/log.hxx>
30 #include <com/sun/star/xml/dom/events/XMutationEvent.hpp>
32 #include "document.hxx"
34 using namespace css::uno
;
35 using namespace css::xml::dom
;
36 using namespace css::xml::dom::events
;
40 CAttr::CAttr(CDocument
const& rDocument
, ::osl::Mutex
const& rMutex
,
41 xmlAttrPtr
const pAttr
)
42 : CAttr_Base(rDocument
, rMutex
,
43 NodeType_ATTRIBUTE_NODE
, reinterpret_cast<xmlNodePtr
>(pAttr
))
48 xmlNsPtr
CAttr::GetNamespace(xmlNodePtr
const pNode
)
54 xmlChar
const*const pUri(reinterpret_cast<xmlChar
const*>(
55 m_oNamespace
->first
.getStr()));
56 xmlChar
const*const pPrefix(reinterpret_cast<xmlChar
const*>(
57 m_oNamespace
->second
.getStr()));
58 xmlNsPtr pNs
= xmlSearchNs(pNode
->doc
, pNode
, pPrefix
);
59 if (pNs
&& (0 != xmlStrcmp(pNs
->href
, pUri
))) {
62 pNs
= xmlNewNs(pNode
, pUri
, pPrefix
);
66 pNs
= xmlSearchNsByHref(pNode
->doc
, pNode
, pUri
);
67 // if (!pNs) hmm... now what? throw?
69 SAL_WARN("unoxml", "CAttr: cannot create namespace");
74 bool CAttr::IsChildTypeAllowed(NodeType
const nodeType
, NodeType
const*const)
77 case NodeType_TEXT_NODE
:
78 case NodeType_ENTITY_REFERENCE_NODE
:
85 OUString SAL_CALL
CAttr::getNodeName()
89 OUString SAL_CALL
CAttr::getNodeValue()
93 OUString SAL_CALL
CAttr::getLocalName()
100 Returns the name of this attribute.
102 OUString SAL_CALL
CAttr::getName()
104 ::osl::MutexGuard
const g(m_rMutex
);
106 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
109 OUString
const aName(reinterpret_cast<char const *>(m_aAttrPtr
->name
),
110 strlen(reinterpret_cast<char const *>(m_aAttrPtr
->name
)), RTL_TEXTENCODING_UTF8
);
115 The Element node this attribute is attached to or null if this
116 attribute is not in use.
118 Reference
< XElement
> SAL_CALL
CAttr::getOwnerElement()
120 ::osl::MutexGuard
const g(m_rMutex
);
122 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
125 if (nullptr == m_aAttrPtr
->parent
) {
128 Reference
< XElement
> const xRet(
129 static_cast< XNode
* >(GetOwnerDocument().GetCNode(
130 m_aAttrPtr
->parent
).get()),
136 If this attribute was explicitly given a value in the original
137 document, this is true; otherwise, it is false.
139 sal_Bool SAL_CALL
CAttr::getSpecified()
141 // FIXME if this DOM implementation supported DTDs it would need
142 // to check that this attribute is not default or something
147 On retrieval, the value of the attribute is returned as a string.
149 OUString SAL_CALL
CAttr::getValue()
151 ::osl::MutexGuard
const g(m_rMutex
);
153 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
156 if (nullptr == m_aAttrPtr
->children
) {
159 char const*const pContent(reinterpret_cast<char const*>(m_aAttrPtr
->children
->content
));
160 return OUString(pContent
, strlen(pContent
), RTL_TEXTENCODING_UTF8
);
164 Sets the value of the attribute from a string.
166 void SAL_CALL
CAttr::setValue(const OUString
& value
)
168 ::osl::ClearableMutexGuard
guard(m_rMutex
);
170 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
174 // remember old value (for mutation event)
175 OUString sOldValue
= getValue();
177 OString o1
= OUStringToOString(value
, RTL_TEXTENCODING_UTF8
);
178 xmlChar
const * pValue
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
179 // this does not work if the attribute was created anew
180 // xmlNodePtr pNode = m_aAttrPtr->parent;
181 // xmlSetProp(pNode, m_aAttrPtr->name, pValue);
182 std::shared_ptr
<xmlChar
const> const buffer(
183 xmlEncodeEntitiesReentrant(m_aAttrPtr
->doc
, pValue
), xmlFree
);
184 xmlFreeNodeList(m_aAttrPtr
->children
);
185 m_aAttrPtr
->children
=
186 xmlStringGetNodeList(m_aAttrPtr
->doc
, buffer
.get());
187 xmlNodePtr tmp
= m_aAttrPtr
->children
;
188 while (tmp
!= nullptr) {
189 tmp
->parent
= m_aNodePtr
;
190 tmp
->doc
= m_aAttrPtr
->doc
;
191 if (tmp
->next
== nullptr)
192 m_aNodePtr
->last
= tmp
;
196 // dispatch DOM events to signal change in attribute value
197 // dispatch DomAttrModified + DOMSubtreeModified
198 OUString
sEventName( "DOMAttrModified" );
199 Reference
< XDocumentEvent
> docevent(getOwnerDocument(), UNO_QUERY
);
200 Reference
< XMutationEvent
> event(docevent
->createEvent(sEventName
),UNO_QUERY
);
201 event
->initMutationEvent(
202 sEventName
, true, false,
203 Reference
<XNode
>( static_cast<XAttr
*>( this ) ),
204 sOldValue
, value
, getName(), AttrChangeType_MODIFICATION
);
206 guard
.clear(); // release mutex before calling event handlers
208 dispatchEvent(event
);
209 dispatchSubtreeModified();
212 void SAL_CALL
CAttr::setPrefix(const OUString
& prefix
)
214 ::osl::MutexGuard
const g(m_rMutex
);
216 if (!m_aNodePtr
) { return; }
220 OSL_ASSERT(!m_aNodePtr
->parent
);
221 m_oNamespace
->second
=
222 OUStringToOString(prefix
, RTL_TEXTENCODING_UTF8
);
226 CNode::setPrefix(prefix
);
230 OUString SAL_CALL
CAttr::getPrefix()
232 ::osl::MutexGuard
const g(m_rMutex
);
234 if (!m_aNodePtr
) { return OUString(); }
238 OSL_ASSERT(!m_aNodePtr
->parent
);
239 OUString
const ret(OStringToOUString(
240 m_oNamespace
->second
, RTL_TEXTENCODING_UTF8
));
245 return CNode::getPrefix();
249 OUString SAL_CALL
CAttr::getNamespaceURI()
251 ::osl::MutexGuard
const g(m_rMutex
);
253 if (!m_aNodePtr
) { return OUString(); }
257 OSL_ASSERT(!m_aNodePtr
->parent
);
258 OUString
const ret(OStringToOUString(
259 m_oNamespace
->first
, RTL_TEXTENCODING_UTF8
));
264 return CNode::getNamespaceURI();
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */