Avoid potential negative array index access to cached text.
[LibreOffice.git] / scripting / source / stringresource / stringresource.hxx
blob50fd8567e0e6b434c748a7ae64a83cbf59c12dcd
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 <com/sun/star/resource/XStringResourceWithStorage.hpp>
23 #include <com/sun/star/resource/XStringResourceWithLocation.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/lang/XInitialization.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
28 #include <com/sun/star/io/XInputStream.hpp>
29 #include <com/sun/star/io/XOutputStream.hpp>
30 #include <cppuhelper/implbase.hxx>
31 #include <comphelper/interfacecontainer4.hxx>
32 #include <mutex>
33 #include <unordered_map>
34 #include <utility>
35 #include <vector>
38 namespace stringresource
42 // class stringresourceImpl
45 // Hashtable to map string ids to string
46 typedef std::unordered_map
48 OUString,
49 OUString
51 IdToStringMap;
53 typedef std::unordered_map
55 OUString,
56 sal_Int32
58 IdToIndexMap;
61 struct LocaleItem
63 css::lang::Locale m_locale;
64 IdToStringMap m_aIdToStringMap;
65 IdToIndexMap m_aIdToIndexMap;
66 sal_Int32 m_nNextIndex;
67 bool m_bLoaded;
68 bool m_bModified;
70 LocaleItem( css::lang::Locale locale, bool bLoaded=true )
71 : m_locale(std::move( locale ))
72 , m_nNextIndex( 0 )
73 , m_bLoaded( bLoaded )
74 , m_bModified( false )
78 typedef ::cppu::WeakImplHelper<
79 css::lang::XServiceInfo,
80 css::resource::XStringResourceManager > StringResourceImpl_BASE;
82 class StringResourceImpl : public StringResourceImpl_BASE
84 protected:
85 std::mutex m_aMutex;
86 css::uno::Reference< css::uno::XComponentContext > m_xContext;
88 LocaleItem* m_pCurrentLocaleItem;
89 LocaleItem* m_pDefaultLocaleItem;
90 bool m_bDefaultModified;
92 ::comphelper::OInterfaceContainerHelper4<css::util::XModifyListener> m_aListenerContainer;
94 std::vector< std::unique_ptr<LocaleItem> > m_aLocaleItemVector;
95 std::vector< std::unique_ptr<LocaleItem> > m_aDeletedLocaleItemVector;
96 std::vector< std::unique_ptr<LocaleItem> > m_aChangedDefaultLocaleVector;
98 bool m_bModified;
99 bool m_bReadOnly;
101 sal_Int32 m_nNextUniqueNumericId;
103 // Scans ResourceID to start with number and adapt m_nNextUniqueNumericId
104 void implScanIdForNumber( const OUString& ResourceID );
105 const static sal_Int32 UNIQUE_NUMBER_NEEDS_INITIALISATION = -1;
107 // Checks read only status and throws exception if it's true
108 /// @throws css::lang::NoSupportException
109 void implCheckReadOnly( const char* pExceptionMsg );
111 // Returns the LocalItem for a given locale, if it exists, otherwise NULL
112 // This method compares the locales exactly, no closest match search is performed
113 /// @throws css::lang::IllegalArgumentException
114 LocaleItem* getItemForLocale( const css::lang::Locale& locale, bool bException );
116 // Returns the LocalItem for a given locale, if it exists, otherwise NULL
117 // This method performs a closest match search, at least the language must match
118 LocaleItem* getClosestMatchItemForLocale( const css::lang::Locale& locale );
119 /// @throws css::lang::IllegalArgumentException
120 /// @throws css::uno::RuntimeException
121 void implSetCurrentLocale( std::unique_lock<std::mutex>& rGuard, const css::lang::Locale& locale,
122 bool FindClosestMatch, bool bUseDefaultIfNoMatch );
124 void implModified(std::unique_lock<std::mutex>&);
125 void implNotifyListeners(std::unique_lock<std::mutex>&);
127 //=== Impl methods for ...ForLocale methods ===
128 /// @throws css::resource::MissingResourceException
129 OUString implResolveString( const OUString& ResourceID, LocaleItem* pLocaleItem );
130 bool implHasEntryForId( const OUString& ResourceID, LocaleItem* pLocaleItem );
131 css::uno::Sequence< OUString > implGetResourceIDs( LocaleItem* pLocaleItem );
132 void implSetString( std::unique_lock<std::mutex>& rGuard, const OUString& ResourceID,
133 const OUString& Str, LocaleItem* pLocaleItem );
134 /// @throws css::resource::MissingResourceException
135 void implRemoveId( std::unique_lock<std::mutex>& rGuard, const OUString& ResourceID, LocaleItem* pLocaleItem );
137 // Method to load a locale if necessary, returns true if loading was
138 // successful. Default implementation in base class just returns true.
139 virtual bool loadLocale( LocaleItem* pLocaleItem );
141 virtual void implLoadAllLocales();
143 public:
144 explicit StringResourceImpl(
145 const css::uno::Reference< css::uno::XComponentContext >& rxContext );
146 virtual ~StringResourceImpl() override;
148 // XServiceInfo
149 virtual OUString SAL_CALL getImplementationName( ) override;
150 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
151 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
153 // XModifyBroadcaster
154 virtual void SAL_CALL addModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
155 virtual void SAL_CALL removeModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
157 // XStringResourceResolver
158 virtual OUString SAL_CALL resolveString( const OUString& ResourceID ) override;
159 virtual OUString SAL_CALL resolveStringForLocale( const OUString& ResourceID,
160 const css::lang::Locale& locale ) override;
161 virtual sal_Bool SAL_CALL hasEntryForId( const OUString& ResourceID ) override;
162 virtual sal_Bool SAL_CALL hasEntryForIdAndLocale( const OUString& ResourceID,
163 const css::lang::Locale& locale ) override;
164 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDs( ) override;
165 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDsForLocale
166 ( const css::lang::Locale& locale ) override;
167 virtual css::lang::Locale SAL_CALL getCurrentLocale( ) override;
168 virtual css::lang::Locale SAL_CALL getDefaultLocale( ) override;
169 virtual css::uno::Sequence< css::lang::Locale > SAL_CALL getLocales( ) override;
171 // XStringResourceManager
172 virtual sal_Bool SAL_CALL isReadOnly() override;
173 virtual void SAL_CALL setCurrentLocale( const css::lang::Locale& locale, sal_Bool FindClosestMatch ) override;
174 virtual void SAL_CALL setDefaultLocale( const css::lang::Locale& locale ) override;
175 virtual void SAL_CALL setString( const OUString& ResourceID, const OUString& Str ) override;
176 virtual void SAL_CALL setStringForLocale( const OUString& ResourceID, const OUString& Str,
177 const css::lang::Locale& locale ) override;
178 virtual void SAL_CALL removeId( const OUString& ResourceID ) override;
179 virtual void SAL_CALL removeIdForLocale( const OUString& ResourceID,
180 const css::lang::Locale& locale ) override;
181 virtual void SAL_CALL newLocale( const css::lang::Locale& locale ) override;
182 virtual void SAL_CALL removeLocale( const css::lang::Locale& locale ) override;
183 virtual ::sal_Int32 SAL_CALL getUniqueNumericId( ) override;
186 typedef ::cppu::ImplInheritanceHelper<
187 StringResourceImpl,
188 css::resource::XStringResourcePersistence > StringResourcePersistenceImpl_BASE;
190 class BinaryOutput;
192 class StringResourcePersistenceImpl : public StringResourcePersistenceImpl_BASE
194 protected:
195 OUString m_aNameBase;
196 OUString m_aComment;
198 /// @throws css::uno::Exception
199 /// @throws css::uno::RuntimeException
200 void implInitializeCommonParameters( std::unique_lock<std::mutex>& rGuard, const css::uno::Sequence< css::uno::Any >& aArguments );
202 // Scan locale properties files
203 virtual void implScanLocales();
205 // Method to load a locale if necessary, returns true if loading was successful
206 virtual bool loadLocale( LocaleItem* pLocaleItem ) override;
208 // does the actual loading
209 virtual bool implLoadLocale( LocaleItem* pLocaleItem );
211 virtual void implLoadAllLocales() override;
213 void implScanLocaleNames( const css::uno::Sequence< OUString >& aContentSeq );
214 static OUString implGetFileNameForLocaleItem( LocaleItem const * pLocaleItem, const OUString& aNameBase );
215 static OUString implGetPathForLocaleItem( LocaleItem const * pLocaleItem, const OUString& aNameBase,
216 std::u16string_view aLocation, bool bDefaultFile=false );
218 bool implReadPropertiesFile( LocaleItem* pLocaleItem,
219 const css::uno::Reference< css::io::XInputStream >& xInput );
221 bool implWritePropertiesFile( LocaleItem const * pLocaleItem,
222 const css::uno::Reference< css::io::XOutputStream >& xOutputStream,
223 const OUString& aComment );
225 void implWriteLocaleBinary( LocaleItem* pLocaleItem, BinaryOutput& rOut );
227 /// @throws css::uno::Exception
228 /// @throws css::uno::RuntimeException
229 void implStoreAtStorage
231 const OUString& aNameBase,
232 const OUString& aComment,
233 const css::uno::Reference< css::embed::XStorage >& Storage,
234 bool bUsedForStore,
235 bool bStoreAll
238 /// @throws css::uno::Exception
239 /// @throws css::uno::RuntimeException
240 void implKillRemovedLocaleFiles
242 std::u16string_view Location,
243 const OUString& aNameBase,
244 const css::uno::Reference< css::ucb::XSimpleFileAccess3 >& xFileAccess
247 /// @throws css::uno::Exception
248 /// @throws css::uno::RuntimeException
249 void implKillChangedDefaultFiles
251 std::u16string_view Location,
252 const OUString& aNameBase,
253 const css::uno::Reference< css::ucb::XSimpleFileAccess3 >& xFileAccess
256 /// @throws css::uno::Exception
257 /// @throws css::uno::RuntimeException
258 void implStoreAtLocation
260 std::u16string_view Location,
261 const OUString& aNameBase,
262 const OUString& aComment,
263 const css::uno::Reference< css::ucb::XSimpleFileAccess3 >& xFileAccess,
264 bool bUsedForStore,
265 bool bStoreAll,
266 bool bKillAll = false
269 public:
270 explicit StringResourcePersistenceImpl(
271 const css::uno::Reference< css::uno::XComponentContext >& rxContext );
272 virtual ~StringResourcePersistenceImpl() override;
274 // XServiceInfo
275 virtual OUString SAL_CALL getImplementationName( ) override;
276 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
277 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
279 // XModifyBroadcaster
280 virtual void SAL_CALL addModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
281 virtual void SAL_CALL removeModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
283 // XStringResourceResolver
284 virtual OUString SAL_CALL resolveString( const OUString& ResourceID ) override;
285 virtual OUString SAL_CALL resolveStringForLocale( const OUString& ResourceID,
286 const css::lang::Locale& locale ) override;
287 virtual sal_Bool SAL_CALL hasEntryForId( const OUString& ResourceID ) override;
288 virtual sal_Bool SAL_CALL hasEntryForIdAndLocale( const OUString& ResourceID,
289 const css::lang::Locale& locale ) override;
290 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDs( ) override;
291 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDsForLocale
292 ( const css::lang::Locale& locale ) override;
293 virtual css::lang::Locale SAL_CALL getCurrentLocale( ) override;
294 virtual css::lang::Locale SAL_CALL getDefaultLocale( ) override;
295 virtual css::uno::Sequence< css::lang::Locale > SAL_CALL getLocales( ) override;
297 // XStringResourceManager
298 virtual sal_Bool SAL_CALL isReadOnly() override;
299 virtual void SAL_CALL setCurrentLocale( const css::lang::Locale& locale, sal_Bool FindClosestMatch ) override;
300 virtual void SAL_CALL setDefaultLocale( const css::lang::Locale& locale ) override;
301 virtual void SAL_CALL setString( const OUString& ResourceID, const OUString& Str ) override;
302 virtual void SAL_CALL setStringForLocale( const OUString& ResourceID, const OUString& Str,
303 const css::lang::Locale& locale ) override;
304 virtual void SAL_CALL removeId( const OUString& ResourceID ) override;
305 virtual void SAL_CALL removeIdForLocale( const OUString& ResourceID,
306 const css::lang::Locale& locale ) override;
307 virtual void SAL_CALL newLocale( const css::lang::Locale& locale ) override;
308 virtual void SAL_CALL removeLocale( const css::lang::Locale& locale ) override;
309 virtual ::sal_Int32 SAL_CALL getUniqueNumericId( ) override;
311 // XStringResourcePersistence
312 virtual void SAL_CALL store( ) override;
313 virtual sal_Bool SAL_CALL isModified( ) override;
314 virtual void SAL_CALL setComment( const OUString& Comment ) override;
315 virtual void SAL_CALL storeToStorage
316 ( const css::uno::Reference< css::embed::XStorage >& Storage,
317 const OUString& NameBase, const OUString& Comment ) override;
318 virtual void SAL_CALL storeToURL( const OUString& URL, const OUString& NameBase,
319 const OUString& Comment, const css::uno::Reference
320 < css::task::XInteractionHandler >& Handler ) override;
321 virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL exportBinary( ) override;
322 virtual void SAL_CALL importBinary( const css::uno::Sequence< ::sal_Int8 >& Data ) override;
326 typedef ::cppu::ImplInheritanceHelper<
327 StringResourcePersistenceImpl,
328 css::lang::XInitialization,
329 css::resource::XStringResourceWithStorage > StringResourceWithStorageImpl_BASE;
331 class StringResourceWithStorageImpl : public StringResourceWithStorageImpl_BASE
333 css::uno::Reference< css::embed::XStorage > m_xStorage;
334 bool m_bStorageChanged;
336 virtual void implScanLocales() override;
337 virtual bool implLoadLocale( LocaleItem* pLocaleItem ) override;
339 public:
340 explicit StringResourceWithStorageImpl( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
341 virtual ~StringResourceWithStorageImpl() override;
343 // XServiceInfo
344 virtual OUString SAL_CALL getImplementationName( ) override;
345 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
346 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
348 // XInitialization
349 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
351 // XModifyBroadcaster
352 virtual void SAL_CALL addModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
353 virtual void SAL_CALL removeModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
355 // XStringResourceResolver
356 virtual OUString SAL_CALL resolveString( const OUString& ResourceID ) override;
357 virtual OUString SAL_CALL resolveStringForLocale( const OUString& ResourceID,
358 const css::lang::Locale& locale ) override;
359 virtual sal_Bool SAL_CALL hasEntryForId( const OUString& ResourceID ) override;
360 virtual sal_Bool SAL_CALL hasEntryForIdAndLocale( const OUString& ResourceID,
361 const css::lang::Locale& locale ) override;
362 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDs( ) override;
363 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDsForLocale
364 ( const css::lang::Locale& locale ) override;
365 virtual css::lang::Locale SAL_CALL getCurrentLocale( ) override;
366 virtual css::lang::Locale SAL_CALL getDefaultLocale( ) override;
367 virtual css::uno::Sequence< css::lang::Locale > SAL_CALL getLocales( ) override;
369 // XStringResourceManager
370 virtual sal_Bool SAL_CALL isReadOnly() override;
371 virtual void SAL_CALL setCurrentLocale( const css::lang::Locale& locale, sal_Bool FindClosestMatch ) override;
372 virtual void SAL_CALL setDefaultLocale( const css::lang::Locale& locale ) override;
373 virtual void SAL_CALL setString( const OUString& ResourceID, const OUString& Str ) override;
374 virtual void SAL_CALL setStringForLocale( const OUString& ResourceID, const OUString& Str,
375 const css::lang::Locale& locale ) override;
376 virtual void SAL_CALL removeId( const OUString& ResourceID ) override;
377 virtual void SAL_CALL removeIdForLocale( const OUString& ResourceID,
378 const css::lang::Locale& locale ) override;
379 virtual void SAL_CALL newLocale( const css::lang::Locale& locale ) override;
380 virtual void SAL_CALL removeLocale( const css::lang::Locale& locale ) override;
381 virtual ::sal_Int32 SAL_CALL getUniqueNumericId( ) override;
383 // XStringResourcePersistence
384 virtual void SAL_CALL store( ) override;
385 virtual sal_Bool SAL_CALL isModified( ) override;
386 virtual void SAL_CALL setComment( const OUString& Comment ) override;
387 virtual void SAL_CALL storeToStorage
388 ( const css::uno::Reference< css::embed::XStorage >& Storage,
389 const OUString& NameBase, const OUString& Comment ) override;
390 virtual void SAL_CALL storeToURL( const OUString& URL, const OUString& NameBase,
391 const OUString& Comment, const css::uno::Reference
392 < css::task::XInteractionHandler >& Handler ) override;
393 virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL exportBinary( ) override;
394 virtual void SAL_CALL importBinary( const css::uno::Sequence< ::sal_Int8 >& Data ) override;
396 // XStringResourceWithStorage
397 virtual void SAL_CALL storeAsStorage
398 ( const css::uno::Reference< css::embed::XStorage >& Storage ) override;
399 virtual void SAL_CALL setStorage
400 ( const css::uno::Reference< css::embed::XStorage >& Storage ) override;
404 typedef ::cppu::ImplInheritanceHelper<
405 StringResourcePersistenceImpl,
406 css::lang::XInitialization,
407 css::resource::XStringResourceWithLocation > StringResourceWithLocationImpl_BASE;
409 class StringResourceWithLocationImpl : public StringResourceWithLocationImpl_BASE
411 OUString m_aLocation;
412 bool m_bLocationChanged;
413 css::uno::Reference< css::ucb::XSimpleFileAccess3 > m_xSFI;
414 css::uno::Reference< css::task::XInteractionHandler > m_xInteractionHandler;
416 const css::uno::Reference< css::ucb::XSimpleFileAccess3 > & getFileAccessImpl();
418 virtual void implScanLocales() override;
419 virtual bool implLoadLocale( LocaleItem* pLocaleItem ) override;
421 public:
422 explicit StringResourceWithLocationImpl( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
423 virtual ~StringResourceWithLocationImpl() override;
425 // XServiceInfo
426 virtual OUString SAL_CALL getImplementationName( ) override;
427 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
428 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
430 // XInitialization
431 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
433 // XModifyBroadcaster
434 virtual void SAL_CALL addModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
435 virtual void SAL_CALL removeModifyListener( const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
437 // XStringResourceResolver
438 virtual OUString SAL_CALL resolveString( const OUString& ResourceID ) override;
439 virtual OUString SAL_CALL resolveStringForLocale( const OUString& ResourceID,
440 const css::lang::Locale& locale ) override;
441 virtual sal_Bool SAL_CALL hasEntryForId( const OUString& ResourceID ) override;
442 virtual sal_Bool SAL_CALL hasEntryForIdAndLocale( const OUString& ResourceID,
443 const css::lang::Locale& locale ) override;
444 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDs( ) override;
445 virtual css::uno::Sequence< OUString > SAL_CALL getResourceIDsForLocale
446 ( const css::lang::Locale& locale ) override;
447 virtual css::lang::Locale SAL_CALL getCurrentLocale( ) override;
448 virtual css::lang::Locale SAL_CALL getDefaultLocale( ) override;
449 virtual css::uno::Sequence< css::lang::Locale > SAL_CALL getLocales( ) override;
451 // XStringResourceManager
452 virtual sal_Bool SAL_CALL isReadOnly() override;
453 virtual void SAL_CALL setCurrentLocale( const css::lang::Locale& locale, sal_Bool FindClosestMatch ) override;
454 virtual void SAL_CALL setDefaultLocale( const css::lang::Locale& locale ) override;
455 virtual void SAL_CALL setString( const OUString& ResourceID, const OUString& Str ) override;
456 virtual void SAL_CALL setStringForLocale( const OUString& ResourceID, const OUString& Str,
457 const css::lang::Locale& locale ) override;
458 virtual void SAL_CALL removeId( const OUString& ResourceID ) override;
459 virtual void SAL_CALL removeIdForLocale( const OUString& ResourceID,
460 const css::lang::Locale& locale ) override;
461 virtual void SAL_CALL newLocale( const css::lang::Locale& locale ) override;
462 virtual void SAL_CALL removeLocale( const css::lang::Locale& locale ) override;
463 virtual ::sal_Int32 SAL_CALL getUniqueNumericId( ) override;
465 // XStringResourcePersistence
466 virtual void SAL_CALL store( ) override;
467 virtual sal_Bool SAL_CALL isModified( ) override;
468 virtual void SAL_CALL setComment( const OUString& Comment ) override;
469 virtual void SAL_CALL storeToStorage
470 ( const css::uno::Reference< css::embed::XStorage >& Storage,
471 const OUString& NameBase, const OUString& Comment ) override;
472 virtual void SAL_CALL storeToURL( const OUString& URL, const OUString& NameBase,
473 const OUString& Comment, const css::uno::Reference
474 < css::task::XInteractionHandler >& Handler ) override;
475 virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL exportBinary( ) override;
476 virtual void SAL_CALL importBinary( const css::uno::Sequence< ::sal_Int8 >& Data ) override;
478 // XStringResourceWithLocation
479 virtual void SAL_CALL storeAsURL( const OUString& URL ) override;
480 virtual void SAL_CALL setURL( const OUString& URL ) override;
484 } // namespace stringtable
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */