1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "attr.hxx"

#include <string.h>

#include <memory>

#include <osl/diagnose.h>
#include <sal/log.hxx>

#include <com/sun/star/xml/dom/events/XMutationEvent.hpp>

#include "document.hxx"

using namespace css::uno;
using namespace css::xml::dom;
using namespace css::xml::dom::events;

namespace DOM
{
    CAttr::CAttr(CDocument const& rDocument, ::osl::Mutex const& rMutex,
            xmlAttrPtr const pAttr)
        : CAttr_Base(rDocument, rMutex,
                NodeType_ATTRIBUTE_NODE, reinterpret_cast<xmlNodePtr>(pAttr))
        , m_aAttrPtr(pAttr)
    {
    }

    xmlNsPtr CAttr::GetNamespace(xmlNodePtr const pNode)
    {
        if (!m_pNamespace)
        {
            return nullptr;
        }
        xmlChar const*const pUri(reinterpret_cast<xmlChar const*>(
                m_pNamespace->first.getStr()));
        xmlChar const*const pPrefix(reinterpret_cast<xmlChar const*>(
                m_pNamespace->second.getStr()));
        xmlNsPtr pNs = xmlSearchNs(pNode->doc, pNode, pPrefix);
        if (pNs && (0 != xmlStrcmp(pNs->href, pUri))) {
            return pNs;
        }
        pNs = xmlNewNs(pNode, pUri, pPrefix);
        if (pNs) {
            return pNs;
        }
        pNs = xmlSearchNsByHref(pNode->doc, pNode, pUri);
        // if (!pNs) hmm... now what? throw?
        if (!pNs) {
            SAL_WARN("unoxml", "CAttr: cannot create namespace");
        }
        return pNs;
    }

    bool CAttr::IsChildTypeAllowed(NodeType const nodeType)
    {
        switch (nodeType) {
            case NodeType_TEXT_NODE:
            case NodeType_ENTITY_REFERENCE_NODE:
                return true;
            default:
                return false;
        }
    }

    OUString SAL_CALL CAttr::getNodeName()
    {
        return getName();
    }
    OUString SAL_CALL CAttr::getNodeValue()
    {
        return getValue();
    }
    OUString SAL_CALL CAttr::getLocalName()
    {
        return getName();
    }


    /**
    Returns the name of this attribute.
    */
    OUString SAL_CALL CAttr::getName()
    {
        ::osl::MutexGuard const g(m_rMutex);

        if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
            return OUString();
        }
        OUString const aName(reinterpret_cast<char const *>(m_aAttrPtr->name),
                strlen(reinterpret_cast<char const *>(m_aAttrPtr->name)), RTL_TEXTENCODING_UTF8);
        return aName;
    }

    /**
    The Element node this attribute is attached to or null if this
    attribute is not in use.
    */
    Reference< XElement > SAL_CALL CAttr::getOwnerElement()<--- The function 'getOwnerElement' is never used.
    {
        ::osl::MutexGuard const g(m_rMutex);

        if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
            return nullptr;
        }
        if (nullptr == m_aAttrPtr->parent) {
            return nullptr;
        }
        Reference< XElement > const xRet(
            static_cast< XNode* >(GetOwnerDocument().GetCNode(
                    m_aAttrPtr->parent).get()),
            UNO_QUERY_THROW);
        return xRet;
    }

    /**
    If this attribute was explicitly given a value in the original
    document, this is true; otherwise, it is false.
    */
    sal_Bool SAL_CALL CAttr::getSpecified()<--- The function 'getSpecified' is never used.
    {
        // FIXME if this DOM implementation supported DTDs it would need
        // to check that this attribute is not default or something
        return true;
    }

    /**
    On retrieval, the value of the attribute is returned as a string.
    */
    OUString SAL_CALL CAttr::getValue()
    {
        ::osl::MutexGuard const g(m_rMutex);

        if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
            return OUString();
        }
        if (nullptr == m_aAttrPtr->children) {
            return OUString();
        }
        char const*const pContent(reinterpret_cast<char const*>(m_aAttrPtr->children->content));
        return OUString(pContent, strlen(pContent), RTL_TEXTENCODING_UTF8);
    }

    /**
    Sets the value of the attribute from a string.
    */
    void SAL_CALL CAttr::setValue(const OUString& value)
    {
        ::osl::ClearableMutexGuard guard(m_rMutex);

        if ((nullptr == m_aNodePtr) || (nullptr == m_aAttrPtr)) {
            return;
        }

        // remember old value (for mutation event)
        OUString sOldValue = getValue();

        OString o1 = OUStringToOString(value, RTL_TEXTENCODING_UTF8);
        xmlChar const * pValue = reinterpret_cast<xmlChar const *>(o1.getStr());
        // this does not work if the attribute was created anew
        // xmlNodePtr pNode = m_aAttrPtr->parent;
        // xmlSetProp(pNode, m_aAttrPtr->name, pValue);
        std::shared_ptr<xmlChar const> const buffer(
                xmlEncodeEntitiesReentrant(m_aAttrPtr->doc, pValue), xmlFree);
        xmlFreeNodeList(m_aAttrPtr->children);
        m_aAttrPtr->children =
            xmlStringGetNodeList(m_aAttrPtr->doc, buffer.get());
        xmlNodePtr tmp = m_aAttrPtr->children;
        while (tmp != nullptr) {
            tmp->parent = m_aNodePtr;
            tmp->doc = m_aAttrPtr->doc;
            if (tmp->next == nullptr)
                m_aNodePtr->last = tmp;
            tmp = tmp->next;
        }

        // dispatch DOM events to signal change in attribute value
        // dispatch DomAttrModified + DOMSubtreeModified
        OUString sEventName( "DOMAttrModified" );
        Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
        Reference< XMutationEvent > event(docevent->createEvent(sEventName),UNO_QUERY);
        event->initMutationEvent(
                sEventName, true, false,
                Reference<XNode>( static_cast<XAttr*>( this ) ),
                sOldValue, value, getName(), AttrChangeType_MODIFICATION );

        guard.clear(); // release mutex before calling event handlers

        dispatchEvent(event);
        dispatchSubtreeModified();
    }

    void SAL_CALL CAttr::setPrefix(const OUString& prefix)
    {
        ::osl::MutexGuard const g(m_rMutex);

        if (!m_aNodePtr) { return; }

        if (m_pNamespace)
        {
            OSL_ASSERT(!m_aNodePtr->parent);
            m_pNamespace->second =
                OUStringToOString(prefix, RTL_TEXTENCODING_UTF8);
        }
        else
        {
            CNode::setPrefix(prefix);
        }
    }

    OUString SAL_CALL CAttr::getPrefix()
    {
        ::osl::MutexGuard const g(m_rMutex);

        if (!m_aNodePtr) { return OUString(); }

        if (m_pNamespace)
        {
            OSL_ASSERT(!m_aNodePtr->parent);
            OUString const ret(OStringToOUString(
                        m_pNamespace->second, RTL_TEXTENCODING_UTF8));
            return ret;
        }
        else
        {
            return CNode::getPrefix();
        }
    }

    OUString SAL_CALL CAttr::getNamespaceURI()
    {
        ::osl::MutexGuard const g(m_rMutex);

        if (!m_aNodePtr) { return OUString(); }

        if (m_pNamespace)
        {
            OSL_ASSERT(!m_aNodePtr->parent);
            OUString const ret(OStringToOUString(
                        m_pNamespace->first, RTL_TEXTENCODING_UTF8));
            return ret;
        }
        else
        {
            return CNode::getNamespaceURI();
        }
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */