build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / unoxml / source / dom / attr.cxx
blob5d53151f2ba491a0d85fe519b46d1d427d238e3a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <attr.hxx>
22 #include <string.h>
24 #include <memory>
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;
37 namespace DOM
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))
43 , m_aAttrPtr(pAttr)
47 xmlNsPtr CAttr::GetNamespace(xmlNodePtr const pNode)
49 if (!m_pNamespace.get()) {
50 return nullptr;
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))) {
58 return pNs;
60 pNs = xmlNewNs(pNode, pUri, pPrefix);
61 if (pNs) {
62 return pNs;
64 pNs = xmlSearchNsByHref(pNode->doc, pNode, pUri);
65 // if (!pNs) hmm... now what? throw?
66 if (!pNs) { OSL_TRACE("CAtttr: cannot create namespace"); }
67 return pNs;
70 bool CAttr::IsChildTypeAllowed(NodeType const nodeType)
72 switch (nodeType) {
73 case NodeType_TEXT_NODE:
74 case NodeType_ENTITY_REFERENCE_NODE:
75 return true;
76 default:
77 return false;
81 OUString SAL_CALL CAttr::getNodeName()
82 throw (RuntimeException, std::exception)
84 return getName();
86 OUString SAL_CALL CAttr::getNodeValue()
87 throw (RuntimeException, std::exception)
89 return getValue();
91 OUString SAL_CALL CAttr::getLocalName()
92 throw (RuntimeException, std::exception)
94 return getName();
98 /**
99 Returns the name of this attribute.
101 OUString SAL_CALL CAttr::getName() throw (RuntimeException, std::exception)
103 ::osl::MutexGuard const g(m_rMutex);
105 if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
106 return OUString();
108 OUString const aName(reinterpret_cast<char const *>(m_aAttrPtr->name),
109 strlen(reinterpret_cast<char const *>(m_aAttrPtr->name)), RTL_TEXTENCODING_UTF8);
110 return aName;
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()
118 throw (RuntimeException, std::exception)
120 ::osl::MutexGuard const g(m_rMutex);
122 if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
123 return nullptr;
125 if (nullptr == m_aAttrPtr->parent) {
126 return nullptr;
128 Reference< XElement > const xRet(
129 static_cast< XNode* >(GetOwnerDocument().GetCNode(
130 m_aAttrPtr->parent).get()),
131 UNO_QUERY_THROW);
132 return xRet;
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()
140 throw (RuntimeException, std::exception)
142 // FIXME if this DOM implementation supported DTDs it would need
143 // to check that this attribute is not default or something
144 return true;
148 On retrieval, the value of the attribute is returned as a string.
150 OUString SAL_CALL CAttr::getValue()
151 throw (RuntimeException, std::exception)
153 ::osl::MutexGuard const g(m_rMutex);
155 if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
156 return OUString();
158 if (nullptr == m_aAttrPtr->children) {
159 return OUString();
161 char const*const pContent((m_aAttrPtr->children)
162 ? reinterpret_cast<char const*>(m_aAttrPtr->children->content)
163 : "");
164 OUString const ret(pContent, strlen(pContent), RTL_TEXTENCODING_UTF8);
165 return ret;
169 Sets the value of the attribute from a string.
171 void SAL_CALL CAttr::setValue(const OUString& value)
172 throw (RuntimeException, DOMException, std::exception)
174 ::osl::ClearableMutexGuard guard(m_rMutex);
176 if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
177 return;
180 // remember old value (for mutation event)
181 OUString sOldValue = getValue();
183 OString o1 = OUStringToOString(value, RTL_TEXTENCODING_UTF8);
184 xmlChar const * pValue = reinterpret_cast<xmlChar const *>(o1.getStr());
185 // this does not work if the attribute was created anew
186 // xmlNodePtr pNode = m_aAttrPtr->parent;
187 // xmlSetProp(pNode, m_aAttrPtr->name, pValue);
188 std::shared_ptr<xmlChar const> const buffer(
189 xmlEncodeEntitiesReentrant(m_aAttrPtr->doc, pValue), xmlFree);
190 xmlFreeNodeList(m_aAttrPtr->children);
191 m_aAttrPtr->children =
192 xmlStringGetNodeList(m_aAttrPtr->doc, buffer.get());
193 xmlNodePtr tmp = m_aAttrPtr->children;
194 while (tmp != nullptr) {
195 tmp->parent = m_aNodePtr;
196 tmp->doc = m_aAttrPtr->doc;
197 if (tmp->next == nullptr)
198 m_aNodePtr->last = tmp;
199 tmp = tmp->next;
202 // dispatch DOM events to signal change in attribute value
203 // dispatch DomAttrModified + DOMSubtreeModified
204 OUString sEventName( "DOMAttrModified" );
205 Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
206 Reference< XMutationEvent > event(docevent->createEvent(sEventName),UNO_QUERY);
207 event->initMutationEvent(
208 sEventName, true, false,
209 Reference<XNode>( static_cast<XAttr*>( this ) ),
210 sOldValue, value, getName(), AttrChangeType_MODIFICATION );
212 guard.clear(); // release mutex before calling event handlers
214 dispatchEvent(event);
215 dispatchSubtreeModified();
218 void SAL_CALL CAttr::setPrefix(const OUString& prefix)
219 throw (RuntimeException, DOMException, std::exception)
221 ::osl::MutexGuard const g(m_rMutex);
223 if (!m_aNodePtr) { return; }
225 if (m_pNamespace.get()) {
226 OSL_ASSERT(!m_aNodePtr->parent);
227 m_pNamespace->second =
228 OUStringToOString(prefix, RTL_TEXTENCODING_UTF8);
229 } else {
230 CNode::setPrefix(prefix);
234 OUString SAL_CALL CAttr::getPrefix()
235 throw (RuntimeException, std::exception)
237 ::osl::MutexGuard const g(m_rMutex);
239 if (!m_aNodePtr) { return OUString(); }
241 if (m_pNamespace.get()) {
242 OSL_ASSERT(!m_aNodePtr->parent);
243 OUString const ret(OStringToOUString(
244 m_pNamespace->second, RTL_TEXTENCODING_UTF8));
245 return ret;
246 } else {
247 return CNode::getPrefix();
251 OUString SAL_CALL CAttr::getNamespaceURI()
252 throw (RuntimeException, std::exception)
254 ::osl::MutexGuard const g(m_rMutex);
256 if (!m_aNodePtr) { return OUString(); }
258 if (m_pNamespace.get()) {
259 OSL_ASSERT(!m_aNodePtr->parent);
260 OUString const ret(OStringToOUString(
261 m_pNamespace->first, RTL_TEXTENCODING_UTF8));
262 return ret;
263 } else {
264 return CNode::getNamespaceURI();
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */