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