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