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