diff options
Diffstat (limited to 'lingucomponent/source')
32 files changed, 6578 insertions, 0 deletions
diff --git a/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/exports.dxp b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/exports.dxp new file mode 100644 index 000000000000..b0f85bf7bebf --- /dev/null +++ b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/exports.dxp @@ -0,0 +1,3 @@ +component_getFactory +component_getImplementationEnvironment +component_writeInfo diff --git a/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hreg.cxx b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hreg.cxx new file mode 100644 index 000000000000..448ecc529bf0 --- /dev/null +++ b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hreg.cxx @@ -0,0 +1,86 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + + + +#include <cppuhelper/factory.hxx> // helper for factories +#include <rtl/string.hxx> + +#include <com/sun/star/registry/XRegistryKey.hpp> + +using namespace com::sun::star::lang; +using namespace com::sun::star::registry; + +//////////////////////////////////////// +// declaration of external RegEntry-functions defined by the service objects +// + +extern sal_Bool SAL_CALL Hyphenator_writeInfo( + void * /*pServiceManager*/, XRegistryKey * pRegistryKey ); + +extern void * SAL_CALL Hyphenator_getFactory( + const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, + void * /*pRegistryKey*/ ); + +//////////////////////////////////////// +// definition of the two functions that are used to provide the services +// + +extern "C" +{ + +sal_Bool SAL_CALL component_writeInfo( + void * pServiceManager, XRegistryKey * pRegistryKey ) +{ + return Hyphenator_writeInfo( pServiceManager, pRegistryKey ); +} + +void SAL_CALL component_getImplementationEnvironment( + const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ ) +{ + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} + +void * SAL_CALL component_getFactory( + const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) +{ + void * pRet = Hyphenator_getFactory( + pImplName, + reinterpret_cast< XMultiServiceFactory * >( pServiceManager ), + pRegistryKey ); + + return pRet; +} + +} + +/////////////////////////////////////////////////////////////////////////// + diff --git a/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hyphenimp.cxx b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hyphenimp.cxx new file mode 100755 index 000000000000..3b0871cc36b6 --- /dev/null +++ b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hyphenimp.cxx @@ -0,0 +1,982 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + + +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp> + +//#include <com/sun/star/linguistic2/SpellFailure.hpp> +#include <cppuhelper/factory.hxx> // helper for factories +#include <com/sun/star/registry/XRegistryKey.hpp> +#include <i18npool/mslangid.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/useroptions.hxx> +#include <tools/debug.hxx> +#include <unotools/processfactory.hxx> +#include <osl/mutex.hxx> + +#include <hyphen.h> + +#ifndef _HYPHENIMP_HXX +#include <hyphenimp.hxx> +#endif + +#include <linguistic/hyphdta.hxx> +#include <rtl/ustring.hxx> + +#include <rtl/ustrbuf.hxx> + +#include <linguistic/lngprops.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/useroptions.hxx> +#include <unotools/lingucfg.hxx> +#include <osl/file.hxx> + +#include "dictmgr.hxx" + +#include <stdio.h> +#include <string.h> + +#include <list> +#include <set> + +using namespace utl; +using namespace osl; +using namespace rtl; +using namespace com::sun::star; +using namespace com::sun::star::beans; +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace com::sun::star::linguistic2; +using namespace linguistic; + +// values asigned to capitalization types +#define CAPTYPE_UNKNOWN 0 +#define CAPTYPE_NOCAP 1 +#define CAPTYPE_INITCAP 2 +#define CAPTYPE_ALLCAP 3 +#define CAPTYPE_MIXED 4 + +// min, max + +//#define Min(a,b) (a < b ? a : b) +#define Max(a,b) (a > b ? a : b) + +/////////////////////////////////////////////////////////////////////////// + + +Hyphenator::Hyphenator() : + aEvtListeners ( GetLinguMutex() ) +{ + bDisposing = FALSE; + pPropHelper = NULL; + aDicts = NULL; + numdict = 0; + +} + + +Hyphenator::~Hyphenator() +{ + if (pPropHelper) + pPropHelper->RemoveAsPropListener(); + + if ((numdict) && (aDicts)) { + for (int i=0; i < numdict; i++) { + if (aDicts[i].apCC) delete aDicts[i].apCC; + aDicts[i].apCC = NULL; + } + } + if (aDicts) delete[] aDicts; + aDicts = NULL; + numdict = 0; +} + + +PropertyHelper_Hyphen & Hyphenator::GetPropHelper_Impl() +{ + + if (!pPropHelper) + { + Reference< XPropertySet > xPropSet( GetLinguProperties(), UNO_QUERY ); + + pPropHelper = new PropertyHelper_Hyphen ((XHyphenator *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + return *pPropHelper; + +} + + +Sequence< Locale > SAL_CALL Hyphenator::getLocales() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + // this routine should return the locales supported by the installed + // dictionaries. + + if (!numdict) + { + SvtLinguConfig aLinguCfg; + + // get list of dictionaries-to-use + // (or better speaking: the list of dictionaries using the + // new configuration entries). + std::list< SvtLinguConfigDictionaryEntry > aDics; + uno::Sequence< rtl::OUString > aFormatList; + aLinguCfg.GetSupportedDictionaryFormatsFor( A2OU("Hyphenators"), + A2OU("org.openoffice.lingu.LibHnjHyphenator"), aFormatList ); + sal_Int32 nLen = aFormatList.getLength(); + for (sal_Int32 i = 0; i < nLen; ++i) + { + std::vector< SvtLinguConfigDictionaryEntry > aTmpDic( + aLinguCfg.GetActiveDictionariesByFormat( aFormatList[i] ) ); + aDics.insert( aDics.end(), aTmpDic.begin(), aTmpDic.end() ); + } + + //!! for compatibility with old dictionaries (the ones not using extensions + //!! or new configuration entries, but still using the dictionary.lst file) + //!! Get the list of old style spell checking dictionaries to use... + std::vector< SvtLinguConfigDictionaryEntry > aOldStyleDics( + GetOldStyleDics( "HYPH" ) ); + + // to prefer dictionaries with configuration entries we will only + // use those old style dictionaries that add a language that + // is not yet supported by the list od new style dictionaries + MergeNewStyleDicsAndOldStyleDics( aDics, aOldStyleDics ); + + numdict = aDics.size(); + if (numdict) + { + // get supported locales from the dictionaries-to-use... + sal_Int32 k = 0; + std::set< rtl::OUString, lt_rtl_OUString > aLocaleNamesSet; + std::list< SvtLinguConfigDictionaryEntry >::const_iterator aDictIt; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + { + uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames ); + sal_Int32 nLen2 = aLocaleNames.getLength(); + for (k = 0; k < nLen2; ++k) + { + aLocaleNamesSet.insert( aLocaleNames[k] ); + } + } + // ... and add them to the resulting sequence + aSuppLocales.realloc( aLocaleNamesSet.size() ); + std::set< rtl::OUString, lt_rtl_OUString >::const_iterator aItB; + k = 0; + for (aItB = aLocaleNamesSet.begin(); aItB != aLocaleNamesSet.end(); ++aItB) + { + Locale aTmp( MsLangId::convertLanguageToLocale( + MsLangId::convertIsoStringToLanguage( *aItB ))); + aSuppLocales[k++] = aTmp; + } + + //! For each dictionary and each locale we need a seperate entry. + //! If this results in more than one dictionary per locale than (for now) + //! it is undefined which dictionary gets used. + //! In the future the implementation should support using several dictionaries + //! for one locale. + numdict = 0; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + numdict = numdict + aDictIt->aLocaleNames.getLength(); + + // add dictionary information + aDicts = new HDInfo[numdict]; +/* + aTEncs = new rtl_TextEncoding [numdict]; + aTLocs = new Locale [numdict]; + aTNames = new OUString [numdict]; + aCharSetInfo = new CharClass* [numdict]; +*/ + k = 0; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + { + if (aDictIt->aLocaleNames.getLength() > 0 && + aDictIt->aLocations.getLength() > 0) + { + uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames ); + sal_Int32 nLocales = aLocaleNames.getLength(); + + // currently only one language per dictionary is supported in the actual implementation... + // Thus here we work-around this by adding the same dictionary several times. + // Once for each of it's supported locales. + for (sal_Int32 i = 0; i < nLocales; ++i) + { + aDicts[k].aPtr = NULL; + aDicts[k].aEnc = 0; + aDicts[k].aLoc = MsLangId::convertLanguageToLocale( + MsLangId::convertIsoStringToLanguage( aDictIt->aLocaleNames[i] )); + aDicts[k].apCC = new CharClass( aDicts[k].aLoc ); + // also both files have to be in the same directory and the + // file names must only differ in the extension (.aff/.dic). + // Thus we use the first location only and strip the extension part. + rtl::OUString aLocation = aDictIt->aLocations[0]; + sal_Int32 nPos = aLocation.lastIndexOf( '.' ); + aLocation = aLocation.copy( 0, nPos ); + aDicts[k].aName = aLocation; + + ++k; + } + } + } + DBG_ASSERT( k == numdict, "index mismatch?" ); + } + else + { + /* no dictionary found so register no dictionaries */ + numdict = 0; + aDicts = NULL; + aSuppLocales.realloc(0); + } + } + + return aSuppLocales; +} + + + +sal_Bool SAL_CALL Hyphenator::hasLocale(const Locale& rLocale) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!aSuppLocales.getLength()) + getLocales(); + + const Locale *pLocale = aSuppLocales.getConstArray(); + INT32 nLen = aSuppLocales.getLength(); + for (INT32 i = 0; i < nLen; ++i) + { + if (rLocale == pLocale[i]) + { + bRes = TRUE; + break; + } + } + return bRes; +} + + +Reference< XHyphenatedWord > SAL_CALL +Hyphenator::hyphenate( const ::rtl::OUString& aWord, + const ::com::sun::star::lang::Locale& aLocale, + sal_Int16 nMaxLeading, + const ::com::sun::star::beans::PropertyValues& aProperties ) + throw (com::sun::star::uno::RuntimeException, + com::sun::star::lang::IllegalArgumentException) +{ + int nHyphenationPos = -1; + int nHyphenationPosAlt = -1; + int nHyphenationPosAltHyph = -1; + int wordlen; + char *hyphens; + char *lcword; + int k = 0; + + PropertyHelper_Hyphen & rHelper = GetPropHelper(); + rHelper.SetTmpPropVals(aProperties); + sal_Int16 minTrail = rHelper.GetMinTrailing(); + sal_Int16 minLead = rHelper.GetMinLeading(); + sal_Int16 minLen = rHelper.GetMinWordLength(); + + HyphenDict *dict = NULL; + rtl_TextEncoding aEnc = 0; + CharClass * pCC = NULL; + + Reference< XHyphenatedWord > xRes; + + k = -1; + for (int j = 0; j < numdict; j++) + if (aLocale == aDicts[j].aLoc) k = j; + + + // if we have a hyphenation dictionary matching this locale + if (k != -1) { + + // if this dictinary has not been loaded yet do that + if (!aDicts[k].aPtr) { + + OUString DictFN = aDicts[k].aName + A2OU(".dic"); + OUString dictpath; + + osl::FileBase::getSystemPathFromFileURL( DictFN, dictpath ); + OString sTmp( OU2ENC( dictpath, osl_getThreadTextEncoding() ) ); + +#if defined(WNT) + // workaround for Windows specifc problem that the + // path length in calls to 'fopen' is limted to somewhat + // about 120+ characters which will usually be exceed when + // using dictionaries as extensions. + sTmp = Win_GetShortPathName( dictpath ); +#endif + + if ( ( dict = hnj_hyphen_load ( sTmp.getStr()) ) == NULL ) + { + fprintf(stderr, "Couldn't find file %s\n", OU2ENC(dictpath, osl_getThreadTextEncoding()) ); + return NULL; + } + aDicts[k].aPtr = dict; + aDicts[k].aEnc = rtl_getTextEncodingFromUnixCharset(dict->cset); + if (aDicts[k].aEnc == RTL_TEXTENCODING_DONTKNOW) { + if (strcmp("ISCII-DEVANAGARI", dict->cset) == 0) { + aDicts[k].aEnc = RTL_TEXTENCODING_ISCII_DEVANAGARI; + } else if (strcmp("UTF-8", dict->cset) == 0) { + aDicts[k].aEnc = RTL_TEXTENCODING_UTF8; + } + } + } + + // other wise hyphenate the word with that dictionary + dict = aDicts[k].aPtr; + aEnc = aDicts[k].aEnc; + pCC = aDicts[k].apCC; + + sal_uInt16 ct = CAPTYPE_UNKNOWN; + ct = capitalType(aWord, pCC); + + // first convert any smart quotes or apostrophes to normal ones + OUStringBuffer rBuf(aWord); + sal_Int32 nc = rBuf.getLength(); + sal_Unicode ch; + for (sal_Int32 ix=0; ix < nc; ix++) { + ch = rBuf.charAt(ix); + if ((ch == 0x201C) || (ch == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022); + if ((ch == 0x2018) || (ch == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027); + } + OUString nWord(rBuf.makeStringAndClear()); + + // now convert word to all lowercase for pattern recognition + OUString nTerm(makeLowerCase(nWord, pCC)); + + // now convert word to needed encoding + OString encWord(OU2ENC(nTerm,aEnc)); + + wordlen = encWord.getLength(); + lcword = new char[wordlen + 1]; + hyphens = new char[wordlen + 5]; + + char ** rep = NULL; // replacements of discretionary hyphenation + int * pos = NULL; // array of [hyphenation point] minus [deletion position] + int * cut = NULL; // length of deletions in original word + + // copy converted word into simple char buffer + strcpy(lcword,encWord.getStr()); + + // now strip off any ending periods + int n = wordlen-1; + while((n >=0) && (lcword[n] == '.')) n--; + n++; + if (n > 0) { + if (hnj_hyphen_hyphenate3(dict, lcword, n, hyphens, NULL, &rep, &pos, &cut, + minLead, minTrail, Max(dict->clhmin, Max(dict->clhmin, 2) + Max(0, minLead - Max(dict->lhmin, 2))), + Max(dict->crhmin, Max(dict->crhmin, 2) + Max(0, minTrail - Max(dict->rhmin, 2))))) + { + //whoops something did not work + delete[] hyphens; + delete[] lcword; + if (rep) { + for(int j = 0; j < n; j++) { + if (rep[j]) free(rep[j]); + } + free(rep); + } + if (pos) free(pos); + if (cut) free(cut); + return NULL; + } + } + + // now backfill hyphens[] for any removed trailing periods + for (int c = n; c < wordlen; c++) hyphens[c] = '0'; + hyphens[wordlen] = '\0'; + + INT32 Leading = GetPosInWordToCheck( aWord, nMaxLeading ); + + for (INT32 i = 0; i < n; i++) + { + int leftrep = 0; + BOOL hit = (n >= minLen); + if (!rep || !rep[i] || (i >= n)) { + hit = hit && (hyphens[i]&1) && (i < Leading); + hit = hit && (i >= (minLead-1) ); + hit = hit && ((n - i - 1) >= minTrail); + } else { + // calculate change character length before hyphenation point signed with '=' + for (char * c = rep[i]; *c && (*c != '='); c++) { + if (aEnc == RTL_TEXTENCODING_UTF8) { + if (((unsigned char) *c) >> 6 != 2) leftrep++; + } else leftrep++; + } + hit = hit && (hyphens[i]&1) && ((i + leftrep - pos[i]) < Leading); + hit = hit && ((i + leftrep - pos[i]) >= (minLead-1) ); + hit = hit && ((n - i - 1 + sal::static_int_cast< sal_sSize >(strlen(rep[i])) - leftrep - 1) >= minTrail); + } + if (hit) { + nHyphenationPos = i; + if (rep && (i < n) && rep[i]) { + nHyphenationPosAlt = i - pos[i]; + nHyphenationPosAltHyph = i + leftrep - pos[i]; + } + } + } + + if (nHyphenationPos == -1) { + xRes = NULL; + } else { + if (rep && rep[nHyphenationPos]) { + // remove equal sign + char * s = rep[nHyphenationPos]; + int eq = 0; + for (; *s; s++) { + if (*s == '=') eq = 1; + if (eq) *s = *(s + 1); + } + OUString repHyphlow(rep[nHyphenationPos], strlen(rep[nHyphenationPos]), aEnc); + OUString repHyph; + switch (ct) { + case CAPTYPE_ALLCAP: + { + repHyph = makeUpperCase(repHyphlow, pCC); + break; + } + case CAPTYPE_INITCAP: + { + if (nHyphenationPosAlt == 0) { + repHyph = makeInitCap(repHyphlow, pCC); + } else { + repHyph = repHyphlow; + } + break; + } + default: + { + repHyph = repHyphlow; + break; + } + } + + // handle shortening + INT16 nPos = (INT16) ((nHyphenationPosAltHyph < nHyphenationPos) ? + nHyphenationPosAltHyph : nHyphenationPos); + // dicretionary hyphenation + xRes = new HyphenatedWord( aWord, LocaleToLanguage( aLocale ), nPos, + aWord.replaceAt(nHyphenationPosAlt + 1, cut[nHyphenationPos], repHyph), + (INT16) nHyphenationPosAltHyph); + } else { + xRes = new HyphenatedWord( aWord, LocaleToLanguage( aLocale ), + (INT16)nHyphenationPos, aWord, (INT16) nHyphenationPos); + } + } + + delete[] lcword; + delete[] hyphens; + if (rep) { + for(int j = 0; j < n; j++) { + if (rep[j]) free(rep[j]); + } + free(rep); + } + if (pos) free(pos); + if (cut) free(cut); + return xRes; + } + return NULL; +} + + +Reference < XHyphenatedWord > SAL_CALL + Hyphenator::queryAlternativeSpelling( const ::rtl::OUString& /*aWord*/, + const ::com::sun::star::lang::Locale& /*aLocale*/, + sal_Int16 /*nIndex*/, + const ::com::sun::star::beans::PropertyValues& /*aProperties*/ ) + throw(::com::sun::star::lang::IllegalArgumentException, + ::com::sun::star::uno::RuntimeException) +{ + /* alternative spelling isn't supported by tex dictionaries */ + /* XXX: OOo's extended libhjn algorithm can support alternative spellings with extended TeX dic. */ + /* TASK: implement queryAlternativeSpelling() */ + return NULL; +} + +Reference< XPossibleHyphens > SAL_CALL + Hyphenator::createPossibleHyphens( const ::rtl::OUString& aWord, + const ::com::sun::star::lang::Locale& aLocale, + const ::com::sun::star::beans::PropertyValues& aProperties ) + throw(::com::sun::star::lang::IllegalArgumentException, + ::com::sun::star::uno::RuntimeException) + +{ + int wordlen; + char *hyphens; + char *lcword; + int k; + + PropertyHelper_Hyphen & rHelper = GetPropHelper(); + rHelper.SetTmpPropVals(aProperties); + sal_Int16 minTrail = rHelper.GetMinTrailing(); + sal_Int16 minLead = rHelper.GetMinLeading(); + + HyphenDict *dict = NULL; + rtl_TextEncoding aEnc = 0; + CharClass* pCC = NULL; + + Reference< XPossibleHyphens > xRes; + + k = -1; + for (int j = 0; j < numdict; j++) + if (aLocale == aDicts[j].aLoc) k = j; + + + // if we have a hyphenation dictionary matching this locale + if (k != -1) { + + // if this dictioanry has not been loaded yet do that + if (!aDicts[k].aPtr) { + + OUString DictFN = aDicts[k].aName + A2OU(".dic"); + OUString dictpath; + + osl::FileBase::getSystemPathFromFileURL( DictFN, dictpath ); + OString sTmp( OU2ENC( dictpath, osl_getThreadTextEncoding() ) ); + +#if defined(WNT) + // workaround for Windows specifc problem that the + // path length in calls to 'fopen' is limted to somewhat + // about 120+ characters which will usually be exceed when + // using dictionaries as extensions. + sTmp = Win_GetShortPathName( dictpath ); +#endif + + if ( ( dict = hnj_hyphen_load ( sTmp.getStr()) ) == NULL ) + { + fprintf(stderr, "Couldn't find file %s and %s\n", sTmp.getStr(), OU2ENC(dictpath, osl_getThreadTextEncoding()) ); + return NULL; + } + aDicts[k].aPtr = dict; + aDicts[k].aEnc = rtl_getTextEncodingFromUnixCharset(dict->cset); + if (aDicts[k].aEnc == RTL_TEXTENCODING_DONTKNOW) { + if (strcmp("ISCII-DEVANAGARI", dict->cset) == 0) { + aDicts[k].aEnc = RTL_TEXTENCODING_ISCII_DEVANAGARI; + } else if (strcmp("UTF-8", dict->cset) == 0) { + aDicts[k].aEnc = RTL_TEXTENCODING_UTF8; + } + } + } + + // other wise hyphenate the word with that dictionary + dict = aDicts[k].aPtr; + aEnc = aDicts[k].aEnc; + pCC = aDicts[k].apCC; + + // first handle smart quotes both single and double + OUStringBuffer rBuf(aWord); + sal_Int32 nc = rBuf.getLength(); + sal_Unicode ch; + for (sal_Int32 ix=0; ix < nc; ix++) { + ch = rBuf.charAt(ix); + if ((ch == 0x201C) || (ch == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022); + if ((ch == 0x2018) || (ch == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027); + } + OUString nWord(rBuf.makeStringAndClear()); + + // now convert word to all lowercase for pattern recognition + OUString nTerm(makeLowerCase(nWord, pCC)); + + // now convert word to needed encoding + OString encWord(OU2ENC(nTerm,aEnc)); + + wordlen = encWord.getLength(); + lcword = new char[wordlen+1]; + hyphens = new char[wordlen+5]; + char ** rep = NULL; // replacements of discretionary hyphenation + int * pos = NULL; // array of [hyphenation point] minus [deletion position] + int * cut = NULL; // length of deletions in original word + + // copy converted word into simple char buffer + strcpy(lcword,encWord.getStr()); + + // first remove any trailing periods + int n = wordlen-1; + while((n >=0) && (lcword[n] == '.')) n--; + n++; + // fprintf(stderr,"hyphenate... %s\n",lcword); fflush(stderr); + if (n > 0) { + if (hnj_hyphen_hyphenate3(dict, lcword, n, hyphens, NULL, &rep, &pos, &cut, + minLead, minTrail, Max(dict->clhmin, Max(dict->clhmin, 2) + Max(0, minLead - Max(dict->lhmin, 2))), + Max(dict->crhmin, Max(dict->crhmin, 2) + Max(0, minTrail - Max(dict->rhmin, 2))))) + { + delete[] hyphens; + delete[] lcword; + + if (rep) { + for(int j = 0; j < n; j++) { + if (rep[j]) free(rep[j]); + } + free(rep); + } + if (pos) free(pos); + if (cut) free(cut); + + return NULL; + } + } + // now backfill hyphens[] for any removed periods + for (int c = n; c < wordlen; c++) hyphens[c] = '0'; + hyphens[wordlen] = '\0'; + // fprintf(stderr,"... %s\n",hyphens); fflush(stderr); + + INT16 nHyphCount = 0; + INT16 i; + + for ( i = 0; i < encWord.getLength(); i++) + if (hyphens[i]&1 && (!rep || !rep[i])) + nHyphCount++; + + Sequence< INT16 > aHyphPos(nHyphCount); + INT16 *pPos = aHyphPos.getArray(); + OUStringBuffer hyphenatedWordBuffer; + OUString hyphenatedWord; + nHyphCount = 0; + + for (i = 0; i < nWord.getLength(); i++) { + hyphenatedWordBuffer.append(aWord[i]); + // hyphenation position (not alternative) + if (hyphens[i]&1 && (!rep || !rep[i])) { + pPos[nHyphCount] = i; + hyphenatedWordBuffer.append(sal_Unicode('=')); + nHyphCount++; + } + } + + hyphenatedWord = hyphenatedWordBuffer.makeStringAndClear(); + //fprintf(stderr,"result is %s\n",OU2A(hyphenatedWord)); + //fflush(stderr); + + xRes = new PossibleHyphens( aWord, LocaleToLanguage( aLocale ), + hyphenatedWord, aHyphPos ); + + delete[] hyphens; + delete[] lcword; + + if (rep) { + for(int j = 0; j < n; j++) { + if (rep[j]) free(rep[j]); + } + free(rep); + } + if (pos) free(pos); + if (cut) free(cut); + + return xRes; + } + + return NULL; + +} + +sal_uInt16 SAL_CALL Hyphenator::capitalType(const OUString& aTerm, CharClass * pCC) +{ + sal_Int32 tlen = aTerm.getLength(); + if ((pCC) && (tlen)) { + String aStr(aTerm); + sal_Int32 nc = 0; + for (xub_StrLen tindex = 0; tindex < tlen; tindex++) { + if (pCC->getCharacterType(aStr,tindex) & + ::com::sun::star::i18n::KCharacterType::UPPER) nc++; + } + + if (nc == 0) return (sal_uInt16) CAPTYPE_NOCAP; + + if (nc == tlen) return (sal_uInt16) CAPTYPE_ALLCAP; + + if ((nc == 1) && (pCC->getCharacterType(aStr,0) & + ::com::sun::star::i18n::KCharacterType::UPPER)) + return (sal_uInt16) CAPTYPE_INITCAP; + + return (sal_uInt16) CAPTYPE_MIXED; + } + return (sal_uInt16) CAPTYPE_UNKNOWN; +} + +OUString SAL_CALL Hyphenator::makeLowerCase(const OUString& aTerm, CharClass * pCC) +{ + if (pCC) + return pCC->toLower_rtl(aTerm, 0, aTerm.getLength()); + return aTerm; +} + +OUString SAL_CALL Hyphenator::makeUpperCase(const OUString& aTerm, CharClass * pCC) +{ + if (pCC) + return pCC->toUpper_rtl(aTerm, 0, aTerm.getLength()); + return aTerm; +} + + +OUString SAL_CALL Hyphenator::makeInitCap(const OUString& aTerm, CharClass * pCC) +{ + sal_Int32 tlen = aTerm.getLength(); + if ((pCC) && (tlen)) { + OUString bTemp = aTerm.copy(0,1); + if (tlen > 1) + return ( pCC->toUpper_rtl(bTemp, 0, 1) + + pCC->toLower_rtl(aTerm,1,(tlen-1)) ); + + return pCC->toUpper_rtl(bTemp, 0, 1); + } + return aTerm; +} + + + + +Reference< XInterface > SAL_CALL Hyphenator_CreateInstance( + const Reference< XMultiServiceFactory > & /*rSMgr*/ ) + throw(Exception) +{ + + Reference< XInterface > xService = (cppu::OWeakObject*) new Hyphenator; + return xService; +} + + +sal_Bool SAL_CALL + Hyphenator::addLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!bDisposing && rxLstnr.is()) + { + bRes = GetPropHelper().addLinguServiceEventListener( rxLstnr ); + } + return bRes; +} + + +sal_Bool SAL_CALL + Hyphenator::removeLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!bDisposing && rxLstnr.is()) + { + DBG_ASSERT( xPropHelper.is(), "xPropHelper non existent" ); + bRes = GetPropHelper().removeLinguServiceEventListener( rxLstnr ); + } + return bRes; +} + + +OUString SAL_CALL + Hyphenator::getServiceDisplayName( const Locale& /*rLocale*/ ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return A2OU( "Libhyphen Hyphenator" ); +} + + +void SAL_CALL + Hyphenator::initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!pPropHelper) + { + INT32 nLen = rArguments.getLength(); + if (2 == nLen) + { + Reference< XPropertySet > xPropSet; + rArguments.getConstArray()[0] >>= xPropSet; + //rArguments.getConstArray()[1] >>= xDicList; + + //! Pointer allows for access of the non-UNO functions. + //! And the reference to the UNO-functions while increasing + //! the ref-count and will implicitly free the memory + //! when the object is not longer used. + pPropHelper = new PropertyHelper_Hyphen( (XHyphenator *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + else { + DBG_ERROR( "wrong number of arguments in sequence" ); + } + + } +} + + +void SAL_CALL + Hyphenator::dispose() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing) + { + bDisposing = TRUE; + EventObject aEvtObj( (XHyphenator *) this ); + aEvtListeners.disposeAndClear( aEvtObj ); + } +} + + +void SAL_CALL + Hyphenator::addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.addInterface( rxListener ); +} + + +void SAL_CALL + Hyphenator::removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.removeInterface( rxListener ); +} + + +/////////////////////////////////////////////////////////////////////////// +// Service specific part +// + +OUString SAL_CALL Hyphenator::getImplementationName() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + return getImplementationName_Static(); +} + + +sal_Bool SAL_CALL Hyphenator::supportsService( const OUString& ServiceName ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNL = getSupportedServiceNames(); + const OUString * pArray = aSNL.getConstArray(); + for( INT32 i = 0; i < aSNL.getLength(); i++ ) + if( pArray[i] == ServiceName ) + return TRUE; + return FALSE; +} + + +Sequence< OUString > SAL_CALL Hyphenator::getSupportedServiceNames() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + return getSupportedServiceNames_Static(); +} + + +Sequence< OUString > Hyphenator::getSupportedServiceNames_Static() + throw() +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNS( 1 ); // auch mehr als 1 Service moeglich + aSNS.getArray()[0] = A2OU( SN_HYPHENATOR ); + return aSNS; +} + + +sal_Bool SAL_CALL Hyphenator_writeInfo( + void * /*pServiceManager*/, registry::XRegistryKey * pRegistryKey ) +{ + + try + { + String aImpl( '/' ); + aImpl += Hyphenator::getImplementationName_Static().getStr(); + aImpl.AppendAscii( "/UNO/SERVICES" ); + Reference< registry::XRegistryKey > xNewKey = + pRegistryKey->createKey( aImpl ); + Sequence< OUString > aServices = + Hyphenator::getSupportedServiceNames_Static(); + for( INT32 i = 0; i < aServices.getLength(); i++ ) + xNewKey->createKey( aServices.getConstArray()[i] ); + + return sal_True; + } + catch(Exception &) + { + return sal_False; + } +} + + +void * SAL_CALL Hyphenator_getFactory( const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, void * ) +{ + void * pRet = 0; + if ( !Hyphenator::getImplementationName_Static().compareToAscii( pImplName ) ) + { + Reference< XSingleServiceFactory > xFactory = + cppu::createOneInstanceFactory( + pServiceManager, + Hyphenator::getImplementationName_Static(), + Hyphenator_CreateInstance, + Hyphenator::getSupportedServiceNames_Static()); + // acquire, because we return an interface pointer instead of a reference + xFactory->acquire(); + pRet = xFactory.get(); + } + return pRet; +} + + +/////////////////////////////////////////////////////////////////////////// + +#undef CAPTYPE_UNKNOWN +#undef CAPTYPE_NOCAP +#undef CAPTYPE_INITCAP +#undef CAPTYPE_ALLCAP +#undef CAPTYPE_MIXED diff --git a/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hyphenimp.hxx b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hyphenimp.hxx new file mode 100644 index 000000000000..031217914754 --- /dev/null +++ b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/hyphenimp.hxx @@ -0,0 +1,210 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + + +#ifndef _LINGU2_HYPHENIMP_HXX_ +#define _LINGU2_HYPHENIMP_HXX_ + +#include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type +#include <cppuhelper/implbase1.hxx> // helper for implementations +#include <cppuhelper/implbase6.hxx> // helper for implementations +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XServiceDisplayName.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/beans/PropertyValues.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/linguistic2/XHyphenator.hpp> +#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp> +#include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp> +#include <tools/table.hxx> + +#include <unotools/charclass.hxx> + +#include <linguistic/misc.hxx> +#include <linguistic/lngprophelp.hxx> + +#include <lingutil.hxx> +#include <stdio.h> + +using namespace ::rtl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::linguistic2; + + +/////////////////////////////////////////////////////////////////////////// + + +struct HDInfo { + HyphenDict * aPtr; + OUString aName; + Locale aLoc; + rtl_TextEncoding aEnc; + CharClass * apCC; +}; + + + +class Hyphenator : + public cppu::WeakImplHelper6 + < + XHyphenator, + XLinguServiceEventBroadcaster, + XInitialization, + XComponent, + XServiceInfo, + XServiceDisplayName + > +{ + Sequence< Locale > aSuppLocales; + HDInfo * aDicts; + sal_Int32 numdict; + + ::cppu::OInterfaceContainerHelper aEvtListeners; + Reference< XPropertyChangeListener > xPropHelper; + Reference< XMultiServiceFactory > rSMgr; + linguistic::PropertyHelper_Hyphen * pPropHelper; + BOOL bDisposing; + + // disallow copy-constructor and assignment-operator for now + Hyphenator(const Hyphenator &); + Hyphenator & operator = (const Hyphenator &); + + linguistic::PropertyHelper_Hyphen & GetPropHelper_Impl(); + linguistic::PropertyHelper_Hyphen & GetPropHelper() + { + return pPropHelper ? *pPropHelper : GetPropHelper_Impl(); + } + +public: + Hyphenator(); + + virtual ~Hyphenator(); + + // XSupportedLocales (for XHyphenator) + virtual Sequence< Locale > SAL_CALL getLocales() + throw(RuntimeException); + virtual sal_Bool SAL_CALL hasLocale( const Locale& rLocale ) + throw(RuntimeException); + + // XHyphenator + virtual ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XHyphenatedWord > SAL_CALL + hyphenate( const ::rtl::OUString& aWord, + const ::com::sun::star::lang::Locale& aLocale, + sal_Int16 nMaxLeading, + const ::com::sun::star::beans::PropertyValues& aProperties ) + throw(::com::sun::star::lang::IllegalArgumentException, + ::com::sun::star::uno::RuntimeException); + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XHyphenatedWord > SAL_CALL + queryAlternativeSpelling( const ::rtl::OUString& aWord, + const ::com::sun::star::lang::Locale& aLocale, + sal_Int16 nIndex, + const ::com::sun::star::beans::PropertyValues& aProperties ) + throw(::com::sun::star::lang::IllegalArgumentException, + ::com::sun::star::uno::RuntimeException); + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XPossibleHyphens > SAL_CALL + createPossibleHyphens( const ::rtl::OUString& aWord, + const ::com::sun::star::lang::Locale& aLocale, + const ::com::sun::star::beans::PropertyValues& aProperties ) + throw(::com::sun::star::lang::IllegalArgumentException, + ::com::sun::star::uno::RuntimeException); + + // XLinguServiceEventBroadcaster + virtual sal_Bool SAL_CALL + addLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException); + virtual sal_Bool SAL_CALL + removeLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException); + + // XServiceDisplayName + virtual OUString SAL_CALL + getServiceDisplayName( const Locale& rLocale ) + throw(RuntimeException); + + // XInitialization + virtual void SAL_CALL + initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException); + + // XComponent + virtual void SAL_CALL + dispose() + throw(RuntimeException); + virtual void SAL_CALL + addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + virtual void SAL_CALL + removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + + //////////////////////////////////////////////////////////// + // Service specific part + // + + // XServiceInfo + virtual OUString SAL_CALL + getImplementationName() + throw(RuntimeException); + virtual sal_Bool SAL_CALL + supportsService( const OUString& rServiceName ) + throw(RuntimeException); + virtual Sequence< OUString > SAL_CALL + getSupportedServiceNames() + throw(RuntimeException); + + + static inline OUString + getImplementationName_Static() throw(); + static Sequence< OUString > + getSupportedServiceNames_Static() throw(); + + + +private: + sal_uInt16 SAL_CALL capitalType(const OUString&, CharClass *); + OUString SAL_CALL makeLowerCase(const OUString&, CharClass *); + OUString SAL_CALL makeUpperCase(const OUString&, CharClass *); + OUString SAL_CALL makeInitCap(const OUString&, CharClass *); +}; + +inline OUString Hyphenator::getImplementationName_Static() throw() +{ + return A2OU( "org.openoffice.lingu.LibHnjHyphenator" ); +} + + +/////////////////////////////////////////////////////////////////////////// + +#endif + diff --git a/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/makefile.mk b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/makefile.mk new file mode 100644 index 000000000000..f3626c70312b --- /dev/null +++ b/lingucomponent/source/hyphenator/altlinuxhyph/hyphen/makefile.mk @@ -0,0 +1,93 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ = ..$/..$/..$/.. + +PRJNAME = lingucomponent +TARGET = hyphen_lib +ENABLE_EXCEPTIONS=TRUE +USE_DEFFILE=TRUE + +.IF "$(GUI)"=="OS2" +HNJLIB=hyphen.lib +.ELIF "$(GUI)"=="UNX" || "$(COM)"=="GCC" +HNJLIB=-lhyphen +.ELSE +HNJLIB=hyphen.lib +.ENDIF + +#----- Settings --------------------------------------------------------- + +.INCLUDE : settings.mk + +# --- Files -------------------------------------------------------- + +.IF "$(SYSTEM_HUNSPELL)" != "YES" +HUNSPELL_CFLAGS += -I$(SOLARINCDIR)$/hunspell +.ENDIF + +CFLAGS += -I..$/..$/..$/lingutil $(HUNSPELL_CFLAGS) + +EXCEPTIONSFILES= \ + $(SLO)$/hyphenimp.obj + +SLOFILES= \ + $(SLO)$/hreg.obj\ + $(SLO)$/hyphenimp.obj + +REALNAME:=hyphen +SHL1TARGET= $(REALNAME)$(DLLPOSTFIX) + +SHL1STDLIBS= \ + $(CPPULIB) \ + $(CPPUHELPERLIB) \ + $(I18NISOLANGLIB) \ + $(TOOLSLIB) \ + $(SVLLIB) \ + $(SALLIB) \ + $(UNOTOOLSLIB) \ + $(LNGLIB) \ + $(HNJLIB) \ + $(ULINGULIB) \ + $(HUNSPELLLIB) + +# build DLL +SHL1LIBS= $(SLB)$/$(TARGET).lib $(SLB)$/libulingu.lib +SHL1IMPLIB= i$(REALNAME) +SHL1DEPN= $(SHL1LIBS) +SHL1DEF= $(MISC)$/$(SHL1TARGET).def + +SHL1VERSIONMAP=$(SOLARENV)/src/component.map + +# build DEF file +DEF1NAME =$(SHL1TARGET) +DEF1EXPORTFILE= exports.dxp + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/lingucomponent/source/languageguessing/altstrfunc.cxx b/lingucomponent/source/languageguessing/altstrfunc.cxx new file mode 100644 index 000000000000..562a3797051b --- /dev/null +++ b/lingucomponent/source/languageguessing/altstrfunc.cxx @@ -0,0 +1,48 @@ +/*************************************************************************** + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" +#include "altstrfunc.hxx" + +#include <sal/types.h> + +int start(const std::string &s1, const std::string &s2){ + size_t i; + int ret = 0; + + size_t min = s1.length(); + if (min > s2.length()) + min = s2.length(); + + for(i = 0; i < min && s2[i] && s1[i] && !ret; i++){ + ret = toupper(s1[i]) - toupper(s2[i]); + if(s1[i] == '.' || s2[i] == '.'){ret = 0;}//. is a neutral character + } + return ret; +} + diff --git a/lingucomponent/source/languageguessing/altstrfunc.hxx b/lingucomponent/source/languageguessing/altstrfunc.hxx new file mode 100644 index 000000000000..b64b5b778da2 --- /dev/null +++ b/lingucomponent/source/languageguessing/altstrfunc.hxx @@ -0,0 +1,41 @@ +/*************************************************************************** + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ALT_STRFUNC_HXX_ +#define _ALT_STRFUNC_HXX_ + +#include <string> +#include <guess.hxx> + +inline bool isSeparator(const char c){ + return c == GUESS_SEPARATOR_OPEN || c == GUESS_SEPARATOR_SEP || c == GUESS_SEPARATOR_CLOSE || c == '\0'; +} + +int start(const std::string &s1, const std::string &s2); + +#endif + diff --git a/lingucomponent/source/languageguessing/guess.cxx b/lingucomponent/source/languageguessing/guess.cxx new file mode 100644 index 000000000000..85ddc881b7e1 --- /dev/null +++ b/lingucomponent/source/languageguessing/guess.cxx @@ -0,0 +1,136 @@ +/*************************************************************************** + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + +#include <iostream> +#include <string.h> + +#include <libtextcat/textcat.h> +#include <altstrfunc.hxx> +#include <guess.hxx> + +using namespace std; + +Guess::Guess() +{ + language_str = DEFAULT_LANGUAGE; + country_str = DEFAULT_COUNTRY; + encoding_str = DEFAULT_ENCODING; +} + +/* +* this use a char * string to build the guess object +* a string like those is made as : [language-country-encoding]... +* +*/ + +Guess::Guess(char * guess_str) +{ + Guess(); + + string lang; + string country; + string enc; + + //if the guess is not like "UNKNOWN" or "SHORT", go into the brackets +// if(strncmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_UNKOWN, strlen(_TEXTCAT_RESULT_UNKOWN)) != 0 +// && +// strncmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_SHORT, strlen(_TEXTCAT_RESULT_SHORT)) != 0) +// { + if(strcmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_UNKOWN) != 0 + && + strcmp((const char*)(guess_str + 1), _TEXTCAT_RESULT_SHORT) != 0) + { + + int current_pointer = 0; + + //this is to go to the first char of the guess string ( the '[' of "[en-US-utf8]" ) + while(!isSeparator(guess_str[current_pointer])){ + current_pointer++; + } + current_pointer++; + + //this is to pick up the language ( the "en" from "[en-US-utf8]" ) + while(!isSeparator(guess_str[current_pointer])){ + lang+=guess_str[current_pointer]; + current_pointer++; + } + current_pointer++; + + //this is to pick up the country ( the "US" from "[en-US-utf8]" ) + while(!isSeparator(guess_str[current_pointer])){ + country+=guess_str[current_pointer]; + current_pointer++; + } + current_pointer++; + + //this is to pick up the encoding ( the "utf8" from "[en-US-utf8]" ) + while(!isSeparator(guess_str[current_pointer])){ + enc+=guess_str[current_pointer]; + current_pointer++; + } + + if(lang!=""){//if not we use the default value + language_str=lang; + } + country_str=country; + + if(enc!=""){//if not we use the default value + encoding_str=enc; + } + } +} + +Guess::~Guess(){} + +string Guess::GetLanguage() +{ + return language_str; +} + +string Guess::GetCountry() +{ + return country_str; +} + +string Guess::GetEncoding() +{ + return encoding_str; +} + +bool Guess::operator==(string lang) +{ + string toString; + toString += GetLanguage(); + toString += "-"; + toString += GetCountry(); + toString += "-"; + toString += GetEncoding(); + return start(toString, lang); +} diff --git a/lingucomponent/source/languageguessing/guess.hxx b/lingucomponent/source/languageguessing/guess.hxx new file mode 100644 index 000000000000..1dd3d765eaed --- /dev/null +++ b/lingucomponent/source/languageguessing/guess.hxx @@ -0,0 +1,71 @@ +/*************************************************************************** + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#ifndef GUESS_H +#define GUESS_H + +#define GUESS_SEPARATOR_OPEN '[' +#define GUESS_SEPARATOR_CLOSE ']' +#define GUESS_SEPARATOR_SEP '-' +#define DEFAULT_LANGUAGE "" +#define DEFAULT_COUNTRY "" +#define DEFAULT_ENCODING "" + +#include <string> + +using namespace std; + +/** +@author Jocelyn Merand + */ +class Guess{ + public: + + /** + * Default init + */ + Guess(); + + /** + * Init from a string like [en-UK-utf8] and the rank + */ + Guess(char * guess_str); + + ~Guess(); + + string GetLanguage(); + string GetCountry(); + string GetEncoding(); + + bool operator==(string lang); + + protected: + string language_str; + string country_str; + string encoding_str; +}; + +#endif diff --git a/lingucomponent/source/languageguessing/guesslang.cxx b/lingucomponent/source/languageguessing/guesslang.cxx new file mode 100644 index 000000000000..c8486c1bc471 --- /dev/null +++ b/lingucomponent/source/languageguessing/guesslang.cxx @@ -0,0 +1,484 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + +#include <iostream> + +#include <tools/debug.hxx> + +#include <sal/config.h> +#include <cppuhelper/factory.hxx> +#include <cppuhelper/implementationentry.hxx> +#include <cppuhelper/implbase2.hxx> +#include <tools/string.hxx> + +#include <simpleguesser.hxx> +#include <guess.hxx> + +//#include <cppuhelper/queryinterface.hxx> // helper for queryInterface() impl + +//#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/registry/XRegistryKey.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/linguistic2/XLanguageGuessing.hpp> +#include <unotools/pathoptions.hxx> +#include <unotools/localfilehelper.hxx> +#include <osl/thread.h> + +using namespace ::rtl; +using namespace ::osl; +using namespace ::cppu; +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::linguistic2; + +namespace css = ::com::sun::star; + +//================================================================================================== + +#define A2OU(x) ::rtl::OUString::createFromAscii( x ) + +#define SERVICENAME "com.sun.star.linguistic2.LanguageGuessing" + +#define IMPLNAME "com.sun.star.lingu2.LanguageGuessing" + +static Sequence< OUString > getSupportedServiceNames_LangGuess_Impl() +{ + Sequence<OUString> names(1); + names[0] = A2OU( SERVICENAME ); + return names; +} + +static OUString getImplementationName_LangGuess_Impl() +{ + return A2OU( IMPLNAME ); +} + +static osl::Mutex & GetLangGuessMutex() +{ + static osl::Mutex aMutex; + return aMutex; +} + + +class LangGuess_Impl : + public ::cppu::WeakImplHelper2< + XLanguageGuessing, + XServiceInfo > +{ + SimpleGuesser m_aGuesser; + bool m_bInitialized; + css::uno::Reference< css::uno::XComponentContext > m_xContext; + + LangGuess_Impl( const LangGuess_Impl & ); // not defined + LangGuess_Impl & operator =( const LangGuess_Impl & ); // not defined + + virtual ~LangGuess_Impl() {} + void EnsureInitialized(); + +public: + explicit LangGuess_Impl(css::uno::Reference< css::uno::XComponentContext > const & rxContext); + + // XServiceInfo implementation + virtual OUString SAL_CALL getImplementationName( ) throw(RuntimeException); + virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException); + virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException); + static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static( ); + + // XLanguageGuessing implementation + virtual ::com::sun::star::lang::Locale SAL_CALL guessPrimaryLanguage( const ::rtl::OUString& aText, ::sal_Int32 nStartPos, ::sal_Int32 nLen ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL disableLanguages( const ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale >& aLanguages ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL enableLanguages( const ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale >& aLanguages ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getAvailableLanguages( ) throw (::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getEnabledLanguages( ) throw (::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > SAL_CALL getDisabledLanguages( ) throw (::com::sun::star::uno::RuntimeException); + + // implementation specific + void SetFingerPrintsDB( const rtl::OUString &fileName ) throw (RuntimeException); + + static const OUString & SAL_CALL getImplementationName_Static() throw(); + +}; + +//************************************************************************* + +LangGuess_Impl::LangGuess_Impl(css::uno::Reference< css::uno::XComponentContext > const & rxContext) : + m_bInitialized( false ), + m_xContext( rxContext ) +{ +} + +//************************************************************************* + +void LangGuess_Impl::EnsureInitialized() +{ + if (!m_bInitialized) + { + // set this to true at the very start to prevent loops because of + // implicitly called functions below + m_bInitialized = true; + + // set default fingerprint path to where those get installed + String aPhysPath; + String aURL( SvtPathOptions().GetFingerprintPath() ); + utl::LocalFileHelper::ConvertURLToPhysicalName( aURL, aPhysPath ); +#ifdef WNT + aPhysPath += '\\'; +#else + aPhysPath += '/'; +#endif + + SetFingerPrintsDB( aPhysPath ); + + // + // disable currently not functional languages... + // + struct LangCountry + { + const char *pLang; + const char *pCountry; + }; + LangCountry aDisable[] = + { + {"gv", ""}, {"sco", ""}, // no lang-id available yet... +// {"hy", ""}, {"drt", ""}, // 0 bytes fingerprints... + {"zh", "CN"}, {"zh", "TW"}, {"ja", ""}, {"ko", ""}, // not yet correct functional... + {"ka", ""}, {"hi", ""}, {"mr", ""}, {"ne", ""}, + {"sa", ""}, {"ta", ""}, {"th", ""}, + {"qu", ""}, {"yi", ""} + }; + sal_Int32 nNum = sizeof(aDisable) / sizeof(aDisable[0]); + Sequence< Locale > aDisableSeq( nNum ); + Locale *pDisableSeq = aDisableSeq.getArray(); + for (sal_Int32 i = 0; i < nNum; ++i) + { + Locale aLocale; + aLocale.Language = OUString::createFromAscii( aDisable[i].pLang ); + aLocale.Country = OUString::createFromAscii( aDisable[i].pCountry ); + pDisableSeq[i] = aLocale; + } + disableLanguages( aDisableSeq ); + DBG_ASSERT( nNum == getDisabledLanguages().getLength(), "size mismatch" ); + } +} + +//************************************************************************* + +/* TL: currently not part of the API +Sequence< com::sun::star::lang::Locale > SAL_CALL LangGuess_Impl::guessLanguages( + const rtl::OUString &rText, + sal_Int32 nStartPos, + sal_Int32 nLen ) + throw (RuntimeException) +{ + Sequence< com::sun::star::lang::Locale > aRes; + + OString o = OUStringToOString( rText, RTL_TEXTENCODING_UTF8 ); + vector<Guess> gs = m_aGuesser.GuessLanguage(o.pData->buffer); + + aRes.realloc(gs.size()); + + com::sun::star::lang::Locale *pRes = aRes.getArray(); + +#ifdef DEBUG + std::cout << " We have " << gs.size() << " candidates" << std::endl; +#endif + + for(int i = 0; i < gs.size() ; i++ ){ + com::sun::star::lang::Locale current_aRes; + + current_aRes.Language = A2OU( gs[i].getLanguage().c_str() ); + current_aRes.Country = A2OU( gs[i].getCountry().c_str() ); + + pRes[i] = current_aRes; + } + + return aRes; +} +*/ +//************************************************************************* + +Locale SAL_CALL LangGuess_Impl::guessPrimaryLanguage( + const ::rtl::OUString& rText, + ::sal_Int32 nStartPos, + ::sal_Int32 nLen ) + throw (lang::IllegalArgumentException, uno::RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + + EnsureInitialized(); + + lang::Locale aRes; + if (nStartPos >=0 && nLen >= 0 && nStartPos + nLen <= rText.getLength()) + { + OString o( OUStringToOString( rText.copy(nStartPos, nLen), RTL_TEXTENCODING_UTF8 ) ); + Guess g = m_aGuesser.GuessPrimaryLanguage((char*)o.getStr()); + aRes.Language = OUString::createFromAscii(g.GetLanguage().c_str()); + aRes.Country = OUString::createFromAscii(g.GetCountry().c_str()); + } + else + throw lang::IllegalArgumentException(); + + return aRes; +} + +//************************************************************************* +#define DEFAULT_CONF_FILE_NAME "fpdb.conf" + +void LangGuess_Impl::SetFingerPrintsDB( + const rtl::OUString &filePath ) + throw (RuntimeException) +{ + //! text encoding for file name / path needs to be in the same encoding the OS uses + OString path = OUStringToOString( filePath, osl_getThreadTextEncoding() ); + OString conf_file_name( DEFAULT_CONF_FILE_NAME ); + OString conf_file_path(path); + conf_file_path += conf_file_name; + + //cout << "Conf file : " << conf_file_path.getStr() << " directory : " << path.getStr() << endl; + + m_aGuesser.SetDBPath((const char*)conf_file_path.getStr(), (const char*)path.getStr()); +} + +//************************************************************************* +uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getAvailableLanguages( ) + throw (uno::RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + + EnsureInitialized(); + + Sequence< com::sun::star::lang::Locale > aRes; + vector<Guess> gs = m_aGuesser.GetAllManagedLanguages(); + aRes.realloc(gs.size()); + + com::sun::star::lang::Locale *pRes = aRes.getArray(); + + for(size_t i = 0; i < gs.size() ; i++ ){ + com::sun::star::lang::Locale current_aRes; + current_aRes.Language = A2OU( gs[i].GetLanguage().c_str() ); + current_aRes.Country = A2OU( gs[i].GetCountry().c_str() ); + pRes[i] = current_aRes; + } + + return aRes; +} + +//************************************************************************* +uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getEnabledLanguages( ) + throw (uno::RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + + EnsureInitialized(); + + Sequence< com::sun::star::lang::Locale > aRes; + vector<Guess> gs = m_aGuesser.GetAvailableLanguages(); + aRes.realloc(gs.size()); + + com::sun::star::lang::Locale *pRes = aRes.getArray(); + + for(size_t i = 0; i < gs.size() ; i++ ){ + com::sun::star::lang::Locale current_aRes; + current_aRes.Language = A2OU( gs[i].GetLanguage().c_str() ); + current_aRes.Country = A2OU( gs[i].GetCountry().c_str() ); + pRes[i] = current_aRes; + } + + return aRes; +} + +//************************************************************************* +uno::Sequence< Locale > SAL_CALL LangGuess_Impl::getDisabledLanguages( ) + throw (uno::RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + + EnsureInitialized(); + + Sequence< com::sun::star::lang::Locale > aRes; + vector<Guess> gs = m_aGuesser.GetUnavailableLanguages(); + aRes.realloc(gs.size()); + + com::sun::star::lang::Locale *pRes = aRes.getArray(); + + for(size_t i = 0; i < gs.size() ; i++ ){ + com::sun::star::lang::Locale current_aRes; + current_aRes.Language = A2OU( gs[i].GetLanguage().c_str() ); + current_aRes.Country = A2OU( gs[i].GetCountry().c_str() ); + pRes[i] = current_aRes; + } + + return aRes; +} + +//************************************************************************* +void SAL_CALL LangGuess_Impl::disableLanguages( + const uno::Sequence< Locale >& rLanguages ) + throw (lang::IllegalArgumentException, uno::RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + + EnsureInitialized(); + + sal_Int32 nLanguages = rLanguages.getLength(); + const Locale *pLanguages = rLanguages.getConstArray(); + + for (sal_Int32 i = 0; i < nLanguages; ++i) + { + string language; + + OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US ); + OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US ); + + language += l.getStr(); + language += "-"; + language += c.getStr(); + m_aGuesser.DisableLanguage(language); + } +} + +//************************************************************************* +void SAL_CALL LangGuess_Impl::enableLanguages( + const uno::Sequence< Locale >& rLanguages ) + throw (lang::IllegalArgumentException, uno::RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + + EnsureInitialized(); + + sal_Int32 nLanguages = rLanguages.getLength(); + const Locale *pLanguages = rLanguages.getConstArray(); + + for (sal_Int32 i = 0; i < nLanguages; ++i) + { + string language; + + OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US ); + OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US ); + + language += l.getStr(); + language += "-"; + language += c.getStr(); + m_aGuesser.EnableLanguage(language); + } +} + +//************************************************************************* +OUString SAL_CALL LangGuess_Impl::getImplementationName( ) + throw(RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + return A2OU( IMPLNAME ); +} + +//************************************************************************* +sal_Bool SAL_CALL LangGuess_Impl::supportsService( const OUString& ServiceName ) + throw(RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + Sequence< OUString > aSNL = getSupportedServiceNames(); + const OUString * pArray = aSNL.getArray(); + for( sal_Int32 i = 0; i < aSNL.getLength(); i++ ) + if( pArray[i] == ServiceName ) + return sal_True; + return sal_False; +} + +//************************************************************************* +Sequence<OUString> SAL_CALL LangGuess_Impl::getSupportedServiceNames( ) + throw(RuntimeException) +{ + osl::MutexGuard aGuard( GetLangGuessMutex() ); + return getSupportedServiceNames_Static(); +} + +//************************************************************************* +Sequence<OUString> SAL_CALL LangGuess_Impl::getSupportedServiceNames_Static( ) +{ + OUString aName( A2OU( SERVICENAME ) ); + return Sequence< OUString >( &aName, 1 ); +} + +//************************************************************************* + + +/** + * Function to create a new component instance; is needed by factory helper implementation. + * @param xMgr service manager to if the components needs other component instances + */ +Reference< XInterface > SAL_CALL LangGuess_Impl_create( + Reference< XComponentContext > const & xContext ) + SAL_THROW( () ) +{ + return static_cast< ::cppu::OWeakObject * >( new LangGuess_Impl(xContext) ); +} + +//################################################################################################## +//#### EXPORTED ### functions to allow for registration and creation of the UNO component +//################################################################################################## + +static struct ::cppu::ImplementationEntry s_component_entries [] = +{ + { + LangGuess_Impl_create, getImplementationName_LangGuess_Impl, + getSupportedServiceNames_LangGuess_Impl, + ::cppu::createSingleComponentFactory, + 0, 0 + }, + { 0, 0, 0, 0, 0, 0 } +}; + +extern "C" +{ + +void SAL_CALL component_getImplementationEnvironment( + sal_Char const ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ ) +{ + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} + +sal_Bool SAL_CALL component_writeInfo( + XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry ) +{ + return ::cppu::component_writeInfoHelper( + xMgr, xRegistry, s_component_entries ); +} + +void * SAL_CALL component_getFactory( + sal_Char const * implName, lang::XMultiServiceFactory * xMgr, + registry::XRegistryKey * xRegistry ) +{ + return ::cppu::component_getFactoryHelper( + implName, xMgr, xRegistry, s_component_entries ); +} + +} + diff --git a/lingucomponent/source/languageguessing/makefile.mk b/lingucomponent/source/languageguessing/makefile.mk new file mode 100644 index 000000000000..ebad4076b3ed --- /dev/null +++ b/lingucomponent/source/languageguessing/makefile.mk @@ -0,0 +1,80 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/.. +PRJNAME=lingucomponent + +TARGET=guesslang + +ENABLE_EXCEPTIONS=TRUE + +#----- Settings --------------------------------------------------------- + +.INCLUDE : settings.mk + +# --- Files -------------------------------------------------------- + +.IF "$(GUI)"=="UNX" || "$(GUI)"=="MAC" || "$(GUI)$(COM)"=="WNTGCC" +LIBTEXTCATLIB=-ltextcat +.ELSE # "$(GUI)"=="UNX" || "$(GUI)"=="MAC" +LIBTEXTCATLIB=ilibtextcat.lib +.ENDIF # "$(GUI)"=="UNX" || "$(GUI)"=="MAC" + +SLOFILES = \ + $(SLO)$/altstrfunc.obj \ + $(SLO)$/guess.obj \ + $(SLO)$/guesslang.obj \ + $(SLO)$/simpleguesser.obj + + +SHL1TARGET= $(TARGET)$(DLLPOSTFIX) + +SHL1STDLIBS= \ + $(CPPUHELPERLIB) \ + $(CPPULIB) \ + $(LIBTEXTCATLIB) \ + $(SALLIB) \ + $(SVLLIB) \ + $(TOOLSLIB) \ + $(UNOTOOLSLIB) + +# build DLL +SHL1LIBS= $(SLB)$/$(TARGET).lib +SHL1IMPLIB= i$(TARGET) +SHL1DEPN= $(SHL1LIBS) +SHL1DEF= $(MISC)$/$(SHL1TARGET).def +.IF "$(OS)"!="MACOSX" +SHL1VERSIONMAP=$(SOLARENV)/src/component.map +.ENDIF + +# build DEF file +DEF1NAME =$(SHL1TARGET) + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/lingucomponent/source/languageguessing/simpleguesser.cxx b/lingucomponent/source/languageguessing/simpleguesser.cxx new file mode 100644 index 000000000000..aa4f670746fd --- /dev/null +++ b/lingucomponent/source/languageguessing/simpleguesser.cxx @@ -0,0 +1,234 @@ +/*************************************************************************** + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + + /** + * + * + * + * + * TODO + * - Add exception throwing when h == NULL + * - Not init h when implicit constructor is launched + */ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + +#include <string.h> +#include <sstream> +#include <iostream> + +#include <libtextcat/textcat.h> +#include <libtextcat/common.h> +#include <libtextcat/constants.h> +#include <libtextcat/fingerprint.h> +#include <libtextcat/utf8misc.h> + +#include <sal/types.h> + +#include "altstrfunc.hxx" +#include "simpleguesser.hxx" + +#ifndef _UTF8_ +#define _UTF8_ +#endif + + +using namespace std; + + +/** + * This 3 following structures are from fingerprint.c and textcat.c + */ + +typedef struct ngram_t { + + sint2 rank; + char str[MAXNGRAMSIZE+1]; + +} ngram_t; + +typedef struct fp_t { + + const char *name; + ngram_t *fprint; + uint4 size; + +} fp_t; + +typedef struct textcat_t{ + + void **fprint; + char *fprint_disable; + uint4 size; + uint4 maxsize; + + char output[MAXOUTPUTSIZE]; + +} textcat_t; +/** end of the 3 structs */ + +SimpleGuesser::SimpleGuesser() +{ + h = NULL; +} + +void SimpleGuesser::operator=(SimpleGuesser& sg){ + if(h){textcat_Done(h);} + h = sg.h; +} + +SimpleGuesser::~SimpleGuesser() +{ + if(h){textcat_Done(h);} +} + + +/*! + \fn SimpleGuesser::GuessLanguage(char* text) + */ +vector<Guess> SimpleGuesser::GuessLanguage(char* text) +{ + vector<Guess> guesses; + + if(!h){return guesses;} + + //calculate le number of unicode charcters (symbols) + int len = utfstrlen(text); + + if( len > MAX_STRING_LENGTH_TO_ANALYSE ){len = MAX_STRING_LENGTH_TO_ANALYSE ;} + + char *guess_list = textcat_Classify(h, text, len); + + if(strcmp(guess_list, _TEXTCAT_RESULT_SHORT) == 0){ + return guesses; + } + + int current_pointer = 0; + + for(int i = 0; guess_list[current_pointer] != '\0'; i++) + { + while(guess_list[current_pointer] != GUESS_SEPARATOR_OPEN && guess_list[current_pointer] != '\0'){ + current_pointer++; + } + if(guess_list[current_pointer] != '\0') + { + Guess g((char*)(guess_list + current_pointer)); + + guesses.push_back(g); + + current_pointer++; + } + } + + return guesses; +} + +/*! + \fn SimpleGuesser::GuessPrimaryLanguage(char* text) + */ +Guess SimpleGuesser::GuessPrimaryLanguage(char* text) +{ + vector<Guess> ret = GuessLanguage(text); + if(ret.size() > 0){ + return GuessLanguage(text)[0]; + } + else{ + return Guess(); + } +} +/** + * Is used to know wich language is available, unavailable or both + * when mask = 0xF0, return only Available + * when mask = 0x0F, return only Unavailable + * when mask = 0xFF, return both Available and Unavailable + */ +vector<Guess> SimpleGuesser::GetManagedLanguages(const char mask) +{ + size_t i; + textcat_t *tables = (textcat_t*)h; + + vector<Guess> lang; + if(!h){return lang;} + + for (i=0; i<tables->size; i++) { + if(tables->fprint_disable[i] & mask){ + string langStr = "["; + langStr += (char*)fp_Name(tables->fprint[i]); + Guess g( (char *)langStr.c_str()); + lang.push_back(g); + } + } + + return lang; +} + +vector<Guess> SimpleGuesser::GetAvailableLanguages(){ + return GetManagedLanguages( sal::static_int_cast< char >( 0xF0 ) ); +} + +vector<Guess> SimpleGuesser::GetUnavailableLanguages(){ + return GetManagedLanguages( sal::static_int_cast< char >( 0x0F )); +} + +vector<Guess> SimpleGuesser::GetAllManagedLanguages(){ + return GetManagedLanguages( sal::static_int_cast< char >( 0xFF )); +} + +void SimpleGuesser::XableLanguage(string lang, char mask){ + size_t i; + textcat_t *tables = (textcat_t*)h; + + if(!h){return;} + + for (i=0; i<tables->size; i++) { + string language(fp_Name(tables->fprint[i])); + if(start(language,lang) == 0){ + //cout << language << endl; + tables->fprint_disable[i] = mask; + //continue; + } + } +} + +void SimpleGuesser::EnableLanguage(string lang){ + XableLanguage(lang, sal::static_int_cast< char >( 0xF0 )); +} + +void SimpleGuesser::DisableLanguage(string lang){ + XableLanguage(lang, sal::static_int_cast< char >( 0x0F )); +} + +/** +* +*/ +void SimpleGuesser::SetDBPath(const char* path, const char* prefix){ + if(h){ + textcat_Done(h); + } + h = special_textcat_Init(path, prefix); +} diff --git a/lingucomponent/source/languageguessing/simpleguesser.hxx b/lingucomponent/source/languageguessing/simpleguesser.hxx new file mode 100644 index 000000000000..0d7274063cd2 --- /dev/null +++ b/lingucomponent/source/languageguessing/simpleguesser.hxx @@ -0,0 +1,121 @@ +/*************************************************************************** + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#ifndef SIMPLEGUESSER_H +#define SIMPLEGUESSER_H + +#include <string.h> +#include <string> +#include <cstdlib> +#include <vector> +#include <guess.hxx> + +#define MAX_STRING_LENGTH_TO_ANALYSE 200 + +using namespace std; + +/** +@author Jocelyn Merand +*/ +class SimpleGuesser{ +public: + /**inits the object with conf file "./conf.txt"*/ + SimpleGuesser(); + + /** Compares the current Simpleguesser with an other + * @param SimpleGuesser& sg the other guesser to compare + */ + void operator=(SimpleGuesser& sg); + + /** + * destroy the object + */ + ~SimpleGuesser(); + + /** + * Analyze a text and return the most probable languages of the text + * @param char* text is the text to analyze + * @return the list of guess + */ + vector<Guess> GuessLanguage(char* text); + + /** + * Analyze a text and return the most probable language of the text + * @param char* text is the text to analyze + * @return the guess (containing language) + */ + Guess GuessPrimaryLanguage(char* text); + + /** + * List all available languages (possibly to be in guesses) + * @return the list of languages + */ + vector<Guess> GetAvailableLanguages(); + + /** + * List all languages (possibly in guesses or not) + * @return the list of languages + */ + vector<Guess> GetAllManagedLanguages(); + + /** + * List all Unavailable languages (disable for any reason) + * @return the list of languages + */ + vector<Guess> GetUnavailableLanguages(); + + /** + * Mark a language enabled + * @param string lang the language to enable (build like language-COUNTRY-encoding) + */ + void EnableLanguage(string lang); + + /** + * Mark a language disabled + * @param string lang the language to disable (build like language-COUNTRY-encoding) + */ + void DisableLanguage(string lang); + + /** + * Load a new DB of fingerprints + * @param const char* thePathOfConfFile self explaining + * @param const char* prefix is the path where the directory witch contains fingerprint files is stored + */ + void SetDBPath(const char* thePathOfConfFile, const char* prefix); + +protected: + + //Where typical fingerprints (n-gram tables) are stored + void* h; + + //Is used to select languages into the fingerprints DB, the mask is used to indicate if we want enabled disabled or both + vector<Guess> GetManagedLanguages(const char mask); + + //Like getManagedLanguages, this function enable or disable a language and it depends of the mask + void XableLanguage(string lang, char mask); +}; + +#endif diff --git a/lingucomponent/source/lingutil/lingutil.cxx b/lingucomponent/source/lingutil/lingutil.cxx new file mode 100644 index 000000000000..ee7d71de4435 --- /dev/null +++ b/lingucomponent/source/lingutil/lingutil.cxx @@ -0,0 +1,272 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + +#if defined(WNT) +#include <tools/prewin.h> +#endif + +#if defined(WNT) +#include <Windows.h> +#endif + +#if defined(WNT) +#include <tools/postwin.h> +#endif + + +#include <osl/thread.h> +#include <osl/file.hxx> +#include <tools/debug.hxx> +#include <tools/urlobj.hxx> +#include <i18npool/mslangid.hxx> +#include <unotools/lingucfg.hxx> +#include <unotools/pathoptions.hxx> +#include <rtl/ustring.hxx> +#include <rtl/string.hxx> +#include <linguistic/misc.hxx> + +#include <set> +#include <vector> +#include <string.h> + +#include <lingutil.hxx> +#include <dictmgr.hxx> + + + + +using ::com::sun::star::lang::Locale; +using namespace ::com::sun::star; + +#if 0 +////////////////////////////////////////////////////////////////////// + +String GetDirectoryPathFromFileURL( const String &rFileURL ) +{ + // get file URL + INetURLObject aURLObj; + aURLObj.SetSmartProtocol( INET_PROT_FILE ); + aURLObj.SetSmartURL( rFileURL ); + aURLObj.removeSegment(); + DBG_ASSERT( !aURLObj.HasError(), "invalid URL" ); + String aRes = aURLObj.GetMainURL( INetURLObject::DECODE_TO_IURI ); + return aRes; +} +#endif + +#if defined(WNT) +rtl::OString Win_GetShortPathName( const rtl::OUString &rLongPathName ) +{ + rtl::OString aRes; + + sal_Unicode aShortBuffer[1024] = {0}; + sal_Int32 nShortBufSize = sizeof( aShortBuffer ) / sizeof( aShortBuffer[0] ); + + // use the version of 'GetShortPathName' that can deal with Unicode... + sal_Int32 nShortLen = GetShortPathNameW( + reinterpret_cast<LPCWSTR>( rLongPathName.getStr() ), + reinterpret_cast<LPWSTR>( aShortBuffer ), + nShortBufSize ); + + if (nShortLen < nShortBufSize) // conversion successful? + aRes = rtl::OString( OU2ENC( rtl::OUString( aShortBuffer, nShortLen ), osl_getThreadTextEncoding()) ); + else + DBG_ERROR( "Win_GetShortPathName: buffer to short" ); + + return aRes; +} +#endif //defined(WNT) + +////////////////////////////////////////////////////////////////////// + +// build list of old style diuctionaries (not as extensions) to use. +// User installed dictionaries (the ones residing in the user paths) +// will get precedence over system installed ones for the same language. +std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicType ) +{ + std::vector< SvtLinguConfigDictionaryEntry > aRes; + + if (!pDicType) + return aRes; + + rtl::OUString aFormatName; + String aDicExtension; +#ifdef SYSTEM_DICTS + rtl::OUString aSystemDir; + rtl::OUString aSystemPrefix; + rtl::OUString aSystemSuffix; +#endif + bool bSpell = false; + bool bHyph = false; + bool bThes = false; + if (strcmp( pDicType, "DICT" ) == 0) + { + aFormatName = A2OU("DICT_SPELL"); + aDicExtension = String::CreateFromAscii( ".dic" ); +#ifdef SYSTEM_DICTS + aSystemDir = A2OU( DICT_SYSTEM_DIR ); + aSystemSuffix = aDicExtension; +#endif + bSpell = true; + } + else if (strcmp( pDicType, "HYPH" ) == 0) + { + aFormatName = A2OU("DICT_HYPH"); + aDicExtension = String::CreateFromAscii( ".dic" ); +#ifdef SYSTEM_DICTS + aSystemDir = A2OU( HYPH_SYSTEM_DIR ); + aSystemPrefix = A2OU( "hyph_" ); + aSystemSuffix = aDicExtension; +#endif + bHyph = true; + } + else if (strcmp( pDicType, "THES" ) == 0) + { + aFormatName = A2OU("DICT_THES"); + aDicExtension = String::CreateFromAscii( ".dat" ); +#ifdef SYSTEM_DICTS + aSystemDir = A2OU( THES_SYSTEM_DIR ); + aSystemPrefix = A2OU( "th_" ); + aSystemSuffix = A2OU( "_v2.dat" ); +#endif + bThes = true; + } + + + if (aFormatName.getLength() == 0 || aDicExtension.Len() == 0) + return aRes; + + // set of languages to remember the language where it is already + // decided to make use of the dictionary. + std::set< LanguageType > aDicLangInUse; + +#ifdef SYSTEM_DICTS + osl::Directory aSystemDicts(aSystemDir); + if (aSystemDicts.open() == osl::FileBase::E_None) + { + osl::DirectoryItem aItem; + osl::FileStatus aFileStatus(FileStatusMask_FileURL); + while (aSystemDicts.getNextItem(aItem) == osl::FileBase::E_None) + { + aItem.getFileStatus(aFileStatus); + rtl::OUString sPath = aFileStatus.getFileURL(); + if (sPath.lastIndexOf(aSystemSuffix) == sPath.getLength()-aSystemSuffix.getLength()) + { + sal_Int32 nStartIndex = sPath.lastIndexOf(sal_Unicode('/')) + 1; + if (!sPath.match(aSystemPrefix, nStartIndex)) + continue; + rtl::OUString sChunk = sPath.copy(0, sPath.getLength() - aSystemSuffix.getLength()); + sal_Int32 nIndex = nStartIndex + aSystemPrefix.getLength(); + rtl::OUString sLang = sChunk.getToken( 0, '_', nIndex ); + if (!sLang.getLength()) + continue; + rtl::OUString sRegion; + if (nIndex != -1) + sRegion = sChunk.copy( nIndex, sChunk.getLength() - nIndex ); + + // Thus we first get the language of the dictionary + LanguageType nLang = MsLangId::convertIsoNamesToLanguage( + sLang, sRegion ); + + if (aDicLangInUse.count( nLang ) == 0) + { + // remember the new language in use + aDicLangInUse.insert( nLang ); + + // add the dictionary to the resulting vector + SvtLinguConfigDictionaryEntry aDicEntry; + aDicEntry.aLocations.realloc(1); + aDicEntry.aLocaleNames.realloc(1); + rtl::OUString aLocaleName( MsLangId::convertLanguageToIsoString( nLang ) ); + aDicEntry.aLocations[0] = sPath; + aDicEntry.aFormatName = aFormatName; + aDicEntry.aLocaleNames[0] = aLocaleName; + aRes.push_back( aDicEntry ); + } + } + } + } + +#endif + + return aRes; +} + + +void MergeNewStyleDicsAndOldStyleDics( + std::list< SvtLinguConfigDictionaryEntry > &rNewStyleDics, + const std::vector< SvtLinguConfigDictionaryEntry > &rOldStyleDics ) +{ + // get list of languages supported by new style dictionaries + std::set< LanguageType > aNewStyleLanguages; + std::list< SvtLinguConfigDictionaryEntry >::const_iterator aIt; + for (aIt = rNewStyleDics.begin() ; aIt != rNewStyleDics.end(); ++aIt) + { + const uno::Sequence< rtl::OUString > aLocaleNames( aIt->aLocaleNames ); + sal_Int32 nLocaleNames = aLocaleNames.getLength(); + for (sal_Int32 k = 0; k < nLocaleNames; ++k) + { + LanguageType nLang = MsLangId::convertIsoStringToLanguage( aLocaleNames[k] ); + aNewStyleLanguages.insert( nLang ); + } + } + + // now check all old style dictionaries if they will add a not yet + // added language. If so add them to the resulting vector + std::vector< SvtLinguConfigDictionaryEntry >::const_iterator aIt2; + for (aIt2 = rOldStyleDics.begin(); aIt2 != rOldStyleDics.end(); ++aIt2) + { + sal_Int32 nOldStyleDics = aIt2->aLocaleNames.getLength(); + + // old style dics should only have one language listed... + DBG_ASSERT( nOldStyleDics, "old style dictionary with more then one language found!"); + if (nOldStyleDics > 0) + { + LanguageType nLang = MsLangId::convertIsoStringToLanguage( aIt2->aLocaleNames[0] ); + + if (nLang == LANGUAGE_DONTKNOW || nLang == LANGUAGE_NONE) + { + DBG_ERROR( "old style dictionary with invalid language found!" ); + continue; + } + + // language not yet added? + if (aNewStyleLanguages.count( nLang ) == 0) + rNewStyleDics.push_back( *aIt2 ); + } + else + { + DBG_ERROR( "old style dictionary with no language found!" ); + } + } +} + +////////////////////////////////////////////////////////////////////// + diff --git a/lingucomponent/source/lingutil/lingutil.hxx b/lingucomponent/source/lingutil/lingutil.hxx new file mode 100644 index 000000000000..8b45b1f509ad --- /dev/null +++ b/lingucomponent/source/lingutil/lingutil.hxx @@ -0,0 +1,105 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _LINGUTIL_HXX_ +#define _LINGUTIL_HXX_ + + +#include <com/sun/star/lang/Locale.hpp> +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> +#include <tools/string.hxx> + +#include <vector> +#include <list> + + +#define A2OU(x) ::rtl::OUString::createFromAscii( x ) + +#define OU2A(rtlOUString) \ + ::rtl::OString((rtlOUString).getStr(), (rtlOUString).getLength(), \ + RTL_TEXTENCODING_ASCII_US).getStr() + +#define OU2UTF8(rtlOUString) \ + ::rtl::OString((rtlOUString).getStr(), (rtlOUString).getLength(), \ + RTL_TEXTENCODING_UTF8).getStr() + +#define OU2ISO_1(rtlOUString) \ + ::rtl::OString((rtlOUString).getStr(), (rtlOUString).getLength(), \ + RTL_TEXTENCODING_ISO_8859_1).getStr() + +#define OU2ENC(rtlOUString, rtlEncoding) \ + ::rtl::OString((rtlOUString).getStr(), (rtlOUString).getLength(), \ + rtlEncoding, RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK).getStr() + + +struct SvtLinguConfigDictionaryEntry; + +/////////////////////////////////////////////////////////////////////////// + +struct lt_rtl_OUString +{ + bool operator() (const rtl::OUString &r1, const rtl::OUString &r2) const + { + return r1 < r2; + } +}; + +inline BOOL operator == ( const ::com::sun::star::lang::Locale &rL1, const ::com::sun::star::lang::Locale &rL2 ) +{ + return rL1.Language == rL2.Language && + rL1.Country == rL2.Country && + rL1.Variant == rL2.Variant; +} + +#if 0 +/////////////////////////////////////////////////////////////////////////// + +String GetDirectoryPathFromFileURL( const String &rFileURL ); +#endif + +#if defined(WNT) +/////////////////////////////////////////////////////////////////////////// +// to be use to get a short path name under Windows that still can be used with +// the 'fopen' call. This is necessary since under Windows there seems to be +// a restriction of only about 110-130 characters length to a path name in order +// for it to work with 'fopen'. And that length is usually easily exceeded +// when using extensions... +rtl::OString Win_GetShortPathName( const rtl::OUString &rLongPathName ); +#endif + +/////////////////////////////////////////////////////////////////////////// + +// temporary function, to be removed when new style dictionaries +// using configuration entries are fully implemented and provided +std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char * pDicType ); +void MergeNewStyleDicsAndOldStyleDics( std::list< SvtLinguConfigDictionaryEntry > &rNewStyleDics, const std::vector< SvtLinguConfigDictionaryEntry > &rOldStyleDics ); + +/////////////////////////////////////////////////////////////////////////// + +#endif + diff --git a/lingucomponent/source/lingutil/makefile.mk b/lingucomponent/source/lingutil/makefile.mk new file mode 100644 index 000000000000..7d19eac9ef22 --- /dev/null +++ b/lingucomponent/source/lingutil/makefile.mk @@ -0,0 +1,63 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ = ..$/.. + +PRJNAME = lingucomponent +TARGET = ulingu +LIBTARGET=NO + +ENABLE_EXCEPTIONS=TRUE + +#----- Settings --------------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SYSTEM_HUNSPELL)" != "YES" +HUNSPELL_CFLAGS += -I$(SOLARINCDIR)$/hunspell +.ENDIF + +.IF "$(SYSTEM_DICTS)" == "YES" +CXXFLAGS += -DSYSTEM_DICTS -DDICT_SYSTEM_DIR=\"$(DICT_SYSTEM_DIR)\" -DHYPH_SYSTEM_DIR=\"$(HYPH_SYSTEM_DIR)\" -DTHES_SYSTEM_DIR=\"$(THES_SYSTEM_DIR)\" +CFLAGSCXX += -DSYSTEM_DICTS -DDICT_SYSTEM_DIR=\"$(DICT_SYSTEM_DIR)\" -DHYPH_SYSTEM_DIR=\"$(HYPH_SYSTEM_DIR)\" -DTHES_SYSTEM_DIR=\"$(THES_SYSTEM_DIR)\" +CFLAGSCC += -DSYSTEM_DICTS -DDICT_SYSTEM_DIR=\"$(DICT_SYSTEM_DIR)\" -DHYPH_SYSTEM_DIR=\"$(HYPH_SYSTEM_DIR)\" -DTHES_SYSTEM_DIR=\"$(THES_SYSTEM_DIR)\" +.ENDIF + +CXXFLAGS += $(HUNSPELL_CFLAGS) +CFLAGSCXX += $(HUNSPELL_CFLAGS) +CFLAGSCC += $(HUNSPELL_CFLAGS) + +SLOFILES = $(SLO)$/lingutil.obj + +LIB1TARGET= $(SLB)$/lib$(TARGET).lib +LIB1ARCHIV= $(LB)/lib$(TARGET).a +LIB1OBJFILES= $(SLOFILES) + + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/lingucomponent/source/spellcheck/macosxspell/macreg.cxx b/lingucomponent/source/spellcheck/macosxspell/macreg.cxx new file mode 100644 index 000000000000..b60ffffe42e9 --- /dev/null +++ b/lingucomponent/source/spellcheck/macosxspell/macreg.cxx @@ -0,0 +1,87 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + + +#include <cppuhelper/factory.hxx> // helper for factories +#include <rtl/string.hxx> + +#include <com/sun/star/registry/XRegistryKey.hpp> + +using namespace rtl; +using namespace com::sun::star::lang; +using namespace com::sun::star::registry; + +//////////////////////////////////////// +// declaration of external RegEntry-functions defined by the service objects +// + +extern sal_Bool SAL_CALL MacSpellChecker_writeInfo( + void * /*pServiceManager*/, XRegistryKey * pRegistryKey ); + +extern void * SAL_CALL MacSpellChecker_getFactory( + const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, + void * /*pRegistryKey*/ ); + +//////////////////////////////////////// +// definition of the two functions that are used to provide the services +// + +extern "C" +{ + +sal_Bool SAL_CALL component_writeInfo( + void * pServiceManager, XRegistryKey * pRegistryKey ) +{ + return MacSpellChecker_writeInfo( pServiceManager, pRegistryKey ); +} + +void SAL_CALL component_getImplementationEnvironment( + const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ ) +{ + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} + +void * SAL_CALL component_getFactory( + const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) +{ + void * pRet = NULL; + pRet = MacSpellChecker_getFactory( + pImplName, + reinterpret_cast< XMultiServiceFactory * >( pServiceManager ), + pRegistryKey ); + + return pRet; +} + +} + +/////////////////////////////////////////////////////////////////////////// + diff --git a/lingucomponent/source/spellcheck/macosxspell/macspellimp.cxx b/lingucomponent/source/spellcheck/macosxspell/macspellimp.cxx new file mode 100644 index 000000000000..70996793c186 --- /dev/null +++ b/lingucomponent/source/spellcheck/macosxspell/macspellimp.cxx @@ -0,0 +1,693 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp> + +#include <com/sun/star/linguistic2/SpellFailure.hpp> +#include <cppuhelper/factory.hxx> // helper for factories +#include <com/sun/star/registry/XRegistryKey.hpp> +#include <tools/debug.hxx> +#include <unotools/processfactory.hxx> +#include <osl/mutex.hxx> + +//#include <hunspell.hxx> +#include <dictmgr.hxx> +#include <macspellimp.hxx> + +//#include <linguistic/lngprops.hxx> +#include <linguistic/spelldta.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/useroptions.hxx> +#include <osl/file.hxx> +#include <rtl/ustrbuf.hxx> + + +using namespace utl; +using namespace osl; +using namespace rtl; +using namespace com::sun::star; +using namespace com::sun::star::beans; +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace com::sun::star::linguistic2; +using namespace linguistic; +/////////////////////////////////////////////////////////////////////////// +// dbg_dump for development +#if OSL_DEBUG_LEVEL > 1 +#include <rtl/strbuf.hxx> +#include <rtl/ustring.hxx> + +const sal_Char *dbg_dump(const rtl::OString &rStr) +{ + static rtl::OStringBuffer aStr; + + aStr = rtl::OStringBuffer(rStr); + aStr.append(static_cast<char>(0)); + return aStr.getStr(); +} + +const sal_Char *dbg_dump(const rtl::OUString &rStr) +{ + return dbg_dump(rtl::OUStringToOString(rStr, RTL_TEXTENCODING_UTF8)); +} + +const sal_Char *dbg_dump(rtl_String *pStr) +{ + return dbg_dump(rtl::OString(pStr)); +} + +const sal_Char *dbg_dump(rtl_uString *pStr) +{ + return dbg_dump(rtl::OUString(pStr)); +} + +#endif +/////////////////////////////////////////////////////////////////////////// + +MacSpellChecker::MacSpellChecker() : + aEvtListeners ( GetLinguMutex() ) +{ +// aDicts = NULL; + aDEncs = NULL; + aDLocs = NULL; + aDNames = NULL; + bDisposing = FALSE; + pPropHelper = NULL; + numdict = 0; + NSApplicationLoad(); + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + macSpell = [NSSpellChecker sharedSpellChecker]; + macTag = [NSSpellChecker uniqueSpellDocumentTag]; + [pool release]; +} + + +MacSpellChecker::~MacSpellChecker() +{ + // if (aDicts) { + // for (int i = 0; i < numdict; i++) { + // if (aDicts[i]) delete aDicts[i]; + // aDicts[i] = NULL; + // } + // delete[] aDicts; + // } + // aDicts = NULL; + numdict = 0; + if (aDEncs) delete[] aDEncs; + aDEncs = NULL; + if (aDLocs) delete[] aDLocs; + aDLocs = NULL; + if (aDNames) delete[] aDNames; + aDNames = NULL; + if (pPropHelper) + pPropHelper->RemoveAsPropListener(); +} + + +PropertyHelper_Spell & MacSpellChecker::GetPropHelper_Impl() +{ + if (!pPropHelper) + { + Reference< XPropertySet > xPropSet( GetLinguProperties(), UNO_QUERY ); + + pPropHelper = new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + return *pPropHelper; +} + + +Sequence< Locale > SAL_CALL MacSpellChecker::getLocales() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + // this routine should return the locales supported by the installed + // dictionaries. So here we need to parse both the user edited + // dictionary list and the shared dictionary list + // to see what dictionaries the admin/user has installed + + int numusr; // number of user dictionary entries + int numshr; // number of shared dictionary entries + dictentry * spdict; // shared dict entry pointer + dictentry * updict; // user dict entry pointer + SvtPathOptions aPathOpt; + rtl_TextEncoding aEnc = RTL_TEXTENCODING_UTF8; + + std::vector<objc_object *> postspdict; + //std::vector<dictentry *> postspdict; + std::vector<dictentry *> postupdict; + + + if (!numdict) { + + // invoke a dictionary manager to get the user dictionary list + // TODO How on Mac OS X? + + // invoke a second dictionary manager to get the shared dictionary list + NSArray *aLocales = [NSLocale availableLocaleIdentifiers]; + + //Test for existence of the dictionaries + for (unsigned int i = 0; i < [aLocales count]; i++) + { + if( [macSpell setLanguage:[aLocales objectAtIndex:i] ] ) + { + postspdict.push_back( [ aLocales objectAtIndex:i ] ); + } + } + + numusr = postupdict.size(); + numshr = postspdict.size(); + + // we really should merge these and remove duplicates but since + // users can name their dictionaries anything they want it would + // be impossible to know if a real duplication exists unless we + // add some unique key to each myspell dictionary + numdict = numshr + numusr; + + if (numdict) { + aDLocs = new Locale [numdict]; + aDEncs = new rtl_TextEncoding [numdict]; + aDNames = new OUString [numdict]; + aSuppLocales.realloc(numdict); + Locale * pLocale = aSuppLocales.getArray(); + int numlocs = 0; + int newloc; + int i,j; + int k = 0; + + //first add the user dictionaries + //TODO for MAC? + + // now add the shared dictionaries + for (i = 0; i < numshr; i++) { + NSDictionary *aLocDict = [ NSLocale componentsFromLocaleIdentifier:postspdict[i] ]; + NSString* aLang = [ aLocDict objectForKey:NSLocaleLanguageCode ]; + NSString* aCountry = [ aLocDict objectForKey:NSLocaleCountryCode ]; + OUString lang([aLang cStringUsingEncoding: NSUTF8StringEncoding], [aLang length], aEnc); + OUString country([ aCountry cStringUsingEncoding: NSUTF8StringEncoding], [aCountry length], aEnc); + Locale nLoc( lang, country, OUString() ); + newloc = 1; + //eliminate duplicates (is this needed for MacOS?) + for (j = 0; j < numlocs; j++) { + if (nLoc == pLocale[j]) newloc = 0; + } + if (newloc) { + pLocale[numlocs] = nLoc; + numlocs++; + } + aDLocs[k] = nLoc; + //pointer to Hunspell dictionary - not needed for MAC + //aDicts[k] = NULL; + aDEncs[k] = 0; + // Dictionary file names not valid for Mac Spell + //aDNames[k] = aPathOpt.GetLinguisticPath() + A2OU("/ooo/") + A2OU(postspdict[i]->filename); + k++; + } + + aSuppLocales.realloc(numlocs); + + } else { + /* no dictionary.lst found so register no dictionaries */ + numdict = 0; + //aDicts = NULL; + aDEncs = NULL; + aDLocs = NULL; + aDNames = NULL; + aSuppLocales.realloc(0); + } + + /* de-allocation of memory is handled inside the DictMgr */ + updict = NULL; + spdict = NULL; + + } + + return aSuppLocales; +} + + + +sal_Bool SAL_CALL MacSpellChecker::hasLocale(const Locale& rLocale) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!aSuppLocales.getLength()) + getLocales(); + + INT32 nLen = aSuppLocales.getLength(); + for (INT32 i = 0; i < nLen; ++i) + { + const Locale *pLocale = aSuppLocales.getConstArray(); + if (rLocale == pLocale[i]) + { + bRes = TRUE; + break; + } + } + return bRes; +} + + +INT16 MacSpellChecker::GetSpellFailure( const OUString &rWord, const Locale &rLocale ) +{ + rtl_TextEncoding aEnc; + + // initialize a myspell object for each dictionary once + // (note: mutex is held higher up in isValid) + + + INT16 nRes = -1; + + // first handle smart quotes both single and double + OUStringBuffer rBuf(rWord); + sal_Int32 n = rBuf.getLength(); + sal_Unicode c; + for (sal_Int32 ix=0; ix < n; ix++) { + c = rBuf.charAt(ix); + if ((c == 0x201C) || (c == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022); + if ((c == 0x2018) || (c == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027); + } + OUString nWord(rBuf.makeStringAndClear()); + + if (n) + { + aEnc = 0; + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + NSString* aNSStr = [[NSString alloc] initWithCharacters: nWord.getStr() length: nWord.getLength()]; + NSString* aLang = [[NSString alloc] initWithCharacters: rLocale.Language.getStr() length: rLocale.Language.getLength()]; + if(rLocale.Country.getLength()>0) + { + NSString* aCountry = [[NSString alloc] initWithCharacters: rLocale.Country.getStr() length: rLocale.Country.getLength()]; + NSString* aTag = @"_"; + NSString* aTaggedCountry = [aTag stringByAppendingString:aCountry]; + [aLang autorelease]; + aLang = [aLang stringByAppendingString:aTaggedCountry]; + } + + int aCount; + NSRange range = [macSpell checkSpellingOfString:aNSStr startingAt:0 language:aLang wrap:FALSE inSpellDocumentWithTag:macTag wordCount:&aCount]; + int rVal = 0; + if(range.length>0) + { + rVal = -1; + } + else + { + rVal = 1; + } + [pool release]; + if (rVal != 1) + { + nRes = SpellFailure::SPELLING_ERROR; + } else { + return -1; + } + } + return nRes; +} + + + +sal_Bool SAL_CALL + MacSpellChecker::isValid( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (rLocale == Locale() || !rWord.getLength()) + return TRUE; + + if (!hasLocale( rLocale )) +#ifdef LINGU_EXCEPTIONS + throw( IllegalArgumentException() ); +#else + return TRUE; +#endif + + // Get property values to be used. + // These are be the default values set in the SN_LINGU_PROPERTIES + // PropertySet which are overridden by the supplied ones from the + // last argument. + // You'll probably like to use a simplier solution than the provided + // one using the PropertyHelper_Spell. + + PropertyHelper_Spell &rHelper = GetPropHelper(); + rHelper.SetTmpPropVals( rProperties ); + + INT16 nFailure = GetSpellFailure( rWord, rLocale ); + if (nFailure != -1) + { + INT16 nLang = LocaleToLanguage( rLocale ); + // postprocess result for errors that should be ignored + if ( (!rHelper.IsSpellUpperCase() && IsUpper( rWord, nLang )) + || (!rHelper.IsSpellWithDigits() && HasDigits( rWord )) + || (!rHelper.IsSpellCapitalization() + && nFailure == SpellFailure::CAPTION_ERROR) + ) + nFailure = -1; + } + + return (nFailure == -1); +} + + +Reference< XSpellAlternatives > + MacSpellChecker::GetProposals( const OUString &rWord, const Locale &rLocale ) +{ + // Retrieves the return values for the 'spell' function call in case + // of a misspelled word. + // Especially it may give a list of suggested (correct) words: + + Reference< XSpellAlternatives > xRes; + // note: mutex is held by higher up by spell which covers both + + INT16 nLang = LocaleToLanguage( rLocale ); + int count; + Sequence< OUString > aStr( 0 ); + + // first handle smart quotes (single and double) + OUStringBuffer rBuf(rWord); + sal_Int32 n = rBuf.getLength(); + sal_Unicode c; + for (sal_Int32 ix=0; ix < n; ix++) { + c = rBuf.charAt(ix); + if ((c == 0x201C) || (c == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022); + if ((c == 0x2018) || (c == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027); + } + OUString nWord(rBuf.makeStringAndClear()); + + if (n) + { + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + NSString* aNSStr = [[NSString alloc] initWithCharacters: nWord.getStr() length: nWord.getLength()]; + NSString* aLang = [[NSString alloc] initWithCharacters: rLocale.Language.getStr() length: rLocale.Language.getLength() ]; + if(rLocale.Country.getLength()>0) + { + NSString* aCountry = [[NSString alloc] initWithCharacters: rLocale.Country.getStr() length: rLocale.Country.getLength() ]; + NSString* aTag = @"_"; + NSString* aTaggedCountry = [aTag stringByAppendingString:aCountry]; + [aLang autorelease]; + aLang = [aLang stringByAppendingString:aTaggedCountry]; + } + [macSpell setLanguage:aLang]; + NSArray *guesses = [macSpell guessesForWord:aNSStr]; + count = [guesses count]; + if (count) + { + aStr.realloc( count ); + OUString *pStr = aStr.getArray(); + for (int ii=0; ii < count; ii++) + { + // if needed add: if (suglst[ii] == NULL) continue; + NSString* guess = [guesses objectAtIndex:ii]; + OUString cvtwrd((const sal_Unicode*)[guess cStringUsingEncoding:NSUnicodeStringEncoding], (sal_Int32)[guess length]); + pStr[ii] = cvtwrd; + } + } + [pool release]; + } + + // now return an empty alternative for no suggestions or the list of alternatives if some found + SpellAlternatives *pAlt = new SpellAlternatives; + String aTmp(rWord); + pAlt->SetWordLanguage( aTmp, nLang ); + pAlt->SetFailureType( SpellFailure::SPELLING_ERROR ); + pAlt->SetAlternatives( aStr ); + xRes = pAlt; + return xRes; + +} + + + + +Reference< XSpellAlternatives > SAL_CALL + MacSpellChecker::spell( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (rLocale == Locale() || !rWord.getLength()) + return NULL; + + if (!hasLocale( rLocale )) +#ifdef LINGU_EXCEPTIONS + throw( IllegalArgumentException() ); +#else + return NULL; +#endif + + Reference< XSpellAlternatives > xAlt; + if (!isValid( rWord, rLocale, rProperties )) + { + xAlt = GetProposals( rWord, rLocale ); + } + return xAlt; +} + + +Reference< XInterface > SAL_CALL MacSpellChecker_CreateInstance( + const Reference< XMultiServiceFactory > & /*rSMgr*/ ) + throw(Exception) +{ + + Reference< XInterface > xService = (cppu::OWeakObject*) new MacSpellChecker; + return xService; +} + + +sal_Bool SAL_CALL + MacSpellChecker::addLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!bDisposing && rxLstnr.is()) + { + bRes = GetPropHelper().addLinguServiceEventListener( rxLstnr ); + } + return bRes; +} + + +sal_Bool SAL_CALL + MacSpellChecker::removeLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!bDisposing && rxLstnr.is()) + { + DBG_ASSERT( xPropHelper.is(), "xPropHelper non existent" ); + bRes = GetPropHelper().removeLinguServiceEventListener( rxLstnr ); + } + return bRes; +} + + +OUString SAL_CALL + MacSpellChecker::getServiceDisplayName( const Locale& /*rLocale*/ ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return A2OU( "Mac OS X Spell Checker" ); +} + + +void SAL_CALL + MacSpellChecker::initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!pPropHelper) + { + INT32 nLen = rArguments.getLength(); + if (2 == nLen) + { + Reference< XPropertySet > xPropSet; + rArguments.getConstArray()[0] >>= xPropSet; + //rArguments.getConstArray()[1] >>= xDicList; + + //! Pointer allows for access of the non-UNO functions. + //! And the reference to the UNO-functions while increasing + //! the ref-count and will implicitly free the memory + //! when the object is not longer used. + pPropHelper = new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + else + DBG_ERROR( "wrong number of arguments in sequence" ); + + } +} + + +void SAL_CALL + MacSpellChecker::dispose() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing) + { + bDisposing = TRUE; + EventObject aEvtObj( (XSpellChecker *) this ); + aEvtListeners.disposeAndClear( aEvtObj ); + } +} + + +void SAL_CALL + MacSpellChecker::addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.addInterface( rxListener ); +} + + +void SAL_CALL + MacSpellChecker::removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.removeInterface( rxListener ); +} + + +/////////////////////////////////////////////////////////////////////////// +// Service specific part +// + +OUString SAL_CALL MacSpellChecker::getImplementationName() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + return getImplementationName_Static(); +} + + +sal_Bool SAL_CALL MacSpellChecker::supportsService( const OUString& ServiceName ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNL = getSupportedServiceNames(); + const OUString * pArray = aSNL.getConstArray(); + for( INT32 i = 0; i < aSNL.getLength(); i++ ) + if( pArray[i] == ServiceName ) + return TRUE; + return FALSE; +} + + +Sequence< OUString > SAL_CALL MacSpellChecker::getSupportedServiceNames() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + return getSupportedServiceNames_Static(); +} + + +Sequence< OUString > MacSpellChecker::getSupportedServiceNames_Static() + throw() +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNS( 1 ); // auch mehr als 1 Service moeglich + aSNS.getArray()[0] = A2OU( SN_SPELLCHECKER ); + return aSNS; +} + + +sal_Bool SAL_CALL MacSpellChecker_writeInfo( + void * /*pServiceManager*/, registry::XRegistryKey * pRegistryKey ) +{ + + try + { + String aImpl( '/' ); + aImpl += MacSpellChecker::getImplementationName_Static().getStr(); + aImpl.AppendAscii( "/UNO/SERVICES" ); + Reference< registry::XRegistryKey > xNewKey = + pRegistryKey->createKey( aImpl ); + Sequence< OUString > aServices = + MacSpellChecker::getSupportedServiceNames_Static(); + for( INT32 i = 0; i < aServices.getLength(); i++ ) + xNewKey->createKey( aServices.getConstArray()[i] ); + + return sal_True; + } + catch(Exception &) + { + return sal_False; + } +} + + +void * SAL_CALL MacSpellChecker_getFactory( const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, void * ) +{ + void * pRet = 0; + if ( !MacSpellChecker::getImplementationName_Static().compareToAscii( pImplName ) ) + { + Reference< XSingleServiceFactory > xFactory = + cppu::createOneInstanceFactory( + pServiceManager, + MacSpellChecker::getImplementationName_Static(), + MacSpellChecker_CreateInstance, + MacSpellChecker::getSupportedServiceNames_Static()); + // acquire, because we return an interface pointer instead of a reference + xFactory->acquire(); + pRet = xFactory.get(); + } + return pRet; +} + + +/////////////////////////////////////////////////////////////////////////// diff --git a/lingucomponent/source/spellcheck/macosxspell/macspellimp.hxx b/lingucomponent/source/spellcheck/macosxspell/macspellimp.hxx new file mode 100644 index 000000000000..b3cfce92fd8b --- /dev/null +++ b/lingucomponent/source/spellcheck/macosxspell/macspellimp.hxx @@ -0,0 +1,193 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _MACSPELLIMP_H_ +#define _MACSPELLIMP_H_ + +#include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type +#include <cppuhelper/implbase1.hxx> // helper for implementations +#include <cppuhelper/implbase6.hxx> // helper for implementations + +#ifdef MACOSX +#include <premac.h> +#include <Carbon/Carbon.h> +#import <Cocoa/Cocoa.h> +#include <postmac.h> +#endif +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XServiceDisplayName.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/beans/PropertyValues.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/linguistic2/XSpellChecker.hpp> +#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp> +#include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp> +#include <tools/table.hxx> + +#include <linguistic/misc.hxx> +#include <linguistic/lngprophelp.hxx> + +#include <lingutil.hxx> + +using namespace ::rtl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::linguistic2; + + +/////////////////////////////////////////////////////////////////////////// + + +class MacSpellChecker : + public cppu::WeakImplHelper6 + < + XSpellChecker, + XLinguServiceEventBroadcaster, + XInitialization, + XComponent, + XServiceInfo, + XServiceDisplayName + > +{ + Sequence< Locale > aSuppLocales; +// Hunspell ** aDicts; + rtl_TextEncoding * aDEncs; + Locale * aDLocs; + OUString * aDNames; + sal_Int32 numdict; + NSSpellChecker * macSpell; + int macTag; //unique tag for this doc + + ::cppu::OInterfaceContainerHelper aEvtListeners; + Reference< XPropertyChangeListener > xPropHelper; + linguistic::PropertyHelper_Spell * pPropHelper; + BOOL bDisposing; + + // disallow copy-constructor and assignment-operator for now + MacSpellChecker(const MacSpellChecker &); + MacSpellChecker & operator = (const MacSpellChecker &); + + linguistic::PropertyHelper_Spell & GetPropHelper_Impl(); + linguistic::PropertyHelper_Spell & GetPropHelper() + { + return pPropHelper ? *pPropHelper : GetPropHelper_Impl(); + } + + INT16 GetSpellFailure( const OUString &rWord, const Locale &rLocale ); + Reference< XSpellAlternatives > + GetProposals( const OUString &rWord, const Locale &rLocale ); + +public: + MacSpellChecker(); + virtual ~MacSpellChecker(); + + // XSupportedLocales (for XSpellChecker) + virtual Sequence< Locale > SAL_CALL + getLocales() + throw(RuntimeException); + virtual sal_Bool SAL_CALL + hasLocale( const Locale& rLocale ) + throw(RuntimeException); + + // XSpellChecker + virtual sal_Bool SAL_CALL + isValid( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, + RuntimeException); + virtual Reference< XSpellAlternatives > SAL_CALL + spell( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, + RuntimeException); + + // XLinguServiceEventBroadcaster + virtual sal_Bool SAL_CALL + addLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException); + virtual sal_Bool SAL_CALL + removeLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException); + + // XServiceDisplayName + virtual OUString SAL_CALL + getServiceDisplayName( const Locale& rLocale ) + throw(RuntimeException); + + // XInitialization + virtual void SAL_CALL + initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException); + + // XComponent + virtual void SAL_CALL + dispose() + throw(RuntimeException); + virtual void SAL_CALL + addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + virtual void SAL_CALL + removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + + //////////////////////////////////////////////////////////// + // Service specific part + // + + // XServiceInfo + virtual OUString SAL_CALL + getImplementationName() + throw(RuntimeException); + virtual sal_Bool SAL_CALL + supportsService( const OUString& rServiceName ) + throw(RuntimeException); + virtual Sequence< OUString > SAL_CALL + getSupportedServiceNames() + throw(RuntimeException); + + + static inline OUString + getImplementationName_Static() throw(); + static Sequence< OUString > + getSupportedServiceNames_Static() throw(); +}; + +inline OUString MacSpellChecker::getImplementationName_Static() throw() +{ + return A2OU( "org.openoffice.lingu.MacOSXSpellChecker" ); +} + + + +/////////////////////////////////////////////////////////////////////////// + +#endif + diff --git a/lingucomponent/source/spellcheck/macosxspell/makefile.mk b/lingucomponent/source/spellcheck/macosxspell/makefile.mk new file mode 100644 index 000000000000..6414560e0f4a --- /dev/null +++ b/lingucomponent/source/spellcheck/macosxspell/makefile.mk @@ -0,0 +1,93 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ = ..$/..$/.. + +PRJNAME = lingucomponent +TARGET = MacOSXSpell +ENABLE_EXCEPTIONS=TRUE +USE_DEFFILE=TRUE + +.IF "$(GUIBASE)"=="aqua" + +#----- Settings --------------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SYSTEM_HUNSPELL)" != "YES" +HUNSPELL_CFLAGS += -I$(SOLARINCDIR)$/hunspell +.ENDIF + +CXXFLAGS += $(HUNSPELL_CFLAGS) +CFLAGSCXX += $(HUNSPELL_CFLAGS) +CFLAGSCC += $(HUNSPELL_CFLAGS) + +# --- Files -------------------------------------------------------- + +CFLAGSCXX+=$(OBJCXXFLAGS) +CFLAGSCXX+=-I..$/..$/lingutil + +EXCEPTIONSFILES= \ + $(SLO)$/macspellimp.obj + + +SLOFILES= \ + $(SLO)$/macreg.obj\ + $(SLO)$/macspellimp.obj + + +SHL1TARGET= $(TARGET)$(DLLPOSTFIX) + +SHL1STDLIBS= \ + $(CPPULIB) \ + $(CPPUHELPERLIB) \ + $(VOSLIB) \ + $(TOOLSLIB) \ + $(SVLLIB) \ + $(SALLIB) \ + $(UCBHELPERLIB) \ + $(UNOTOOLSLIB) \ + $(LNGLIB) + +SHL1STDLIBS+= -framework Cocoa + +# build DLL +SHL1LIBS= $(SLB)$/$(TARGET).lib +SHL1IMPLIB= i$(TARGET) +SHL1DEPN= $(SHL1LIBS) +SHL1DEF= $(MISC)$/$(SHL1TARGET).def + +SHL1VERSIONMAP=$(SOLARENV)/src/component.map + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + +.ELSE +dummy: + @echo " Nothing to build for GUIBASE=$(GUIBASE)" +.ENDIF diff --git a/lingucomponent/source/spellcheck/spell/exports.dxp b/lingucomponent/source/spellcheck/spell/exports.dxp new file mode 100644 index 000000000000..b0f85bf7bebf --- /dev/null +++ b/lingucomponent/source/spellcheck/spell/exports.dxp @@ -0,0 +1,3 @@ +component_getFactory +component_getImplementationEnvironment +component_writeInfo diff --git a/lingucomponent/source/spellcheck/spell/makefile.mk b/lingucomponent/source/spellcheck/spell/makefile.mk new file mode 100644 index 000000000000..4173d4dbae22 --- /dev/null +++ b/lingucomponent/source/spellcheck/spell/makefile.mk @@ -0,0 +1,87 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ = ..$/..$/.. + +PRJNAME = lingucomponent +TARGET = spell +ENABLE_EXCEPTIONS=TRUE +USE_DEFFILE=TRUE + +.IF "$(SYSTEM_HUNSPELL)" != "YES" +HUNSPELL_CFLAGS += -I$(SOLARINCDIR)$/hunspell +.ENDIF + +#----- Settings --------------------------------------------------------- + +.INCLUDE : settings.mk + +# --- Files -------------------------------------------------------- + +CXXFLAGS += -I$(PRJ)$/source$/lingutil $(HUNSPELL_CFLAGS) +CFLAGSCXX += -I$(PRJ)$/source$/lingutil $(HUNSPELL_CFLAGS) +CFLAGSCC += -I$(PRJ)$/source$/lingutil $(HUNSPELL_CFLAGS) + +EXCEPTIONSFILES= \ + $(SLO)$/sspellimp.obj + +SLOFILES= \ + $(SLO)$/sreg.obj\ + $(SLO)$/sspellimp.obj + + +SHL1TARGET= $(TARGET)$(DLLPOSTFIX) + +SHL1STDLIBS= \ + $(CPPULIB) \ + $(CPPUHELPERLIB) \ + $(I18NISOLANGLIB) \ + $(TOOLSLIB) \ + $(UNOTOOLSLIB) \ + $(SVLLIB) \ + $(SALLIB) \ + $(LNGLIB) \ + $(ULINGULIB) \ + $(ICUUCLIB) \ + $(HUNSPELLLIB) + +# build DLL +SHL1LIBS= $(SLB)$/$(TARGET).lib $(SLB)$/libulingu.lib +SHL1IMPLIB= i$(TARGET) +SHL1DEPN= $(SHL1LIBS) +SHL1DEF= $(MISC)$/$(SHL1TARGET).def + +SHL1VERSIONMAP=$(SOLARENV)/src/component.map + +# build DEF file +DEF1NAME =$(SHL1TARGET) +DEF1EXPORTFILE= exports.dxp + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/lingucomponent/source/spellcheck/spell/sreg.cxx b/lingucomponent/source/spellcheck/spell/sreg.cxx new file mode 100644 index 000000000000..bd97403c89d6 --- /dev/null +++ b/lingucomponent/source/spellcheck/spell/sreg.cxx @@ -0,0 +1,86 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + + +#include <cppuhelper/factory.hxx> // helper for factories +#include <rtl/string.hxx> + +#include <com/sun/star/registry/XRegistryKey.hpp> + +using namespace rtl; +using namespace com::sun::star::lang; +using namespace com::sun::star::registry; + +//////////////////////////////////////// +// declaration of external RegEntry-functions defined by the service objects +// + +extern sal_Bool SAL_CALL SpellChecker_writeInfo( + void * /*pServiceManager*/, XRegistryKey * pRegistryKey ); + +extern void * SAL_CALL SpellChecker_getFactory( + const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, + void * /*pRegistryKey*/ ); +//////////////////////////////////////// +// definition of the two functions that are used to provide the services +// + +extern "C" +{ + +sal_Bool SAL_CALL component_writeInfo( + void * pServiceManager, XRegistryKey * pRegistryKey ) +{ + return SpellChecker_writeInfo( pServiceManager, pRegistryKey ); +} + +void SAL_CALL component_getImplementationEnvironment( + const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ ) +{ + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} + +void * SAL_CALL component_getFactory( + const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) +{ + void * pRet = NULL; + pRet = SpellChecker_getFactory( + pImplName, + reinterpret_cast< XMultiServiceFactory * >( pServiceManager ), + pRegistryKey ); + + return pRet; +} + +} + +/////////////////////////////////////////////////////////////////////////// + diff --git a/lingucomponent/source/spellcheck/spell/sspellimp.cxx b/lingucomponent/source/spellcheck/spell/sspellimp.cxx new file mode 100644 index 000000000000..0cb6ad8ec3da --- /dev/null +++ b/lingucomponent/source/spellcheck/spell/sspellimp.cxx @@ -0,0 +1,722 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp> + +#include <com/sun/star/linguistic2/SpellFailure.hpp> +#include <cppuhelper/factory.hxx> // helper for factories +#include <com/sun/star/registry/XRegistryKey.hpp> +#include <tools/debug.hxx> +#include <unotools/processfactory.hxx> +#include <osl/mutex.hxx> + +#include <hunspell.hxx> +#include <dictmgr.hxx> + +#ifndef _SPELLIMP_HXX +#include <sspellimp.hxx> +#endif + +#include <linguistic/lngprops.hxx> +#include <linguistic/spelldta.hxx> +#include <i18npool/mslangid.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/lingucfg.hxx> +#include <unotools/useroptions.hxx> +#include <osl/file.hxx> +#include <rtl/ustrbuf.hxx> + +#include <lingutil.hxx> + +#include <list> +#include <set> +#include <string.h> + +using namespace utl; +using namespace osl; +using namespace rtl; +using namespace com::sun::star; +using namespace com::sun::star::beans; +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace com::sun::star::linguistic2; +using namespace linguistic; + +// XML-header of SPELLML queries +#define SPELLML_HEADER "<?xml?>" + +/////////////////////////////////////////////////////////////////////////// + +SpellChecker::SpellChecker() : + aEvtListeners ( GetLinguMutex() ) +{ + aDicts = NULL; + aDEncs = NULL; + aDLocs = NULL; + aDNames = NULL; + bDisposing = FALSE; + pPropHelper = NULL; + numdict = 0; +} + + +SpellChecker::~SpellChecker() +{ + if (aDicts) { + for (int i = 0; i < numdict; i++) { + if (aDicts[i]) delete aDicts[i]; + aDicts[i] = NULL; + } + delete[] aDicts; + } + aDicts = NULL; + numdict = 0; + if (aDEncs) delete[] aDEncs; + aDEncs = NULL; + if (aDLocs) delete[] aDLocs; + aDLocs = NULL; + if (aDNames) delete[] aDNames; + aDNames = NULL; + if (pPropHelper) + pPropHelper->RemoveAsPropListener(); +} + + +PropertyHelper_Spell & SpellChecker::GetPropHelper_Impl() +{ + if (!pPropHelper) + { + Reference< XPropertySet > xPropSet( GetLinguProperties(), UNO_QUERY ); + + pPropHelper = new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + return *pPropHelper; +} + + +Sequence< Locale > SAL_CALL SpellChecker::getLocales() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + // this routine should return the locales supported by the installed + // dictionaries. + + if (!numdict) + { + SvtLinguConfig aLinguCfg; + + // get list of extension dictionaries-to-use + // (or better speaking: the list of dictionaries using the + // new configuration entries). + std::list< SvtLinguConfigDictionaryEntry > aDics; + uno::Sequence< rtl::OUString > aFormatList; + aLinguCfg.GetSupportedDictionaryFormatsFor( A2OU("SpellCheckers"), + A2OU("org.openoffice.lingu.MySpellSpellChecker"), aFormatList ); + sal_Int32 nLen = aFormatList.getLength(); + for (sal_Int32 i = 0; i < nLen; ++i) + { + std::vector< SvtLinguConfigDictionaryEntry > aTmpDic( + aLinguCfg.GetActiveDictionariesByFormat( aFormatList[i] ) ); + aDics.insert( aDics.end(), aTmpDic.begin(), aTmpDic.end() ); + } + + //!! for compatibility with old dictionaries (the ones not using extensions + //!! or new configuration entries, but still using the dictionary.lst file) + //!! Get the list of old style spell checking dictionaries to use... + std::vector< SvtLinguConfigDictionaryEntry > aOldStyleDics( + GetOldStyleDics( "DICT" ) ); + + // to prefer dictionaries with configuration entries we will only + // use those old style dictionaries that add a language that + // is not yet supported by the list od new style dictionaries + MergeNewStyleDicsAndOldStyleDics( aDics, aOldStyleDics ); + + numdict = aDics.size(); + if (numdict) + { + // get supported locales from the dictionaries-to-use... + sal_Int32 k = 0; + std::set< rtl::OUString, lt_rtl_OUString > aLocaleNamesSet; + std::list< SvtLinguConfigDictionaryEntry >::const_iterator aDictIt; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + { + uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames ); + sal_Int32 nLen2 = aLocaleNames.getLength(); + for (k = 0; k < nLen2; ++k) + { + aLocaleNamesSet.insert( aLocaleNames[k] ); + } + } + // ... and add them to the resulting sequence + aSuppLocales.realloc( aLocaleNamesSet.size() ); + std::set< rtl::OUString, lt_rtl_OUString >::const_iterator aItB; + k = 0; + for (aItB = aLocaleNamesSet.begin(); aItB != aLocaleNamesSet.end(); ++aItB) + { + Locale aTmp( MsLangId::convertLanguageToLocale( + MsLangId::convertIsoStringToLanguage( *aItB ))); + aSuppLocales[k++] = aTmp; + } + + //! For each dictionary and each locale we need a seperate entry. + //! If this results in more than one dictionary per locale than (for now) + //! it is undefined which dictionary gets used. + //! In the future the implementation should support using several dictionaries + //! for one locale. + numdict = 0; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + numdict = numdict + aDictIt->aLocaleNames.getLength(); + + // add dictionary information + aDicts = new Hunspell* [numdict]; + aDEncs = new rtl_TextEncoding [numdict]; + aDLocs = new Locale [numdict]; + aDNames = new OUString [numdict]; + k = 0; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + { + if (aDictIt->aLocaleNames.getLength() > 0 && + aDictIt->aLocations.getLength() > 0) + { + uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames ); + sal_Int32 nLocales = aLocaleNames.getLength(); + + // currently only one language per dictionary is supported in the actual implementation... + // Thus here we work-around this by adding the same dictionary several times. + // Once for each of it's supported locales. + for (sal_Int32 i = 0; i < nLocales; ++i) + { + aDicts[k] = NULL; + aDEncs[k] = 0; + aDLocs[k] = MsLangId::convertLanguageToLocale( + MsLangId::convertIsoStringToLanguage( aLocaleNames[i] )); + // also both files have to be in the same directory and the + // file names must only differ in the extension (.aff/.dic). + // Thus we use the first location only and strip the extension part. + rtl::OUString aLocation = aDictIt->aLocations[0]; + sal_Int32 nPos = aLocation.lastIndexOf( '.' ); + aLocation = aLocation.copy( 0, nPos ); + aDNames[k] = aLocation; + + ++k; + } + } + } + DBG_ASSERT( k == numdict, "index mismatch?" ); + } + else + { + /* no dictionary found so register no dictionaries */ + numdict = 0; + aDicts = NULL; + aDEncs = NULL; + aDLocs = NULL; + aDNames = NULL; + aSuppLocales.realloc(0); + } + } + + return aSuppLocales; +} + + +sal_Bool SAL_CALL SpellChecker::hasLocale(const Locale& rLocale) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!aSuppLocales.getLength()) + getLocales(); + + INT32 nLen = aSuppLocales.getLength(); + for (INT32 i = 0; i < nLen; ++i) + { + const Locale *pLocale = aSuppLocales.getConstArray(); + if (rLocale == pLocale[i]) + { + bRes = TRUE; + break; + } + } + return bRes; +} + +INT16 SpellChecker::GetSpellFailure( const OUString &rWord, const Locale &rLocale ) +{ + Hunspell * pMS; + rtl_TextEncoding aEnc; + + // initialize a myspell object for each dictionary once + // (note: mutex is held higher up in isValid) + + + INT16 nRes = -1; + + // first handle smart quotes both single and double + OUStringBuffer rBuf(rWord); + sal_Int32 n = rBuf.getLength(); + sal_Unicode c; + for (sal_Int32 ix=0; ix < n; ix++) { + c = rBuf.charAt(ix); + if ((c == 0x201C) || (c == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022); + if ((c == 0x2018) || (c == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027); + } + OUString nWord(rBuf.makeStringAndClear()); + + if (n) + { + for (sal_Int32 i = 0; i < numdict; ++i) { + pMS = NULL; + aEnc = 0; + + if (rLocale == aDLocs[i]) + { + if (!aDicts[i]) + { + OUString dicpath = aDNames[i] + A2OU(".dic"); + OUString affpath = aDNames[i] + A2OU(".aff"); + OUString dict; + OUString aff; + osl::FileBase::getSystemPathFromFileURL(dicpath,dict); + osl::FileBase::getSystemPathFromFileURL(affpath,aff); + OString aTmpaff(OU2ENC(aff,osl_getThreadTextEncoding())); + OString aTmpdict(OU2ENC(dict,osl_getThreadTextEncoding())); + +#if defined(WNT) + // workaround for Windows specifc problem that the + // path length in calls to 'fopen' is limted to somewhat + // about 120+ characters which will usually be exceed when + // using dictionaries as extensions. + aTmpaff = Win_GetShortPathName( aff ); + aTmpdict = Win_GetShortPathName( dict ); +#endif + + aDicts[i] = new Hunspell(aTmpaff.getStr(),aTmpdict.getStr()); + aDEncs[i] = 0; + if (aDicts[i]) { + char * dic_encoding = aDicts[i]->get_dic_encoding(); + aDEncs[i] = rtl_getTextEncodingFromUnixCharset(aDicts[i]->get_dic_encoding()); + if (aDEncs[i] == RTL_TEXTENCODING_DONTKNOW) { + if (strcmp("ISCII-DEVANAGARI", dic_encoding) == 0) { + aDEncs[i] = RTL_TEXTENCODING_ISCII_DEVANAGARI; + } else if (strcmp("UTF-8", dic_encoding) == 0) { + aDEncs[i] = RTL_TEXTENCODING_UTF8; + } + } + } + } + pMS = aDicts[i]; + aEnc = aDEncs[i]; + } + if (pMS) + { + OString aWrd(OU2ENC(nWord,aEnc)); + int rVal = pMS->spell((char*)aWrd.getStr()); + if (rVal != 1) + { + nRes = SpellFailure::SPELLING_ERROR; + } else { + return -1; + } + pMS = NULL; + } + } + } + + return nRes; +} + + +sal_Bool SAL_CALL + SpellChecker::isValid( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (rLocale == Locale() || !rWord.getLength()) + return TRUE; + + if (!hasLocale( rLocale )) +#ifdef LINGU_EXCEPTIONS + throw( IllegalArgumentException() ); +#else + return TRUE; +#endif + + // Get property values to be used. + // These are be the default values set in the SN_LINGU_PROPERTIES + // PropertySet which are overridden by the supplied ones from the + // last argument. + // You'll probably like to use a simplier solution than the provided + // one using the PropertyHelper_Spell. + + PropertyHelper_Spell &rHelper = GetPropHelper(); + rHelper.SetTmpPropVals( rProperties ); + + INT16 nFailure = GetSpellFailure( rWord, rLocale ); + if (nFailure != -1 && !rWord.match(A2OU(SPELLML_HEADER), 0)) + { + INT16 nLang = LocaleToLanguage( rLocale ); + // postprocess result for errors that should be ignored + if ( (!rHelper.IsSpellUpperCase() && IsUpper( rWord, nLang )) + || (!rHelper.IsSpellWithDigits() && HasDigits( rWord )) + || (!rHelper.IsSpellCapitalization() + && nFailure == SpellFailure::CAPTION_ERROR) + ) + nFailure = -1; + } + + return (nFailure == -1); +} + + +Reference< XSpellAlternatives > + SpellChecker::GetProposals( const OUString &rWord, const Locale &rLocale ) +{ + // Retrieves the return values for the 'spell' function call in case + // of a misspelled word. + // Especially it may give a list of suggested (correct) words: + + Reference< XSpellAlternatives > xRes; + // note: mutex is held by higher up by spell which covers both + + Hunspell* pMS; + rtl_TextEncoding aEnc; + int count; + int numsug = 0; + + // first handle smart quotes (single and double) + OUStringBuffer rBuf(rWord); + sal_Int32 n = rBuf.getLength(); + sal_Unicode c; + for (sal_Int32 ix=0; ix < n; ix++) { + c = rBuf.charAt(ix); + if ((c == 0x201C) || (c == 0x201D)) rBuf.setCharAt(ix,(sal_Unicode)0x0022); + if ((c == 0x2018) || (c == 0x2019)) rBuf.setCharAt(ix,(sal_Unicode)0x0027); + } + OUString nWord(rBuf.makeStringAndClear()); + + if (n) + { + INT16 nLang = LocaleToLanguage( rLocale ); + + Sequence< OUString > aStr( 0 ); + + for (int i =0; i < numdict; i++) { + pMS = NULL; + aEnc = 0; + count = 0; + + if (rLocale == aDLocs[i]) + { + pMS = aDicts[i]; + aEnc = aDEncs[i]; + } + + if (pMS) + { + char ** suglst = NULL; + OString aWrd(OU2ENC(nWord,aEnc)); + count = pMS->suggest(&suglst, (const char *) aWrd.getStr()); + + if (count) { + aStr.realloc( numsug + count ); + OUString *pStr = aStr.getArray(); + for (int ii=0; ii < count; ++ii) + { + OUString cvtwrd(suglst[ii],strlen(suglst[ii]),aEnc); + pStr[numsug + ii] = cvtwrd; + } + pMS->free_list(&suglst, count); + numsug += count; + } + } + } + + // now return an empty alternative for no suggestions or the list of alternatives if some found + SpellAlternatives *pAlt = new SpellAlternatives; + String aTmp(rWord); + pAlt->SetWordLanguage( aTmp, nLang ); + pAlt->SetFailureType( SpellFailure::SPELLING_ERROR ); + pAlt->SetAlternatives( aStr ); + xRes = pAlt; + return xRes; + + } + return xRes; +} + + + + +Reference< XSpellAlternatives > SAL_CALL + SpellChecker::spell( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (rLocale == Locale() || !rWord.getLength()) + return NULL; + + if (!hasLocale( rLocale )) +#ifdef LINGU_EXCEPTIONS + throw( IllegalArgumentException() ); +#else + return NULL; +#endif + + Reference< XSpellAlternatives > xAlt; + if (!isValid( rWord, rLocale, rProperties )) + { + xAlt = GetProposals( rWord, rLocale ); + } + return xAlt; +} + + +Reference< XInterface > SAL_CALL SpellChecker_CreateInstance( + const Reference< XMultiServiceFactory > & /*rSMgr*/ ) + throw(Exception) +{ + + Reference< XInterface > xService = (cppu::OWeakObject*) new SpellChecker; + return xService; +} + + +sal_Bool SAL_CALL + SpellChecker::addLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!bDisposing && rxLstnr.is()) + { + bRes = GetPropHelper().addLinguServiceEventListener( rxLstnr ); + } + return bRes; +} + + +sal_Bool SAL_CALL + SpellChecker::removeLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!bDisposing && rxLstnr.is()) + { + DBG_ASSERT( xPropHelper.is(), "xPropHelper non existent" ); + bRes = GetPropHelper().removeLinguServiceEventListener( rxLstnr ); + } + return bRes; +} + + +OUString SAL_CALL + SpellChecker::getServiceDisplayName( const Locale& /*rLocale*/ ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return A2OU( "Hunspell SpellChecker" ); +} + + +void SAL_CALL + SpellChecker::initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!pPropHelper) + { + INT32 nLen = rArguments.getLength(); + if (2 == nLen) + { + Reference< XPropertySet > xPropSet; + rArguments.getConstArray()[0] >>= xPropSet; + //rArguments.getConstArray()[1] >>= xDicList; + + //! Pointer allows for access of the non-UNO functions. + //! And the reference to the UNO-functions while increasing + //! the ref-count and will implicitly free the memory + //! when the object is not longer used. + pPropHelper = new PropertyHelper_Spell( (XSpellChecker *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + else { + DBG_ERROR( "wrong number of arguments in sequence" ); + } + + } +} + + +void SAL_CALL + SpellChecker::dispose() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing) + { + bDisposing = TRUE; + EventObject aEvtObj( (XSpellChecker *) this ); + aEvtListeners.disposeAndClear( aEvtObj ); + } +} + + +void SAL_CALL + SpellChecker::addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.addInterface( rxListener ); +} + + +void SAL_CALL + SpellChecker::removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.removeInterface( rxListener ); +} + + +/////////////////////////////////////////////////////////////////////////// +// Service specific part +// + +OUString SAL_CALL SpellChecker::getImplementationName() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + return getImplementationName_Static(); +} + + +sal_Bool SAL_CALL SpellChecker::supportsService( const OUString& ServiceName ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNL = getSupportedServiceNames(); + const OUString * pArray = aSNL.getConstArray(); + for( INT32 i = 0; i < aSNL.getLength(); i++ ) + if( pArray[i] == ServiceName ) + return TRUE; + return FALSE; +} + + +Sequence< OUString > SAL_CALL SpellChecker::getSupportedServiceNames() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + return getSupportedServiceNames_Static(); +} + + +Sequence< OUString > SpellChecker::getSupportedServiceNames_Static() + throw() +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNS( 1 ); // auch mehr als 1 Service moeglich + aSNS.getArray()[0] = A2OU( SN_SPELLCHECKER ); + return aSNS; +} + + +sal_Bool SAL_CALL SpellChecker_writeInfo( + void * /*pServiceManager*/, registry::XRegistryKey * pRegistryKey ) +{ + + try + { + String aImpl( '/' ); + aImpl += SpellChecker::getImplementationName_Static().getStr(); + aImpl.AppendAscii( "/UNO/SERVICES" ); + Reference< registry::XRegistryKey > xNewKey = + pRegistryKey->createKey( aImpl ); + Sequence< OUString > aServices = + SpellChecker::getSupportedServiceNames_Static(); + for( INT32 i = 0; i < aServices.getLength(); i++ ) + xNewKey->createKey( aServices.getConstArray()[i] ); + + return sal_True; + } + catch(Exception &) + { + return sal_False; + } +} + + +void * SAL_CALL SpellChecker_getFactory( const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, void * ) +{ + void * pRet = 0; + if ( !SpellChecker::getImplementationName_Static().compareToAscii( pImplName ) ) + { + Reference< XSingleServiceFactory > xFactory = + cppu::createOneInstanceFactory( + pServiceManager, + SpellChecker::getImplementationName_Static(), + SpellChecker_CreateInstance, + SpellChecker::getSupportedServiceNames_Static()); + // acquire, because we return an interface pointer instead of a reference + xFactory->acquire(); + pRet = xFactory.get(); + } + return pRet; +} + + +/////////////////////////////////////////////////////////////////////////// diff --git a/lingucomponent/source/spellcheck/spell/sspellimp.hxx b/lingucomponent/source/spellcheck/spell/sspellimp.hxx new file mode 100644 index 000000000000..94c57fa8b742 --- /dev/null +++ b/lingucomponent/source/spellcheck/spell/sspellimp.hxx @@ -0,0 +1,184 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _LINGU2_SPELLIMP_HXX_ +#define _LINGU2_SPELLIMP_HXX_ + +#include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type +#include <cppuhelper/implbase1.hxx> // helper for implementations +#include <cppuhelper/implbase6.hxx> // helper for implementations +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XServiceDisplayName.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/beans/PropertyValues.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/linguistic2/XSpellChecker.hpp> +#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp> +#include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp> +#include <tools/table.hxx> + +#include <linguistic/misc.hxx> +#include <linguistic/lngprophelp.hxx> + +#include <lingutil.hxx> + +using namespace ::rtl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::linguistic2; + + +/////////////////////////////////////////////////////////////////////////// + + +class SpellChecker : + public cppu::WeakImplHelper6 + < + XSpellChecker, + XLinguServiceEventBroadcaster, + XInitialization, + XComponent, + XServiceInfo, + XServiceDisplayName + > +{ + Sequence< Locale > aSuppLocales; + Hunspell ** aDicts; + rtl_TextEncoding * aDEncs; + Locale * aDLocs; + OUString * aDNames; + sal_Int32 numdict; + + ::cppu::OInterfaceContainerHelper aEvtListeners; + Reference< XPropertyChangeListener > xPropHelper; + linguistic::PropertyHelper_Spell * pPropHelper; + BOOL bDisposing; + + // disallow copy-constructor and assignment-operator for now + SpellChecker(const SpellChecker &); + SpellChecker & operator = (const SpellChecker &); + + linguistic::PropertyHelper_Spell & GetPropHelper_Impl(); + linguistic::PropertyHelper_Spell & GetPropHelper() + { + return pPropHelper ? *pPropHelper : GetPropHelper_Impl(); + } + + INT16 GetSpellFailure( const OUString &rWord, const Locale &rLocale ); + Reference< XSpellAlternatives > + GetProposals( const OUString &rWord, const Locale &rLocale ); + +public: + SpellChecker(); + virtual ~SpellChecker(); + + // XSupportedLocales (for XSpellChecker) + virtual Sequence< Locale > SAL_CALL + getLocales() + throw(RuntimeException); + virtual sal_Bool SAL_CALL + hasLocale( const Locale& rLocale ) + throw(RuntimeException); + + // XSpellChecker + virtual sal_Bool SAL_CALL + isValid( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, + RuntimeException); + virtual Reference< XSpellAlternatives > SAL_CALL + spell( const OUString& rWord, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, + RuntimeException); + + // XLinguServiceEventBroadcaster + virtual sal_Bool SAL_CALL + addLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException); + virtual sal_Bool SAL_CALL + removeLinguServiceEventListener( + const Reference< XLinguServiceEventListener >& rxLstnr ) + throw(RuntimeException); + + // XServiceDisplayName + virtual OUString SAL_CALL + getServiceDisplayName( const Locale& rLocale ) + throw(RuntimeException); + + // XInitialization + virtual void SAL_CALL + initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException); + + // XComponent + virtual void SAL_CALL + dispose() + throw(RuntimeException); + virtual void SAL_CALL + addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + virtual void SAL_CALL + removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + + //////////////////////////////////////////////////////////// + // Service specific part + // + + // XServiceInfo + virtual OUString SAL_CALL + getImplementationName() + throw(RuntimeException); + virtual sal_Bool SAL_CALL + supportsService( const OUString& rServiceName ) + throw(RuntimeException); + virtual Sequence< OUString > SAL_CALL + getSupportedServiceNames() + throw(RuntimeException); + + + static inline OUString + getImplementationName_Static() throw(); + static Sequence< OUString > + getSupportedServiceNames_Static() throw(); +}; + +inline OUString SpellChecker::getImplementationName_Static() throw() +{ + return A2OU( "org.openoffice.lingu.MySpellSpellChecker" ); +} + + + +/////////////////////////////////////////////////////////////////////////// + +#endif + diff --git a/lingucomponent/source/thesaurus/libnth/exports.dxp b/lingucomponent/source/thesaurus/libnth/exports.dxp new file mode 100644 index 000000000000..b0f85bf7bebf --- /dev/null +++ b/lingucomponent/source/thesaurus/libnth/exports.dxp @@ -0,0 +1,3 @@ +component_getFactory +component_getImplementationEnvironment +component_writeInfo diff --git a/lingucomponent/source/thesaurus/libnth/makefile.mk b/lingucomponent/source/thesaurus/libnth/makefile.mk new file mode 100644 index 000000000000..58e4d97e5561 --- /dev/null +++ b/lingucomponent/source/thesaurus/libnth/makefile.mk @@ -0,0 +1,107 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ = ..$/..$/.. + +PRJNAME = lingucomponent +TARGET = lnth +ENABLE_EXCEPTIONS=TRUE +USE_DEFFILE=TRUE + + +.IF "$(MYTHESLIB)"=="" +.IF "$(GUI)"=="UNX" +MYTHESLIB=-lmythes +.ENDIF # unx +.IF "$(GUI)"=="OS2" +MYTHESLIB=$(SLB)\libmythes.lib +.ENDIF # os2 +.IF "$(GUI)"=="WNT" +MYTHESLIB=libmythes.lib +.ENDIF # wnt +.ENDIF + +#----- Settings --------------------------------------------------------- + +.INCLUDE : settings.mk + +# --- Files -------------------------------------------------------- + +.IF "$(SYSTEM_HUNSPELL)" != "YES" +HUNSPELL_CFLAGS += -I$(SOLARINCDIR)$/hunspell +.ENDIF + +.IF "$(SYSTEM_MYTHES)" != "YES" +CXXFLAGS += -I..$/mythes +CFLAGSCXX += -I..$/mythes +CFLAGSCC += -I..$/mythes +.ENDIF +CXXFLAGS += -I$(PRJ)$/source$/lingutil $(HUNSPELL_CFLAGS) +CFLAGSCXX += -I$(PRJ)$/source$/lingutil $(HUNSPELL_CFLAGS) +CFLAGSCC += -I$(PRJ)$/source$/lingutil $(HUNSPELL_CFLAGS) + +EXCEPTIONSFILES= \ + $(SLO)$/nthesimp.obj \ + $(SLO)$/nthesdta.obj + +SLOFILES= \ + $(SLO)$/nthesdta.obj\ + $(SLO)$/ntreg.obj\ + $(SLO)$/nthesimp.obj + + +SHL1TARGET= $(TARGET)$(DLLPOSTFIX) + +SHL1STDLIBS= \ + $(CPPULIB) \ + $(CPPUHELPERLIB) \ + $(TOOLSLIB) \ + $(I18NISOLANGLIB) \ + $(SVLLIB) \ + $(SALLIB) \ + $(UNOTOOLSLIB) \ + $(LNGLIB) \ + $(ULINGULIB) \ + $(MYTHESLIB) \ + $(HUNSPELLLIB) + +# build DLL +SHL1LIBS= $(SLB)$/$(TARGET).lib $(SLB)$/libulingu.lib +SHL1IMPLIB= i$(TARGET) +SHL1DEPN= $(SHL1LIBS) +SHL1DEF= $(MISC)$/$(SHL1TARGET).def + +SHL1VERSIONMAP=$(SOLARENV)/src/component.map + +# build DEF file +DEF1NAME =$(SHL1TARGET) +DEF1EXPORTFILE= exports.dxp + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/lingucomponent/source/thesaurus/libnth/nthesdta.cxx b/lingucomponent/source/thesaurus/libnth/nthesdta.cxx new file mode 100644 index 000000000000..0c071a4eeb1f --- /dev/null +++ b/lingucomponent/source/thesaurus/libnth/nthesdta.cxx @@ -0,0 +1,116 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" +#include <com/sun/star/uno/Reference.h> +#include <tools/debug.hxx> +#include <unotools/processfactory.hxx> +#include <osl/mutex.hxx> + +#include "nthesdta.hxx" +#include <linguistic/misc.hxx> + +// #include "lngsvcmgr.hxx" + + +using namespace utl; +using namespace osl; +using namespace rtl; +using namespace com::sun::star; +using namespace com::sun::star::beans; +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace com::sun::star::linguistic2; + +namespace linguistic +{ + +/////////////////////////////////////////////////////////////////////////// + +Meaning::Meaning( +#if 0 + const OUString &rTerm, INT16 nLang, + const PropertyHelper_Thes &rHelper ) : +#else + const OUString &rTerm, INT16 nLang) : +#endif + + aSyn ( Sequence< OUString >(1) ), + aTerm (rTerm), + nLanguage (nLang) + +{ +#if 0 + // this is for future use by a german thesaurus when one exists + bIsGermanPreReform = rHelper.IsGermanPreReform; +#endif +} + + +Meaning::~Meaning() +{ +} + + +OUString SAL_CALL Meaning::getMeaning() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return aTerm; +} + + +Sequence< OUString > SAL_CALL Meaning::querySynonyms() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return aSyn; +} + + +void Meaning::SetSynonyms( const Sequence< OUString > &rSyn ) +{ + MutexGuard aGuard( GetLinguMutex() ); + aSyn = rSyn; +} + +void Meaning::SetMeaning( const OUString &rTerm ) +{ + MutexGuard aGuard( GetLinguMutex() ); + aTerm = rTerm; +} + +/////////////////////////////////////////////////////////////////////////// + +} // namespace linguistic + + + + + + diff --git a/lingucomponent/source/thesaurus/libnth/nthesdta.hxx b/lingucomponent/source/thesaurus/libnth/nthesdta.hxx new file mode 100644 index 000000000000..259bf5454359 --- /dev/null +++ b/lingucomponent/source/thesaurus/libnth/nthesdta.hxx @@ -0,0 +1,96 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _LINGUISTIC_THESDTA_HXX_ +#define _LINGUISTIC_THESDTA_HXX_ + + +#include <com/sun/star/linguistic2/XMeaning.hpp> + +#include <tools/solar.h> + +#include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type +#include <cppuhelper/implbase1.hxx> // helper for implementations + + +namespace linguistic +{ + +class Meaning : + public cppu::WeakImplHelper1 + < + ::com::sun::star::linguistic2::XMeaning + > +{ + ::com::sun::star::uno::Sequence< ::rtl::OUString > aSyn; // list of synonyms, may be empty. + ::rtl::OUString aTerm; + INT16 nLanguage; + +#if 0 + // this is for future use by a German thesaurus + BOOL bIsGermanPreReform; +#endif + + // disallow copy-constructor and assignment-operator for now + Meaning(const Meaning &); + Meaning & operator = (const Meaning &); + +public: +#if 0 + Meaning(const ::rtl::OUString &rTerm, INT16 nLang, const PropertyHelper_Thes &rHelper); +#else + Meaning(const ::rtl::OUString &rTerm, INT16 nLang); +#endif + virtual ~Meaning(); + + // XMeaning + virtual ::rtl::OUString SAL_CALL + getMeaning() + throw(::com::sun::star::uno::RuntimeException); + + virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL + querySynonyms() + throw(::com::sun::star::uno::RuntimeException); + + // non-interface specific functions + void SetSynonyms( + const ::com::sun::star::uno::Sequence< ::rtl::OUString > + &rSyn ); + + void SetMeaning(const ::rtl::OUString &rTerm ); + + +}; + + +/////////////////////////////////////////////////////////////////////////// + +} // namespace linguistic + +#endif + + diff --git a/lingucomponent/source/thesaurus/libnth/nthesimp.cxx b/lingucomponent/source/thesaurus/libnth/nthesimp.cxx new file mode 100644 index 000000000000..bd6afffdbd6a --- /dev/null +++ b/lingucomponent/source/thesaurus/libnth/nthesimp.cxx @@ -0,0 +1,795 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + +// include <stdio.h> +#include <com/sun/star/uno/Reference.h> + +#include <cppuhelper/factory.hxx> // helper for factories +#include <com/sun/star/registry/XRegistryKey.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <i18npool/mslangid.hxx> +#include <tools/debug.hxx> +#include <unotools/processfactory.hxx> +#include <osl/mutex.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/lingucfg.hxx> + +#include <rtl/string.hxx> +#include <rtl/ustrbuf.hxx> +#include "nthesimp.hxx" +#include <linguistic/misc.hxx> +#include <linguistic/lngprops.hxx> +#include "nthesdta.hxx" +#include <dictmgr.hxx> + +#include <list> +#include <set> +#include <string.h> + +// values asigned to capitalization types +#define CAPTYPE_UNKNOWN 0 +#define CAPTYPE_NOCAP 1 +#define CAPTYPE_INITCAP 2 +#define CAPTYPE_ALLCAP 3 +#define CAPTYPE_MIXED 4 + +// XML-header to query SPELLML support +#define SPELLML_SUPPORT "<?xml?>" + +using namespace utl; +using namespace osl; +using namespace rtl; +using namespace com::sun::star; +using namespace com::sun::star::beans; +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace com::sun::star::linguistic2; +using namespace linguistic; + + + +/////////////////////////////////////////////////////////////////////////// + +static uno::Reference< XLinguServiceManager > GetLngSvcMgr_Impl() +{ + uno::Reference< XLinguServiceManager > xRes; + uno::Reference< XMultiServiceFactory > xMgr = getProcessServiceFactory(); + if (xMgr.is()) + { + xRes = uno::Reference< XLinguServiceManager > ( xMgr->createInstance( + OUString( RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.linguistic2.LinguServiceManager" ) ) ), UNO_QUERY ) ; + } + return xRes; +} + +Thesaurus::Thesaurus() : + aEvtListeners ( GetLinguMutex() ) +{ + bDisposing = FALSE; + pPropHelper = NULL; + aThes = NULL; + aCharSetInfo = NULL; + aTEncs = NULL; + aTLocs = NULL; + aTNames = NULL; + numthes = 0; +} + + +Thesaurus::~Thesaurus() +{ + + if (aThes) { + for (int i = 0; i < numthes; i++) { + if (aThes[i]) delete aThes[i]; + aThes[i] = NULL; + } + delete[] aThes; + } + aThes = NULL; + if (aCharSetInfo) { + for (int i = 0; i < numthes; i++) { + if (aCharSetInfo[i]) delete aCharSetInfo[i]; + aCharSetInfo[i] = NULL; + } + delete[] aCharSetInfo; + } + aCharSetInfo = NULL; + numthes = 0; + if (aTEncs) delete[] aTEncs; + aTEncs = NULL; + if (aTLocs) delete[] aTLocs; + aTLocs = NULL; + if (aTNames) delete[] aTNames; + aTNames = NULL; + + if (pPropHelper) + pPropHelper->RemoveAsPropListener(); +} + + +PropertyHelper_Thes & Thesaurus::GetPropHelper_Impl() +{ + if (!pPropHelper) + { + Reference< XPropertySet > xPropSet( GetLinguProperties(), UNO_QUERY ); + + pPropHelper = new PropertyHelper_Thes( (XThesaurus *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + return *pPropHelper; +} + + +Sequence< Locale > SAL_CALL Thesaurus::getLocales() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + // this routine should return the locales supported by the installed + // dictionaries. + + if (!numthes) + { + SvtLinguConfig aLinguCfg; + + // get list of dictionaries-to-use + std::list< SvtLinguConfigDictionaryEntry > aDics; + uno::Sequence< rtl::OUString > aFormatList; + aLinguCfg.GetSupportedDictionaryFormatsFor( A2OU("Thesauri"), + A2OU("org.openoffice.lingu.new.Thesaurus"), aFormatList ); + sal_Int32 nLen = aFormatList.getLength(); + for (sal_Int32 i = 0; i < nLen; ++i) + { + std::vector< SvtLinguConfigDictionaryEntry > aTmpDic( + aLinguCfg.GetActiveDictionariesByFormat( aFormatList[i] ) ); + aDics.insert( aDics.end(), aTmpDic.begin(), aTmpDic.end() ); + } + + //!! for compatibility with old dictionaries (the ones not using extensions + //!! or new configuration entries, but still using the dictionary.lst file) + //!! Get the list of old style spell checking dictionaries to use... + std::vector< SvtLinguConfigDictionaryEntry > aOldStyleDics( + GetOldStyleDics( "THES" ) ); + + // to prefer dictionaries with configuration entries we will only + // use those old style dictionaries that add a language that + // is not yet supported by the list od new style dictionaries + MergeNewStyleDicsAndOldStyleDics( aDics, aOldStyleDics ); + + numthes = aDics.size(); + if (numthes) + { + // get supported locales from the dictionaries-to-use... + sal_Int32 k = 0; + std::set< rtl::OUString, lt_rtl_OUString > aLocaleNamesSet; + std::list< SvtLinguConfigDictionaryEntry >::const_iterator aDictIt; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + { + uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames ); + sal_Int32 nLen2 = aLocaleNames.getLength(); + for (k = 0; k < nLen2; ++k) + { + aLocaleNamesSet.insert( aLocaleNames[k] ); + } + } + // ... and add them to the resulting sequence + aSuppLocales.realloc( aLocaleNamesSet.size() ); + std::set< rtl::OUString, lt_rtl_OUString >::const_iterator aItB; + k = 0; + for (aItB = aLocaleNamesSet.begin(); aItB != aLocaleNamesSet.end(); ++aItB) + { + Locale aTmp( MsLangId::convertLanguageToLocale( + MsLangId::convertIsoStringToLanguage( *aItB ))); + aSuppLocales[k++] = aTmp; + } + + //! For each dictionary and each locale we need a seperate entry. + //! If this results in more than one dictionary per locale than (for now) + //! it is undefined which dictionary gets used. + //! In the future the implementation should support using several dictionaries + //! for one locale. + numthes = 0; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + numthes = numthes + aDictIt->aLocaleNames.getLength(); + + // add dictionary information + aThes = new MyThes* [numthes]; + aTEncs = new rtl_TextEncoding [numthes]; + aTLocs = new Locale [numthes]; + aTNames = new OUString [numthes]; + aCharSetInfo = new CharClass* [numthes]; + + k = 0; + for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt) + { + if (aDictIt->aLocaleNames.getLength() > 0 && + aDictIt->aLocations.getLength() > 0) + { + uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames ); + sal_Int32 nLocales = aLocaleNames.getLength(); + + // currently only one language per dictionary is supported in the actual implementation... + // Thus here we work-around this by adding the same dictionary several times. + // Once for each of it's supported locales. + for (sal_Int32 i = 0; i < nLocales; ++i) + { + aThes[k] = NULL; + aTEncs[k] = 0; + aTLocs[k] = MsLangId::convertLanguageToLocale( + MsLangId::convertIsoStringToLanguage( aDictIt->aLocaleNames[i] )); + aCharSetInfo[k] = new CharClass( aTLocs[k] ); + // also both files have to be in the same directory and the + // file names must only differ in the extension (.aff/.dic). + // Thus we use the first location only and strip the extension part. + rtl::OUString aLocation = aDictIt->aLocations[0]; + sal_Int32 nPos = aLocation.lastIndexOf( '.' ); + aLocation = aLocation.copy( 0, nPos ); + aTNames[k] = aLocation; + + ++k; + } + } + } + DBG_ASSERT( k == numthes, "index mismatch?" ); + } + else + { + /* no dictionary found so register no dictionaries */ + numthes = 0; + aThes = NULL; + aTEncs = NULL; + aTLocs = NULL; + aTNames = NULL; + aCharSetInfo = NULL; + aSuppLocales.realloc(0); + } + } + + return aSuppLocales; +} + + + +sal_Bool SAL_CALL Thesaurus::hasLocale(const Locale& rLocale) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + BOOL bRes = FALSE; + if (!aSuppLocales.getLength()) + getLocales(); + INT32 nLen = aSuppLocales.getLength(); + for (INT32 i = 0; i < nLen; ++i) + { + const Locale *pLocale = aSuppLocales.getConstArray(); + if (rLocale == pLocale[i]) + { + bRes = TRUE; + break; + } + } + return bRes; +} + + +Sequence < Reference < ::com::sun::star::linguistic2::XMeaning > > SAL_CALL + Thesaurus::queryMeanings( const OUString& qTerm, const Locale& rLocale, + const PropertyValues& rProperties) + throw(IllegalArgumentException, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + uno::Sequence< Reference< XMeaning > > aMeanings( 1 ); + uno::Sequence< Reference< XMeaning > > noMeanings( 0 ); + uno::Reference< XLinguServiceManager > xLngSvcMgr( GetLngSvcMgr_Impl() ); + uno::Reference< XSpellChecker1 > xSpell; + + OUString rTerm(qTerm); + OUString pTerm(qTerm); + sal_uInt16 ct = CAPTYPE_UNKNOWN; + sal_Int32 stem = 0; + sal_Int32 stem2 = 0; + + INT16 nLanguage = LocaleToLanguage( rLocale ); + + if (nLanguage == LANGUAGE_NONE || !rTerm.getLength()) + return noMeanings; + + if (!hasLocale( rLocale )) +#ifdef LINGU_EXCEPTIONS + throw( IllegalArgumentException() ); +#else + return noMeanings; +#endif + + if (prevTerm == qTerm && prevLocale == nLanguage) return prevMeanings; + + mentry * pmean = NULL; + sal_Int32 nmean = 0; + + PropertyHelper_Thes &rHelper = GetPropHelper(); + rHelper.SetTmpPropVals( rProperties ); + + MyThes * pTH = NULL; + rtl_TextEncoding aEnc = 0; + CharClass * pCC = NULL; + + // find the first thesaurus that matches the locale + for (int i =0; i < numthes; i++) { + if (rLocale == aTLocs[i]) + { + // open up and intialize this thesaurus if need be + if (!aThes[i]) + { + OUString datpath = aTNames[i] + A2OU(".dat"); + OUString idxpath = aTNames[i] + A2OU(".idx"); + OUString ndat; + OUString nidx; + osl::FileBase::getSystemPathFromFileURL(datpath,ndat); + osl::FileBase::getSystemPathFromFileURL(idxpath,nidx); + OString aTmpidx(OU2ENC(nidx,osl_getThreadTextEncoding())); + OString aTmpdat(OU2ENC(ndat,osl_getThreadTextEncoding())); + +#if defined(WNT) + // workaround for Windows specifc problem that the + // path length in calls to 'fopen' is limted to somewhat + // about 120+ characters which will usually be exceed when + // using dictionaries as extensions. + aTmpidx = Win_GetShortPathName( nidx ); + aTmpdat = Win_GetShortPathName( ndat ); +#endif + + aThes[i] = new MyThes(aTmpidx.getStr(),aTmpdat.getStr()); + if (aThes[i]) { + const char * enc_string = aThes[i]->get_th_encoding(); + if (!enc_string) { + aTEncs[i] = rtl_getTextEncodingFromUnixCharset("ISO8859-1"); + } else { + aTEncs[i] = rtl_getTextEncodingFromUnixCharset(enc_string); + if (aTEncs[i] == RTL_TEXTENCODING_DONTKNOW) { + if (strcmp("ISCII-DEVANAGARI", enc_string) == 0) { + aTEncs[i] = RTL_TEXTENCODING_ISCII_DEVANAGARI; + } else if (strcmp("UTF-8", enc_string) == 0) { + aTEncs[i] = RTL_TEXTENCODING_UTF8; + } + } + } + } + } + pTH = aThes[i]; + aEnc = aTEncs[i]; + pCC = aCharSetInfo[i]; + + if (pTH) + break; + } + } + + while (pTH) { + // convert word to all lower case for searching + if (!stem) ct = capitalType(rTerm, pCC); + OUString nTerm(makeLowerCase(rTerm, pCC)); + OString aTmp( OU2ENC(nTerm, aEnc) ); + nmean = pTH->Lookup(aTmp.getStr(),aTmp.getLength(),&pmean); + + if (nmean) aMeanings.realloc( nmean ); + + mentry * pe = pmean; + OUString codeTerm = qTerm; + Reference< XSpellAlternatives > xTmpRes2; + + if (stem) { + xTmpRes2 = xSpell->spell( A2OU("<?xml?><query type='analyze'><word>") + + pTerm + A2OU("</word></query>"), nLanguage, rProperties ); + if (xTmpRes2.is()) { + Sequence<OUString>seq = xTmpRes2->getAlternatives(); + if (seq.getLength() > 0) { + codeTerm = seq[0]; + stem2 = 1; + } +#if 0 + OString o = OUStringToOString(codeTerm, rtl_getTextEncodingFromUnixCharset("UTF-8")); + fprintf(stderr, "CODETERM: %s\n", o.pData->buffer); +#endif + } + } + + for (int j = 0; j < nmean; j++) { + int count = pe->count; + if (count) { + Sequence< OUString > aStr( count ); + OUString *pStr = aStr.getArray(); + + for (int i=0; i < count; i++) { + OUString sTerm(pe->psyns[i],strlen(pe->psyns[i]),aEnc ); + sal_Int32 catpos = sTerm.indexOf('('); + sal_Int32 catpos2 = 0; + OUString catst; + OUString catst2; + if (catpos > 2) { + // remove category name for affixation and casing + catst = A2OU(" ") + sTerm.copy(catpos); + sTerm = sTerm.copy(0, catpos); + sTerm = sTerm.trim(); + } + // generate synonyms with affixes + if (stem && stem2) { + Reference< XSpellAlternatives > xTmpRes; + xTmpRes = xSpell->spell( A2OU("<?xml?><query type='generate'><word>") + + sTerm + A2OU("</word>") + codeTerm + A2OU("</query>"), nLanguage, rProperties ); + if (xTmpRes.is()) { + Sequence<OUString>seq = xTmpRes->getAlternatives(); + for (int k = 0; k < seq.getLength(); k++) { + OString o = OUStringToOString(seq[k], rtl_getTextEncodingFromUnixCharset("UTF-8")); + } + if (seq.getLength() > 0) sTerm = seq[0]; + } + } + if (catpos2) sTerm = catst2 + sTerm; + + sal_uInt16 ct1 = capitalType(sTerm, pCC); + if (CAPTYPE_MIXED == ct1) + ct = ct1; + OUString cTerm; + switch (ct) { + case CAPTYPE_ALLCAP: + { + cTerm = makeUpperCase(sTerm, pCC); + break; + } + case CAPTYPE_INITCAP: + { + cTerm = makeInitCap(sTerm, pCC); + break; + } + default: + { + cTerm = sTerm; + break; + } + } + OUString aAlt( cTerm + catst); + pStr[i] = aAlt; + } +#if 0 + Meaning * pMn = new Meaning(rTerm,nLanguage,rHelper); +#endif + Meaning * pMn = new Meaning(rTerm,nLanguage); + OUString dTerm(pe->defn,strlen(pe->defn),aEnc ); + pMn->SetMeaning(dTerm); + pMn->SetSynonyms(aStr); + Reference<XMeaning>* pMeaning = aMeanings.getArray(); + pMeaning[j] = pMn; + } + pe++; + } + pTH->CleanUpAfterLookup(&pmean,nmean); + + if (nmean) { + prevTerm = qTerm; + prevMeanings = aMeanings; + prevLocale = nLanguage; + return aMeanings; + } + + if (stem || !xLngSvcMgr.is()) return noMeanings; + stem = 1; + + xSpell = uno::Reference< XSpellChecker1 >( xLngSvcMgr->getSpellChecker(), UNO_QUERY ); + if (!xSpell.is() || !xSpell->isValid( A2OU(SPELLML_SUPPORT), nLanguage, rProperties )) { + return noMeanings; + } + Reference< XSpellAlternatives > xTmpRes; + xTmpRes = xSpell->spell( A2OU("<?xml?><query type='stem'><word>") + + rTerm + A2OU("</word></query>"), nLanguage, rProperties ); + if (xTmpRes.is()) { + Sequence<OUString>seq = xTmpRes->getAlternatives(); +#if 0 + for (int i = 0; i < seq.getLength(); i++) { + OString o = OUStringToOString(seq[i], rtl_getTextEncodingFromUnixCharset("UTF-8")); + fprintf(stderr, "%d: %s\n", i + 1, o.pData->buffer); + } +#endif + if (seq.getLength() > 0) { + rTerm = seq[0]; // XXX Use only the first stem + continue; + } + } + + // stem the last word of the synonym (for categories after affixation) + rTerm = rTerm.trim(); + sal_Int32 pos = rTerm.lastIndexOf(' '); + if (!pos) return noMeanings; + xTmpRes = xSpell->spell( A2OU("<?xml?><query type='stem'><word>") + + rTerm.copy(pos + 1) + A2OU("</word></query>"), nLanguage, rProperties ); + if (xTmpRes.is()) { + Sequence<OUString>seq = xTmpRes->getAlternatives(); + if (seq.getLength() > 0) { + pTerm = rTerm.copy(pos + 1); + rTerm = rTerm.copy(0, pos + 1) + seq[0]; +#if 0 + for (int i = 0; i < seq.getLength(); i++) { + OString o = OUStringToOString(seq[i], rtl_getTextEncodingFromUnixCharset("UTF-8")); + fprintf(stderr, "%d: %s\n", i + 1, o.pData->buffer); + } +#endif + continue; + } + } + break; + } + return noMeanings; +} + + + +Reference< XInterface > SAL_CALL Thesaurus_CreateInstance( + const Reference< XMultiServiceFactory > & /*rSMgr*/ ) + throw(Exception) +{ + Reference< XInterface > xService = (cppu::OWeakObject*) new Thesaurus; + return xService; +} + + +OUString SAL_CALL + Thesaurus::getServiceDisplayName( const Locale& /*rLocale*/ ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return A2OU( "OpenOffice.org New Thesaurus" ); +} + + +void SAL_CALL + Thesaurus::initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!pPropHelper) + { + INT32 nLen = rArguments.getLength(); + if (1 == nLen) + { + Reference< XPropertySet > xPropSet; + rArguments.getConstArray()[0] >>= xPropSet; + + //! Pointer allows for access of the non-UNO functions. + //! And the reference to the UNO-functions while increasing + //! the ref-count and will implicitly free the memory + //! when the object is not longer used. + pPropHelper = new PropertyHelper_Thes( (XThesaurus *) this, xPropSet ); + xPropHelper = pPropHelper; + pPropHelper->AddAsPropListener(); //! after a reference is established + } + else + DBG_ERROR( "wrong number of arguments in sequence" ); + } +} + + + +sal_uInt16 SAL_CALL Thesaurus::capitalType(const OUString& aTerm, CharClass * pCC) +{ + sal_Int32 tlen = aTerm.getLength(); + if ((pCC) && (tlen)) { + String aStr(aTerm); + sal_Int32 nc = 0; + for (USHORT tindex = 0; tindex < tlen; tindex++) { + if (pCC->getCharacterType(aStr,tindex) & + ::com::sun::star::i18n::KCharacterType::UPPER) nc++; + } + + if (nc == 0) return (sal_uInt16) CAPTYPE_NOCAP; + + if (nc == tlen) return (sal_uInt16) CAPTYPE_ALLCAP; + + if ((nc == 1) && (pCC->getCharacterType(aStr,0) & + ::com::sun::star::i18n::KCharacterType::UPPER)) + return (sal_uInt16) CAPTYPE_INITCAP; + + return (sal_uInt16) CAPTYPE_MIXED; + } + return (sal_uInt16) CAPTYPE_UNKNOWN; +} + + + +OUString SAL_CALL Thesaurus::makeLowerCase(const OUString& aTerm, CharClass * pCC) +{ + if (pCC) + return pCC->toLower_rtl(aTerm, 0, aTerm.getLength()); + return aTerm; +} + + +OUString SAL_CALL Thesaurus::makeUpperCase(const OUString& aTerm, CharClass * pCC) +{ + if (pCC) + return pCC->toUpper_rtl(aTerm, 0, aTerm.getLength()); + return aTerm; +} + + +OUString SAL_CALL Thesaurus::makeInitCap(const OUString& aTerm, CharClass * pCC) +{ + sal_Int32 tlen = aTerm.getLength(); + if ((pCC) && (tlen)) { + OUString bTemp = aTerm.copy(0,1); + if (tlen > 1) + return ( pCC->toUpper_rtl(bTemp, 0, 1) + + pCC->toLower_rtl(aTerm,1,(tlen-1)) ); + + return pCC->toUpper_rtl(bTemp, 0, 1); + } + return aTerm; +} + + + +void SAL_CALL + Thesaurus::dispose() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing) + { + bDisposing = TRUE; + EventObject aEvtObj( (XThesaurus *) this ); + aEvtListeners.disposeAndClear( aEvtObj ); + } +} + + +void SAL_CALL + Thesaurus::addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.addInterface( rxListener ); +} + + +void SAL_CALL + Thesaurus::removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + if (!bDisposing && rxListener.is()) + aEvtListeners.removeInterface( rxListener ); +} + + +/////////////////////////////////////////////////////////////////////////// +// Service specific part +// + +OUString SAL_CALL Thesaurus::getImplementationName() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return getImplementationName_Static(); +} + + +sal_Bool SAL_CALL Thesaurus::supportsService( const OUString& ServiceName ) + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNL = getSupportedServiceNames(); + const OUString * pArray = aSNL.getConstArray(); + for( INT32 i = 0; i < aSNL.getLength(); i++ ) + if( pArray[i] == ServiceName ) + return TRUE; + return FALSE; +} + + +Sequence< OUString > SAL_CALL Thesaurus::getSupportedServiceNames() + throw(RuntimeException) +{ + MutexGuard aGuard( GetLinguMutex() ); + return getSupportedServiceNames_Static(); +} + + +Sequence< OUString > Thesaurus::getSupportedServiceNames_Static() + throw() +{ + MutexGuard aGuard( GetLinguMutex() ); + + Sequence< OUString > aSNS( 1 ); // auch mehr als 1 Service moeglich + aSNS.getArray()[0] = A2OU( SN_THESAURUS ); + return aSNS; +} + + +sal_Bool SAL_CALL Thesaurus_writeInfo( + void * /*pServiceManager*/, registry::XRegistryKey * pRegistryKey ) +{ + try + { + String aImpl( '/' ); + aImpl += Thesaurus::getImplementationName_Static().getStr(); + aImpl.AppendAscii( "/UNO/SERVICES" ); + Reference< registry::XRegistryKey > xNewKey = + pRegistryKey->createKey( aImpl ); + Sequence< OUString > aServices = + Thesaurus::getSupportedServiceNames_Static(); + for( INT32 i = 0; i < aServices.getLength(); i++ ) + xNewKey->createKey( aServices.getConstArray()[i] ); + + return sal_True; + } + catch(Exception &) + { + return sal_False; + } +} + + +void * SAL_CALL Thesaurus_getFactory( const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, void * ) +{ + void * pRet = 0; + if ( !Thesaurus::getImplementationName_Static().compareToAscii( pImplName ) ) + { + + Reference< XSingleServiceFactory > xFactory = + cppu::createOneInstanceFactory( + pServiceManager, + Thesaurus::getImplementationName_Static(), + Thesaurus_CreateInstance, + Thesaurus::getSupportedServiceNames_Static()); + // acquire, because we return an interface pointer instead of a reference + xFactory->acquire(); + pRet = xFactory.get(); + } + return pRet; +} + + +/////////////////////////////////////////////////////////////////////////// + + +#undef CAPTYPE_UNKNOWN +#undef CAPTYPE_NOCAP +#undef CAPTYPE_INITCAP +#undef CAPTYPE_ALLCAP +#undef CAPTYPE_MIXED diff --git a/lingucomponent/source/thesaurus/libnth/nthesimp.hxx b/lingucomponent/source/thesaurus/libnth/nthesimp.hxx new file mode 100644 index 000000000000..94bbb6fb3ea6 --- /dev/null +++ b/lingucomponent/source/thesaurus/libnth/nthesimp.hxx @@ -0,0 +1,199 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _LINGU2_THESIMP_HXX_ +#define _LINGU2_THESIMP_HXX_ + +#include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type +#include <cppuhelper/implbase1.hxx> // helper for implementations +#include <cppuhelper/implbase5.hxx> // helper for implementations +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Sequence.h> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XServiceDisplayName.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/beans/PropertyValues.hpp> + +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/linguistic2/XMeaning.hpp> +#include <com/sun/star/linguistic2/XThesaurus.hpp> + +#include <com/sun/star/linguistic2/XLinguServiceManager.hpp> +#include <com/sun/star/linguistic2/XSpellChecker1.hpp> + +#include <tools/table.hxx> + +#include <unotools/charclass.hxx> + +#include <lingutil.hxx> +#include <linguistic/misc.hxx> +#include <linguistic/lngprophelp.hxx> + +#include <osl/file.hxx> +#include "mythes.hxx" + +using namespace ::rtl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::linguistic2; + +namespace com { namespace sun { namespace star { namespace beans { + class XPropertySet; +}}}} + + + +/////////////////////////////////////////////////////////////////////////// + + +class Thesaurus : + public cppu::WeakImplHelper5 + < + XThesaurus, + XInitialization, + XComponent, + XServiceInfo, + XServiceDisplayName + > +{ + Sequence< Locale > aSuppLocales; + + ::cppu::OInterfaceContainerHelper aEvtListeners; + Reference< XPropertyChangeListener > xPropHelper; + linguistic::PropertyHelper_Thes * pPropHelper; + BOOL bDisposing; + CharClass ** aCharSetInfo; + MyThes ** aThes; + rtl_TextEncoding * aTEncs; + Locale * aTLocs; + OUString * aTNames; + sal_Int32 numthes; + + // cache for the Thesaurus dialog + Sequence < Reference < ::com::sun::star::linguistic2::XMeaning > > prevMeanings; + OUString prevTerm; + INT16 prevLocale; + + // disallow copy-constructor and assignment-operator for now + Thesaurus(const Thesaurus &); + Thesaurus & operator = (const Thesaurus &); + + linguistic::PropertyHelper_Thes & GetPropHelper_Impl(); + linguistic::PropertyHelper_Thes & GetPropHelper() + { + return pPropHelper ? *pPropHelper : GetPropHelper_Impl(); + } + + +public: + Thesaurus(); + virtual ~Thesaurus(); + + // XSupportedLocales (for XThesaurus) + virtual Sequence< Locale > SAL_CALL + getLocales() + throw(RuntimeException); + virtual sal_Bool SAL_CALL + hasLocale( const Locale& rLocale ) + throw(RuntimeException); + + // XThesaurus + virtual Sequence< Reference < ::com::sun::star::linguistic2::XMeaning > > SAL_CALL + queryMeanings( const OUString& rTerm, const Locale& rLocale, + const PropertyValues& rProperties ) + throw(IllegalArgumentException, + RuntimeException); + + // XServiceDisplayName + virtual OUString SAL_CALL + getServiceDisplayName( const Locale& rLocale ) + throw(RuntimeException); + + // XInitialization + virtual void SAL_CALL + initialize( const Sequence< Any >& rArguments ) + throw(Exception, RuntimeException); + + // XComponent + virtual void SAL_CALL + dispose() + throw(RuntimeException); + virtual void SAL_CALL + addEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + virtual void SAL_CALL + removeEventListener( const Reference< XEventListener >& rxListener ) + throw(RuntimeException); + + //////////////////////////////////////////////////////////// + // Service specific part + // + + // XServiceInfo + virtual OUString SAL_CALL + getImplementationName() + throw(RuntimeException); + virtual sal_Bool SAL_CALL + supportsService( const OUString& rServiceName ) + throw(RuntimeException); + virtual Sequence< OUString > SAL_CALL + getSupportedServiceNames() + throw(RuntimeException); + + + static inline OUString + getImplementationName_Static() throw(); + static Sequence< OUString > + getSupportedServiceNames_Static() throw(); + +private: + sal_uInt16 SAL_CALL capitalType(const OUString&, CharClass *); + OUString SAL_CALL makeLowerCase(const OUString&, CharClass *); + OUString SAL_CALL makeUpperCase(const OUString&, CharClass *); + OUString SAL_CALL makeInitCap(const OUString&, CharClass *); + +/* static ::com::sun::star::uno::Reference< + ::com::sun::star::linguistic2::XLinguServiceManager > xLngSvcMgr; + static ::com::sun::star::uno::Reference< + ::com::sun::star::linguistic2::XSpellChecker1 > xSpell; +*/ + static ::com::sun::star::uno::Reference< + ::com::sun::star::linguistic2::XLinguServiceManager > GetLngSvcMgr(); + +}; + +inline OUString Thesaurus::getImplementationName_Static() throw() +{ + return A2OU( "org.openoffice.lingu.new.Thesaurus" ); +} + + +/////////////////////////////////////////////////////////////////////////// + +#endif diff --git a/lingucomponent/source/thesaurus/libnth/ntreg.cxx b/lingucomponent/source/thesaurus/libnth/ntreg.cxx new file mode 100644 index 000000000000..14b3b3c896d4 --- /dev/null +++ b/lingucomponent/source/thesaurus/libnth/ntreg.cxx @@ -0,0 +1,85 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_lingucomponent.hxx" + + +#include <cppuhelper/factory.hxx> // helper for factories +#include <rtl/string.hxx> + +#include <com/sun/star/registry/XRegistryKey.hpp> + +using namespace com::sun::star::lang; +using namespace com::sun::star::registry; + +//////////////////////////////////////// +// declaration of external RegEntry-functions defined by the service objects +// + +extern sal_Bool SAL_CALL Thesaurus_writeInfo( + void * /*pServiceManager*/, XRegistryKey * pRegistryKey ); + +extern void * SAL_CALL Thesaurus_getFactory( + const sal_Char * pImplName, + XMultiServiceFactory * pServiceManager, + void * /*pRegistryKey*/ ); + +//////////////////////////////////////// +// definition of the two functions that are used to provide the services +// + +extern "C" +{ + +sal_Bool SAL_CALL component_writeInfo( + void * pServiceManager, XRegistryKey * pRegistryKey ) +{ + return Thesaurus_writeInfo( pServiceManager, pRegistryKey ); +} + +void SAL_CALL component_getImplementationEnvironment( + const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ ) +{ + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} + +void * SAL_CALL component_getFactory( + const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) +{ + void * pRet = Thesaurus_getFactory( + pImplName, + reinterpret_cast< XMultiServiceFactory * >( pServiceManager ), + pRegistryKey ); + + return pRet; +} + +} + +/////////////////////////////////////////////////////////////////////////// + |