LibreOffice Module xmloff (master) 1
xmlbasicscript.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include "xmlbasicscript.hxx"
23#include <sal/log.hxx>
24#include <xmlscript/xmlns.h>
25#include <utility>
27#include <xmloff/xmltkmap.hxx>
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/lang/XMultiComponentFactory.hpp>
30#include <com/sun/star/document/XEmbeddedScripts.hpp>
31#include <com/sun/star/xml/sax/SAXException.hpp>
34
35using namespace ::com::sun::star;
36using namespace ::com::sun::star::lang;
37using namespace ::com::sun::star::uno;
38using namespace ::com::sun::star::xml::sax;
39using namespace ::xmloff::token;
40
41namespace xmloff
42{
43// BasicElementBase
44
46 : SvXMLImportContext(rImport)
47{
48}
49
50bool BasicElementBase::getBoolAttr(bool* pRet, sal_Int32 nToken,
51 const Reference<XFastAttributeList>& xAttributes)
52{
53 OUString aValue = xAttributes->getOptionalValue(nToken);
54 if (!aValue.isEmpty())
55 {
56 if (aValue == "true")
57 {
58 *pRet = true;
59 return true;
60 }
61 else if (aValue == "false")
62 {
63 *pRet = false;
64 return true;
65 }
66 else
67 {
68 throw xml::sax::SAXException(SvXMLImport::getNameFromToken(nToken)
69 + ": no boolean value (true|false)!",
70 Reference<XInterface>(), Any());
71 }
72 }
73 return false;
74}
75
76// BasicLibrariesElement
77
79 const css::uno::Reference<css::frame::XModel>& rxModel)
80 : BasicElementBase(rImport)
81{
82 // try the XEmbeddedScripts interface
83 Reference<document::XEmbeddedScripts> xDocumentScripts(rxModel, UNO_QUERY_THROW);
84 m_xLibContainer = xDocumentScripts->getBasicLibraries();
85
86 if (!m_xLibContainer.is())
87 {
88 // try the "BasicLibraries" property (old-style, for compatibility)
89 Reference<beans::XPropertySet> xPSet(rxModel, UNO_QUERY);
90 if (xPSet.is())
91 xPSet->getPropertyValue("BasicLibraries") >>= m_xLibContainer;
92 }
93
94 SAL_WARN_IF(!m_xLibContainer.is(), "xmlscript.xmlflat",
95 "BasicImport::startRootElement: nowhere to import to!");
96
97 if (!m_xLibContainer.is())
98 {
99 throw xml::sax::SAXException("nowhere to import to", Reference<XInterface>(), Any());
100 }
101}
102
103// XElement
104
105Reference<XFastContextHandler>
107 const Reference<XFastAttributeList>& xAttributes)
108{
110 {
111 throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
112 }
113 else if ((nElement & TOKEN_MASK) == XML_LIBRARY_LINKED)
114 {
115 OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);
116
117 OUString aStorageURL = xAttributes->getValue(XML_ELEMENT(XLINK, XML_HREF));
118
119 bool bReadOnly = false;
121
122 if (m_xLibContainer.is())
123 {
124 try
125 {
126 Reference<container::XNameAccess> xLib(
127 m_xLibContainer->createLibraryLink(aName, aStorageURL, bReadOnly));
128 if (xLib.is())
129 return new BasicElementBase(GetImport());
130 }
131 catch (const container::ElementExistException&)
132 {
133 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
134 "BasicLibrariesElement::startChildElement");
135 }
136 catch (const lang::IllegalArgumentException&)
137 {
138 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
139 "BasicLibrariesElement::startChildElement");
140 }
141 }
142 }
143 else if ((nElement & TOKEN_MASK) == XML_LIBRARY_EMBEDDED)
144 {
145 // TODO: create password protected libraries
146
147 OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);
148
149 bool bReadOnly = false;
151
152 if (m_xLibContainer.is())
153 {
154 try
155 {
156 Reference<container::XNameContainer> xLib;
157 if (m_xLibContainer->hasByName(aName))
158 {
159 // Standard library
160 m_xLibContainer->getByName(aName) >>= xLib;
161 }
162 else
163 {
164 xLib.set(m_xLibContainer->createLibrary(aName));
165 }
166
167 if (xLib.is())
169 bReadOnly);
170 }
171 catch (const lang::IllegalArgumentException&)
172 {
173 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
174 "BasicLibrariesElement::startChildElement");
175 }
176 }
177 }
178 else
179 {
180 throw xml::sax::SAXException("expected library-linked or library-embedded element!",
181 Reference<XInterface>(), Any());
182 }
183
184 return nullptr;
185}
186
187// BasicEmbeddedLibraryElement
188
190 SvXMLImport& rImport, const Reference<script::XLibraryContainer2>& rxLibContainer,
191 OUString aLibName, bool bReadOnly)
192 : BasicElementBase(rImport)
193 , m_xLibContainer(rxLibContainer)
194 , m_aLibName(std::move(aLibName))
196{
197 try
198 {
199 if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName))
200 m_xLibContainer->getByName(m_aLibName) >>= m_xLib;
201 }
202 catch (const lang::WrappedTargetException&)
203 {
204 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicEmbeddedLibraryElement::CTOR:");
205 }
206}
207
209 sal_Int32 nElement, const Reference<XFastAttributeList>& xAttributes)
210{
212 {
213 throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
214 }
215 else if ((nElement & TOKEN_MASK) == XML_MODULE)
216 {
217 OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);
218
219 if (m_xLib.is() && !aName.isEmpty())
221 }
222 else
223 {
224 throw xml::sax::SAXException("expected module element!", Reference<XInterface>(), Any());
225 }
226
227 return nullptr;
228}
229
231{
232 if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName) && m_bReadOnly)
233 m_xLibContainer->setLibraryReadOnly(m_aLibName, m_bReadOnly);
234}
235
236// BasicModuleElement
237
239 const Reference<container::XNameContainer>& rxLib,
240 OUString aName)
241 : BasicElementBase(rImport)
242 , m_xLib(rxLib)
243 , m_aName(std::move(aName))
244{
245}
246
247Reference<XFastContextHandler>
249 const Reference<XFastAttributeList>& xAttributes)
250{
251 // TODO: <byte-code>
252
254 {
255 throw xml::sax::SAXException("illegal namespace!", Reference<XInterface>(), Any());
256 }
257 else if ((nElement & TOKEN_MASK) == XML_SOURCE_CODE)
258 {
259 // TODO: password protected libraries
260
261 if (xAttributes.is())
262 {
263 if (m_xLib.is() && !m_aName.isEmpty())
265 }
266 }
267 else
268 {
269 throw xml::sax::SAXException("expected source-code element!", Reference<XInterface>(),
270 Any());
271 }
272
273 return nullptr;
274}
275
276// BasicSourceCodeElement
277
279 const Reference<container::XNameContainer>& rxLib,
280 OUString rName)
281 : BasicElementBase(rImport)
282 , m_xLib(rxLib)
283 , m_aName(std::move(rName))
284{
285}
286
287// XElement
288
289void BasicSourceCodeElement::characters(const OUString& rChars) { m_aBuffer.append(rChars); }
290
292{
293 try
294 {
295 if (m_xLib.is() && !m_aName.isEmpty())
296 {
297 Any aElement;
298 aElement <<= m_aBuffer.makeStringAndClear();
299 m_xLib->insertByName(m_aName, aElement);
300 }
301 }
302 catch (const container::ElementExistException&)
303 {
304 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
305 }
306 catch (const lang::IllegalArgumentException&)
307 {
308 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
309 }
310 catch (const lang::WrappedTargetException&)
311 {
312 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
313 }
314}
315}
316
317/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
BasicElementBase(SvXMLImport &rImport)
static bool getBoolAttr(bool *pRet, sal_Int32 nToken, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttributes)
css::uno::Reference< css::container::XNameContainer > m_xLib
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
css::uno::Reference< css::script::XLibraryContainer2 > m_xLibContainer
BasicEmbeddedLibraryElement(SvXMLImport &rImport, const css::uno::Reference< css::script::XLibraryContainer2 > &rxLibContainer, OUString aLibName, bool bReadOnly)
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
css::uno::Reference< css::script::XLibraryContainer2 > m_xLibContainer
BasicLibrariesElement(SvXMLImport &rImport, const css::uno::Reference< css::frame::XModel > &rxModel)
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
BasicModuleElement(SvXMLImport &rImport, const css::uno::Reference< css::container::XNameContainer > &rxLib, OUString aName)
css::uno::Reference< css::container::XNameContainer > m_xLib
css::uno::Reference< css::container::XNameContainer > m_xLib
virtual void SAL_CALL characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
BasicSourceCodeElement(SvXMLImport &rImport, const css::uno::Reference< css::container::XNameContainer > &rxLib, OUString rName)
#define TOOLS_INFO_EXCEPTION(area, stream)
bool bReadOnly
bool m_bReadOnly
OUString aName
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
OUString m_aName
DefTokenId nToken
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:98
constexpr sal_Int32 NAMESPACE_TOKEN(sal_uInt16 prefixToken)
Definition: xmlimp.hxx:100
constexpr bool IsTokenInNamespace(sal_Int32 nToken, sal_uInt16 nNamespacePrefix)
Definition: xmlimp.hxx:105
constexpr sal_Int32 TOKEN_MASK
Definition: xmlimp.hxx:95
constexpr sal_uInt16 XML_NAMESPACE_OOO