Files
libreoffice/linguistic/source/dicimp.cxx

1063 lines
30 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-11-17 11:37:46 +00:00
#include <cppuhelper/factory.hxx>
2000-11-17 11:37:46 +00:00
#include <dicimp.hxx>
#include <hyphdsp.hxx>
#include <i18nlangtag/lang.h>
#include <i18nlangtag/languagetag.hxx>
#include <osl/mutex.hxx>
2000-11-17 11:37:46 +00:00
#include <tools/debug.hxx>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <comphelper/sequence.hxx>
#include <unotools/ucbstreamhelper.hxx>
2000-11-17 11:37:46 +00:00
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
2000-11-17 11:37:46 +00:00
#include <com/sun/star/linguistic2/DictionaryType.hpp>
#include <com/sun/star/linguistic2/DictionaryEventFlags.hpp>
#include <com/sun/star/registry/XRegistryKey.hpp>
#include <com/sun/star/io/TempFile.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/io/XOutputStream.hpp>
#include "defs.hxx"
2000-11-17 11:37:46 +00:00
#include <algorithm>
2000-11-17 11:37:46 +00:00
using namespace utl;
using namespace osl;
using namespace com::sun::star;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace com::sun::star::linguistic2;
using namespace linguistic;
2011-02-26 15:54:59 +01:00
#define BUFSIZE 4096
2000-11-17 11:37:46 +00:00
#define VERS2_NOLANGUAGE 1024
#define MAX_HEADER_LENGTH 16
static const sal_Char* pVerStr2 = "WBSWG2";
static const sal_Char* pVerStr5 = "WBSWG5";
static const sal_Char* pVerStr6 = "WBSWG6";
static const sal_Char* pVerOOo7 = "OOoUserDict1";
static const sal_Int16 DIC_VERSION_DONTKNOW = -1;
static const sal_Int16 DIC_VERSION_2 = 2;
static const sal_Int16 DIC_VERSION_5 = 5;
static const sal_Int16 DIC_VERSION_6 = 6;
static const sal_Int16 DIC_VERSION_7 = 7;
static bool getTag(const OString &rLine, const sal_Char *pTagName,
OString &rTagValue)
{
2012-01-22 14:34:51 +00:00
sal_Int32 nPos = rLine.indexOf(pTagName);
if (nPos == -1)
return false;
2012-01-22 14:34:51 +00:00
rTagValue = comphelper::string::strip(rLine.copy(nPos + rtl_str_getLength(pTagName)),
' ');
return true;
}
sal_Int16 ReadDicVersion( SvStreamPtr &rpStream, sal_uInt16 &nLng, bool &bNeg )
{
// Sniff the header
sal_Int16 nDicVersion = DIC_VERSION_DONTKNOW;
sal_Char pMagicHeader[MAX_HEADER_LENGTH];
nLng = LANGUAGE_NONE;
bNeg = false;
if (!rpStream.get() || rpStream->GetError())
return -1;
sal_uInt64 const nSniffPos = rpStream->Tell();
static std::size_t nVerOOo7Len = sal::static_int_cast< std::size_t >(strlen( pVerOOo7 ));
pMagicHeader[ nVerOOo7Len ] = '\0';
if ((rpStream->ReadBytes(static_cast<void *>(pMagicHeader), nVerOOo7Len) == nVerOOo7Len) &&
!strcmp(pMagicHeader, pVerOOo7))
{
bool bSuccess;
OString aLine;
nDicVersion = DIC_VERSION_7;
// 1st skip magic / header line
rpStream->ReadLine(aLine);
// 2nd line: language all | en-US | pt-BR ...
while ((bSuccess = rpStream->ReadLine(aLine)))
{
OString aTagValue;
2012-01-22 14:34:51 +00:00
if (aLine[0] == '#') // skip comments
continue;
// lang: field
if (getTag(aLine, "lang: ", aTagValue))
{
if (aTagValue == "<none>")
nLng = LANGUAGE_NONE;
else
nLng = LanguageTag::convertToLanguageType(
OStringToOUString( aTagValue, RTL_TEXTENCODING_ASCII_US));
}
// type: negative / positive
if (getTag(aLine, "type: ", aTagValue))
{
if (aTagValue == "negative")
bNeg = true;
else
bNeg = false;
}
if (aLine.indexOf("---") != -1) // end of header
break;
}
if (!bSuccess)
return -2;
}
else
{
sal_uInt16 nLen;
rpStream->Seek (nSniffPos );
rpStream->ReadUInt16( nLen );
if (nLen >= MAX_HEADER_LENGTH)
return -1;
rpStream->ReadBytes(pMagicHeader, nLen);
pMagicHeader[nLen] = '\0';
// Check version magic
if (0 == strcmp( pMagicHeader, pVerStr6 ))
nDicVersion = DIC_VERSION_6;
else if (0 == strcmp( pMagicHeader, pVerStr5 ))
nDicVersion = DIC_VERSION_5;
else if (0 == strcmp( pMagicHeader, pVerStr2 ))
nDicVersion = DIC_VERSION_2;
else
nDicVersion = DIC_VERSION_DONTKNOW;
if (DIC_VERSION_2 == nDicVersion ||
DIC_VERSION_5 == nDicVersion ||
DIC_VERSION_6 == nDicVersion)
{
// The language of the dictionary
rpStream->ReadUInt16( nLng );
if (VERS2_NOLANGUAGE == nLng)
nLng = LANGUAGE_NONE;
// Negative Flag
rpStream->ReadCharAsBool( bNeg );
}
}
return nDicVersion;
}
2000-11-17 11:37:46 +00:00
DictionaryNeo::DictionaryNeo(const OUString &rName,
sal_Int16 nLang, DictionaryType eType,
const OUString &rMainURL,
bool bWriteable) :
2000-11-17 11:37:46 +00:00
aDicEvtListeners( GetLinguMutex() ),
aDicName (rName),
aMainURL (rMainURL),
2000-11-17 11:37:46 +00:00
eDicType (eType),
nLanguage (nLang)
2000-11-17 11:37:46 +00:00
{
nDicVersion = DIC_VERSION_DONTKNOW;
bNeedEntries = true;
bIsModified = bIsActive = false;
bIsReadonly = !bWriteable;
2000-11-17 11:37:46 +00:00
if( !rMainURL.isEmpty())
2000-11-17 11:37:46 +00:00
{
bool bExists = FileExists( rMainURL );
if( !bExists )
2000-11-17 11:37:46 +00:00
{
// save new dictionaries with in Format 7 (UTF8 plain text)
nDicVersion = DIC_VERSION_7;
2000-11-17 11:37:46 +00:00
//! create physical representation of an **empty** dictionary
//! that could be found by the dictionary-list implementation
2000-11-17 11:37:46 +00:00
// (Note: empty dictionaries are not just empty files!)
DBG_ASSERT( !bIsReadonly,
"DictionaryNeo: dictionaries should be writeable if they are to be saved" );
if (!bIsReadonly)
saveEntries( rMainURL );
bNeedEntries = false;
2000-11-17 11:37:46 +00:00
}
}
else
{
// non persistent dictionaries (like IgnoreAllList) should always be writable
bIsReadonly = false;
bNeedEntries = false;
2000-11-17 11:37:46 +00:00
}
}
DictionaryNeo::~DictionaryNeo()
{
}
sal_uLong DictionaryNeo::loadEntries(const OUString &rMainURL)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
// counter check that it is safe to set bIsModified to sal_False at
2000-11-17 11:37:46 +00:00
// the end of the function
DBG_ASSERT(!bIsModified, "lng : dictionary already modified!");
// function should only be called once in order to load entries from file
bNeedEntries = false;
2000-11-17 11:37:46 +00:00
if (rMainURL.isEmpty())
2000-11-17 11:37:46 +00:00
return 0;
uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
2000-11-17 11:37:46 +00:00
// get XInputStream stream
uno::Reference< io::XInputStream > xStream;
try
{
uno::Reference< ucb::XSimpleFileAccess3 > xAccess( ucb::SimpleFileAccess::create(xContext) );
xStream = xAccess->openFileRead( rMainURL );
}
2011-12-20 12:06:48 +09:00
catch (const uno::Exception &)
{
SAL_WARN( "linguistic", "failed to get input stream" );
}
if (!xStream.is())
return static_cast< sal_uLong >(-1);
SvStreamPtr pStream = SvStreamPtr( utl::UcbStreamHelper::CreateStream( xStream ) );
2011-02-08 20:59:20 +01:00
// read header
bool bNegativ;
sal_uInt16 nLang;
nDicVersion = ReadDicVersion(pStream, nLang, bNegativ);
sal_uLong nErr = pStream->GetError();
if (0 != nErr)
2000-11-17 11:37:46 +00:00
return nErr;
nLanguage = nLang;
2000-11-17 11:37:46 +00:00
eDicType = bNegativ ? DictionaryType_NEGATIVE : DictionaryType_POSITIVE;
rtl_TextEncoding eEnc = osl_getThreadTextEncoding();
if (nDicVersion >= DIC_VERSION_6)
eEnc = RTL_TEXTENCODING_UTF8;
aEntries.clear();
if (DIC_VERSION_6 == nDicVersion ||
DIC_VERSION_5 == nDicVersion ||
DIC_VERSION_2 == nDicVersion)
2000-11-17 11:37:46 +00:00
{
sal_uInt16 nLen = 0;
sal_Char aWordBuf[ BUFSIZE ];
2000-11-17 11:37:46 +00:00
2011-02-08 20:59:20 +01:00
// Read the first word
2000-11-17 11:37:46 +00:00
if (!pStream->IsEof())
{
pStream->ReadUInt16( nLen );
if (0 != (nErr = pStream->GetError()))
2000-11-17 11:37:46 +00:00
return nErr;
if ( nLen < BUFSIZE )
{
pStream->ReadBytes(aWordBuf, nLen);
if (0 != (nErr = pStream->GetError()))
2000-11-17 11:37:46 +00:00
return nErr;
*(aWordBuf + nLen) = 0;
}
else
return SVSTREAM_READ_ERROR;
2000-11-17 11:37:46 +00:00
}
while(!pStream->IsEof())
2000-11-17 11:37:46 +00:00
{
2011-02-08 20:59:20 +01:00
// Read from file
// Paste in dictionary without converting
if(*aWordBuf)
{
OUString aText(aWordBuf, rtl_str_getLength(aWordBuf), eEnc);
2007-06-07 13:21:55 +00:00
uno::Reference< XDictionaryEntry > xEntry =
new DicEntry( aText, bNegativ );
addEntry_Impl( xEntry, true ); //! don't launch events here
}
2000-11-17 11:37:46 +00:00
pStream->ReadUInt16( nLen );
2011-02-08 20:59:20 +01:00
if (pStream->IsEof())
break;
if (0 != (nErr = pStream->GetError()))
return nErr;
2000-11-17 11:37:46 +00:00
if (nLen < BUFSIZE)
{
pStream->ReadBytes(aWordBuf, nLen);
if (0 != (nErr = pStream->GetError()))
return nErr;
}
else
return SVSTREAM_READ_ERROR;
*(aWordBuf + nLen) = 0;
}
}
else if (DIC_VERSION_7 == nDicVersion)
{
OString aLine;
// remaining lines - stock strings (a [==] b)
while (pStream->ReadLine(aLine))
2000-11-17 11:37:46 +00:00
{
2012-01-22 14:34:51 +00:00
if (aLine[0] == '#') // skip comments
continue;
OUString aText = OStringToOUString(aLine, RTL_TEXTENCODING_UTF8);
2007-06-07 13:21:55 +00:00
uno::Reference< XDictionaryEntry > xEntry =
new DicEntry( aText, eDicType == DictionaryType_NEGATIVE );
addEntry_Impl( xEntry, true ); //! don't launch events here
2000-11-17 11:37:46 +00:00
}
}
SAL_WARN_IF(!isSorted(), "linguistic", "dictionary is not sorted");
2000-11-17 11:37:46 +00:00
// since this routine should be called only initially (prior to any
2000-11-17 11:37:46 +00:00
// modification to be saved) we reset the bIsModified flag here that
// was implicitly set by addEntry_Impl
bIsModified = false;
2000-11-17 11:37:46 +00:00
return pStream->GetError();
}
static OString formatForSave(const uno::Reference< XDictionaryEntry > &xEntry,
2011-11-27 20:37:42 +00:00
rtl_TextEncoding eEnc )
{
OStringBuffer aStr(OUStringToOString(xEntry->getDictionaryWord(), eEnc));
if (xEntry->isNegative())
{
aStr.append("==");
aStr.append(OUStringToOString(xEntry->getReplacementText(), eEnc));
}
2011-11-27 20:37:42 +00:00
return aStr.makeStringAndClear();
}
sal_uLong DictionaryNeo::saveEntries(const OUString &rURL)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
2000-11-17 11:37:46 +00:00
if (rURL.isEmpty())
2000-11-17 11:37:46 +00:00
return 0;
DBG_ASSERT(!INetURLObject( rURL ).HasError(), "lng : invalid URL");
2000-11-17 11:37:46 +00:00
uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
2000-11-17 11:37:46 +00:00
// get XOutputStream stream
uno::Reference<io::XStream> xStream;
try
{
xStream = io::TempFile::create(xContext);
}
catch (const uno::Exception &)
{
DBG_ASSERT( 0, "failed to get input stream" );
}
if (!xStream.is())
return static_cast< sal_uLong >(-1);
SvStreamPtr pStream = SvStreamPtr( utl::UcbStreamHelper::CreateStream( xStream ) );
// Always write as the latest version, i.e. DIC_VERSION_7
rtl_TextEncoding eEnc = RTL_TEXTENCODING_UTF8;
pStream->WriteLine(OString(pVerOOo7));
sal_uLong nErr = pStream->GetError();
if (0 != nErr)
return nErr;
/* XXX: the <none> case could be differentiated, is it absence or
* undetermined or multiple? Earlier versions did not know about 'und' and
* 'mul' and 'zxx' codes. Sync with ReadDicVersion() */
if (LinguIsUnspecified(nLanguage))
pStream->WriteLine(OString("lang: <none>"));
else
{
OStringBuffer aLine("lang: ");
aLine.append(OUStringToOString(LanguageTag::convertToBcp47(nLanguage), eEnc));
2011-11-27 20:37:42 +00:00
pStream->WriteLine(aLine.makeStringAndClear());
}
if (0 != (nErr = pStream->GetError()))
return nErr;
if (eDicType == DictionaryType_POSITIVE)
pStream->WriteLine(OString("type: positive"));
else
pStream->WriteLine(OString("type: negative"));
if (0 != (nErr = pStream->GetError()))
return nErr;
pStream->WriteLine(OString("---"));
if (0 != (nErr = pStream->GetError()))
return nErr;
for (Reference<XDictionaryEntry> & aEntrie : aEntries)
2000-11-17 11:37:46 +00:00
{
OString aOutStr = formatForSave(aEntrie, eEnc);
pStream->WriteLine (aOutStr);
if (0 != (nErr = pStream->GetError()))
return nErr;
2000-11-17 11:37:46 +00:00
}
try
{
pStream.reset();
uno::Reference< ucb::XSimpleFileAccess3 > xAccess(ucb::SimpleFileAccess::create(xContext));
Reference<io::XInputStream> xInputStream(xStream, UNO_QUERY_THROW);
uno::Reference<io::XSeekable> xSeek(xInputStream, UNO_QUERY_THROW);
xSeek->seek(0);
xAccess->writeFile(rURL, xInputStream);
//If we are migrating from an older version, then on first successful
//write, we're now converted to the latest version, i.e. DIC_VERSION_7
nDicVersion = DIC_VERSION_7;
}
catch (const uno::Exception &)
{
DBG_ASSERT( 0, "failed to write stream" );
return static_cast< sal_uLong >(-1);
}
2000-11-17 11:37:46 +00:00
return nErr;
2000-11-17 11:37:46 +00:00
}
void DictionaryNeo::launchEvent(sal_Int16 nEvent,
const uno::Reference< XDictionaryEntry >& xEntry)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
DictionaryEvent aEvt;
2007-06-07 13:21:55 +00:00
aEvt.Source = uno::Reference< XDictionary >( this );
2000-11-17 11:37:46 +00:00
aEvt.nEvent = nEvent;
aEvt.xDictionaryEntry = xEntry;
aDicEvtListeners.notifyEach( &XDictionaryEventListener::processDictionaryEvent, aEvt);
2000-11-17 11:37:46 +00:00
}
int DictionaryNeo::cmpDicEntry(const OUString& rWord1,
const OUString &rWord2,
bool bSimilarOnly)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
// returns 0 if rWord1 is equal to rWord2
// " a value < 0 if rWord1 is less than rWord2
// " a value > 0 if rWord1 is greater than rWord2
int nRes = 0;
sal_Int32 nLen1 = rWord1.getLength(),
nLen2 = rWord2.getLength();
2000-11-17 11:37:46 +00:00
if (bSimilarOnly)
{
const sal_Unicode cChar = '.';
if (nLen1 && cChar == rWord1[ nLen1 - 1 ])
2000-11-17 11:37:46 +00:00
nLen1--;
if (nLen2 && cChar == rWord2[ nLen2 - 1 ])
2000-11-17 11:37:46 +00:00
nLen2--;
}
const sal_Unicode cIgnChar = '=';
const sal_Unicode cIgnBeg = '['; // for alternative hyphenation, eg. Schif[f]fahrt, Zuc[1k]ker
const sal_Unicode cIgnEnd = ']'; // planned: gee"[1-/e]rfde or ge[-/1e]e"rfde (gee"rfde -> ge=erfde)
sal_Int32 nIdx1 = 0,
2000-11-17 11:37:46 +00:00
nIdx2 = 0,
nNumIgnChar1 = 0,
nNumIgnChar2 = 0;
bool IgnState;
2000-11-17 11:37:46 +00:00
sal_Int32 nDiff = 0;
sal_Unicode cChar1 = '\0';
sal_Unicode cChar2 = '\0';
2000-11-17 11:37:46 +00:00
do
{
// skip chars to be ignored
IgnState = false;
while (nIdx1 < nLen1 && ((cChar1 = rWord1[ nIdx1 ]) == cIgnChar || cChar1 == cIgnBeg || IgnState ))
2000-11-17 11:37:46 +00:00
{
if ( cChar1 == cIgnBeg )
IgnState = true;
else if (cChar1 == cIgnEnd)
IgnState = false;
2000-11-17 11:37:46 +00:00
nIdx1++;
nNumIgnChar1++;
}
IgnState = false;
while (nIdx2 < nLen2 && ((cChar2 = rWord2[ nIdx2 ]) == cIgnChar || cChar2 == cIgnBeg || IgnState ))
2000-11-17 11:37:46 +00:00
{
if ( cChar2 == cIgnBeg )
IgnState = true;
else if (cChar2 == cIgnEnd)
IgnState = false;
2000-11-17 11:37:46 +00:00
nIdx2++;
nNumIgnChar2++;
}
if (nIdx1 < nLen1 && nIdx2 < nLen2)
{
nDiff = cChar1 - cChar2;
if (nDiff)
break;
nIdx1++;
nIdx2++;
}
} while (nIdx1 < nLen1 && nIdx2 < nLen2);
if (nDiff)
nRes = nDiff;
else
{ // the string with the smallest count of not ignored chars is the
// shorter one
// count remaining IgnChars
IgnState = false;
2000-11-17 11:37:46 +00:00
while (nIdx1 < nLen1 )
{
if (rWord1[ nIdx1 ] == cIgnBeg)
IgnState = true;
if (IgnState || rWord1[ nIdx1 ] == cIgnChar)
2000-11-17 11:37:46 +00:00
nNumIgnChar1++;
if (rWord1[ nIdx1] == cIgnEnd)
IgnState = false;
nIdx1++;
2000-11-17 11:37:46 +00:00
}
IgnState = false;
2000-11-17 11:37:46 +00:00
while (nIdx2 < nLen2 )
{
if (rWord2[ nIdx2 ] == cIgnBeg)
IgnState = true;
if (IgnState || rWord2[ nIdx2 ] == cIgnChar)
2000-11-17 11:37:46 +00:00
nNumIgnChar2++;
if (rWord2[ nIdx2 ] == cIgnEnd)
IgnState = false;
nIdx2++;
2000-11-17 11:37:46 +00:00
}
nRes = ((sal_Int32) nLen1 - nNumIgnChar1) - ((sal_Int32) nLen2 - nNumIgnChar2);
2000-11-17 11:37:46 +00:00
}
return nRes;
}
bool DictionaryNeo::seekEntry(const OUString &rWord,
sal_Int32 *pPos, bool bSimilarOnly)
2000-11-17 11:37:46 +00:00
{
// look for entry with binary search.
// return sal_True if found sal_False else.
2000-11-17 11:37:46 +00:00
// if pPos != NULL it will become the position of the found entry, or
// if that was not found the position where it has to be inserted
// to keep the entries sorted
MutexGuard aGuard( GetLinguMutex() );
sal_Int32 nUpperIdx = getCount(),
2000-11-17 11:37:46 +00:00
nMidIdx,
nLowerIdx = 0;
if( nUpperIdx > 0 )
{
nUpperIdx--;
while( nLowerIdx <= nUpperIdx )
{
nMidIdx = (nLowerIdx + nUpperIdx) / 2;
DBG_ASSERT(aEntries[nMidIdx].is(), "lng : empty entry encountered");
2000-11-17 11:37:46 +00:00
int nCmp = - cmpDicEntry( aEntries[nMidIdx]->getDictionaryWord(),
2000-11-17 11:37:46 +00:00
rWord, bSimilarOnly );
if(nCmp == 0)
{
if( pPos ) *pPos = nMidIdx;
return true;
2000-11-17 11:37:46 +00:00
}
else if(nCmp > 0)
nLowerIdx = nMidIdx + 1;
else if( nMidIdx == 0 )
{
if( pPos ) *pPos = nLowerIdx;
return false;
2000-11-17 11:37:46 +00:00
}
else
nUpperIdx = nMidIdx - 1;
}
}
if( pPos ) *pPos = nLowerIdx;
return false;
2000-11-17 11:37:46 +00:00
}
bool DictionaryNeo::isSorted()
2000-11-17 11:37:46 +00:00
{
bool bRes = true;
2000-11-17 11:37:46 +00:00
sal_Int32 nEntries = getCount();
sal_Int32 i;
2000-11-17 11:37:46 +00:00
for (i = 1; i < nEntries; i++)
{
if (cmpDicEntry( aEntries[i-1]->getDictionaryWord(),
aEntries[i]->getDictionaryWord() ) > 0)
2000-11-17 11:37:46 +00:00
{
bRes = false;
2000-11-17 11:37:46 +00:00
break;
}
}
return bRes;
}
bool DictionaryNeo::addEntry_Impl(const uno::Reference< XDictionaryEntry >& xDicEntry,
bool bIsLoadEntries)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
bool bRes = false;
2000-11-17 11:37:46 +00:00
if ( bIsLoadEntries || (!bIsReadonly && xDicEntry.is()) )
{
bool bIsNegEntry = xDicEntry->isNegative();
bool bAddEntry = !isFull() &&
2000-11-17 11:37:46 +00:00
( ( eDicType == DictionaryType_POSITIVE && !bIsNegEntry )
|| ( eDicType == DictionaryType_NEGATIVE && bIsNegEntry )
|| ( eDicType == DictionaryType_MIXED ) );
// look for position to insert entry at
// if there is already an entry do not insert the new one
sal_Int32 nPos = 0;
2000-11-17 11:37:46 +00:00
if (bAddEntry)
{
const bool bFound = seekEntry( xDicEntry->getDictionaryWord(), &nPos );
2000-11-17 11:37:46 +00:00
if (bFound)
bAddEntry = false;
2000-11-17 11:37:46 +00:00
}
if (bAddEntry)
{
DBG_ASSERT(!bNeedEntries, "lng : entries still not loaded");
2000-11-17 11:37:46 +00:00
// insert new entry at specified position
aEntries.insert(aEntries.begin() + nPos, xDicEntry);
SAL_WARN_IF(!isSorted(), "linguistic", "dictionary entries unsorted");
2000-11-17 11:37:46 +00:00
bIsModified = true;
bRes = true;
2000-11-17 11:37:46 +00:00
if (!bIsLoadEntries)
launchEvent( DictionaryEventFlags::ADD_ENTRY, xDicEntry );
}
}
return bRes;
}
OUString SAL_CALL DictionaryNeo::getName( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return aDicName;
}
void SAL_CALL DictionaryNeo::setName( const OUString& aName )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (aDicName != aName)
{
aDicName = aName;
launchEvent(DictionaryEventFlags::CHG_NAME, nullptr);
2000-11-17 11:37:46 +00:00
}
}
DictionaryType SAL_CALL DictionaryNeo::getDictionaryType( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return eDicType;
}
void SAL_CALL DictionaryNeo::setActive( sal_Bool bActivate )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (bIsActive != bool(bActivate))
2000-11-17 11:37:46 +00:00
{
bIsActive = bActivate;
sal_Int16 nEvent = bIsActive ?
2000-11-17 11:37:46 +00:00
DictionaryEventFlags::ACTIVATE_DIC : DictionaryEventFlags::DEACTIVATE_DIC;
// remove entries from memory if dictionary is deactivated
if (!bIsActive)
2000-11-17 11:37:46 +00:00
{
bool bIsEmpty = aEntries.empty();
2000-11-17 11:37:46 +00:00
// save entries first if necessary
if (bIsModified && hasLocation() && !isReadonly())
{
store();
aEntries.clear();
2000-11-17 11:37:46 +00:00
bNeedEntries = !bIsEmpty;
}
DBG_ASSERT( !bIsModified || !hasLocation() || isReadonly(),
"lng : dictionary is still modified" );
}
launchEvent(nEvent, nullptr);
2000-11-17 11:37:46 +00:00
}
}
sal_Bool SAL_CALL DictionaryNeo::isActive( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return bIsActive;
}
2008-12-15 12:01:46 +00:00
sal_Int32 SAL_CALL DictionaryNeo::getCount( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (bNeedEntries)
loadEntries( aMainURL );
return (sal_Int32)aEntries.size();
2000-11-17 11:37:46 +00:00
}
Locale SAL_CALL DictionaryNeo::getLocale( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return LanguageTag::convertToLocale( nLanguage );
2000-11-17 11:37:46 +00:00
}
void SAL_CALL DictionaryNeo::setLocale( const Locale& aLocale )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
sal_Int16 nLanguageP = LinguLocaleToLanguage( aLocale );
2000-11-17 11:37:46 +00:00
if (!bIsReadonly && nLanguage != nLanguageP)
{
nLanguage = nLanguageP;
bIsModified = true; // new language needs to be saved with dictionary
2000-11-17 11:37:46 +00:00
launchEvent( DictionaryEventFlags::CHG_LANGUAGE, nullptr );
2000-11-17 11:37:46 +00:00
}
}
2007-06-07 13:21:55 +00:00
uno::Reference< XDictionaryEntry > SAL_CALL DictionaryNeo::getEntry(
2000-11-17 11:37:46 +00:00
const OUString& aWord )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (bNeedEntries)
loadEntries( aMainURL );
sal_Int32 nPos;
bool bFound = seekEntry( aWord, &nPos, true );
DBG_ASSERT(!bFound || nPos < (sal_Int32)aEntries.size(), "lng : index out of range");
2000-11-17 11:37:46 +00:00
return bFound ? aEntries[ nPos ]
2007-06-07 13:21:55 +00:00
: uno::Reference< XDictionaryEntry >();
2000-11-17 11:37:46 +00:00
}
sal_Bool SAL_CALL DictionaryNeo::addEntry(
2007-06-07 13:21:55 +00:00
const uno::Reference< XDictionaryEntry >& xDicEntry )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
bool bRes = false;
2000-11-17 11:37:46 +00:00
if (!bIsReadonly)
{
if (bNeedEntries)
loadEntries( aMainURL );
bRes = addEntry_Impl( xDicEntry );
}
return bRes;
}
sal_Bool SAL_CALL
DictionaryNeo::add( const OUString& rWord, sal_Bool bIsNegative,
const OUString& rRplcText )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
bool bRes = false;
2000-11-17 11:37:46 +00:00
if (!bIsReadonly)
{
2007-06-07 13:21:55 +00:00
uno::Reference< XDictionaryEntry > xEntry =
2000-11-17 11:37:46 +00:00
new DicEntry( rWord, bIsNegative, rRplcText );
bRes = addEntry_Impl( xEntry );
}
return bRes;
}
sal_Bool SAL_CALL DictionaryNeo::remove( const OUString& aWord )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
bool bRemoved = false;
2000-11-17 11:37:46 +00:00
if (!bIsReadonly)
{
if (bNeedEntries)
loadEntries( aMainURL );
sal_Int32 nPos;
bool bFound = seekEntry( aWord, &nPos );
DBG_ASSERT(!bFound || nPos < (sal_Int32)aEntries.size(), "lng : index out of range");
2000-11-17 11:37:46 +00:00
// remove element if found
if (bFound)
{
// entry to be removed
2007-06-07 13:21:55 +00:00
uno::Reference< XDictionaryEntry >
xDicEntry( aEntries[ nPos ] );
DBG_ASSERT(xDicEntry.is(), "lng : dictionary entry is NULL");
2000-11-17 11:37:46 +00:00
aEntries.erase(aEntries.begin() + nPos);
2000-11-17 11:37:46 +00:00
bRemoved = bIsModified = true;
2000-11-17 11:37:46 +00:00
launchEvent( DictionaryEventFlags::DEL_ENTRY, xDicEntry );
}
}
return bRemoved;
}
sal_Bool SAL_CALL DictionaryNeo::isFull( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (bNeedEntries)
loadEntries( aMainURL );
return aEntries.size() >= DIC_MAX_ENTRIES;
2000-11-17 11:37:46 +00:00
}
2007-06-07 13:21:55 +00:00
uno::Sequence< uno::Reference< XDictionaryEntry > >
2000-11-17 11:37:46 +00:00
SAL_CALL DictionaryNeo::getEntries( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (bNeedEntries)
loadEntries( aMainURL );
return comphelper::containerToSequence(aEntries);
2000-11-17 11:37:46 +00:00
}
void SAL_CALL DictionaryNeo::clear( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (!bIsReadonly && !aEntries.empty())
2000-11-17 11:37:46 +00:00
{
// release all references to old entries
aEntries.clear();
2000-11-17 11:37:46 +00:00
bNeedEntries = false;
bIsModified = true;
2000-11-17 11:37:46 +00:00
launchEvent( DictionaryEventFlags::ENTRIES_CLEARED , nullptr );
2000-11-17 11:37:46 +00:00
}
}
sal_Bool SAL_CALL DictionaryNeo::addDictionaryEventListener(
2007-06-07 13:21:55 +00:00
const uno::Reference< XDictionaryEventListener >& xListener )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
bool bRes = false;
2000-11-17 11:37:46 +00:00
if (xListener.is())
{
sal_Int32 nLen = aDicEvtListeners.getLength();
bRes = aDicEvtListeners.addInterface( xListener ) != nLen;
2000-11-17 11:37:46 +00:00
}
return bRes;
}
sal_Bool SAL_CALL DictionaryNeo::removeDictionaryEventListener(
2007-06-07 13:21:55 +00:00
const uno::Reference< XDictionaryEventListener >& xListener )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
bool bRes = false;
2000-11-17 11:37:46 +00:00
if (xListener.is())
{
sal_Int32 nLen = aDicEvtListeners.getLength();
bRes = aDicEvtListeners.removeInterface( xListener ) != nLen;
2000-11-17 11:37:46 +00:00
}
return bRes;
}
sal_Bool SAL_CALL DictionaryNeo::hasLocation()
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return !aMainURL.isEmpty();
2000-11-17 11:37:46 +00:00
}
OUString SAL_CALL DictionaryNeo::getLocation()
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return aMainURL;
}
sal_Bool SAL_CALL DictionaryNeo::isReadonly()
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return bIsReadonly;
}
void SAL_CALL DictionaryNeo::store()
throw(io::IOException, RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (bIsModified && hasLocation() && !isReadonly())
{
if (!saveEntries( aMainURL ))
bIsModified = false;
2000-11-17 11:37:46 +00:00
}
}
void SAL_CALL DictionaryNeo::storeAsURL(
const OUString& aURL,
const uno::Sequence< beans::PropertyValue >& /*rArgs*/ )
throw(io::IOException, RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
if (!saveEntries( aURL ))
2000-11-17 11:37:46 +00:00
{
aMainURL = aURL;
bIsModified = false;
bIsReadonly = IsReadOnly( getLocation() );
2000-11-17 11:37:46 +00:00
}
}
void SAL_CALL DictionaryNeo::storeToURL(
const OUString& aURL,
const uno::Sequence< beans::PropertyValue >& /*rArgs*/ )
throw(io::IOException, RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
saveEntries(aURL);
2000-11-17 11:37:46 +00:00
}
DicEntry::DicEntry(const OUString &rDicFileWord,
bool bIsNegativWord)
2000-11-17 11:37:46 +00:00
{
if (!rDicFileWord.isEmpty())
2000-11-17 11:37:46 +00:00
splitDicFileWord( rDicFileWord, aDicWord, aReplacement );
bIsNegativ = bIsNegativWord;
}
DicEntry::DicEntry(const OUString &rDicWord, bool bNegativ,
2000-11-17 11:37:46 +00:00
const OUString &rRplcText) :
aDicWord (rDicWord),
aReplacement (rRplcText),
bIsNegativ (bNegativ)
2000-11-17 11:37:46 +00:00
{
}
DicEntry::~DicEntry()
{
}
void DicEntry::splitDicFileWord(const OUString &rDicFileWord,
OUString &rDicWord,
OUString &rReplacement)
{
MutexGuard aGuard( GetLinguMutex() );
static const char aDelim[] = "==";
2000-11-17 11:37:46 +00:00
sal_Int32 nDelimPos = rDicFileWord.indexOf( aDelim );
if (-1 != nDelimPos)
{
sal_Int32 nTriplePos = nDelimPos + 2;
2000-11-17 11:37:46 +00:00
if ( nTriplePos < rDicFileWord.getLength()
&& rDicFileWord[ nTriplePos ] == '=' )
++nDelimPos;
rDicWord = rDicFileWord.copy( 0, nDelimPos );
rReplacement = rDicFileWord.copy( nDelimPos + 2 );
}
else
{
rDicWord = rDicFileWord;
rReplacement.clear();
2000-11-17 11:37:46 +00:00
}
}
OUString SAL_CALL DicEntry::getDictionaryWord( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return aDicWord;
}
sal_Bool SAL_CALL DicEntry::isNegative( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return bIsNegativ;
}
OUString SAL_CALL DicEntry::getReplacementText( )
throw(RuntimeException, std::exception)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
return aReplacement;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */