LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/linguistic/source - convdicxml.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 166 0.0 %
Date: 2013-07-09 Functions: 0 36 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10