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>
27 #include <sal/log.hxx>
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
)
53 xmlChar
const*const pUri(reinterpret_cast<xmlChar
const*>(
54 m_oNamespace
->first
.getStr()));
55 xmlChar
const*const pPrefix(reinterpret_cast<xmlChar
const*>(
56 m_oNamespace
->second
.getStr()));
57 xmlNsPtr pNs
= xmlSearchNs(pNode
->doc
, pNode
, pPrefix
);
58 if (pNs
&& (0 != xmlStrcmp(pNs
->href
, pUri
))) {
61 pNs
= xmlNewNs(pNode
, pUri
, pPrefix
);
65 pNs
= xmlSearchNsByHref(pNode
->doc
, pNode
, pUri
);
66 // if (!pNs) hmm... now what? throw?
68 SAL_WARN("unoxml", "CAttr: cannot create namespace");
73 bool CAttr::IsChildTypeAllowed(NodeType
const nodeType
, NodeType
const*const)
76 case NodeType_TEXT_NODE
:
77 case NodeType_ENTITY_REFERENCE_NODE
:
84 OUString SAL_CALL
CAttr::getNodeName()
88 OUString SAL_CALL
CAttr::getNodeValue()
92 OUString SAL_CALL
CAttr::getLocalName()
99 Returns the name of this attribute.
101 OUString SAL_CALL
CAttr::getName()
103 ::osl::MutexGuard
const g(m_rMutex
);
105 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
108 OUString
const aName(reinterpret_cast<char const *>(m_aAttrPtr
->name
),
109 strlen(reinterpret_cast<char const *>(m_aAttrPtr
->name
)), RTL_TEXTENCODING_UTF8
);
114 The Element node this attribute is attached to or null if this
115 attribute is not in use.
117 Reference
< XElement
> SAL_CALL
CAttr::getOwnerElement()
119 ::osl::MutexGuard
const g(m_rMutex
);
121 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
124 if (nullptr == m_aAttrPtr
->parent
) {
127 Reference
< XElement
> const xRet(
128 static_cast< XNode
* >(GetOwnerDocument().GetCNode(
129 m_aAttrPtr
->parent
).get()),
135 If this attribute was explicitly given a value in the original
136 document, this is true; otherwise, it is false.
138 sal_Bool SAL_CALL
CAttr::getSpecified()
140 // FIXME if this DOM implementation supported DTDs it would need
141 // to check that this attribute is not default or something
146 On retrieval, the value of the attribute is returned as a string.
148 OUString SAL_CALL
CAttr::getValue()
150 ::osl::MutexGuard
const g(m_rMutex
);
152 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
155 if (nullptr == m_aAttrPtr
->children
) {
158 char const*const pContent(reinterpret_cast<char const*>(m_aAttrPtr
->children
->content
));
159 return OUString(pContent
, strlen(pContent
), RTL_TEXTENCODING_UTF8
);
163 Sets the value of the attribute from a string.
165 void SAL_CALL
CAttr::setValue(const OUString
& value
)
167 ::osl::ClearableMutexGuard
guard(m_rMutex
);
169 if ((nullptr == m_aNodePtr
) || (nullptr == m_aAttrPtr
)) {
173 // remember old value (for mutation event)
174 OUString sOldValue
= getValue();
176 OString o1
= OUStringToOString(value
, RTL_TEXTENCODING_UTF8
);
177 xmlChar
const * pValue
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
178 // this does not work if the attribute was created anew
179 // xmlNodePtr pNode = m_aAttrPtr->parent;
180 // xmlSetProp(pNode, m_aAttrPtr->name, pValue);
181 std::shared_ptr
<xmlChar
const> const buffer(
182 xmlEncodeEntitiesReentrant(m_aAttrPtr
->doc
, pValue
), xmlFree
);
183 xmlFreeNodeList(m_aAttrPtr
->children
);
184 m_aAttrPtr
->children
=
185 xmlStringGetNodeList(m_aAttrPtr
->doc
, buffer
.get());
186 xmlNodePtr tmp
= m_aAttrPtr
->children
;
187 while (tmp
!= nullptr) {
188 tmp
->parent
= m_aNodePtr
;
189 tmp
->doc
= m_aAttrPtr
->doc
;
190 if (tmp
->next
== nullptr)
191 m_aNodePtr
->last
= tmp
;
195 // dispatch DOM events to signal change in attribute value
196 // dispatch DomAttrModified + DOMSubtreeModified
197 OUString
sEventName( "DOMAttrModified" );
198 Reference
< XDocumentEvent
> docevent(getOwnerDocument(), UNO_QUERY
);
199 Reference
< XMutationEvent
> event(docevent
->createEvent(sEventName
),UNO_QUERY
);
200 event
->initMutationEvent(
201 sEventName
, true, false,
202 Reference
<XNode
>( static_cast<XAttr
*>( this ) ),
203 sOldValue
, value
, getName(), AttrChangeType_MODIFICATION
);
205 guard
.clear(); // release mutex before calling event handlers
207 dispatchEvent(event
);
208 dispatchSubtreeModified();
211 void SAL_CALL
CAttr::setPrefix(const OUString
& prefix
)
213 ::osl::MutexGuard
const g(m_rMutex
);
215 if (!m_aNodePtr
) { return; }
219 OSL_ASSERT(!m_aNodePtr
->parent
);
220 m_oNamespace
->second
=
221 OUStringToOString(prefix
, RTL_TEXTENCODING_UTF8
);
225 CNode::setPrefix(prefix
);
229 OUString SAL_CALL
CAttr::getPrefix()
231 ::osl::MutexGuard
const g(m_rMutex
);
233 if (!m_aNodePtr
) { return OUString(); }
237 OSL_ASSERT(!m_aNodePtr
->parent
);
238 OUString
const ret(OStringToOUString(
239 m_oNamespace
->second
, RTL_TEXTENCODING_UTF8
));
244 return CNode::getPrefix();
248 OUString SAL_CALL
CAttr::getNamespaceURI()
250 ::osl::MutexGuard
const g(m_rMutex
);
252 if (!m_aNodePtr
) { return OUString(); }
256 OSL_ASSERT(!m_aNodePtr
->parent
);
257 OUString
const ret(OStringToOUString(
258 m_oNamespace
->first
, RTL_TEXTENCODING_UTF8
));
263 return CNode::getNamespaceURI();
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */