Update git submodules
[LibreOffice.git] / scripting / source / stringresource / stringresource.cxx
blobd568af7a1b47d57ee5c5aecb31f3cb3ac2674c19
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 #include <memory>
21 #include "stringresource.hxx"
22 #include <com/sun/star/io/TempFile.hpp>
23 #include <com/sun/star/io/TextInputStream.hpp>
24 #include <com/sun/star/io/TextOutputStream.hpp>
25 #include <com/sun/star/io/XStream.hpp>
26 #include <com/sun/star/io/XSeekable.hpp>
27 #include <com/sun/star/embed/ElementModes.hpp>
28 #include <com/sun/star/lang/NoSupportException.hpp>
29 #include <com/sun/star/resource/MissingResourceException.hpp>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/container/ElementExistException.hpp>
33 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
35 #include <osl/diagnose.h>
36 #include <o3tl/string_view.hxx>
37 #include <rtl/ref.hxx>
38 #include <rtl/tencinfo.h>
39 #include <rtl/ustrbuf.hxx>
40 #include <tools/urlobj.hxx>
41 #include <unotools/tempfile.hxx>
42 #include <i18nlangtag/languagetag.hxx>
43 #include <sal/log.hxx>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::ucb;
49 using namespace ::com::sun::star::util;
50 using namespace ::com::sun::star::embed;
51 using namespace ::com::sun::star::container;
54 namespace stringresource
57 // StringResourceImpl
59 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
60 scripting_StringResourcePersistenceImpl_implementation(
61 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
63 return cppu::acquire(new StringResourcePersistenceImpl(context));
67 StringResourceImpl::StringResourceImpl( const Reference< XComponentContext >& rxContext )
68 : m_xContext( rxContext )
69 , m_pCurrentLocaleItem( nullptr )
70 , m_pDefaultLocaleItem( nullptr )
71 , m_bDefaultModified( false )
72 , m_bModified( false )
73 , m_bReadOnly( false )
74 , m_nNextUniqueNumericId( UNIQUE_NUMBER_NEEDS_INITIALISATION )
79 StringResourceImpl::~StringResourceImpl()
84 // XServiceInfo
86 OUString StringResourceImpl::getImplementationName( )
88 return "com.sun.star.comp.scripting.StringResource";
91 sal_Bool StringResourceImpl::supportsService( const OUString& rServiceName )
93 return cppu::supportsService(this, rServiceName);
96 Sequence< OUString > StringResourceImpl::getSupportedServiceNames( )
98 return { "com.sun.star.resource.StringResource" };
102 // XModifyBroadcaster
104 void StringResourceImpl::addModifyListener( const Reference< XModifyListener >& aListener )
106 if( !aListener.is() )
107 throw RuntimeException();
109 std::unique_lock aGuard( m_aMutex );
110 m_aListenerContainer.addInterface( aGuard, aListener );
113 void StringResourceImpl::removeModifyListener( const Reference< XModifyListener >& aListener )
115 if( !aListener.is() )
116 throw RuntimeException();
118 std::unique_lock aGuard( m_aMutex );
119 m_aListenerContainer.removeInterface( aGuard, aListener );
123 // XStringResourceResolver
125 OUString StringResourceImpl::implResolveString
126 ( const OUString& ResourceID, LocaleItem* pLocaleItem )
128 OUString aRetStr;
129 bool bSuccess = false;
130 if( pLocaleItem != nullptr && loadLocale( pLocaleItem ) )
132 IdToStringMap::iterator it = pLocaleItem->m_aIdToStringMap.find( ResourceID );
133 if( it != pLocaleItem->m_aIdToStringMap.end() )
135 aRetStr = (*it).second;
136 bSuccess = true;
139 if( !bSuccess )
141 throw css::resource::MissingResourceException( "StringResourceImpl: No entry for ResourceID: " + ResourceID );
143 return aRetStr;
146 OUString StringResourceImpl::resolveString( const OUString& ResourceID )
148 std::unique_lock aGuard( m_aMutex );
149 return implResolveString( ResourceID, m_pCurrentLocaleItem );
152 OUString StringResourceImpl::resolveStringForLocale( const OUString& ResourceID, const Locale& locale )
154 std::unique_lock aGuard( m_aMutex );
155 LocaleItem* pLocaleItem = getItemForLocale( locale, false );
156 return implResolveString( ResourceID, pLocaleItem );
159 bool StringResourceImpl::implHasEntryForId( const OUString& ResourceID, LocaleItem* pLocaleItem )
161 bool bSuccess = false;
162 if( pLocaleItem != nullptr && loadLocale( pLocaleItem ) )
164 IdToStringMap::iterator it = pLocaleItem->m_aIdToStringMap.find( ResourceID );
165 if( it != pLocaleItem->m_aIdToStringMap.end() )
166 bSuccess = true;
168 return bSuccess;
171 sal_Bool StringResourceImpl::hasEntryForId( const OUString& ResourceID )
173 std::unique_lock aGuard( m_aMutex );
174 return implHasEntryForId( ResourceID, m_pCurrentLocaleItem );
177 sal_Bool StringResourceImpl::hasEntryForIdAndLocale( const OUString& ResourceID,
178 const Locale& locale )
180 std::unique_lock aGuard( m_aMutex );
181 LocaleItem* pLocaleItem = getItemForLocale( locale, false );
182 return implHasEntryForId( ResourceID, pLocaleItem );
185 Sequence< OUString > StringResourceImpl::implGetResourceIDs( LocaleItem* pLocaleItem )
187 Sequence< OUString > aIDSeq( 0 );
188 if( pLocaleItem && loadLocale( pLocaleItem ) )
190 const IdToStringMap& rHashMap = pLocaleItem->m_aIdToStringMap;
191 sal_Int32 nResourceIDCount = rHashMap.size();
192 aIDSeq.realloc( nResourceIDCount );
193 OUString* pStrings = aIDSeq.getArray();
195 int iTarget = 0;
196 for( const auto& rEntry : rHashMap )
198 pStrings[iTarget] = rEntry.first;
199 iTarget++;
202 return aIDSeq;
205 Sequence< OUString > StringResourceImpl::getResourceIDsForLocale
206 ( const Locale& locale )
208 std::unique_lock aGuard( m_aMutex );
209 LocaleItem* pLocaleItem = getItemForLocale( locale, false );
210 return implGetResourceIDs( pLocaleItem );
213 Sequence< OUString > StringResourceImpl::getResourceIDs( )
215 std::unique_lock aGuard( m_aMutex );
216 return implGetResourceIDs( m_pCurrentLocaleItem );
219 Locale StringResourceImpl::getCurrentLocale()
221 std::unique_lock aGuard( m_aMutex );
223 Locale aRetLocale;
224 if( m_pCurrentLocaleItem != nullptr )
225 aRetLocale = m_pCurrentLocaleItem->m_locale;
226 return aRetLocale;
229 Locale StringResourceImpl::getDefaultLocale( )
231 std::unique_lock aGuard( m_aMutex );
233 Locale aRetLocale;
234 if( m_pDefaultLocaleItem != nullptr )
235 aRetLocale = m_pDefaultLocaleItem->m_locale;
236 return aRetLocale;
239 Sequence< Locale > StringResourceImpl::getLocales( )
241 std::unique_lock aGuard( m_aMutex );
243 sal_Int32 nSize = m_aLocaleItemVector.size();
244 Sequence< Locale > aLocalSeq( nSize );
245 Locale* pLocales = aLocalSeq.getArray();
246 int iTarget = 0;
247 for( const auto& pLocaleItem : m_aLocaleItemVector )
249 pLocales[iTarget] = pLocaleItem->m_locale;
250 iTarget++;
252 return aLocalSeq;
256 // XStringResourceManager
258 void StringResourceImpl::implCheckReadOnly( const char* pExceptionMsg )
260 if( m_bReadOnly )
262 OUString errorMsg = OUString::createFromAscii( pExceptionMsg );
263 throw NoSupportException( errorMsg );
267 sal_Bool StringResourceImpl::isReadOnly()
269 return m_bReadOnly;
272 void StringResourceImpl::implSetCurrentLocale( std::unique_lock<std::mutex>& rGuard, const Locale& locale,
273 bool FindClosestMatch, bool bUseDefaultIfNoMatch )
275 LocaleItem* pLocaleItem = nullptr;
276 if( FindClosestMatch )
277 pLocaleItem = getClosestMatchItemForLocale( locale );
278 else
279 pLocaleItem = getItemForLocale( locale, true );
281 if( pLocaleItem == nullptr && bUseDefaultIfNoMatch )
282 pLocaleItem = m_pDefaultLocaleItem;
284 if( pLocaleItem != nullptr )
286 (void)loadLocale( pLocaleItem );
287 m_pCurrentLocaleItem = pLocaleItem;
289 // Only notify without modifying
290 implNotifyListeners(rGuard);
294 void StringResourceImpl::setCurrentLocale( const Locale& locale, sal_Bool FindClosestMatch )
296 std::unique_lock aGuard( m_aMutex );
297 implSetCurrentLocale( aGuard, locale, FindClosestMatch, false/*bUseDefaultIfNoMatch*/ );
300 void StringResourceImpl::setDefaultLocale( const Locale& locale )
302 std::unique_lock aGuard( m_aMutex );
303 implCheckReadOnly( "StringResourceImpl::setDefaultLocale(): Read only" );
305 LocaleItem* pLocaleItem = getItemForLocale( locale, true );
306 if( pLocaleItem && pLocaleItem != m_pDefaultLocaleItem )
308 if( m_pDefaultLocaleItem )
310 m_aChangedDefaultLocaleVector.push_back(
311 std::make_unique<LocaleItem>( m_pDefaultLocaleItem->m_locale ) );
314 m_pDefaultLocaleItem = pLocaleItem;
315 m_bDefaultModified = true;
316 implModified(aGuard);
320 void StringResourceImpl::implSetString( std::unique_lock<std::mutex>& rGuard, const OUString& ResourceID,
321 const OUString& Str, LocaleItem* pLocaleItem )
323 if( !(pLocaleItem != nullptr && loadLocale( pLocaleItem )) )
324 return;
326 IdToStringMap& rHashMap = pLocaleItem->m_aIdToStringMap;
328 IdToStringMap::iterator it = rHashMap.find( ResourceID );
329 bool bNew = ( it == rHashMap.end() );
330 if( bNew )
332 IdToIndexMap& rIndexMap = pLocaleItem->m_aIdToIndexMap;
333 rIndexMap[ ResourceID ] = pLocaleItem->m_nNextIndex++;
334 implScanIdForNumber( ResourceID );
336 rHashMap[ ResourceID ] = Str;
337 pLocaleItem->m_bModified = true;
338 implModified(rGuard);
341 void StringResourceImpl::setString( const OUString& ResourceID, const OUString& Str )
343 std::unique_lock aGuard( m_aMutex );
344 implCheckReadOnly( "StringResourceImpl::setString(): Read only" );
345 implSetString( aGuard, ResourceID, Str, m_pCurrentLocaleItem );
348 void StringResourceImpl::setStringForLocale
349 ( const OUString& ResourceID, const OUString& Str, const Locale& locale )
351 std::unique_lock aGuard( m_aMutex );
352 implCheckReadOnly( "StringResourceImpl::setStringForLocale(): Read only" );
353 LocaleItem* pLocaleItem = getItemForLocale( locale, false );
354 implSetString( aGuard, ResourceID, Str, pLocaleItem );
357 void StringResourceImpl::implRemoveId( std::unique_lock<std::mutex>& rGuard, const OUString& ResourceID, LocaleItem* pLocaleItem )
359 if( pLocaleItem != nullptr && loadLocale( pLocaleItem ) )
361 IdToStringMap& rHashMap = pLocaleItem->m_aIdToStringMap;
362 IdToStringMap::iterator it = rHashMap.find( ResourceID );
363 if( it == rHashMap.end() )
365 throw css::resource::MissingResourceException( "StringResourceImpl: No entries for ResourceID: " + ResourceID );
367 rHashMap.erase( it );
368 pLocaleItem->m_bModified = true;
369 implModified(rGuard);
373 void StringResourceImpl::removeId( const OUString& ResourceID )
375 std::unique_lock aGuard( m_aMutex );
376 implCheckReadOnly( "StringResourceImpl::removeId(): Read only" );
377 implRemoveId( aGuard, ResourceID, m_pCurrentLocaleItem );
380 void StringResourceImpl::removeIdForLocale( const OUString& ResourceID, const Locale& locale )
382 std::unique_lock aGuard( m_aMutex );
383 implCheckReadOnly( "StringResourceImpl::removeIdForLocale(): Read only" );
384 LocaleItem* pLocaleItem = getItemForLocale( locale, false );
385 implRemoveId( aGuard, ResourceID, pLocaleItem );
388 void StringResourceImpl::newLocale( const Locale& locale )
390 std::unique_lock aGuard( m_aMutex );
391 implCheckReadOnly( "StringResourceImpl::newLocale(): Read only" );
393 if( getItemForLocale( locale, false ) != nullptr )
395 throw ElementExistException( "StringResourceImpl: locale already exists" );
398 // TODO?: Check if locale is valid? How?
399 //if (!bValid)
401 // OUString errorMsg("StringResourceImpl: Invalid locale");
402 // throw IllegalArgumentException( errorMsg, Reference< XInterface >(), 0 );
405 LocaleItem* pLocaleItem = new LocaleItem( locale );
406 m_aLocaleItemVector.emplace_back( pLocaleItem );
407 pLocaleItem->m_bModified = true;
409 // Copy strings from default locale
410 LocaleItem* pCopyFromItem = m_pDefaultLocaleItem;
411 if( pCopyFromItem == nullptr )
412 pCopyFromItem = m_pCurrentLocaleItem;
413 if( pCopyFromItem != nullptr && loadLocale( pCopyFromItem ) )
415 const IdToStringMap& rSourceMap = pCopyFromItem->m_aIdToStringMap;
416 IdToStringMap& rTargetMap = pLocaleItem->m_aIdToStringMap;
417 for( const auto& rEntry : rSourceMap )
419 OUString aId = rEntry.first;
420 rTargetMap[ aId ] = rEntry.second;
423 const IdToIndexMap& rSourceIndexMap = pCopyFromItem->m_aIdToIndexMap;
424 IdToIndexMap& rTargetIndexMap = pLocaleItem->m_aIdToIndexMap;
425 for( const auto& rIndex : rSourceIndexMap )
427 OUString aId = rIndex.first;
428 sal_Int32 nIndex = rIndex.second;
429 rTargetIndexMap[ aId ] = nIndex;
431 pLocaleItem->m_nNextIndex = pCopyFromItem->m_nNextIndex;
434 if( m_pCurrentLocaleItem == nullptr )
435 m_pCurrentLocaleItem = pLocaleItem;
437 if( m_pDefaultLocaleItem == nullptr )
439 m_pDefaultLocaleItem = pLocaleItem;
440 m_bDefaultModified = true;
443 implModified(aGuard);
446 void StringResourceImpl::removeLocale( const Locale& locale )
448 std::unique_lock aGuard( m_aMutex );
449 implCheckReadOnly( "StringResourceImpl::removeLocale(): Read only" );
451 LocaleItem* pRemoveItem = getItemForLocale( locale, true );
452 if( !pRemoveItem )
453 return;
455 // Last locale?
456 sal_Int32 nLocaleCount = m_aLocaleItemVector.size();
457 if( nLocaleCount > 1 )
459 if( m_pCurrentLocaleItem == pRemoveItem ||
460 m_pDefaultLocaleItem == pRemoveItem )
462 LocaleItem* pFallbackItem = nullptr;
463 for( const auto& pLocaleItem : m_aLocaleItemVector )
465 if( pLocaleItem.get() != pRemoveItem )
467 pFallbackItem = pLocaleItem.get();
468 break;
471 if( m_pCurrentLocaleItem == pRemoveItem )
473 setCurrentLocale( pFallbackItem->m_locale, false/*FindClosestMatch*/ );
475 if( m_pDefaultLocaleItem == pRemoveItem )
477 setDefaultLocale( pFallbackItem->m_locale );
481 auto it = std::find_if(m_aLocaleItemVector.begin(), m_aLocaleItemVector.end(),
482 [&pRemoveItem](const std::unique_ptr<LocaleItem>& rxItem) { return rxItem.get() == pRemoveItem; });
483 if (it == m_aLocaleItemVector.end())
484 return;
486 // Remember locale item to delete file while storing
487 m_aDeletedLocaleItemVector.push_back( std::move(*it) );
489 // Last locale?
490 if( nLocaleCount == 1 )
492 m_nNextUniqueNumericId = 0;
493 if( m_pDefaultLocaleItem )
495 m_aChangedDefaultLocaleVector.push_back(
496 std::make_unique<LocaleItem>( m_pDefaultLocaleItem->m_locale ) );
498 m_pCurrentLocaleItem = nullptr;
499 m_pDefaultLocaleItem = nullptr;
502 m_aLocaleItemVector.erase( it );
504 implModified(aGuard);
507 void StringResourceImpl::implScanIdForNumber( const OUString& ResourceID )
509 const sal_Unicode* pSrc = ResourceID.getStr();
510 sal_Int32 nLen = ResourceID.getLength();
512 sal_Int32 nNumber = 0;
513 for( sal_Int32 i = 0 ; i < nLen ; i++ )
515 sal_Unicode c = pSrc[i];
516 if( c >= '0' && c <= '9' )
518 sal_uInt16 nDigitVal = c - '0';
519 nNumber = 10*nNumber + nDigitVal;
521 else
522 break;
525 if( m_nNextUniqueNumericId < nNumber + 1 )
526 m_nNextUniqueNumericId = nNumber + 1;
529 sal_Int32 StringResourceImpl::getUniqueNumericId( )
531 if( m_nNextUniqueNumericId == UNIQUE_NUMBER_NEEDS_INITIALISATION )
533 implLoadAllLocales();
534 m_nNextUniqueNumericId = 0;
537 if( m_nNextUniqueNumericId < UNIQUE_NUMBER_NEEDS_INITIALISATION )
539 throw NoSupportException( "getUniqueNumericId: Extended sal_Int32 range" );
541 return m_nNextUniqueNumericId;
545 // Private helper methods
547 LocaleItem* StringResourceImpl::getItemForLocale
548 ( const Locale& locale, bool bException )
550 LocaleItem* pRetItem = nullptr;
552 // Search for locale
553 for( auto& pLocaleItem : m_aLocaleItemVector )
555 if( pLocaleItem )
557 Locale& cmp_locale = pLocaleItem->m_locale;
558 if( cmp_locale.Language == locale.Language &&
559 cmp_locale.Country == locale.Country &&
560 cmp_locale.Variant == locale.Variant )
562 pRetItem = pLocaleItem.get();
563 break;
568 if( pRetItem == nullptr && bException )
570 throw IllegalArgumentException( "StringResourceImpl: Invalid locale", Reference< XInterface >(), 0 );
572 return pRetItem;
575 // Returns the LocaleItem for a given locale, if it exists, otherwise NULL.
576 // This method performs a closest match search, at least the language must match.
577 LocaleItem* StringResourceImpl::getClosestMatchItemForLocale( const Locale& locale )
579 LocaleItem* pRetItem = nullptr;
581 ::std::vector< Locale > aLocales( m_aLocaleItemVector.size());
582 size_t i = 0;
583 for( const auto& pLocaleItem : m_aLocaleItemVector )
585 aLocales[i] = (pLocaleItem ? pLocaleItem->m_locale : Locale());
586 ++i;
588 ::std::vector< Locale >::const_iterator iFound( LanguageTag::getMatchingFallback( aLocales, locale));
589 if (iFound != aLocales.end())
590 pRetItem = (m_aLocaleItemVector.begin() + (iFound - aLocales.begin()))->get();
592 return pRetItem;
595 void StringResourceImpl::implModified(std::unique_lock<std::mutex>& rGuard)
597 m_bModified = true;
598 implNotifyListeners(rGuard);
601 void StringResourceImpl::implNotifyListeners(std::unique_lock<std::mutex>& rGuard)
603 EventObject aEvent;
604 aEvent.Source = getXWeak();
605 m_aListenerContainer.forEach(rGuard,
606 [&aEvent](const css::uno::Reference<XModifyListener>& xListener)
608 xListener->modified(aEvent);
614 // Loading
616 bool StringResourceImpl::loadLocale( LocaleItem* )
618 // Base implementation has nothing to load
619 return true;
622 void StringResourceImpl::implLoadAllLocales()
624 // Base implementation has nothing to load
628 // StringResourcePersistenceImpl
631 StringResourcePersistenceImpl::StringResourcePersistenceImpl( const Reference< XComponentContext >& rxContext )
632 : StringResourcePersistenceImpl_BASE( rxContext )
637 StringResourcePersistenceImpl::~StringResourcePersistenceImpl()
642 // XServiceInfo
645 OUString StringResourcePersistenceImpl::getImplementationName( )
647 return "com.sun.star.comp.scripting.StringResource";
651 sal_Bool StringResourcePersistenceImpl::supportsService( const OUString& rServiceName )
653 return cppu::supportsService( this, rServiceName );
657 Sequence< OUString > StringResourcePersistenceImpl::getSupportedServiceNames( )
659 return StringResourceImpl::getSupportedServiceNames();
663 // XInitialization base functionality for derived classes
666 constexpr OUString aNameBaseDefaultStr = u"strings"_ustr;
668 void StringResourcePersistenceImpl::implInitializeCommonParameters
669 ( std::unique_lock<std::mutex>& rGuard, const Sequence< Any >& aArguments )
671 bool bReadOnlyOk = (aArguments[1] >>= m_bReadOnly);
672 if( !bReadOnlyOk )
674 throw IllegalArgumentException( "XInitialization::initialize: Expected ReadOnly flag", Reference< XInterface >(), 1 );
677 css::lang::Locale aCurrentLocale;
678 bool bLocaleOk = (aArguments[2] >>= aCurrentLocale);
679 if( !bLocaleOk )
681 throw IllegalArgumentException( "XInitialization::initialize: Expected Locale", Reference< XInterface >(), 2 );
684 bool bNameBaseOk = (aArguments[3] >>= m_aNameBase);
685 if( !bNameBaseOk )
687 throw IllegalArgumentException( "XInitialization::initialize: Expected NameBase string", Reference< XInterface >(), 3 );
689 if( m_aNameBase.isEmpty() )
690 m_aNameBase = aNameBaseDefaultStr;
692 bool bCommentOk = (aArguments[4] >>= m_aComment);
693 if( !bCommentOk )
695 throw IllegalArgumentException( "XInitialization::initialize: Expected Comment string", Reference< XInterface >(), 4 );
698 implScanLocales();
700 implSetCurrentLocale( rGuard, aCurrentLocale, true/*FindClosestMatch*/, true/*bUseDefaultIfNoMatch*/ );
704 // Forwarding calls to base class
706 // XModifyBroadcaster
707 void StringResourcePersistenceImpl::addModifyListener( const Reference< XModifyListener >& aListener )
709 StringResourceImpl::addModifyListener( aListener );
711 void StringResourcePersistenceImpl::removeModifyListener( const Reference< XModifyListener >& aListener )
713 StringResourceImpl::removeModifyListener( aListener );
716 // XStringResourceResolver
717 OUString StringResourcePersistenceImpl::resolveString( const OUString& ResourceID )
719 return StringResourceImpl::resolveString( ResourceID ) ;
721 OUString StringResourcePersistenceImpl::resolveStringForLocale( const OUString& ResourceID, const Locale& locale )
723 return StringResourceImpl::resolveStringForLocale( ResourceID, locale );
725 sal_Bool StringResourcePersistenceImpl::hasEntryForId( const OUString& ResourceID )
727 return StringResourceImpl::hasEntryForId( ResourceID ) ;
729 sal_Bool StringResourcePersistenceImpl::hasEntryForIdAndLocale( const OUString& ResourceID,
730 const Locale& locale )
732 return StringResourceImpl::hasEntryForIdAndLocale( ResourceID, locale );
734 Locale StringResourcePersistenceImpl::getCurrentLocale()
736 return StringResourceImpl::getCurrentLocale();
738 Locale StringResourcePersistenceImpl::getDefaultLocale( )
740 return StringResourceImpl::getDefaultLocale();
742 Sequence< Locale > StringResourcePersistenceImpl::getLocales( )
744 return StringResourceImpl::getLocales();
747 // XStringResourceManager
748 sal_Bool StringResourcePersistenceImpl::isReadOnly()
750 return StringResourceImpl::isReadOnly();
752 void StringResourcePersistenceImpl::setCurrentLocale( const Locale& locale, sal_Bool FindClosestMatch )
754 StringResourceImpl::setCurrentLocale( locale, FindClosestMatch );
756 void StringResourcePersistenceImpl::setDefaultLocale( const Locale& locale )
758 StringResourceImpl::setDefaultLocale( locale );
760 Sequence< OUString > StringResourcePersistenceImpl::getResourceIDs( )
762 return StringResourceImpl::getResourceIDs();
764 void StringResourcePersistenceImpl::setString( const OUString& ResourceID, const OUString& Str )
766 StringResourceImpl::setString( ResourceID, Str );
768 void StringResourcePersistenceImpl::setStringForLocale
769 ( const OUString& ResourceID, const OUString& Str, const Locale& locale )
771 StringResourceImpl::setStringForLocale( ResourceID, Str, locale );
773 Sequence< OUString > StringResourcePersistenceImpl::getResourceIDsForLocale
774 ( const Locale& locale )
776 return StringResourceImpl::getResourceIDsForLocale( locale );
778 void StringResourcePersistenceImpl::removeId( const OUString& ResourceID )
780 StringResourceImpl::removeId( ResourceID );
782 void StringResourcePersistenceImpl::removeIdForLocale( const OUString& ResourceID, const Locale& locale )
784 StringResourceImpl::removeIdForLocale( ResourceID, locale );
786 void StringResourcePersistenceImpl::newLocale( const Locale& locale )
788 StringResourceImpl::newLocale( locale );
790 void StringResourcePersistenceImpl::removeLocale( const Locale& locale )
792 StringResourceImpl::removeLocale( locale );
794 sal_Int32 StringResourcePersistenceImpl::getUniqueNumericId( )
796 return StringResourceImpl::getUniqueNumericId();
800 // XStringResourcePersistence
802 void StringResourcePersistenceImpl::store()
806 sal_Bool StringResourcePersistenceImpl::isModified( )
808 std::unique_lock aGuard( m_aMutex );
810 return m_bModified;
813 void StringResourcePersistenceImpl::setComment( const OUString& Comment )
815 m_aComment = Comment;
818 void StringResourcePersistenceImpl::storeToStorage( const Reference< XStorage >& Storage,
819 const OUString& NameBase, const OUString& Comment )
821 std::unique_lock aGuard( m_aMutex );
823 implStoreAtStorage( NameBase, Comment, Storage, false/*bUsedForStore*/, true/*bStoreAll*/ );
826 void StringResourcePersistenceImpl::implStoreAtStorage
828 const OUString& aNameBase,
829 const OUString& aComment,
830 const Reference< css::embed::XStorage >& Storage,
831 bool bUsedForStore,
832 bool bStoreAll
835 // Delete files for deleted locales
836 if( bUsedForStore )
838 for( auto& pLocaleItem : m_aDeletedLocaleItemVector )
840 if( pLocaleItem )
842 OUString aStreamName = implGetFileNameForLocaleItem( pLocaleItem.get(), m_aNameBase ) + ".properties";
846 Storage->removeElement( aStreamName );
848 catch( Exception& )
851 pLocaleItem.reset();
854 m_aDeletedLocaleItemVector.clear();
857 for( auto& pLocaleItem : m_aLocaleItemVector )
859 if( pLocaleItem != nullptr && (bStoreAll || pLocaleItem->m_bModified) &&
860 loadLocale( pLocaleItem.get() ) )
862 OUString aStreamName = implGetFileNameForLocaleItem( pLocaleItem.get(), aNameBase ) + ".properties";
864 Reference< io::XStream > xElementStream =
865 Storage->openStreamElement( aStreamName, ElementModes::READWRITE );
867 uno::Reference< beans::XPropertySet > xProps( xElementStream, uno::UNO_QUERY );
868 OSL_ENSURE( xProps.is(), "The StorageStream must implement XPropertySet interface!" );
869 if ( xProps.is() )
871 OUString aPropName("MediaType");
872 xProps->setPropertyValue( aPropName, uno::Any( OUString("text/plain") ) );
874 aPropName = "UseCommonStoragePasswordEncryption";
875 xProps->setPropertyValue( aPropName, uno::Any( true ) );
878 Reference< io::XOutputStream > xOutputStream = xElementStream->getOutputStream();
879 if( xOutputStream.is() )
880 implWritePropertiesFile( pLocaleItem.get(), xOutputStream, aComment );
881 xOutputStream->closeOutput();
883 if( bUsedForStore )
884 pLocaleItem->m_bModified = false;
888 // Delete files for changed defaults
889 if( bUsedForStore )
891 for( auto& pLocaleItem : m_aChangedDefaultLocaleVector )
893 OUString aStreamName = implGetFileNameForLocaleItem( pLocaleItem.get(), m_aNameBase ) + ".default";
897 Storage->removeElement( aStreamName );
899 catch( Exception& )
902 pLocaleItem.reset();
904 m_aChangedDefaultLocaleVector.clear();
907 // Default locale
908 if( !(m_pDefaultLocaleItem != nullptr && (bStoreAll || m_bDefaultModified)) )
909 return;
911 OUString aStreamName = implGetFileNameForLocaleItem( m_pDefaultLocaleItem, aNameBase ) + ".default";
913 Reference< io::XStream > xElementStream =
914 Storage->openStreamElement( aStreamName, ElementModes::READWRITE );
916 // Only create stream without content
917 Reference< io::XOutputStream > xOutputStream = xElementStream->getOutputStream();
918 xOutputStream->closeOutput();
920 if( bUsedForStore )
921 m_bDefaultModified = false;
924 void StringResourcePersistenceImpl::storeToURL( const OUString& URL,
925 const OUString& NameBase, const OUString& Comment,
926 const Reference< css::task::XInteractionHandler >& Handler )
928 std::unique_lock aGuard( m_aMutex );
930 Reference< ucb::XSimpleFileAccess3 > xFileAccess = ucb::SimpleFileAccess::create(m_xContext);
931 if( xFileAccess.is() && Handler.is() )
932 xFileAccess->setInteractionHandler( Handler );
934 implStoreAtLocation( URL, NameBase, Comment, xFileAccess, false/*bUsedForStore*/, true/*bStoreAll*/ );
937 void StringResourcePersistenceImpl::implKillRemovedLocaleFiles
939 std::u16string_view Location,
940 const OUString& aNameBase,
941 const css::uno::Reference< css::ucb::XSimpleFileAccess3 >& xFileAccess
944 // Delete files for deleted locales
945 for( auto& pLocaleItem : m_aDeletedLocaleItemVector )
947 if( pLocaleItem )
949 OUString aCompleteFileName =
950 implGetPathForLocaleItem( pLocaleItem.get(), aNameBase, Location );
951 if( xFileAccess->exists( aCompleteFileName ) )
952 xFileAccess->kill( aCompleteFileName );
954 pLocaleItem.reset();
957 m_aDeletedLocaleItemVector.clear();
960 void StringResourcePersistenceImpl::implKillChangedDefaultFiles
962 std::u16string_view Location,
963 const OUString& aNameBase,
964 const css::uno::Reference< css::ucb::XSimpleFileAccess3 >& xFileAccess
967 // Delete files for changed defaults
968 for( auto& pLocaleItem : m_aChangedDefaultLocaleVector )
970 OUString aCompleteFileName =
971 implGetPathForLocaleItem( pLocaleItem.get(), aNameBase, Location, true );
972 if( xFileAccess->exists( aCompleteFileName ) )
973 xFileAccess->kill( aCompleteFileName );
974 pLocaleItem.reset();
976 m_aChangedDefaultLocaleVector.clear();
979 void StringResourcePersistenceImpl::implStoreAtLocation
981 std::u16string_view Location,
982 const OUString& aNameBase,
983 const OUString& aComment,
984 const Reference< ucb::XSimpleFileAccess3 >& xFileAccess,
985 bool bUsedForStore,
986 bool bStoreAll,
987 bool bKillAll
990 // Delete files for deleted locales
991 if( bUsedForStore || bKillAll )
992 implKillRemovedLocaleFiles( Location, aNameBase, xFileAccess );
994 for( auto& pLocaleItem : m_aLocaleItemVector )
996 if( pLocaleItem != nullptr && (bStoreAll || bKillAll || pLocaleItem->m_bModified) &&
997 loadLocale( pLocaleItem.get() ) )
999 OUString aCompleteFileName =
1000 implGetPathForLocaleItem( pLocaleItem.get(), aNameBase, Location );
1001 if( xFileAccess->exists( aCompleteFileName ) )
1002 xFileAccess->kill( aCompleteFileName );
1004 if( !bKillAll )
1006 // Create Output stream
1007 Reference< io::XOutputStream > xOutputStream = xFileAccess->openFileWrite( aCompleteFileName );
1008 if( xOutputStream.is() )
1010 implWritePropertiesFile( pLocaleItem.get(), xOutputStream, aComment );
1011 xOutputStream->closeOutput();
1013 if( bUsedForStore )
1014 pLocaleItem->m_bModified = false;
1019 // Delete files for changed defaults
1020 if( bUsedForStore || bKillAll )
1021 implKillChangedDefaultFiles( Location, aNameBase, xFileAccess );
1023 // Default locale
1024 if( !(m_pDefaultLocaleItem != nullptr && (bStoreAll || bKillAll || m_bDefaultModified)) )
1025 return;
1027 OUString aCompleteFileName =
1028 implGetPathForLocaleItem( m_pDefaultLocaleItem, aNameBase, Location, true );
1029 if( xFileAccess->exists( aCompleteFileName ) )
1030 xFileAccess->kill( aCompleteFileName );
1032 if( !bKillAll )
1034 // Create Output stream
1035 Reference< io::XOutputStream > xOutputStream = xFileAccess->openFileWrite( aCompleteFileName );
1036 if( xOutputStream.is() )
1037 xOutputStream->closeOutput();
1039 if( bUsedForStore )
1040 m_bDefaultModified = false;
1045 // BinaryOutput, helper class for exportBinary
1047 class BinaryOutput
1049 rtl::Reference< utl::TempFileFastService > m_xTempFile;
1050 Reference< io::XOutputStream > m_xOutputStream;
1052 public:
1053 explicit BinaryOutput();
1055 const Reference< io::XOutputStream >& getOutputStream() const
1056 { return m_xOutputStream; }
1058 Sequence< ::sal_Int8 > closeAndGetData();
1060 // Template to be used with sal_Int16 and sal_Unicode
1061 template< class T >
1062 void write16BitInt( T n );
1063 void writeInt16( sal_Int16 n )
1064 { write16BitInt( n ); }
1065 void writeUnicodeChar( sal_Unicode n )
1066 { write16BitInt( n ); }
1067 void writeInt32( sal_Int32 n );
1068 void writeString( const OUString& aStr );
1071 BinaryOutput::BinaryOutput()
1073 m_xTempFile = new utl::TempFileFastService;
1074 m_xOutputStream = m_xTempFile;
1077 template< class T >
1078 void BinaryOutput::write16BitInt( T n )
1080 if( !m_xOutputStream.is() )
1081 return;
1083 Sequence< sal_Int8 > aSeq( 2 );
1084 sal_Int8* p = aSeq.getArray();
1086 sal_Int8 nLow = sal_Int8( n & 0xff );
1087 sal_Int8 nHigh = sal_Int8( n >> 8 );
1089 p[0] = nLow;
1090 p[1] = nHigh;
1091 m_xOutputStream->writeBytes( aSeq );
1094 void BinaryOutput::writeInt32( sal_Int32 n )
1096 if( !m_xOutputStream.is() )
1097 return;
1099 Sequence< sal_Int8 > aSeq( 4 );
1100 sal_Int8* p = aSeq.getArray();
1102 for( sal_Int16 i = 0 ; i < 4 ; i++ )
1104 p[i] = sal_Int8( n & 0xff );
1105 n >>= 8;
1107 m_xOutputStream->writeBytes( aSeq );
1110 void BinaryOutput::writeString( const OUString& aStr )
1112 sal_Int32 nLen = aStr.getLength();
1113 const sal_Unicode* pStr = aStr.getStr();
1115 for( sal_Int32 i = 0 ; i < nLen ; i++ )
1116 writeUnicodeChar( pStr[i] );
1118 writeUnicodeChar( 0 );
1121 Sequence< ::sal_Int8 > BinaryOutput::closeAndGetData()
1123 Sequence< ::sal_Int8 > aRetSeq;
1124 if( !m_xOutputStream.is() )
1125 return aRetSeq;
1127 m_xOutputStream->closeOutput();
1129 sal_Int32 nSize = static_cast<sal_Int32>(m_xTempFile->getPosition());
1131 m_xTempFile->seek( 0 );
1132 sal_Int32 nRead = m_xTempFile->readBytes( aRetSeq, nSize );
1133 OSL_ENSURE( nRead == nSize, "BinaryOutput::closeAndGetData: nRead != nSize" );
1135 return aRetSeq;
1139 // Binary format:
1141 // Header
1142 // Byte Content
1143 // 0 + 1 sal_Int16: Version, currently 0, low byte first
1144 // 2 + 3 sal_Int16: Locale count = n, low byte first
1145 // 4 + 5 sal_Int16: Default Locale position in Locale list, == n if none
1146 // 6 - 7 sal_Int32: Start index locale block 0, lowest byte first
1147 // (n-1) * sal_Int32: Start index locale block 1 to n, lowest byte first
1148 // 6 + 4*n sal_Int32: "Start index" non existing locale block n+1,
1149 // marks the first invalid index, kind of EOF
1151 // Locale block
1152 // All strings are stored as 2-Byte-0 terminated sequence
1153 // of 16 bit Unicode characters, each with low byte first
1154 // Empty strings only contain the 2-Byte-0
1156 // Members of com.sun.star.lang.Locale
1157 // with l1 = Locale.Language.getLength()
1158 // with l2 = Locale.Country.getLength()
1159 // with l3 = Locale.Variant.getLength()
1160 // pos0 = 0 Locale.Language
1161 // pos1 = 2 * (l1 + 1) Locale.Country
1162 // pos2 = pos1 + 2 * (l2 + 1) Locale.Variant
1163 // pos3 = pos2 + 2 * (l3 + 1)
1164 // pos3 Properties file written by implWritePropertiesFile
1166 Sequence< sal_Int8 > StringResourcePersistenceImpl::exportBinary( )
1168 BinaryOutput aOut;
1170 sal_Int32 nLocaleCount = m_aLocaleItemVector.size();
1171 std::vector<Sequence< sal_Int8 >> aLocaleDataSeq(nLocaleCount);
1173 sal_Int32 iLocale = 0;
1174 sal_Int32 iDefault = 0;
1175 for( auto& pLocaleItem : m_aLocaleItemVector )
1177 if( pLocaleItem != nullptr && loadLocale( pLocaleItem.get() ) )
1179 if( m_pDefaultLocaleItem == pLocaleItem.get() )
1180 iDefault = iLocale;
1182 BinaryOutput aLocaleOut;
1183 implWriteLocaleBinary( pLocaleItem.get(), aLocaleOut );
1185 aLocaleDataSeq[iLocale] = aLocaleOut.closeAndGetData();
1187 ++iLocale;
1190 // Write header
1191 sal_Int16 nLocaleCount16 = static_cast<sal_Int16>(nLocaleCount);
1192 sal_Int16 iDefault16 = static_cast<sal_Int16>(iDefault);
1193 aOut.writeInt16( 0 ); // nVersion
1194 aOut.writeInt16( nLocaleCount16 );
1195 aOut.writeInt16( iDefault16 );
1197 // Write data positions
1198 sal_Int32 nDataPos = 6 + 4 * (nLocaleCount + 1);
1199 for( iLocale = 0; iLocale < nLocaleCount; iLocale++ )
1201 aOut.writeInt32( nDataPos );
1203 Sequence< sal_Int8 >& rSeq = aLocaleDataSeq[iLocale];
1204 sal_Int32 nSeqLen = rSeq.getLength();
1205 nDataPos += nSeqLen;
1207 // Write final position
1208 aOut.writeInt32( nDataPos );
1210 // Write data
1211 Reference< io::XOutputStream > xOutputStream = aOut.getOutputStream();
1212 if( xOutputStream.is() )
1214 for( iLocale = 0; iLocale < nLocaleCount; iLocale++ )
1216 Sequence< sal_Int8 >& rSeq = aLocaleDataSeq[iLocale];
1217 xOutputStream->writeBytes( rSeq );
1221 Sequence< sal_Int8 > aRetSeq = aOut.closeAndGetData();
1222 return aRetSeq;
1225 void StringResourcePersistenceImpl::implWriteLocaleBinary
1226 ( LocaleItem* pLocaleItem, BinaryOutput& rOut )
1228 Reference< io::XOutputStream > xOutputStream = rOut.getOutputStream();
1229 if( !xOutputStream.is() )
1230 return;
1232 Locale& rLocale = pLocaleItem->m_locale;
1233 rOut.writeString( rLocale.Language );
1234 rOut.writeString( rLocale.Country );
1235 rOut.writeString( rLocale.Variant );
1236 implWritePropertiesFile( pLocaleItem, xOutputStream, m_aComment );
1240 // BinaryOutput, helper class for exportBinary
1242 namespace {
1244 class BinaryInput
1246 Sequence< sal_Int8 > m_aData;
1248 const sal_Int8* m_pData;
1249 sal_Int32 m_nCurPos;
1250 sal_Int32 m_nSize;
1252 public:
1253 BinaryInput( const Sequence< ::sal_Int8 >& aData );
1255 Reference< io::XInputStream > getInputStreamForSection( sal_Int32 nSize );
1257 void seek( sal_Int32 nPos );
1258 sal_Int32 getPosition() const
1259 { return m_nCurPos; }
1261 sal_Int16 readInt16();
1262 sal_Int32 readInt32();
1263 sal_Unicode readUnicodeChar();
1264 OUString readString();
1269 BinaryInput::BinaryInput( const Sequence< ::sal_Int8 >& aData )
1270 : m_aData( aData )
1272 m_pData = m_aData.getConstArray();
1273 m_nCurPos = 0;
1274 m_nSize = m_aData.getLength();
1277 Reference< io::XInputStream > BinaryInput::getInputStreamForSection( sal_Int32 nSize )
1279 Reference< io::XInputStream > xIn;
1280 if( m_nCurPos + nSize <= m_nSize )
1282 rtl::Reference< utl::TempFileFastService > xTempOut = new utl::TempFileFastService;
1283 Sequence< sal_Int8 > aSection( m_pData + m_nCurPos, nSize );
1284 xTempOut->writeBytes( aSection );
1285 xTempOut->seek( 0 );
1286 xIn = xTempOut;
1288 else
1289 OSL_FAIL( "BinaryInput::getInputStreamForSection(): Read past end" );
1291 return xIn;
1294 void BinaryInput::seek( sal_Int32 nPos )
1296 if( nPos <= m_nSize )
1297 m_nCurPos = nPos;
1298 else
1299 OSL_FAIL( "BinaryInput::seek(): Position past end" );
1303 sal_Int16 BinaryInput::readInt16()
1305 sal_Int16 nRet = 0;
1306 if( m_nCurPos + 2 <= m_nSize )
1308 nRet = nRet + sal_Int16( sal_uInt8( m_pData[m_nCurPos++] ) );
1309 nRet += 256 * sal_Int16( sal_uInt8( m_pData[m_nCurPos++] ) );
1311 else
1312 OSL_FAIL( "BinaryInput::readInt16(): Read past end" );
1314 return nRet;
1317 sal_Int32 BinaryInput::readInt32()
1319 sal_Int32 nRet = 0;
1320 if( m_nCurPos + 4 <= m_nSize )
1322 sal_Int32 nFactor = 1;
1323 for( sal_Int16 i = 0; i < 4; i++ )
1325 nRet += sal_uInt8( m_pData[m_nCurPos++] ) * nFactor;
1326 nFactor *= 256;
1329 else
1330 OSL_FAIL( "BinaryInput::readInt32(): Read past end" );
1332 return nRet;
1335 sal_Unicode BinaryInput::readUnicodeChar()
1337 sal_uInt16 nRet = 0;
1338 if( m_nCurPos + 2 <= m_nSize )
1340 nRet = nRet + sal_uInt8( m_pData[m_nCurPos++] );
1341 nRet += 256 * sal_uInt8( m_pData[m_nCurPos++] );
1343 else
1344 OSL_FAIL( "BinaryInput::readUnicodeChar(): Read past end" );
1346 sal_Unicode cRet = nRet;
1347 return cRet;
1350 OUString BinaryInput::readString()
1352 OUStringBuffer aBuf;
1353 sal_Unicode c;
1356 c = readUnicodeChar();
1357 if( c != 0 )
1358 aBuf.append( c );
1360 while( c != 0 );
1362 OUString aRetStr = aBuf.makeStringAndClear();
1363 return aRetStr;
1366 void StringResourcePersistenceImpl::importBinary( const Sequence< ::sal_Int8 >& Data )
1368 // Init: Remove all locales
1369 sal_Int32 nOldLocaleCount = 0;
1372 Sequence< Locale > aLocaleSeq = getLocales();
1373 nOldLocaleCount = aLocaleSeq.getLength();
1374 if( nOldLocaleCount > 0 )
1376 Locale aLocale = aLocaleSeq[0];
1377 removeLocale( aLocale );
1380 while( nOldLocaleCount > 0 );
1382 // Import data
1383 BinaryInput aIn( Data );
1385 aIn.readInt16(); // version
1386 sal_Int32 nLocaleCount = aIn.readInt16();
1387 sal_Int32 iDefault = aIn.readInt16();
1389 std::unique_ptr<sal_Int32[]> pPositions( new sal_Int32[nLocaleCount + 1] );
1390 for( sal_Int32 i = 0; i < nLocaleCount + 1; i++ )
1391 pPositions[i] = aIn.readInt32();
1393 // Import locales
1394 LocaleItem* pUseAsDefaultItem = nullptr;
1395 for( sal_Int32 i = 0; i < nLocaleCount; i++ )
1397 sal_Int32 nPos = pPositions[i];
1398 aIn.seek( nPos );
1400 Locale aLocale;
1401 aLocale.Language = aIn.readString();
1402 aLocale.Country = aIn.readString();
1403 aLocale.Variant = aIn.readString();
1405 sal_Int32 nAfterStringPos = aIn.getPosition();
1406 sal_Int32 nSize = pPositions[i+1] - nAfterStringPos;
1407 Reference< io::XInputStream > xInput = aIn.getInputStreamForSection( nSize );
1408 if( xInput.is() )
1410 LocaleItem* pLocaleItem = new LocaleItem( std::move(aLocale) );
1411 if( iDefault == i )
1412 pUseAsDefaultItem = pLocaleItem;
1413 m_aLocaleItemVector.emplace_back( pLocaleItem );
1414 implReadPropertiesFile( pLocaleItem, xInput );
1418 if( pUseAsDefaultItem != nullptr )
1419 setDefaultLocale( pUseAsDefaultItem->m_locale );
1423 // Private helper methods
1425 static bool checkNamingSceme( std::u16string_view aName, std::u16string_view aNameBase,
1426 Locale& aLocale )
1428 bool bSuccess = false;
1430 size_t nNameLen = aName.size();
1431 size_t nNameBaseLen = aNameBase.size();
1433 // Name has to start with NameBase followed
1434 // by a '_' and at least one more character
1435 if( o3tl::starts_with(aName, aNameBase) && nNameBaseLen < nNameLen-1 &&
1436 aName[nNameBaseLen] == '_' )
1438 bSuccess = true;
1440 /* FIXME-BCP47: this uses '_' underscore character as separator and
1441 * also appends Variant, which can't be blindly changed as it would
1442 * violate the naming scheme in use. */
1444 sal_Int32 iStart = nNameBaseLen + 1;
1445 size_t iNext_ = aName.find( '_', iStart );
1446 if( iNext_ != std::u16string_view::npos && iNext_ < nNameLen-1 )
1448 aLocale.Language = aName.substr( iStart, iNext_ - iStart );
1450 iStart = iNext_ + 1;
1451 iNext_ = aName.find( '_', iStart );
1452 if( iNext_ != std::u16string_view::npos && iNext_ < nNameLen-1 )
1454 aLocale.Country = aName.substr( iStart, iNext_ - iStart );
1455 aLocale.Variant = aName.substr( iNext_ + 1 );
1457 else
1458 aLocale.Country = aName.substr( iStart );
1460 else
1461 aLocale.Language = aName.substr( iStart );
1463 return bSuccess;
1466 void StringResourcePersistenceImpl::implLoadAllLocales()
1468 for( auto& pLocaleItem : m_aLocaleItemVector )
1469 if( pLocaleItem )
1470 loadLocale( pLocaleItem.get() );
1473 // Scan locale properties files helper
1474 void StringResourcePersistenceImpl::implScanLocaleNames( const Sequence< OUString >& aContentSeq )
1476 Locale aDefaultLocale;
1477 bool bDefaultFound = false;
1479 for( const OUString& aCompleteName : aContentSeq )
1481 OUString aPureName;
1482 OUString aExtension;
1483 sal_Int32 iDot = aCompleteName.lastIndexOf( '.' );
1484 sal_Int32 iSlash = aCompleteName.lastIndexOf( '/' );
1485 if( iDot != -1 && iDot > iSlash)
1487 sal_Int32 iCopyFrom = (iSlash != -1) ? iSlash + 1 : 0;
1488 aPureName = aCompleteName.copy( iCopyFrom, iDot-iCopyFrom );
1489 aExtension = aCompleteName.copy( iDot + 1 );
1492 if ( aExtension == "properties" )
1494 //OUString aName = aInetObj.getBase();
1495 Locale aLocale;
1497 if( checkNamingSceme( aPureName, m_aNameBase, aLocale ) )
1499 LocaleItem* pLocaleItem = new LocaleItem( std::move(aLocale), false );
1500 m_aLocaleItemVector.emplace_back( pLocaleItem );
1502 if( m_pCurrentLocaleItem == nullptr )
1503 m_pCurrentLocaleItem = pLocaleItem;
1505 if( m_pDefaultLocaleItem == nullptr )
1507 m_pDefaultLocaleItem = pLocaleItem;
1508 m_bDefaultModified = true;
1512 else if( !bDefaultFound && aExtension == "default" )
1514 if( checkNamingSceme( aPureName, m_aNameBase, aDefaultLocale ) )
1515 bDefaultFound = true;
1518 if( bDefaultFound )
1520 LocaleItem* pLocaleItem = getItemForLocale( aDefaultLocale, false );
1521 if( pLocaleItem )
1523 m_pDefaultLocaleItem = pLocaleItem;
1524 m_bDefaultModified = false;
1529 // Scan locale properties files
1530 void StringResourcePersistenceImpl::implScanLocales()
1532 // Dummy implementation, method not called for this
1533 // base class, but pure virtual not possible-
1536 bool StringResourcePersistenceImpl::loadLocale( LocaleItem* pLocaleItem )
1538 bool bSuccess = false;
1540 OSL_ENSURE( pLocaleItem, "StringResourcePersistenceImpl::loadLocale(): pLocaleItem == NULL" );
1541 if( pLocaleItem )
1543 if( pLocaleItem->m_bLoaded )
1545 bSuccess = true;
1547 else
1549 bSuccess = implLoadLocale( pLocaleItem );
1550 pLocaleItem->m_bLoaded = true; // = bSuccess??? -> leads to more tries
1553 return bSuccess;
1556 bool StringResourcePersistenceImpl::implLoadLocale( LocaleItem* )
1558 // Dummy implementation, method not called for this
1559 // base class, but pure virtual not possible-
1560 return false;
1563 static OUString implGetNameScemeForLocaleItem( const LocaleItem* pLocaleItem )
1565 /* FIXME-BCP47: this uses '_' underscore character as separator and
1566 * also appends Variant, which can't be blindly changed as it would
1567 * violate the naming scheme in use. */
1569 static const char aUnder[] = "_";
1571 OSL_ENSURE( pLocaleItem,
1572 "StringResourcePersistenceImpl::implGetNameScemeForLocaleItem(): pLocaleItem == NULL" );
1573 Locale aLocale = pLocaleItem->m_locale;
1575 OUString aRetStr = aUnder + aLocale.Language;
1577 OUString aCountry = aLocale.Country;
1578 if( !aCountry.isEmpty() )
1580 aRetStr += aUnder + aCountry;
1583 OUString aVariant = aLocale.Variant;
1584 if( !aVariant.isEmpty() )
1586 aRetStr += aUnder + aVariant;
1588 return aRetStr;
1591 OUString StringResourcePersistenceImpl::implGetFileNameForLocaleItem
1592 ( LocaleItem const * pLocaleItem, const OUString& aNameBase )
1594 OUString aFileName = aNameBase;
1595 if( aFileName.isEmpty() )
1596 aFileName = aNameBaseDefaultStr;
1598 aFileName += implGetNameScemeForLocaleItem( pLocaleItem );
1599 return aFileName;
1602 OUString StringResourcePersistenceImpl::implGetPathForLocaleItem
1603 ( LocaleItem const * pLocaleItem, const OUString& aNameBase,
1604 std::u16string_view aLocation, bool bDefaultFile )
1606 OUString aFileName = implGetFileNameForLocaleItem( pLocaleItem, aNameBase );
1607 INetURLObject aInetObj( aLocation );
1608 aInetObj.insertName( aFileName, true, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
1609 if( bDefaultFile )
1610 aInetObj.setExtension( u"default" );
1611 else
1612 aInetObj.setExtension( u"properties" );
1613 OUString aCompleteFileName = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1614 return aCompleteFileName;
1617 // White space according to Java property files specification in
1618 // http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html#load(java.io.InputStream)
1619 static bool isWhiteSpace( sal_Unicode c )
1621 bool bWhite = ( c == 0x0020 || // space
1622 c == 0x0009 || // tab
1623 c == 0x000a || // line feed, not always handled by TextInputStream
1624 c == 0x000d || // carriage return, not always handled by TextInputStream
1625 c == 0x000C ); // form feed
1626 return bWhite;
1629 static void skipWhites( const sal_Unicode* pBuf, sal_Int32 nLen, sal_Int32& ri )
1631 while( ri < nLen )
1633 if( !isWhiteSpace( pBuf[ri] ) )
1634 break;
1635 ri++;
1639 static bool isHexDigit( sal_Unicode c, sal_uInt16& nDigitVal )
1641 bool bRet = true;
1642 if( c >= '0' && c <= '9' )
1643 nDigitVal = c - '0';
1644 else if( c >= 'a' && c <= 'f' )
1645 nDigitVal = c - 'a' + 10;
1646 else if( c >= 'A' && c <= 'F' )
1647 nDigitVal = c - 'A' + 10;
1648 else
1649 bRet = false;
1650 return bRet;
1653 static sal_Unicode getEscapeChar( const sal_Unicode* pBuf, sal_Int32 nLen, sal_Int32& ri )
1655 sal_Int32 i = ri;
1657 sal_Unicode cRet = 0;
1658 sal_Unicode c = pBuf[i];
1659 switch( c )
1661 case 't':
1662 cRet = 0x0009;
1663 break;
1664 case 'n':
1665 cRet = 0x000a;
1666 break;
1667 case 'f':
1668 cRet = 0x000c;
1669 break;
1670 case 'r':
1671 cRet = 0x000d;
1672 break;
1673 case '\\':
1674 cRet = '\\';
1675 break;
1676 case 'u':
1678 // Skip multiple u
1679 i++;
1680 while( i < nLen && pBuf[i] == 'u' )
1681 i++;
1683 // Process hex digits
1684 sal_Int32 nDigitCount = 0;
1685 sal_uInt16 nDigitVal;
1686 while( i < nLen && isHexDigit( pBuf[i], nDigitVal ) )
1688 cRet = 16 * cRet + nDigitVal;
1690 nDigitCount++;
1691 if( nDigitCount == 4 )
1693 // Write back position
1694 ri = i;
1695 break;
1697 i++;
1699 break;
1701 default:
1702 cRet = c;
1705 return cRet;
1708 static void CheckContinueInNextLine( const Reference< io::XTextInputStream2 >& xTextInputStream,
1709 OUString& aLine, bool& bEscapePending, const sal_Unicode*& pBuf,
1710 sal_Int32& nLen, sal_Int32& i )
1712 if( !(i == nLen && bEscapePending) )
1713 return;
1715 bEscapePending = false;
1717 if( !xTextInputStream->isEOF() )
1719 aLine = xTextInputStream->readLine();
1720 nLen = aLine.getLength();
1721 pBuf = aLine.getStr();
1722 i = 0;
1724 skipWhites( pBuf, nLen, i );
1728 bool StringResourcePersistenceImpl::implReadPropertiesFile
1729 ( LocaleItem* pLocaleItem, const Reference< io::XInputStream >& xInputStream )
1731 if( !xInputStream.is() || pLocaleItem == nullptr )
1732 return false;
1734 Reference< io::XTextInputStream2 > xTextInputStream = io::TextInputStream::create( m_xContext );
1736 xTextInputStream->setInputStream( xInputStream );
1738 OUString aEncodingStr = OUString::createFromAscii
1739 ( rtl_getMimeCharsetFromTextEncoding( RTL_TEXTENCODING_ISO_8859_1 ) );
1740 xTextInputStream->setEncoding( aEncodingStr );
1742 OUString aLine;
1743 while( !xTextInputStream->isEOF() )
1745 aLine = xTextInputStream->readLine();
1747 sal_Int32 nLen = aLine.getLength();
1748 if( 0 == nLen )
1749 continue;
1750 const sal_Unicode* pBuf = aLine.getStr();
1751 OUStringBuffer aBuf;
1752 sal_Unicode c = 0;
1753 sal_Int32 i = 0;
1755 skipWhites( pBuf, nLen, i );
1756 if( i == nLen )
1757 continue; // line contains only white spaces
1759 // Comment?
1760 c = pBuf[i];
1761 if( c == '#' || c == '!' )
1762 continue;
1764 // Scan key
1765 OUString aResourceID;
1766 bool bEscapePending = false;
1767 bool bStrComplete = false;
1768 while( i < nLen && !bStrComplete )
1770 c = pBuf[i];
1771 if( bEscapePending )
1773 aBuf.append( getEscapeChar( pBuf, nLen, i ) );
1774 bEscapePending = false;
1776 else
1778 if( c == '\\' )
1780 bEscapePending = true;
1782 else
1784 if( c == ':' || c == '=' || isWhiteSpace( c ) )
1785 bStrComplete = true;
1786 else
1787 aBuf.append( c );
1790 i++;
1792 CheckContinueInNextLine( xTextInputStream, aLine, bEscapePending, pBuf, nLen, i );
1793 if( i == nLen )
1794 bStrComplete = true;
1796 if( bStrComplete )
1797 aResourceID = aBuf.makeStringAndClear();
1800 // Ignore lines with empty keys
1801 if( aResourceID.isEmpty() )
1802 continue;
1804 // Scan value
1805 skipWhites( pBuf, nLen, i );
1807 OUString aValueStr;
1808 bEscapePending = false;
1809 bStrComplete = false;
1810 while( i < nLen && !bStrComplete )
1812 c = pBuf[i];
1813 if( c == 0x000a || c == 0x000d ) // line feed/carriage return, not always handled by TextInputStream
1815 i++;
1817 else
1819 if( bEscapePending )
1821 aBuf.append( getEscapeChar( pBuf, nLen, i ) );
1822 bEscapePending = false;
1824 else if( c == '\\' )
1825 bEscapePending = true;
1826 else
1827 aBuf.append( c );
1828 i++;
1830 CheckContinueInNextLine( xTextInputStream, aLine, bEscapePending, pBuf, nLen, i );
1832 if( i == nLen )
1833 bStrComplete = true;
1835 if( bStrComplete )
1836 aValueStr = aBuf.makeStringAndClear();
1839 // Push into table
1840 pLocaleItem->m_aIdToStringMap[ aResourceID ] = aValueStr;
1841 implScanIdForNumber( aResourceID );
1842 IdToIndexMap& rIndexMap = pLocaleItem->m_aIdToIndexMap;
1843 rIndexMap[ aResourceID ] = pLocaleItem->m_nNextIndex++;
1846 return true;
1850 static sal_Unicode getHexCharForDigit( sal_uInt16 nDigitVal )
1852 sal_Unicode cRet = ( nDigitVal < 10 ) ? ('0' + nDigitVal) : ('a' + (nDigitVal-10));
1853 return cRet;
1856 static void implWriteCharToBuffer( OUStringBuffer& aBuf, sal_Unicode cu, bool bKey )
1858 if( cu == '\\' )
1860 aBuf.append( '\\' );
1861 aBuf.append( '\\' );
1863 else if( cu == 0x000a )
1865 aBuf.append( '\\' );
1866 aBuf.append( 'n' );
1868 else if( cu == 0x000d )
1870 aBuf.append( '\\' );
1871 aBuf.append( 'r' );
1873 else if( bKey && cu == '=' )
1875 aBuf.append( '\\' );
1876 aBuf.append( '=' );
1878 else if( bKey && cu == ':' )
1880 aBuf.append( '\\' );
1881 aBuf.append( ':' );
1883 // ISO/IEC 8859-1 range according to:
1884 // http://en.wikipedia.org/wiki/ISO/IEC_8859-1
1885 else if( cu >= 0x20 && cu <= 0x7e )
1886 //TODO: Check why (cu >= 0xa0 && cu <= 0xFF)
1887 //is encoded in sample properties files
1888 //else if( (cu >= 0x20 && cu <= 0x7e) ||
1889 // (cu >= 0xa0 && cu <= 0xFF) )
1891 aBuf.append( cu );
1893 else
1895 // Unicode encoding
1896 aBuf.append( '\\' );
1897 aBuf.append( 'u' );
1899 sal_uInt16 nVal = cu;
1900 for( sal_uInt16 i = 0 ; i < 4 ; i++ )
1902 sal_uInt16 nDigit = nVal / 0x1000;
1903 nVal -= nDigit * 0x1000;
1904 nVal *= 0x10;
1905 aBuf.append( getHexCharForDigit( nDigit ) );
1910 static void implWriteStringWithEncoding( const OUString& aStr,
1911 Reference< io::XTextOutputStream2 > const & xTextOutputStream, bool bKey )
1913 static const sal_Unicode cLineFeed = 0xa;
1915 OUStringBuffer aBuf;
1916 sal_Int32 nLen = aStr.getLength();
1917 const sal_Unicode* pSrc = aStr.getStr();
1918 for( sal_Int32 i = 0 ; i < nLen ; i++ )
1920 sal_Unicode cu = pSrc[i];
1921 implWriteCharToBuffer( aBuf, cu, bKey );
1922 // TODO?: split long lines
1924 if( !bKey )
1925 aBuf.append( cLineFeed );
1927 OUString aWriteStr = aBuf.makeStringAndClear();
1928 xTextOutputStream->writeString( aWriteStr );
1931 bool StringResourcePersistenceImpl::implWritePropertiesFile( LocaleItem const * pLocaleItem,
1932 const Reference< io::XOutputStream >& xOutputStream, const OUString& aComment )
1934 if( !xOutputStream.is() || pLocaleItem == nullptr )
1935 return false;
1937 bool bSuccess = false;
1938 Reference< io::XTextOutputStream2 > xTextOutputStream = io::TextOutputStream::create(m_xContext);
1940 xTextOutputStream->setOutputStream( xOutputStream );
1942 OUString aEncodingStr = OUString::createFromAscii
1943 ( rtl_getMimeCharsetFromTextEncoding( RTL_TEXTENCODING_ISO_8859_1 ) );
1944 xTextOutputStream->setEncoding( aEncodingStr );
1946 xTextOutputStream->writeString( aComment );
1947 xTextOutputStream->writeString( "\n" );
1949 const IdToStringMap& rHashMap = pLocaleItem->m_aIdToStringMap;
1950 if( !rHashMap.empty() )
1952 // Sort ids according to read order
1953 const IdToIndexMap& rIndexMap = pLocaleItem->m_aIdToIndexMap;
1955 // Find max/min index
1956 auto itMinMax = std::minmax_element(rIndexMap.begin(), rIndexMap.end(),
1957 [](const IdToIndexMap::value_type& a, const IdToIndexMap::value_type& b) { return a.second < b.second; });
1958 sal_Int32 nMinIndex = itMinMax.first->second;
1959 sal_Int32 nMaxIndex = itMinMax.second->second;
1960 sal_Int32 nTabSize = nMaxIndex - nMinIndex + 1;
1962 // Create sorted array of pointers to the id strings
1963 std::unique_ptr<const OUString*[]> pIdPtrs( new const OUString*[nTabSize] );
1964 for(sal_Int32 i = 0 ; i < nTabSize ; i++ )
1965 pIdPtrs[i] = nullptr;
1966 for( const auto& rIndex : rIndexMap )
1968 sal_Int32 nIndex = rIndex.second;
1969 pIdPtrs[nIndex - nMinIndex] = &(rIndex.first);
1972 // Write lines in correct order
1973 for(sal_Int32 i = 0 ; i < nTabSize ; i++ )
1975 const OUString* pStr = pIdPtrs[i];
1976 if( pStr != nullptr )
1978 OUString aResourceID = *pStr;
1979 IdToStringMap::const_iterator it = rHashMap.find( aResourceID );
1980 if( it != rHashMap.end() )
1982 implWriteStringWithEncoding( aResourceID, xTextOutputStream, true );
1983 xTextOutputStream->writeString( "=" );
1984 OUString aValStr = (*it).second;
1985 implWriteStringWithEncoding( aValStr, xTextOutputStream, false );
1991 bSuccess = true;
1993 return bSuccess;
1997 // StringResourceWithStorageImpl
1999 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
2000 scripting_StringResourceWithStorageImpl_get_implementation(
2001 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
2003 return cppu::acquire(new StringResourceWithStorageImpl(context));
2007 StringResourceWithStorageImpl::StringResourceWithStorageImpl( const Reference< XComponentContext >& rxContext )
2008 : StringResourceWithStorageImpl_BASE( rxContext )
2009 , m_bStorageChanged( false )
2014 StringResourceWithStorageImpl::~StringResourceWithStorageImpl()
2019 // XServiceInfo
2022 OUString StringResourceWithStorageImpl::getImplementationName( )
2024 return "com.sun.star.comp.scripting.StringResourceWithStorage";
2027 sal_Bool StringResourceWithStorageImpl::supportsService( const OUString& rServiceName )
2029 return cppu::supportsService(this, rServiceName);
2032 Sequence< OUString > StringResourceWithStorageImpl::getSupportedServiceNames( )
2034 return { "com.sun.star.resource.StringResourceWithStorage" };
2038 // XInitialization
2041 void StringResourceWithStorageImpl::initialize( const Sequence< Any >& aArguments )
2043 std::unique_lock aGuard( m_aMutex );
2045 if ( aArguments.getLength() != 5 )
2047 throw RuntimeException(
2048 "StringResourceWithStorageImpl::initialize: invalid number of arguments!" );
2051 bool bOk = (aArguments[0] >>= m_xStorage);
2052 if( bOk && !m_xStorage.is() )
2053 bOk = false;
2055 if( !bOk )
2057 throw IllegalArgumentException( "StringResourceWithStorageImpl::initialize: invalid storage", Reference< XInterface >(), 0 );
2060 implInitializeCommonParameters( aGuard, aArguments );
2064 // Forwarding calls to base class
2066 // XModifyBroadcaster
2067 void StringResourceWithStorageImpl::addModifyListener( const Reference< XModifyListener >& aListener )
2069 StringResourceImpl::addModifyListener( aListener );
2071 void StringResourceWithStorageImpl::removeModifyListener( const Reference< XModifyListener >& aListener )
2073 StringResourceImpl::removeModifyListener( aListener );
2076 // XStringResourceResolver
2077 OUString StringResourceWithStorageImpl::resolveString( const OUString& ResourceID )
2079 return StringResourceImpl::resolveString( ResourceID ) ;
2081 OUString StringResourceWithStorageImpl::resolveStringForLocale( const OUString& ResourceID, const Locale& locale )
2083 return StringResourceImpl::resolveStringForLocale( ResourceID, locale );
2085 sal_Bool StringResourceWithStorageImpl::hasEntryForId( const OUString& ResourceID )
2087 return StringResourceImpl::hasEntryForId( ResourceID ) ;
2089 sal_Bool StringResourceWithStorageImpl::hasEntryForIdAndLocale( const OUString& ResourceID,
2090 const Locale& locale )
2092 return StringResourceImpl::hasEntryForIdAndLocale( ResourceID, locale );
2094 Sequence< OUString > StringResourceWithStorageImpl::getResourceIDs( )
2096 return StringResourceImpl::getResourceIDs();
2098 Sequence< OUString > StringResourceWithStorageImpl::getResourceIDsForLocale
2099 ( const Locale& locale )
2101 return StringResourceImpl::getResourceIDsForLocale( locale );
2103 Locale StringResourceWithStorageImpl::getCurrentLocale()
2105 return StringResourceImpl::getCurrentLocale();
2107 Locale StringResourceWithStorageImpl::getDefaultLocale( )
2109 return StringResourceImpl::getDefaultLocale();
2111 Sequence< Locale > StringResourceWithStorageImpl::getLocales( )
2113 return StringResourceImpl::getLocales();
2116 // XStringResourceManager
2117 sal_Bool StringResourceWithStorageImpl::isReadOnly()
2119 return StringResourceImpl::isReadOnly();
2121 void StringResourceWithStorageImpl::setCurrentLocale( const Locale& locale, sal_Bool FindClosestMatch )
2123 StringResourceImpl::setCurrentLocale( locale, FindClosestMatch );
2125 void StringResourceWithStorageImpl::setDefaultLocale( const Locale& locale )
2127 StringResourceImpl::setDefaultLocale( locale );
2129 void StringResourceWithStorageImpl::setString( const OUString& ResourceID, const OUString& Str )
2131 StringResourceImpl::setString( ResourceID, Str );
2133 void StringResourceWithStorageImpl::setStringForLocale
2134 ( const OUString& ResourceID, const OUString& Str, const Locale& locale )
2136 StringResourceImpl::setStringForLocale( ResourceID, Str, locale );
2138 void StringResourceWithStorageImpl::removeId( const OUString& ResourceID )
2140 StringResourceImpl::removeId( ResourceID );
2142 void StringResourceWithStorageImpl::removeIdForLocale( const OUString& ResourceID, const Locale& locale )
2144 StringResourceImpl::removeIdForLocale( ResourceID, locale );
2146 void StringResourceWithStorageImpl::newLocale( const Locale& locale )
2148 StringResourceImpl::newLocale( locale );
2150 void StringResourceWithStorageImpl::removeLocale( const Locale& locale )
2152 StringResourceImpl::removeLocale( locale );
2154 sal_Int32 StringResourceWithStorageImpl::getUniqueNumericId( )
2156 return StringResourceImpl::getUniqueNumericId();
2159 // XStringResourcePersistence
2160 void StringResourceWithStorageImpl::store()
2162 std::unique_lock aGuard( m_aMutex );
2163 implCheckReadOnly( "StringResourceWithStorageImpl::store(): Read only" );
2165 bool bStoreAll = m_bStorageChanged;
2166 m_bStorageChanged = false;
2167 if( !m_bModified && !bStoreAll )
2168 return;
2170 implStoreAtStorage( m_aNameBase, m_aComment, m_xStorage, true/*bUsedForStore*/, bStoreAll );
2171 m_bModified = false;
2174 sal_Bool StringResourceWithStorageImpl::isModified( )
2176 return StringResourcePersistenceImpl::isModified();
2178 void StringResourceWithStorageImpl::setComment( const OUString& Comment )
2180 StringResourcePersistenceImpl::setComment( Comment );
2182 void StringResourceWithStorageImpl::storeToStorage( const Reference< XStorage >& Storage,
2183 const OUString& NameBase, const OUString& Comment )
2185 StringResourcePersistenceImpl::storeToStorage( Storage, NameBase, Comment );
2187 void StringResourceWithStorageImpl::storeToURL( const OUString& URL,
2188 const OUString& NameBase, const OUString& Comment,
2189 const Reference< css::task::XInteractionHandler >& Handler )
2191 StringResourcePersistenceImpl::storeToURL( URL, NameBase, Comment, Handler );
2193 Sequence< ::sal_Int8 > StringResourceWithStorageImpl::exportBinary( )
2195 return StringResourcePersistenceImpl::exportBinary();
2197 void StringResourceWithStorageImpl::importBinary( const Sequence< ::sal_Int8 >& Data )
2199 StringResourcePersistenceImpl::importBinary( Data );
2203 // XStringResourceWithStorage
2205 void StringResourceWithStorageImpl::storeAsStorage( const Reference< XStorage >& Storage )
2207 setStorage( Storage );
2208 store();
2211 void StringResourceWithStorageImpl::setStorage( const Reference< XStorage >& Storage )
2213 std::unique_lock aGuard( m_aMutex );
2215 if( !Storage.is() )
2217 throw IllegalArgumentException( "StringResourceWithStorageImpl::setStorage: invalid storage", Reference< XInterface >(), 0 );
2220 implLoadAllLocales();
2222 m_xStorage = Storage;
2223 m_bStorageChanged = true;
2227 // Private helper methods
2230 // Scan locale properties files
2231 void StringResourceWithStorageImpl::implScanLocales()
2233 if( m_xStorage.is() )
2235 Sequence< OUString > aContentSeq = m_xStorage->getElementNames();
2236 implScanLocaleNames( aContentSeq );
2239 implLoadAllLocales();
2242 // Loading
2243 bool StringResourceWithStorageImpl::implLoadLocale( LocaleItem* pLocaleItem )
2245 bool bSuccess = false;
2248 OUString aStreamName = implGetFileNameForLocaleItem( pLocaleItem, m_aNameBase ) + ".properties";
2250 Reference< io::XStream > xElementStream =
2251 m_xStorage->openStreamElement( aStreamName, ElementModes::READ );
2253 if( xElementStream.is() )
2255 Reference< io::XInputStream > xInputStream = xElementStream->getInputStream();
2256 if( xInputStream.is() )
2258 bSuccess = StringResourcePersistenceImpl::implReadPropertiesFile( pLocaleItem, xInputStream );
2259 xInputStream->closeInput();
2263 catch( uno::Exception& )
2266 return bSuccess;
2270 // StringResourceWithLocationImpl
2273 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
2274 scripting_StringResourceWithLocationImpl_get_implementation(
2275 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
2277 return cppu::acquire(new StringResourceWithLocationImpl(context));
2282 StringResourceWithLocationImpl::StringResourceWithLocationImpl( const Reference< XComponentContext >& rxContext )
2283 : StringResourceWithLocationImpl_BASE( rxContext )
2284 , m_bLocationChanged( false )
2289 StringResourceWithLocationImpl::~StringResourceWithLocationImpl()
2294 // XServiceInfo
2297 OUString StringResourceWithLocationImpl::getImplementationName( )
2299 return "com.sun.star.comp.scripting.StringResourceWithLocation";
2302 sal_Bool StringResourceWithLocationImpl::supportsService( const OUString& rServiceName )
2304 return cppu::supportsService(this, rServiceName);
2307 Sequence< OUString > StringResourceWithLocationImpl::getSupportedServiceNames( )
2309 return { "com.sun.star.resource.StringResourceWithLocation" };
2313 // XInitialization
2316 void StringResourceWithLocationImpl::initialize( const Sequence< Any >& aArguments )
2318 std::unique_lock aGuard( m_aMutex );
2320 if ( aArguments.getLength() != 6 )
2322 throw RuntimeException(
2323 "XInitialization::initialize: invalid number of arguments!" );
2326 bool bOk = (aArguments[0] >>= m_aLocation);
2327 sal_Int32 nLen = m_aLocation.getLength();
2328 if( bOk && nLen == 0 )
2330 bOk = false;
2332 else
2334 if( m_aLocation[nLen - 1] != '/' )
2335 m_aLocation += "/";
2338 if( !bOk )
2340 throw IllegalArgumentException( "XInitialization::initialize: invalid URL", Reference< XInterface >(), 0 );
2344 bOk = (aArguments[5] >>= m_xInteractionHandler);
2345 if( !bOk )
2347 throw IllegalArgumentException( "StringResourceWithStorageImpl::initialize: invalid type", Reference< XInterface >(), 5 );
2350 implInitializeCommonParameters( aGuard, aArguments );
2354 // Forwarding calls to base class
2356 // XModifyBroadcaster
2357 void StringResourceWithLocationImpl::addModifyListener( const Reference< XModifyListener >& aListener )
2359 StringResourceImpl::addModifyListener( aListener );
2361 void StringResourceWithLocationImpl::removeModifyListener( const Reference< XModifyListener >& aListener )
2363 StringResourceImpl::removeModifyListener( aListener );
2366 // XStringResourceResolver
2367 OUString StringResourceWithLocationImpl::resolveString( const OUString& ResourceID )
2369 return StringResourceImpl::resolveString( ResourceID ) ;
2371 OUString StringResourceWithLocationImpl::resolveStringForLocale( const OUString& ResourceID, const Locale& locale )
2373 return StringResourceImpl::resolveStringForLocale( ResourceID, locale );
2375 sal_Bool StringResourceWithLocationImpl::hasEntryForId( const OUString& ResourceID )
2377 return StringResourceImpl::hasEntryForId( ResourceID ) ;
2379 sal_Bool StringResourceWithLocationImpl::hasEntryForIdAndLocale( const OUString& ResourceID,
2380 const Locale& locale )
2382 return StringResourceImpl::hasEntryForIdAndLocale( ResourceID, locale );
2384 Sequence< OUString > StringResourceWithLocationImpl::getResourceIDs( )
2386 return StringResourceImpl::getResourceIDs();
2388 Sequence< OUString > StringResourceWithLocationImpl::getResourceIDsForLocale
2389 ( const Locale& locale )
2391 return StringResourceImpl::getResourceIDsForLocale( locale );
2393 Locale StringResourceWithLocationImpl::getCurrentLocale()
2395 return StringResourceImpl::getCurrentLocale();
2397 Locale StringResourceWithLocationImpl::getDefaultLocale( )
2399 return StringResourceImpl::getDefaultLocale();
2401 Sequence< Locale > StringResourceWithLocationImpl::getLocales( )
2403 return StringResourceImpl::getLocales();
2406 // XStringResourceManager
2407 sal_Bool StringResourceWithLocationImpl::isReadOnly()
2409 return StringResourceImpl::isReadOnly();
2411 void StringResourceWithLocationImpl::setCurrentLocale( const Locale& locale, sal_Bool FindClosestMatch )
2413 StringResourceImpl::setCurrentLocale( locale, FindClosestMatch );
2415 void StringResourceWithLocationImpl::setDefaultLocale( const Locale& locale )
2417 StringResourceImpl::setDefaultLocale( locale );
2419 void StringResourceWithLocationImpl::setString( const OUString& ResourceID, const OUString& Str )
2421 StringResourceImpl::setString( ResourceID, Str );
2423 void StringResourceWithLocationImpl::setStringForLocale
2424 ( const OUString& ResourceID, const OUString& Str, const Locale& locale )
2426 StringResourceImpl::setStringForLocale( ResourceID, Str, locale );
2428 void StringResourceWithLocationImpl::removeId( const OUString& ResourceID )
2430 StringResourceImpl::removeId( ResourceID );
2432 void StringResourceWithLocationImpl::removeIdForLocale( const OUString& ResourceID, const Locale& locale )
2434 StringResourceImpl::removeIdForLocale( ResourceID, locale );
2436 void StringResourceWithLocationImpl::newLocale( const Locale& locale )
2438 StringResourceImpl::newLocale( locale );
2440 void StringResourceWithLocationImpl::removeLocale( const Locale& locale )
2442 StringResourceImpl::removeLocale( locale );
2444 sal_Int32 StringResourceWithLocationImpl::getUniqueNumericId( )
2446 return StringResourceImpl::getUniqueNumericId();
2449 // XStringResourcePersistence
2450 void StringResourceWithLocationImpl::store()
2452 std::unique_lock aGuard( m_aMutex );
2453 implCheckReadOnly( "StringResourceWithLocationImpl::store(): Read only" );
2455 bool bStoreAll = m_bLocationChanged;
2456 m_bLocationChanged = false;
2457 if( !m_bModified && !bStoreAll )
2458 return;
2460 Reference< ucb::XSimpleFileAccess3 > xFileAccess = getFileAccessImpl();
2461 implStoreAtLocation( m_aLocation, m_aNameBase, m_aComment,
2462 xFileAccess, true/*bUsedForStore*/, bStoreAll );
2463 m_bModified = false;
2466 sal_Bool StringResourceWithLocationImpl::isModified( )
2468 return StringResourcePersistenceImpl::isModified();
2470 void StringResourceWithLocationImpl::setComment( const OUString& Comment )
2472 StringResourcePersistenceImpl::setComment( Comment );
2474 void StringResourceWithLocationImpl::storeToStorage( const Reference< XStorage >& Storage,
2475 const OUString& NameBase, const OUString& Comment )
2477 StringResourcePersistenceImpl::storeToStorage( Storage, NameBase, Comment );
2479 void StringResourceWithLocationImpl::storeToURL( const OUString& URL,
2480 const OUString& NameBase, const OUString& Comment,
2481 const Reference< css::task::XInteractionHandler >& Handler )
2483 StringResourcePersistenceImpl::storeToURL( URL, NameBase, Comment, Handler );
2485 Sequence< ::sal_Int8 > StringResourceWithLocationImpl::exportBinary( )
2487 return StringResourcePersistenceImpl::exportBinary();
2489 void StringResourceWithLocationImpl::importBinary( const Sequence< ::sal_Int8 >& Data )
2491 StringResourcePersistenceImpl::importBinary( Data );
2495 // XStringResourceWithLocation
2497 // XStringResourceWithLocation
2498 void StringResourceWithLocationImpl::storeAsURL( const OUString& URL )
2500 setURL( URL );
2501 store();
2504 void StringResourceWithLocationImpl::setURL( const OUString& URL )
2506 std::unique_lock aGuard( m_aMutex );
2507 implCheckReadOnly( "StringResourceWithLocationImpl::setURL(): Read only" );
2509 sal_Int32 nLen = URL.getLength();
2510 if( nLen == 0 )
2512 throw IllegalArgumentException( "StringResourceWithLocationImpl::setURL: invalid URL", Reference< XInterface >(), 0 );
2515 implLoadAllLocales();
2517 // Delete files at old location
2518 implStoreAtLocation( m_aLocation, m_aNameBase, m_aComment,
2519 getFileAccessImpl(), false/*bUsedForStore*/, false/*bStoreAll*/, true/*bKillAll*/ );
2521 m_aLocation = URL;
2522 m_bLocationChanged = true;
2526 // Private helper methods
2529 // Scan locale properties files
2530 void StringResourceWithLocationImpl::implScanLocales()
2532 const Reference< ucb::XSimpleFileAccess3 > xFileAccess = getFileAccessImpl();
2533 if( xFileAccess.is() && xFileAccess->isFolder( m_aLocation ) )
2535 Sequence< OUString > aContentSeq = xFileAccess->getFolderContents( m_aLocation, false );
2536 implScanLocaleNames( aContentSeq );
2540 // Loading
2541 bool StringResourceWithLocationImpl::implLoadLocale( LocaleItem* pLocaleItem )
2543 bool bSuccess = false;
2545 const Reference< ucb::XSimpleFileAccess3 > xFileAccess = getFileAccessImpl();
2546 if( xFileAccess.is() )
2548 OUString aCompleteFileName =
2549 implGetPathForLocaleItem( pLocaleItem, m_aNameBase, m_aLocation );
2551 Reference< io::XInputStream > xInputStream;
2554 xInputStream = xFileAccess->openFileRead( aCompleteFileName );
2556 catch( Exception& )
2558 if( xInputStream.is() )
2560 bSuccess = StringResourcePersistenceImpl::implReadPropertiesFile( pLocaleItem, xInputStream );
2561 xInputStream->closeInput();
2565 return bSuccess;
2568 const Reference< ucb::XSimpleFileAccess3 > & StringResourceWithLocationImpl::getFileAccessImpl()
2570 if( !m_xSFI.is() )
2572 m_xSFI = ucb::SimpleFileAccess::create(m_xContext);
2574 if( m_xSFI.is() && m_xInteractionHandler.is() )
2575 m_xSFI->setInteractionHandler( m_xInteractionHandler );
2577 return m_xSFI;
2580 } // namespace stringresource
2583 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */