tdf#120397 - odf export fix missing texts in text box control
[LibreOffice.git] / xmloff / source / script / xmlbasicscript.cxx
blobdc99e90a7d39dc32a4ece194bd90179862ce6c52
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 <sal/config.h>
22 #include "xmlbasicscript.hxx"
23 #include <sal/log.hxx>
24 #include <utility>
25 #include <xmloff/xmlnamespace.hxx>
26 #include <xmloff/xmltoken.hxx>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/document/XEmbeddedScripts.hpp>
29 #include <com/sun/star/xml/sax/SAXException.hpp>
30 #include <comphelper/diagnose_ex.hxx>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::xml::sax;
36 using namespace ::xmloff::token;
38 namespace xmloff
40 // BasicElementBase
42 BasicElementBase::BasicElementBase(SvXMLImport& rImport)
43 : SvXMLImportContext(rImport)
47 bool BasicElementBase::getBoolAttr(bool* pRet, sal_Int32 nToken,
48 const Reference<XFastAttributeList>& xAttributes)
50 OUString aValue = xAttributes->getOptionalValue(nToken);
51 if (!aValue.isEmpty())
53 if (aValue == "true")
55 *pRet = true;
56 return true;
58 else if (aValue == "false")
60 *pRet = false;
61 return true;
63 else
65 throw xml::sax::SAXException(SvXMLImport::getNameFromToken(nToken)
66 + ": no boolean value (true|false)!",
67 Reference<XInterface>(), Any());
70 return false;
73 // BasicLibrariesElement
75 BasicLibrariesElement::BasicLibrariesElement(SvXMLImport& rImport,
76 const css::uno::Reference<css::frame::XModel>& rxModel)
77 : BasicElementBase(rImport)
79 // try the XEmbeddedScripts interface
80 Reference<document::XEmbeddedScripts> xDocumentScripts(rxModel, UNO_QUERY_THROW);
81 m_xLibContainer = xDocumentScripts->getBasicLibraries();
83 if (!m_xLibContainer.is())
85 // try the "BasicLibraries" property (old-style, for compatibility)
86 Reference<beans::XPropertySet> xPSet(rxModel, UNO_QUERY);
87 if (xPSet.is())
88 xPSet->getPropertyValue(u"BasicLibraries"_ustr) >>= m_xLibContainer;
91 SAL_WARN_IF(!m_xLibContainer.is(), "xmlscript.xmlflat",
92 "BasicImport::startRootElement: nowhere to import to!");
94 if (!m_xLibContainer.is())
96 throw xml::sax::SAXException(u"nowhere to import to"_ustr, Reference<XInterface>(), Any());
100 // XElement
102 Reference<XFastContextHandler>
103 BasicLibrariesElement::createFastChildContext(sal_Int32 nElement,
104 const Reference<XFastAttributeList>& xAttributes)
106 if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO))
108 throw xml::sax::SAXException(u"illegal namespace!"_ustr, Reference<XInterface>(), Any());
110 else if ((nElement & TOKEN_MASK) == XML_LIBRARY_LINKED)
112 OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);
114 OUString aStorageURL = xAttributes->getValue(XML_ELEMENT(XLINK, XML_HREF));
116 bool bReadOnly = false;
117 getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes);
119 if (m_xLibContainer.is())
123 Reference<container::XNameAccess> xLib(
124 m_xLibContainer->createLibraryLink(aName, aStorageURL, bReadOnly));
125 if (xLib.is())
126 return new BasicElementBase(GetImport());
128 catch (const container::ElementExistException&)
130 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
131 "BasicLibrariesElement::startChildElement");
133 catch (const lang::IllegalArgumentException&)
135 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
136 "BasicLibrariesElement::startChildElement");
140 else if ((nElement & TOKEN_MASK) == XML_LIBRARY_EMBEDDED)
142 // TODO: create password protected libraries
144 OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);
146 bool bReadOnly = false;
147 getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes);
149 if (m_xLibContainer.is())
153 Reference<container::XNameContainer> xLib;
154 if (m_xLibContainer->hasByName(aName))
156 // Standard library
157 m_xLibContainer->getByName(aName) >>= xLib;
159 else
161 xLib.set(m_xLibContainer->createLibrary(aName));
164 if (xLib.is())
165 return new BasicEmbeddedLibraryElement(GetImport(), m_xLibContainer, aName,
166 bReadOnly);
168 catch (const lang::IllegalArgumentException&)
170 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat",
171 "BasicLibrariesElement::startChildElement");
175 else
177 throw xml::sax::SAXException(u"expected library-linked or library-embedded element!"_ustr,
178 Reference<XInterface>(), Any());
181 return nullptr;
184 // BasicEmbeddedLibraryElement
186 BasicEmbeddedLibraryElement::BasicEmbeddedLibraryElement(
187 SvXMLImport& rImport, const Reference<script::XLibraryContainer2>& rxLibContainer,
188 OUString aLibName, bool bReadOnly)
189 : BasicElementBase(rImport)
190 , m_xLibContainer(rxLibContainer)
191 , m_aLibName(std::move(aLibName))
192 , m_bReadOnly(bReadOnly)
196 if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName))
197 m_xLibContainer->getByName(m_aLibName) >>= m_xLib;
199 catch (const lang::WrappedTargetException&)
201 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicEmbeddedLibraryElement::CTOR:");
205 Reference<XFastContextHandler> BasicEmbeddedLibraryElement::createFastChildContext(
206 sal_Int32 nElement, const Reference<XFastAttributeList>& xAttributes)
208 if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO))
210 throw xml::sax::SAXException(u"illegal namespace!"_ustr, Reference<XInterface>(), Any());
212 else if ((nElement & TOKEN_MASK) == XML_MODULE)
214 OUString aName = xAttributes->getValue(NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_NAME);
216 if (m_xLib.is() && !aName.isEmpty())
217 return new BasicModuleElement(GetImport(), m_xLib, aName);
219 else
221 throw xml::sax::SAXException(u"expected module element!"_ustr, Reference<XInterface>(),
222 Any());
225 return nullptr;
228 void BasicEmbeddedLibraryElement::endFastElement(sal_Int32)
230 if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName) && m_bReadOnly)
231 m_xLibContainer->setLibraryReadOnly(m_aLibName, m_bReadOnly);
234 // BasicModuleElement
236 BasicModuleElement::BasicModuleElement(SvXMLImport& rImport,
237 const Reference<container::XNameContainer>& rxLib,
238 OUString aName)
239 : BasicElementBase(rImport)
240 , m_xLib(rxLib)
241 , m_aName(std::move(aName))
245 Reference<XFastContextHandler>
246 BasicModuleElement::createFastChildContext(sal_Int32 nElement,
247 const Reference<XFastAttributeList>& xAttributes)
249 // TODO: <byte-code>
251 if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO))
253 throw xml::sax::SAXException(u"illegal namespace!"_ustr, Reference<XInterface>(), Any());
255 else if ((nElement & TOKEN_MASK) == XML_SOURCE_CODE)
257 // TODO: password protected libraries
259 if (xAttributes.is())
261 if (m_xLib.is() && !m_aName.isEmpty())
262 return new BasicSourceCodeElement(GetImport(), m_xLib, m_aName);
265 else
267 throw xml::sax::SAXException(u"expected source-code element!"_ustr, Reference<XInterface>(),
268 Any());
271 return nullptr;
274 // BasicSourceCodeElement
276 BasicSourceCodeElement::BasicSourceCodeElement(SvXMLImport& rImport,
277 const Reference<container::XNameContainer>& rxLib,
278 OUString rName)
279 : BasicElementBase(rImport)
280 , m_xLib(rxLib)
281 , m_aName(std::move(rName))
285 // XElement
287 void BasicSourceCodeElement::characters(const OUString& rChars) { m_aBuffer.append(rChars); }
289 void BasicSourceCodeElement::endFastElement(sal_Int32)
293 if (m_xLib.is() && !m_aName.isEmpty())
295 Any aElement;
296 aElement <<= m_aBuffer.makeStringAndClear();
297 m_xLib->insertByName(m_aName, aElement);
300 catch (const container::ElementExistException&)
302 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
304 catch (const lang::IllegalArgumentException&)
306 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
308 catch (const lang::WrappedTargetException&)
310 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement");
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */