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