summaryrefslogtreecommitdiffstats
path: root/xmloff/source/script/xmlbasicscript.cxx
blob: efc655eca6820ff0ab9b7ff98d75f058640e93c0 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- 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 <sal/config.h>

#include "xmlbasicscript.hxx"
#include <sal/log.hxx>
#include <xmlscript/xmlns.h>
#include <xmloff/xmlnmspe.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/document/XEmbeddedScripts.hpp>
#include <com/sun/star/xml/sax/SAXException.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <tools/diagnose_ex.h>

using namespace ::com::sun::star;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::xml::sax;
using namespace ::xmloff::token;

namespace xmloff
{
// BasicElementBase

BasicElementBase::BasicElementBase(SvXMLImport& rImport)
    : SvXMLImportContext(rImport)
{
}

bool BasicElementBase::getBoolAttr(bool* pRet, sal_Int32 nToken,
                                   const Reference<XFastAttributeList>& xAttributes)
{
    OUString aValue = xAttributes->getOptionalValue(nToken);
    if (!aValue.isEmpty())
    {
        if (aValue == "true")
        {
            *pRet = true;
            return true;
        }
        else if (aValue == "false")
        {
            *pRet = false;
            return true;
        }
        else
        {
            throw xml::sax::SAXException(SvXMLImport::getNameFromToken(nToken)
                                             + ": no boolean value (true|false)!",
                                         Reference<XInterface>(), Any());
        }
    }
    return false;
}

void BasicElementBase::startFastElement(sal_Int32 /*nElement*/,
                                        const Reference<XFastAttributeList>& /*xAttributes*/)
{
}

// BasicLibrariesElement

BasicLibrariesElement::BasicLibrariesElement(SvXMLImport& rImport,
                                             const css::uno::Reference<css::frame::XModel>& rxModel)
    : BasicElementBase(rImport)
{
    // try the XEmbeddedScripts interface
    Reference<document::XEmbeddedScripts> xDocumentScripts(rxModel, UNO_QUERY_THROW);
    m_xLibContainer.set(xDocumentScripts->getBasicLibraries().get());

    if (!m_xLibContainer.is())
    {
        // try the "BasicLibraries" property (old-style, for compatibility)
        Reference<beans::XPropertySet> xPSet(rxModel, UNO_QUERY);
        if (xPSet.is())
            xPSet->getPropertyValue("BasicLibraries") >>= m_xLibContainer;
    }

    SAL_WARN_IF(!m_xLibContainer.is(), "xmlscript.xmlflat",
                "BasicImport::startRootElement: nowhere to import to!");

    if (!m_xLibContainer.is())
    {
        throw xml::sax::SAXException("nowhere to import to", Reference<XInterface>(), Any());
    }
}

// XElement

Reference<XFastContextHandler>
BasicLibrariesElement::createFastChildContext(sal_Int32 nElement,
                                              const Reference<XFastAttributeList>& xAttributes)
{
    if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO))
    {
        throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
    }
    else if ((nElement & TOKEN_MASK) == XML_LIBRARY_LINKED)
    {
        OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);

        OUString aStorageURL = xAttributes->getValue(XML_ELEMENT(XLINK, XML_HREF));

        bool bReadOnly = false;
        getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes);

        if (m_xLibContainer.is())
        {
            try
            {
                Reference<container::XNameAccess> xLib(
                    m_xLibContainer->createLibraryLink(aName, aStorageURL, bReadOnly));
                if (xLib.is())
                    return new BasicElementBase(GetImport());
            }
            catch (const container::ElementExistException&)
            {
                TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
                                     "BasicLibrariesElement::startChildElement");
            }
            catch (const lang::IllegalArgumentException&)
            {
                TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
                                     "BasicLibrariesElement::startChildElement");
            }
        }
    }
    else if ((nElement & TOKEN_MASK) == XML_LIBRARY_EMBEDDED)
    {
        // TODO: create password protected libraries

        OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);

        bool bReadOnly = false;
        getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes);

        if (m_xLibContainer.is())
        {
            try
            {
                Reference<container::XNameContainer> xLib;
                if (m_xLibContainer->hasByName(aName))
                {
                    // Standard library
                    m_xLibContainer->getByName(aName) >>= xLib;
                }
                else
                {
                    xLib.set(m_xLibContainer->createLibrary(aName));
                }

                if (xLib.is())
                    return new BasicEmbeddedLibraryElement(GetImport(), m_xLibContainer, aName,
                                                           bReadOnly);
            }
            catch (const lang::IllegalArgumentException&)
            {
                TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
                                     "BasicLibrariesElement::startChildElement");
            }
        }
    }
    else
    {
        throw xml::sax::SAXException("expected library-linked or library-embedded element!",
                                     Reference<XInterface>(), Any());
    }

    return nullptr;
}

// BasicEmbeddedLibraryElement

BasicEmbeddedLibraryElement::BasicEmbeddedLibraryElement(
    SvXMLImport& rImport, const Reference<script::XLibraryContainer2>& rxLibContainer,
    const OUString& rLibName, bool bReadOnly)
    : BasicElementBase(rImport)
    , m_xLibContainer(rxLibContainer)
    , m_aLibName(rLibName)
    , m_bReadOnly(bReadOnly)
{
    try
    {
        if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName))
            m_xLibContainer->getByName(m_aLibName) >>= m_xLib;
    }
    catch (const lang::WrappedTargetException&)
    {
        TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicEmbeddedLibraryElement::CTOR:");
    }
}

Reference<XFastContextHandler> BasicEmbeddedLibraryElement::createFastChildContext(
    sal_Int32 nElement, const Reference<XFastAttributeList>& xAttributes)
{
    if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO))
    {
        throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
    }
    else if ((nElement & TOKEN_MASK) == XML_MODULE)
    {
        OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);

        if (m_xLib.is() && !aName.isEmpty())
            return new BasicModuleElement(GetImport(), m_xLib, aName);
    }
    else
    {
        throw xml::sax::SAXException("expected module element!", Reference<XInterface>(), Any());
    }

    return nullptr;
}

void BasicEmbeddedLibraryElement::endFastElement(sal_Int32)
{
    if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName) && m_bReadOnly)
        m_xLibContainer->setLibraryReadOnly(m_aLibName, m_bReadOnly);
}

// BasicModuleElement

BasicModuleElement::BasicModuleElement(SvXMLImport& rImport,
                                       const Reference<container::XNameContainer>& rxLib,
                                       const OUString& rName)
    : BasicElementBase(rImport)
    , m_xLib(rxLib)
    , m_aName(rName)
{
}

Reference<XFastContextHandler>
BasicModuleElement::createFastChildContext(sal_Int32 nElement,
                                           const Reference<XFastAttributeList>& xAttributes)
{
    // TODO: <byte-code>

    if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO))
    {
        throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
    }
    else if ((nElement & TOKEN_MASK) == XML_SOURCE_CODE)
    {
        // TODO: password protected libraries

        if (xAttributes.is())
        {
            if (m_xLib.is() && !m_aName.isEmpty())
                return new BasicSourceCodeElement(GetImport(), m_xLib, m_aName);
        }
    }
    else
    {
        throw xml::sax::SAXException("expected source-code element!", Reference<XInterface>(),
                                     Any());
    }

    return nullptr;
}

// BasicSourceCodeElement

BasicSourceCodeElement::BasicSourceCodeElement(SvXMLImport& rImport,
                                               const Reference<container::XNameContainer>& rxLib,
                                               const OUString& rName)
    : BasicElementBase(rImport)
    , m_xLib(rxLib)
    , m_aName(rName)
{
}

// XElement

void BasicSourceCodeElement::characters(const OUString& rChars) { m_aBuffer.append(rChars); }

void BasicSourceCodeElement::endFastElement(sal_Int32)
{
    try
    {
        if (m_xLib.is() && !m_aName.isEmpty())
        {
            Any aElement;
            aElement <<= m_aBuffer.makeStringAndClear();
            m_xLib->insertByName(m_aName, aElement);
        }
    }
    catch (const container::ElementExistException&)
    {
        TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
    }
    catch (const lang::IllegalArgumentException&)
    {
        TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
    }
    catch (const lang::WrappedTargetException&)
    {
        TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
    }
}
}

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