| 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 <config_features.h> |
| 24 |
#include <sal/log.hxx> |
| 25 |
#include <utility> |
| 26 |
#include <xmloff/xmlnamespace.hxx> |
| 27 |
#include <xmloff/xmltoken.hxx> |
| 28 |
#include <com/sun/star/beans/XPropertySet.hpp> |
| 29 |
#include <com/sun/star/document/XEmbeddedScripts.hpp> |
| 30 |
#include <com/sun/star/xml/sax/SAXException.hpp> |
| 31 |
#include <comphelper/diagnose_ex.hxx> |
| 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 |
|
| 43 |
BasicElementBase::BasicElementBase(SvXMLImport& rImport) |
| 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)!", |
| 68 |
Reference<XInterface>(), Any()); |
| 69 |
} |
| 70 |
} |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
// BasicLibrariesElement |
| 75 |
|
| 76 |
BasicLibrariesElement::BasicLibrariesElement(SvXMLImport& rImport, |
| 77 |
const css::uno::Reference<css::frame::XModel>& rxModel) |
| 78 |
: BasicElementBase(rImport) |
| 79 |
{ |
| 80 |
// try the XEmbeddedScripts interface |
| 81 |
Reference<document::XEmbeddedScripts> xDocumentScripts(rxModel, UNO_QUERY_THROW); |
| 82 |
m_xLibContainer = xDocumentScripts->getBasicLibraries(); |
| 83 |
|
| 84 |
if (!m_xLibContainer.is()) |
| 85 |
{ |
| 86 |
// try the "BasicLibraries" property (old-style, for compatibility) |
| 87 |
Reference<beans::XPropertySet> xPSet(rxModel, UNO_QUERY); |
| 88 |
if (xPSet.is()) |
| 89 |
xPSet->getPropertyValue(u"BasicLibraries"_ustr) >>= m_xLibContainer; |
| 90 |
} |
| 91 |
|
| 92 |
SAL_WARN_IF(!m_xLibContainer.is(), "xmlscript.xmlflat", |
| 93 |
"BasicImport::startRootElement: nowhere to import to!"); |
| 94 |
|
| 95 |
#if HAVE_FEATURE_SCRIPTING |
| 96 |
if (!m_xLibContainer.is()) |
| 97 |
{ |
| 98 |
throw xml::sax::SAXException(u"nowhere to import to"_ustr, Reference<XInterface>(), Any()); |
| 99 |
} |
| 100 |
#endif |
| 101 |
} |
| 102 |
|
| 103 |
// XElement |
| 104 |
|
| 105 |
Reference<XFastContextHandler> |
| 106 |
BasicLibrariesElement::createFastChildContext(sal_Int32 nElement, |
| 107 |
const Reference<XFastAttributeList>& xAttributes) |
| 108 |
{ |
| 109 |
if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO)) |
| 110 |
{ |
| 111 |
throw xml::sax::SAXException(u"illegal namespace!"_ustr, 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; |
| 120 |
getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes); |
| 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; |
| 150 |
getBoolAttr(&bReadOnly, NAMESPACE_TOKEN(XML_NAMESPACE_OOO) | XML_READONLY, xAttributes); |
| 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()) |
| 168 |
return new BasicEmbeddedLibraryElement(GetImport(), m_xLibContainer, aName, |
| 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(u"expected library-linked or library-embedded element!"_ustr, |
| 181 |
Reference<XInterface>(), Any()); |
| 182 |
} |
| 183 |
|
| 184 |
return nullptr; |
| 185 |
} |
| 186 |
|
| 187 |
// BasicEmbeddedLibraryElement |
| 188 |
|
| 189 |
BasicEmbeddedLibraryElement::BasicEmbeddedLibraryElement( |
| 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)) |
| 195 |
, m_bReadOnly(bReadOnly) |
| 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 |
|
| 208 |
Reference<XFastContextHandler> BasicEmbeddedLibraryElement::createFastChildContext( |
| 209 |
sal_Int32 nElement, const Reference<XFastAttributeList>& xAttributes) |
| 210 |
{ |
| 211 |
if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO)) |
| 212 |
{ |
| 213 |
throw xml::sax::SAXException(u"illegal namespace!"_ustr, 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()) |
| 220 |
return new BasicModuleElement(GetImport(), m_xLib, aName); |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
throw xml::sax::SAXException(u"expected module element!"_ustr, Reference<XInterface>(), |
| 225 |
Any()); |
| 226 |
} |
| 227 |
|
| 228 |
return nullptr; |
| 229 |
} |
| 230 |
|
| 231 |
void BasicEmbeddedLibraryElement::endFastElement(sal_Int32) |
| 232 |
{ |
| 233 |
if (m_xLibContainer.is() && m_xLibContainer->hasByName(m_aLibName) && m_bReadOnly) |
| 234 |
m_xLibContainer->setLibraryReadOnly(m_aLibName, m_bReadOnly); |
| 235 |
} |
| 236 |
|
| 237 |
// BasicModuleElement |
| 238 |
|
| 239 |
BasicModuleElement::BasicModuleElement(SvXMLImport& rImport, |
| 240 |
const Reference<container::XNameContainer>& rxLib, |
| 241 |
OUString aName) |
| 242 |
: BasicElementBase(rImport) |
| 243 |
, m_xLib(rxLib) |
| 244 |
, m_aName(std::move(aName)) |
| 245 |
{ |
| 246 |
} |
| 247 |
|
| 248 |
Reference<XFastContextHandler> |
| 249 |
BasicModuleElement::createFastChildContext(sal_Int32 nElement, |
| 250 |
const Reference<XFastAttributeList>& xAttributes) |
| 251 |
{ |
| 252 |
// TODO: <byte-code> |
| 253 |
|
| 254 |
if (!IsTokenInNamespace(nElement, XML_NAMESPACE_OOO)) |
| 255 |
{ |
| 256 |
throw xml::sax::SAXException(u"illegal namespace!"_ustr, Reference<XInterface>(), Any()); |
| 257 |
} |
| 258 |
else if ((nElement & TOKEN_MASK) == XML_SOURCE_CODE) |
| 259 |
{ |
| 260 |
// TODO: password protected libraries |
| 261 |
|
| 262 |
if (xAttributes.is()) |
| 263 |
{ |
| 264 |
if (m_xLib.is() && !m_aName.isEmpty()) |
| 265 |
return new BasicSourceCodeElement(GetImport(), m_xLib, m_aName); |
| 266 |
} |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
throw xml::sax::SAXException(u"expected source-code element!"_ustr, Reference<XInterface>(), |
| 271 |
Any()); |
| 272 |
} |
| 273 |
|
| 274 |
return nullptr; |
| 275 |
} |
| 276 |
|
| 277 |
// BasicSourceCodeElement |
| 278 |
|
| 279 |
BasicSourceCodeElement::BasicSourceCodeElement(SvXMLImport& rImport, |
| 280 |
const Reference<container::XNameContainer>& rxLib, |
| 281 |
OUString rName) |
| 282 |
: BasicElementBase(rImport) |
| 283 |
, m_xLib(rxLib) |
| 284 |
, m_aName(std::move(rName)) |
| 285 |
{ |
| 286 |
} |
| 287 |
|
| 288 |
// XElement |
| 289 |
|
| 290 |
void BasicSourceCodeElement::characters(const OUString& rChars) { m_aBuffer.append(rChars); } |
| 291 |
|
| 292 |
void BasicSourceCodeElement::endFastElement(sal_Int32) |
| 293 |
{ |
| 294 |
try |
| 295 |
{ |
| 296 |
if (m_xLib.is() && !m_aName.isEmpty()) |
| 297 |
{ |
| 298 |
Any aElement; |
| 299 |
aElement <<= m_aBuffer.makeStringAndClear(); |
| 300 |
m_xLib->insertByName(m_aName, aElement); |
| 301 |
} |
| 302 |
} |
| 303 |
catch (const container::ElementExistException&) |
| 304 |
{ |
| 305 |
TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement"); |
| 306 |
} |
| 307 |
catch (const lang::IllegalArgumentException&) |
| 308 |
{ |
| 309 |
TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement"); |
| 310 |
} |
| 311 |
catch (const lang::WrappedTargetException&) |
| 312 |
{ |
| 313 |
TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "BasicSourceCodeElement::endElement"); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |