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

Generated by: LCOV version 1.10