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