Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmlscript / source / xmllib_imexp / imp_share.hxx
blobf88474907922823401ed6d166564cb1f339b3ab5
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 #pragma once
22 #include <xmlscript/xmllib_imexp.hxx>
24 #include <cppuhelper/implbase.hxx>
26 #include <com/sun/star/xml/input/XRoot.hpp>
27 #include <com/sun/star/xml/sax/SAXException.hpp>
28 #include <rtl/ref.hxx>
29 #include <o3tl/string_view.hxx>
31 #include <vector>
33 namespace xmlscript
35 inline sal_Int32 toInt32( std::u16string_view rStr )
37 sal_Int32 nVal;
38 if (rStr.size() > 2 && rStr[ 0 ] == '0' && rStr[ 1 ] == 'x')
40 nVal = o3tl::toUInt32(rStr.substr( 2 ), 16);
42 else
44 nVal = o3tl::toInt32(rStr);
46 return nVal;
48 inline bool getBoolAttr(
49 bool * pRet, OUString const & rAttrName,
50 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, sal_Int32 uid )
52 OUString aValue(
53 xAttributes->getValueByUidName( uid, rAttrName ) );
54 if (!aValue.isEmpty())
56 if ( aValue == "true" )
58 *pRet = true;
59 return true;
61 else if ( aValue == "false" )
63 *pRet = false;
64 return true;
66 else
68 throw css::xml::sax::SAXException(rAttrName + ": no boolean value (true|false)!", css::uno::Reference< css::uno::XInterface >(), css::uno::Any() );
71 return false;
74 inline bool getStringAttr(
75 OUString * pRet, OUString const & rAttrName,
76 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, sal_Int32 uid )
78 *pRet = xAttributes->getValueByUidName( uid, rAttrName );
79 return (!pRet->isEmpty());
82 inline bool getLongAttr(
83 sal_Int32 * pRet, OUString const & rAttrName,
84 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes,
85 sal_Int32 uid )
87 OUString aValue(
88 xAttributes->getValueByUidName( uid, rAttrName ) );
89 if (!aValue.isEmpty())
91 *pRet = toInt32( aValue );
92 return true;
94 return false;
97 // Library import
99 struct LibraryImport
100 : public ::cppu::WeakImplHelper< css::xml::input::XRoot >
102 friend class LibrariesElement;
103 friend class LibraryElement;
105 LibDescriptorArray* mpLibArray;
106 LibDescriptor* const mpLibDesc; // Single library mode
108 sal_Int32 XMLNS_LIBRARY_UID;
109 sal_Int32 XMLNS_XLINK_UID;
111 public:
112 explicit LibraryImport( LibDescriptorArray* pLibArray )
113 : mpLibArray(pLibArray)
114 , mpLibDesc(nullptr)
115 , XMLNS_LIBRARY_UID(0)
116 , XMLNS_XLINK_UID(0)
120 // Single library mode
121 explicit LibraryImport(LibDescriptor* pLibDesc)
122 : mpLibArray(nullptr)
123 , mpLibDesc(pLibDesc)
124 , XMLNS_LIBRARY_UID(0)
125 , XMLNS_XLINK_UID(0)
129 virtual ~LibraryImport() override;
131 // XRoot
132 virtual void SAL_CALL startDocument(
133 css::uno::Reference< css::xml::input::XNamespaceMapping > const & xNamespaceMapping ) override;
134 virtual void SAL_CALL endDocument() override;
135 virtual void SAL_CALL processingInstruction(
136 OUString const & rTarget, OUString const & rData ) override;
137 virtual void SAL_CALL setDocumentLocator(
138 css::uno::Reference< css::xml::sax::XLocator > const & xLocator ) override;
139 virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startRootElement(
140 sal_Int32 nUid, OUString const & rLocalName,
141 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override;
144 class LibElementBase
145 : public ::cppu::WeakImplHelper< css::xml::input::XElement >
147 protected:
148 rtl::Reference<LibraryImport> mxImport;
149 rtl::Reference<LibElementBase> mxParent;
150 private:
151 OUString const _aLocalName;
152 css::uno::Reference< css::xml::input::XAttributes > _xAttributes;
154 public:
155 LibElementBase(
156 OUString aLocalName,
157 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes,
158 LibElementBase * pParent, LibraryImport * pImport );
159 virtual ~LibElementBase() override;
161 // XElement
162 virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL getParent() override;
163 virtual OUString SAL_CALL getLocalName() override;
164 virtual sal_Int32 SAL_CALL getUid() override;
165 virtual css::uno::Reference< css::xml::input::XAttributes > SAL_CALL getAttributes() override;
166 virtual void SAL_CALL ignorableWhitespace(
167 OUString const & rWhitespaces ) override;
168 virtual void SAL_CALL characters( OUString const & rChars ) override;
169 virtual void SAL_CALL processingInstruction(
170 OUString const & rTarget, OUString const & rData ) override;
171 virtual void SAL_CALL endElement() override;
172 virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement(
173 sal_Int32 nUid, OUString const & rLocalName,
174 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override;
177 class LibrariesElement : public LibElementBase
179 friend class LibraryElement;
181 std::vector< LibDescriptor > mLibDescriptors;
183 public:
184 virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement(
185 sal_Int32 nUid, OUString const & rLocalName,
186 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override;
187 virtual void SAL_CALL endElement() override;
189 LibrariesElement(
190 OUString const & rLocalName,
191 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes,
192 LibraryImport * pImport )
193 : LibElementBase( rLocalName, xAttributes, nullptr, pImport )
197 class LibraryElement : public LibElementBase
199 std::vector< OUString > mElements;
201 public:
203 virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement(
204 sal_Int32 nUid, OUString const & rLocalName,
205 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override;
206 virtual void SAL_CALL endElement() override;
208 LibraryElement(
209 OUString const & rLocalName,
210 css::uno::Reference< css::xml::input::XAttributes > const & xAttributes,
211 LibElementBase * pParent, LibraryImport * pImport )
212 : LibElementBase( rLocalName, xAttributes, pParent, pImport )
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */