1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_linguistic.hxx" 30*cdf0e10cSrcweir #include <tools/urlobj.hxx> 31*cdf0e10cSrcweir #include <tools/debug.hxx> 32*cdf0e10cSrcweir #include <tools/fsys.hxx> 33*cdf0e10cSrcweir #include <tools/string.hxx> 34*cdf0e10cSrcweir #include <i18npool/mslangid.hxx> 35*cdf0e10cSrcweir #include <tools/stream.hxx> 36*cdf0e10cSrcweir #include <osl/mutex.hxx> 37*cdf0e10cSrcweir #include <unotools/processfactory.hxx> 38*cdf0e10cSrcweir #include <ucbhelper/content.hxx> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> // helper for factories 41*cdf0e10cSrcweir #include <com/sun/star/linguistic2/XConversionDictionary.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/linguistic2/ConversionPropertyType.hpp> 44*cdf0e10cSrcweir #include <com/sun/star/util/XFlushable.hpp> 45*cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp> 46*cdf0e10cSrcweir #include <com/sun/star/lang/EventObject.hpp> 47*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UNO_REFERENCE_HPP_ 48*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h> 49*cdf0e10cSrcweir #endif 50*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp> 51*cdf0e10cSrcweir #include <com/sun/star/util/XFlushListener.hpp> 52*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp> 53*cdf0e10cSrcweir #include <com/sun/star/document/XFilter.hpp> 54*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 55*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx> 56*cdf0e10cSrcweir #include <xmloff/xmlnmspe.hxx> 57*cdf0e10cSrcweir #include <unotools/streamwrap.hxx> 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir #include "convdic.hxx" 60*cdf0e10cSrcweir #include "convdicxml.hxx" 61*cdf0e10cSrcweir #include "linguistic/misc.hxx" 62*cdf0e10cSrcweir #include "defs.hxx" 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir using namespace std; 65*cdf0e10cSrcweir using namespace utl; 66*cdf0e10cSrcweir using namespace osl; 67*cdf0e10cSrcweir using namespace rtl; 68*cdf0e10cSrcweir using namespace com::sun::star; 69*cdf0e10cSrcweir using namespace com::sun::star::lang; 70*cdf0e10cSrcweir using namespace com::sun::star::uno; 71*cdf0e10cSrcweir using namespace com::sun::star::linguistic2; 72*cdf0e10cSrcweir using namespace linguistic; 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir #define XML_NAMESPACE_TCD_STRING "http://openoffice.org/2003/text-conversion-dictionary" 75*cdf0e10cSrcweir #define CONV_TYPE_HANGUL_HANJA "Hangul / Hanja" 76*cdf0e10cSrcweir #define CONV_TYPE_SCHINESE_TCHINESE "Chinese simplified / Chinese traditional" 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir static const OUString ConversionTypeToText( sal_Int16 nConversionType ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir OUString aRes; 83*cdf0e10cSrcweir if (nConversionType == ConversionDictionaryType::HANGUL_HANJA) 84*cdf0e10cSrcweir aRes = A2OU( CONV_TYPE_HANGUL_HANJA ); 85*cdf0e10cSrcweir else if (nConversionType == ConversionDictionaryType::SCHINESE_TCHINESE) 86*cdf0e10cSrcweir aRes = A2OU( CONV_TYPE_SCHINESE_TCHINESE ); 87*cdf0e10cSrcweir return aRes; 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir static sal_Int16 GetConversionTypeFromText( const String &rText ) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir sal_Int16 nRes = -1; 93*cdf0e10cSrcweir if (rText.EqualsAscii( CONV_TYPE_HANGUL_HANJA )) 94*cdf0e10cSrcweir nRes = ConversionDictionaryType::HANGUL_HANJA; 95*cdf0e10cSrcweir else if (rText.EqualsAscii( CONV_TYPE_SCHINESE_TCHINESE )) 96*cdf0e10cSrcweir nRes = ConversionDictionaryType::SCHINESE_TCHINESE; 97*cdf0e10cSrcweir return nRes; 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir class ConvDicXMLImportContext : 103*cdf0e10cSrcweir public SvXMLImportContext 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir public: 106*cdf0e10cSrcweir ConvDicXMLImportContext( 107*cdf0e10cSrcweir ConvDicXMLImport &rImport, 108*cdf0e10cSrcweir sal_uInt16 nPrfx, const OUString& rLName ) : 109*cdf0e10cSrcweir SvXMLImportContext( rImport, nPrfx, rLName ) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir const ConvDicXMLImport & GetConvDicImport() const 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir return (const ConvDicXMLImport &) GetImport(); 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir ConvDicXMLImport & GetConvDicImport() 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir return (ConvDicXMLImport &) GetImport(); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir // SvXMLImportContext 124*cdf0e10cSrcweir virtual void Characters( const OUString &rChars ); 125*cdf0e10cSrcweir virtual SvXMLImportContext * CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList); 126*cdf0e10cSrcweir }; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir class ConvDicXMLDictionaryContext_Impl : 130*cdf0e10cSrcweir public ConvDicXMLImportContext 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir sal_Int16 nLanguage; 133*cdf0e10cSrcweir sal_Int16 nConversionType; 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir public: 136*cdf0e10cSrcweir ConvDicXMLDictionaryContext_Impl( ConvDicXMLImport &rImport, 137*cdf0e10cSrcweir sal_uInt16 nPrefix, const OUString& rLName) : 138*cdf0e10cSrcweir ConvDicXMLImportContext( rImport, nPrefix, rLName ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir nLanguage = LANGUAGE_NONE; 141*cdf0e10cSrcweir nConversionType = -1; 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // SvXMLImportContext 145*cdf0e10cSrcweir virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ); 146*cdf0e10cSrcweir virtual SvXMLImportContext * CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList ); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir sal_Int16 GetLanguage() const { return nLanguage; } 149*cdf0e10cSrcweir sal_Int16 GetConversionType() const { return nConversionType; } 150*cdf0e10cSrcweir }; 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir class ConvDicXMLEntryTextContext_Impl : 154*cdf0e10cSrcweir public ConvDicXMLImportContext 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir OUString aLeftText; 157*cdf0e10cSrcweir sal_Int16 nPropertyType; // used for Chinese simplified/traditional conversion 158*cdf0e10cSrcweir ConvDicXMLDictionaryContext_Impl &rDicContext; 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir public: 161*cdf0e10cSrcweir ConvDicXMLEntryTextContext_Impl( 162*cdf0e10cSrcweir ConvDicXMLImport &rImport, 163*cdf0e10cSrcweir sal_uInt16 nPrefix, const OUString& rLName, 164*cdf0e10cSrcweir ConvDicXMLDictionaryContext_Impl &rParentContext ) : 165*cdf0e10cSrcweir ConvDicXMLImportContext( rImport, nPrefix, rLName ), 166*cdf0e10cSrcweir nPropertyType( ConversionPropertyType::NOT_DEFINED ), 167*cdf0e10cSrcweir rDicContext( rParentContext ) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir // SvXMLImportContext 172*cdf0e10cSrcweir virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ); 173*cdf0e10cSrcweir virtual SvXMLImportContext * CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList ); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir const OUString & GetLeftText() const { return aLeftText; } 176*cdf0e10cSrcweir sal_Int16 GetPropertyType() const { return nPropertyType; } 177*cdf0e10cSrcweir void SetPropertyType( sal_Int16 nVal ) { nPropertyType = nVal; } 178*cdf0e10cSrcweir }; 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir class ConvDicXMLRightTextContext_Impl : 182*cdf0e10cSrcweir public ConvDicXMLImportContext 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir OUString aRightText; 185*cdf0e10cSrcweir ConvDicXMLEntryTextContext_Impl &rEntryContext; 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir public: 188*cdf0e10cSrcweir ConvDicXMLRightTextContext_Impl( 189*cdf0e10cSrcweir ConvDicXMLImport &rImport, 190*cdf0e10cSrcweir sal_uInt16 nPrefix, const OUString& rLName, 191*cdf0e10cSrcweir ConvDicXMLEntryTextContext_Impl &rParentContext ) : 192*cdf0e10cSrcweir ConvDicXMLImportContext( rImport, nPrefix, rLName ), 193*cdf0e10cSrcweir rEntryContext( rParentContext ) 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir // SvXMLImportContext 198*cdf0e10cSrcweir virtual void EndElement(); 199*cdf0e10cSrcweir virtual SvXMLImportContext * CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList > &rxAttrList ); 200*cdf0e10cSrcweir virtual void Characters( const OUString &rChars ); 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir const OUString & GetRightText() const { return aRightText; } 203*cdf0e10cSrcweir const OUString & GetLeftText() const { return rEntryContext.GetLeftText(); } 204*cdf0e10cSrcweir ConvDic * GetDic() { return GetConvDicImport().GetDic(); } 205*cdf0e10cSrcweir }; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir void ConvDicXMLImportContext::Characters(const OUString & /*rChars*/) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir /* 212*cdf0e10cSrcweir Whitespace occurring within the content of token elements is "trimmed" 213*cdf0e10cSrcweir from the ends (i.e. all whitespace at the beginning and end of the 214*cdf0e10cSrcweir content is removed), and "collapsed" internally (i.e. each sequence of 215*cdf0e10cSrcweir 1 or more whitespace characters is replaced with one blank character). 216*cdf0e10cSrcweir */ 217*cdf0e10cSrcweir //collapsing not done yet! 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir // warning-free code: since the result is not used there is no need for trimming... 220*cdf0e10cSrcweir //const OUString &rChars2 = rChars.trim(); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir SvXMLImportContext * ConvDicXMLImportContext::CreateChildContext( 224*cdf0e10cSrcweir sal_uInt16 nPrefix, const OUString& rLocalName, 225*cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir SvXMLImportContext *pContext = 0; 228*cdf0e10cSrcweir if (nPrefix == XML_NAMESPACE_TCD && rLocalName.equalsAscii( "text-conversion-dictionary" )) 229*cdf0e10cSrcweir pContext = new ConvDicXMLDictionaryContext_Impl( GetConvDicImport(), nPrefix, rLocalName ); 230*cdf0e10cSrcweir else 231*cdf0e10cSrcweir pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 232*cdf0e10cSrcweir return pContext; 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir //////////////////////////////////////// 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir void ConvDicXMLDictionaryContext_Impl::StartElement( 238*cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList > &rxAttrList ) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir sal_Int16 nAttrCount = rxAttrList.is() ? rxAttrList->getLength() : 0; 241*cdf0e10cSrcweir for (sal_Int16 i = 0; i < nAttrCount; ++i) 242*cdf0e10cSrcweir { 243*cdf0e10cSrcweir OUString aAttrName = rxAttrList->getNameByIndex(i); 244*cdf0e10cSrcweir OUString aLocalName; 245*cdf0e10cSrcweir sal_uInt16 nPrefix = GetImport().GetNamespaceMap(). 246*cdf0e10cSrcweir GetKeyByAttrName( aAttrName, &aLocalName ); 247*cdf0e10cSrcweir OUString aValue = rxAttrList->getValueByIndex(i); 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir if (nPrefix == XML_NAMESPACE_TCD && aLocalName.equalsAscii( "lang" )) 250*cdf0e10cSrcweir nLanguage = MsLangId::convertIsoStringToLanguage( aValue ); 251*cdf0e10cSrcweir else if (nPrefix == XML_NAMESPACE_TCD && aLocalName.equalsAscii( "conversion-type" )) 252*cdf0e10cSrcweir nConversionType = GetConversionTypeFromText( aValue ); 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir GetConvDicImport().SetLanguage( nLanguage ); 255*cdf0e10cSrcweir GetConvDicImport().SetConversionType( nConversionType ); 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir //!! hack to stop the parser from reading the rest of the file !! 258*cdf0e10cSrcweir //!! when only the header (language, conversion type) is needed !! 259*cdf0e10cSrcweir // if (GetConvDicImport().GetDic() == 0) 260*cdf0e10cSrcweir // throw uno::RuntimeException(); 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir SvXMLImportContext * ConvDicXMLDictionaryContext_Impl::CreateChildContext( 264*cdf0e10cSrcweir sal_uInt16 nPrefix, const OUString& rLocalName, 265*cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) 266*cdf0e10cSrcweir { 267*cdf0e10cSrcweir SvXMLImportContext *pContext = 0; 268*cdf0e10cSrcweir if (nPrefix == XML_NAMESPACE_TCD && rLocalName.equalsAscii( "entry" )) 269*cdf0e10cSrcweir pContext = new ConvDicXMLEntryTextContext_Impl( GetConvDicImport(), nPrefix, rLocalName, *this ); 270*cdf0e10cSrcweir else 271*cdf0e10cSrcweir pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 272*cdf0e10cSrcweir return pContext; 273*cdf0e10cSrcweir } 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir //////////////////////////////////////// 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir SvXMLImportContext * ConvDicXMLEntryTextContext_Impl::CreateChildContext( 278*cdf0e10cSrcweir sal_uInt16 nPrefix, const OUString& rLocalName, 279*cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) 280*cdf0e10cSrcweir { 281*cdf0e10cSrcweir SvXMLImportContext *pContext = 0; 282*cdf0e10cSrcweir if (nPrefix == XML_NAMESPACE_TCD && rLocalName.equalsAscii( "right-text" )) 283*cdf0e10cSrcweir pContext = new ConvDicXMLRightTextContext_Impl( GetConvDicImport(), nPrefix, rLocalName, *this ); 284*cdf0e10cSrcweir else 285*cdf0e10cSrcweir pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 286*cdf0e10cSrcweir return pContext; 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir void ConvDicXMLEntryTextContext_Impl::StartElement( 290*cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList >& rxAttrList ) 291*cdf0e10cSrcweir { 292*cdf0e10cSrcweir sal_Int16 nAttrCount = rxAttrList.is() ? rxAttrList->getLength() : 0; 293*cdf0e10cSrcweir for (sal_Int16 i = 0; i < nAttrCount; ++i) 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir OUString aAttrName = rxAttrList->getNameByIndex(i); 296*cdf0e10cSrcweir OUString aLocalName; 297*cdf0e10cSrcweir sal_uInt16 nPrefix = GetImport().GetNamespaceMap(). 298*cdf0e10cSrcweir GetKeyByAttrName( aAttrName, &aLocalName ); 299*cdf0e10cSrcweir OUString aValue = rxAttrList->getValueByIndex(i); 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir if (nPrefix == XML_NAMESPACE_TCD && aLocalName.equalsAscii( "left-text" )) 302*cdf0e10cSrcweir aLeftText = aValue; 303*cdf0e10cSrcweir if (nPrefix == XML_NAMESPACE_TCD && aLocalName.equalsAscii( "property-type" )) 304*cdf0e10cSrcweir nPropertyType = (sal_Int16) aValue.toInt32(); 305*cdf0e10cSrcweir } 306*cdf0e10cSrcweir } 307*cdf0e10cSrcweir 308*cdf0e10cSrcweir //////////////////////////////////////// 309*cdf0e10cSrcweir 310*cdf0e10cSrcweir SvXMLImportContext * ConvDicXMLRightTextContext_Impl::CreateChildContext( 311*cdf0e10cSrcweir sal_uInt16 nPrefix, const OUString& rLocalName, 312*cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList > & /*rxAttrList*/ ) 313*cdf0e10cSrcweir { 314*cdf0e10cSrcweir // leaf: return default (empty) context 315*cdf0e10cSrcweir SvXMLImportContext *pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 316*cdf0e10cSrcweir return pContext; 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir 319*cdf0e10cSrcweir void ConvDicXMLRightTextContext_Impl::Characters( const OUString &rChars ) 320*cdf0e10cSrcweir { 321*cdf0e10cSrcweir aRightText += rChars; 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir 324*cdf0e10cSrcweir void ConvDicXMLRightTextContext_Impl::EndElement() 325*cdf0e10cSrcweir { 326*cdf0e10cSrcweir ConvDic *pDic = GetDic(); 327*cdf0e10cSrcweir if (pDic) 328*cdf0e10cSrcweir pDic->AddEntry( GetLeftText(), GetRightText() ); 329*cdf0e10cSrcweir } 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir sal_Bool ConvDicXMLExport::Export() 335*cdf0e10cSrcweir { 336*cdf0e10cSrcweir sal_Bool bRet = sal_False; 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir uno::Reference< document::XExporter > xExporter( this ); 339*cdf0e10cSrcweir uno::Reference< document::XFilter > xFilter( xExporter, UNO_QUERY ); 340*cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aProps(0); 341*cdf0e10cSrcweir xFilter->filter( aProps ); // calls exportDoc implicitly 342*cdf0e10cSrcweir 343*cdf0e10cSrcweir return bRet = bSuccess; 344*cdf0e10cSrcweir } 345*cdf0e10cSrcweir 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir sal_uInt32 ConvDicXMLExport::exportDoc( enum ::xmloff::token::XMLTokenEnum /*eClass*/ ) 348*cdf0e10cSrcweir { 349*cdf0e10cSrcweir _GetNamespaceMap().Add( A2OU( "tcd" ), 350*cdf0e10cSrcweir A2OU( XML_NAMESPACE_TCD_STRING ), XML_NAMESPACE_TCD ); 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir GetDocHandler()->startDocument(); 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir // Add xmlns line and some other arguments 355*cdf0e10cSrcweir AddAttribute( _GetNamespaceMap().GetAttrNameByKey( XML_NAMESPACE_TCD ), 356*cdf0e10cSrcweir _GetNamespaceMap().GetNameByKey( XML_NAMESPACE_TCD ) ); 357*cdf0e10cSrcweir AddAttributeASCII( XML_NAMESPACE_TCD, "package", "org.openoffice.Office" ); 358*cdf0e10cSrcweir 359*cdf0e10cSrcweir OUString aIsoLang( MsLangId::convertLanguageToIsoString( rDic.nLanguage ) ); 360*cdf0e10cSrcweir AddAttribute( XML_NAMESPACE_TCD, "lang", aIsoLang ); 361*cdf0e10cSrcweir OUString aConvType( ConversionTypeToText( rDic.nConversionType ) ); 362*cdf0e10cSrcweir AddAttribute( XML_NAMESPACE_TCD, "conversion-type", aConvType ); 363*cdf0e10cSrcweir 364*cdf0e10cSrcweir //!! block necessary in order to have SvXMLElementExport d-tor called 365*cdf0e10cSrcweir //!! before the call to endDocument 366*cdf0e10cSrcweir { 367*cdf0e10cSrcweir SvXMLElementExport aRoot( *this, XML_NAMESPACE_TCD, "text-conversion-dictionary", sal_True, sal_True ); 368*cdf0e10cSrcweir _ExportContent(); 369*cdf0e10cSrcweir } 370*cdf0e10cSrcweir 371*cdf0e10cSrcweir GetDocHandler()->endDocument(); 372*cdf0e10cSrcweir 373*cdf0e10cSrcweir bSuccess = sal_True; 374*cdf0e10cSrcweir return 0; 375*cdf0e10cSrcweir } 376*cdf0e10cSrcweir 377*cdf0e10cSrcweir 378*cdf0e10cSrcweir void ConvDicXMLExport::_ExportContent() 379*cdf0e10cSrcweir { 380*cdf0e10cSrcweir // aquire sorted list of all keys 381*cdf0e10cSrcweir ConvMapKeySet aKeySet; 382*cdf0e10cSrcweir ConvMap::iterator aIt; 383*cdf0e10cSrcweir for (aIt = rDic.aFromLeft.begin(); aIt != rDic.aFromLeft.end(); ++aIt) 384*cdf0e10cSrcweir aKeySet.insert( (*aIt).first ); 385*cdf0e10cSrcweir 386*cdf0e10cSrcweir ConvMapKeySet::iterator aKeyIt; 387*cdf0e10cSrcweir for (aKeyIt = aKeySet.begin(); aKeyIt != aKeySet.end(); ++aKeyIt) 388*cdf0e10cSrcweir { 389*cdf0e10cSrcweir OUString aLeftText( *aKeyIt ); 390*cdf0e10cSrcweir AddAttribute( XML_NAMESPACE_TCD, "left-text", aLeftText ); 391*cdf0e10cSrcweir if (rDic.pConvPropType.get()) // property-type list available? 392*cdf0e10cSrcweir { 393*cdf0e10cSrcweir sal_Int16 nPropertyType = -1; 394*cdf0e10cSrcweir PropTypeMap::iterator aIt2 = rDic.pConvPropType->find( aLeftText ); 395*cdf0e10cSrcweir if (aIt2 != rDic.pConvPropType->end()) 396*cdf0e10cSrcweir nPropertyType = (*aIt2).second; 397*cdf0e10cSrcweir DBG_ASSERT( nPropertyType, "property-type not found" ); 398*cdf0e10cSrcweir if (nPropertyType == -1) 399*cdf0e10cSrcweir nPropertyType = ConversionPropertyType::NOT_DEFINED; 400*cdf0e10cSrcweir AddAttribute( XML_NAMESPACE_TCD, "property-type", OUString::valueOf( (sal_Int32) nPropertyType ) ); 401*cdf0e10cSrcweir } 402*cdf0e10cSrcweir SvXMLElementExport aEntryMain( *this, XML_NAMESPACE_TCD, 403*cdf0e10cSrcweir "entry" , sal_True, sal_True ); 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir pair< ConvMap::iterator, ConvMap::iterator > aRange = 406*cdf0e10cSrcweir rDic.aFromLeft.equal_range( *aKeyIt ); 407*cdf0e10cSrcweir for (aIt = aRange.first; aIt != aRange.second; ++aIt) 408*cdf0e10cSrcweir { 409*cdf0e10cSrcweir DBG_ASSERT( *aKeyIt == (*aIt).first, "key <-> entry mismatch" ); 410*cdf0e10cSrcweir OUString aRightText( (*aIt).second ); 411*cdf0e10cSrcweir SvXMLElementExport aEntryRightText( *this, XML_NAMESPACE_TCD, 412*cdf0e10cSrcweir "right-text" , sal_True, sal_False ); 413*cdf0e10cSrcweir Characters( aRightText ); 414*cdf0e10cSrcweir } 415*cdf0e10cSrcweir } 416*cdf0e10cSrcweir } 417*cdf0e10cSrcweir 418*cdf0e10cSrcweir ::rtl::OUString SAL_CALL ConvDicXMLExport::getImplementationName() 419*cdf0e10cSrcweir throw( uno::RuntimeException ) 420*cdf0e10cSrcweir { 421*cdf0e10cSrcweir return A2OU( "com.sun.star.lingu2.ConvDicXMLExport" ); 422*cdf0e10cSrcweir } 423*cdf0e10cSrcweir 424*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 425*cdf0e10cSrcweir 426*cdf0e10cSrcweir void SAL_CALL ConvDicXMLImport::startDocument(void) 427*cdf0e10cSrcweir throw( xml::sax::SAXException, uno::RuntimeException ) 428*cdf0e10cSrcweir { 429*cdf0e10cSrcweir // register namespace at first possible opportunity 430*cdf0e10cSrcweir GetNamespaceMap().Add( A2OU( "tcd" ), 431*cdf0e10cSrcweir A2OU( XML_NAMESPACE_TCD_STRING ), XML_NAMESPACE_TCD ); 432*cdf0e10cSrcweir SvXMLImport::startDocument(); 433*cdf0e10cSrcweir } 434*cdf0e10cSrcweir 435*cdf0e10cSrcweir void SAL_CALL ConvDicXMLImport::endDocument(void) 436*cdf0e10cSrcweir throw( xml::sax::SAXException, uno::RuntimeException ) 437*cdf0e10cSrcweir { 438*cdf0e10cSrcweir SvXMLImport::endDocument(); 439*cdf0e10cSrcweir } 440*cdf0e10cSrcweir 441*cdf0e10cSrcweir SvXMLImportContext * ConvDicXMLImport::CreateContext( 442*cdf0e10cSrcweir sal_uInt16 nPrefix, 443*cdf0e10cSrcweir const rtl::OUString &rLocalName, 444*cdf0e10cSrcweir const uno::Reference < xml::sax::XAttributeList > & /*rxAttrList*/ ) 445*cdf0e10cSrcweir { 446*cdf0e10cSrcweir SvXMLImportContext *pContext = 0; 447*cdf0e10cSrcweir if (nPrefix == XML_NAMESPACE_TCD && rLocalName.equalsAscii( "text-conversion-dictionary" )) 448*cdf0e10cSrcweir pContext = new ConvDicXMLDictionaryContext_Impl( *this, nPrefix, rLocalName ); 449*cdf0e10cSrcweir else 450*cdf0e10cSrcweir pContext = new SvXMLImportContext( *this, nPrefix, rLocalName ); 451*cdf0e10cSrcweir return pContext; 452*cdf0e10cSrcweir } 453*cdf0e10cSrcweir 454*cdf0e10cSrcweir 455*cdf0e10cSrcweir OUString SAL_CALL ConvDicXMLImport::getImplementationName() 456*cdf0e10cSrcweir throw( uno::RuntimeException ) 457*cdf0e10cSrcweir { 458*cdf0e10cSrcweir return A2OU( "com.sun.star.lingu2.ConvDicXMLImport" ); 459*cdf0e10cSrcweir } 460*cdf0e10cSrcweir 461*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 462*cdf0e10cSrcweir 463