LCOV - code coverage report
Current view: top level - oox/source/mathml - importutils.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 158 0.0 %
Date: 2014-04-14 Functions: 0 27 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             : 
      10             : #include "oox/mathml/importutils.hxx"
      11             : 
      12             : #include <assert.h>
      13             : #include <stdio.h>
      14             : 
      15             : #include <oox/token/namespacemap.hxx>
      16             : #include <oox/token/tokenmap.hxx>
      17             : #include <oox/token/tokens.hxx>
      18             : #include <oox/token/namespaces.hxx>
      19             : #include <rtl/ustring.hxx>
      20             : 
      21             : #define OPENING( token ) XML_STREAM_OPENING( token )
      22             : #define CLOSING( token ) XML_STREAM_CLOSING( token )
      23             : 
      24             : using namespace com::sun::star;
      25             : 
      26             : namespace oox
      27             : {
      28             : 
      29             : namespace formulaimport
      30             : {
      31             : 
      32             : namespace
      33             : {
      34             : // a class that inherits from AttributeList, builds the internal data and then will be sliced off
      35             : // during conversion to the base class
      36           0 : class AttributeListBuilder
      37             :     : public XmlStream::AttributeList
      38             : {
      39             : public:
      40             :     AttributeListBuilder( const uno::Reference< xml::sax::XFastAttributeList >& a );
      41             : };
      42             : 
      43           0 : AttributeListBuilder::AttributeListBuilder( const uno::Reference< xml::sax::XFastAttributeList >& a )
      44             : {
      45           0 :     if( a.get() == NULL )
      46           0 :         return;
      47           0 :     uno::Sequence< xml::FastAttribute > aFastAttrSeq = a->getFastAttributes();
      48           0 :     const xml::FastAttribute* pFastAttr = aFastAttrSeq.getConstArray();
      49           0 :     sal_Int32 nFastAttrLength = aFastAttrSeq.getLength();
      50           0 :     for( int i = 0;
      51             :          i < nFastAttrLength;
      52             :          ++i )
      53             :     {
      54           0 :         attrs[ pFastAttr[ i ].Token ] = pFastAttr[ i ].Value;
      55           0 :     }
      56             : }
      57             : 
      58           0 : static OUString tokenToString( int token )
      59             : {
      60           0 :     OUString tokenname = StaticTokenMap::get().getUnicodeTokenName( token & TOKEN_MASK );
      61           0 :     if( tokenname.isEmpty())
      62           0 :         tokenname = "???";
      63           0 :     int nmsp = ( token & NMSP_MASK & ~( TAG_OPENING | TAG_CLOSING ));
      64             : #if 0 // this is awfully long
      65             :     OUString namespacename = StaticNamespaceMap::get().count( nmsp ) != 0
      66             :         ? StaticNamespaceMap::get()[ nmsp ] : OUString( "???" );
      67             : #else
      68           0 :     OUString namespacename;
      69             :     // only few are needed actually
      70           0 :     switch( nmsp )
      71             :     {
      72             :         case NMSP_officeMath:
      73           0 :             namespacename = "m";
      74           0 :             break;
      75             :         case NMSP_doc:
      76           0 :             namespacename = "w";
      77           0 :             break;
      78             :         default:
      79           0 :             namespacename = "?";
      80           0 :             break;
      81             :     }
      82             : #endif
      83           0 :     if( token == OPENING( token ))
      84           0 :         return "<" + namespacename + ":" + tokenname + ">";
      85           0 :     if( token == CLOSING( token ))
      86           0 :         return "</" + namespacename + ":" + tokenname + ">";
      87             :     // just the name itself, not specified whether opening or closing
      88           0 :     return namespacename + ":" + tokenname;
      89             : }
      90             : 
      91             : } // namespace
      92             : 
      93           0 : OUString& XmlStream::AttributeList::operator[] (int token)
      94             : {
      95           0 :     return attrs[token];
      96             : }
      97             : 
      98           0 : OUString XmlStream::AttributeList::attribute( int token, const OUString& def ) const
      99             : {
     100           0 :     std::map< int, OUString >::const_iterator find = attrs.find( token );
     101           0 :     if( find != attrs.end())
     102           0 :         return find->second;
     103           0 :     return def;
     104             : }
     105             : 
     106           0 : bool XmlStream::AttributeList::attribute( int token, bool def ) const
     107             : {
     108           0 :     std::map< int, OUString >::const_iterator find = attrs.find( token );
     109           0 :     if( find != attrs.end())
     110             :     {
     111           0 :         const OUString sValue = find->second;
     112           0 :         if( sValue.equalsIgnoreAsciiCase("true") ||
     113           0 :             sValue.equalsIgnoreAsciiCase("on") ||
     114           0 :             sValue.equalsIgnoreAsciiCase("t") ||
     115           0 :             sValue.equalsIgnoreAsciiCase("1") )
     116           0 :             return true;
     117           0 :         if( sValue.equalsIgnoreAsciiCase("false") ||
     118           0 :             sValue.equalsIgnoreAsciiCase("off") ||
     119           0 :             sValue.equalsIgnoreAsciiCase("f") ||
     120           0 :             sValue.equalsIgnoreAsciiCase("0") )
     121           0 :             return false;
     122           0 :         SAL_WARN( "oox.xmlstream", "Cannot convert \'" << sValue << "\' to bool." );
     123             :     }
     124           0 :     return def;
     125             : }
     126             : 
     127           0 : sal_Unicode XmlStream::AttributeList::attribute( int token, sal_Unicode def ) const
     128             : {
     129           0 :     std::map< int, OUString >::const_iterator find = attrs.find( token );
     130           0 :     if( find != attrs.end())
     131             :     {
     132           0 :         if( !find->second.isEmpty() )
     133             :         {
     134           0 :             if( find->second.getLength() != 1 )
     135             :                 SAL_WARN( "oox.xmlstream", "Cannot convert \'" << find->second << "\' to sal_Unicode, stripping." );
     136           0 :             return find->second[ 0 ];
     137             :         }
     138             :     }
     139           0 :     return def;
     140             : }
     141             : 
     142           0 : XmlStream::Tag::Tag( int t, const uno::Reference< xml::sax::XFastAttributeList >& a, const OUString& txt )
     143             : : token( t )
     144             : , attributes( AttributeListBuilder( a ))
     145           0 : , text( txt )
     146             : {
     147           0 : }
     148             : 
     149           0 : XmlStream::Tag::Tag( int t, const AttributeList& a )
     150             : : token( t )
     151           0 : , attributes( a )
     152             : {
     153           0 : }
     154             : 
     155             : 
     156           0 : XmlStream::Tag::operator bool() const
     157             : {
     158           0 :     return token != XML_TOKEN_INVALID;
     159             : }
     160             : 
     161           0 : XmlStream::XmlStream()
     162           0 : : pos( 0 )
     163             : {
     164             :     // make sure our extra bit does not conflict with values used by oox
     165             :     assert( TAG_OPENING > ( 1024 << NMSP_SHIFT ));
     166           0 : }
     167             : 
     168           0 : bool XmlStream::atEnd() const
     169             : {
     170           0 :     return pos >= tags.size();
     171             : }
     172             : 
     173           0 : XmlStream::Tag XmlStream::currentTag() const
     174             : {
     175           0 :     if( pos >= tags.size())
     176           0 :         return Tag();
     177           0 :     return tags[ pos ];
     178             : }
     179             : 
     180           0 : int XmlStream::currentToken() const
     181             : {
     182           0 :     if( pos >= tags.size())
     183           0 :         return XML_TOKEN_INVALID;
     184           0 :     return tags[ pos ].token;
     185             : }
     186             : 
     187           0 : void XmlStream::moveToNextTag()
     188             : {
     189           0 :     if( pos < tags.size())
     190           0 :         ++pos;
     191           0 : }
     192             : 
     193           0 : XmlStream::Tag XmlStream::ensureOpeningTag( int token )
     194             : {
     195           0 :     return checkTag( OPENING( token ), false );
     196             : }
     197             : 
     198           0 : XmlStream::Tag XmlStream::checkOpeningTag( int token )
     199             : {
     200           0 :     return checkTag( OPENING( token ), true );
     201             : }
     202             : 
     203           0 : void XmlStream::ensureClosingTag( int token )
     204             : {
     205           0 :     checkTag( CLOSING( token ), false );
     206           0 : }
     207             : 
     208           0 : XmlStream::Tag XmlStream::checkTag( int token, bool optional )
     209             : {
     210             :     // either it's the following tag, or find it
     211           0 :     int savedPos = pos;
     212           0 :     if( optional )
     213             :     { // avoid printing debug messages about skipping tags if the optional one
     214             :       // will not be found and the position will be reset back
     215           0 :         if( currentToken() != token && !findTagInternal( token, true ))
     216             :         {
     217           0 :             pos = savedPos;
     218           0 :             return Tag();
     219             :         }
     220             :     }
     221           0 :     if( currentToken() == token || findTag( token ))
     222             :     {
     223           0 :         Tag ret = currentTag();
     224           0 :         moveToNextTag();
     225           0 :         return ret; // ok
     226             :     }
     227           0 :     if( optional )
     228             :     { // not a problem, just rewind
     229           0 :         pos = savedPos;
     230           0 :         return Tag();
     231             :     }
     232             :     SAL_WARN( "oox.xmlstream", "Expected tag " << tokenToString( token ) << " not found." );
     233           0 :     return Tag();
     234             : }
     235             : 
     236           0 : bool XmlStream::findTag( int token )
     237             : {
     238           0 :     return findTagInternal( token, false );
     239             : }
     240             : 
     241           0 : bool XmlStream::findTagInternal( int token, bool silent )
     242             : {
     243           0 :     int depth = 0;
     244           0 :     for(;
     245           0 :          !atEnd();
     246           0 :          moveToNextTag())
     247             :     {
     248           0 :         if( depth > 0 ) // we're inside a nested element, skip those
     249             :         {
     250           0 :             if( currentToken() == OPENING( currentToken()))
     251             :             {
     252           0 :                 if( !silent )
     253             :                     SAL_INFO( "oox.xmlstream", "Skipping tag " << tokenToString( currentToken()));
     254           0 :                 ++depth;
     255             :             }
     256           0 :             else if( currentToken() == CLOSING( currentToken()))
     257             :             {
     258           0 :                 if( !silent )
     259             :                     SAL_INFO( "oox.xmlstream", "Skipping tag " << tokenToString( currentToken()));
     260           0 :                 --depth;
     261             :             }
     262             :             else
     263             :             {
     264           0 :                 if( !silent )
     265             :                     SAL_WARN( "oox.xmlstream", "Malformed token " << currentToken() << " ("
     266             :                         << tokenToString( currentToken()) << ")" );
     267           0 :                 abort();
     268             :             }
     269           0 :             continue;
     270             :         }
     271           0 :         if( currentToken() == token )
     272           0 :             return true; // ok, found
     273           0 :         if( currentToken() == CLOSING( currentToken()))
     274           0 :             return false; // that would be leaving current element, so not found
     275           0 :         if( currentToken() == OPENING( currentToken()))
     276             :         {
     277           0 :             if( !silent )
     278             :                 SAL_INFO( "oox.xmlstream", "Skipping tag " << tokenToString( currentToken()));
     279           0 :             ++depth;
     280             :         }
     281             :         else
     282           0 :             abort();
     283             :     }
     284           0 :     if( !silent )
     285             :         SAL_WARN( "oox.xmlstream", "Unexpected end of stream reached." );
     286           0 :     return false;
     287             : }
     288             : 
     289           0 : void XmlStream::skipElementInternal( int token, bool silent )
     290             : {
     291           0 :     int closing = ( token & ~TAG_OPENING ) | TAG_CLOSING; // make it a closing tag
     292             :     assert( currentToken() == OPENING( token ));
     293           0 :     if( !silent )
     294             :         SAL_INFO( "oox.xmlstream", "Skipping unexpected element " << tokenToString( currentToken()));
     295           0 :     moveToNextTag();
     296             :     // and just find the matching closing tag
     297           0 :     if( findTag( closing ))
     298             :     {
     299           0 :         if( !silent )
     300             :             SAL_INFO( "oox.xmlstream", "Skipped unexpected element " << tokenToString( token ));
     301           0 :         moveToNextTag(); // and skip it too
     302           0 :         return;
     303             :     }
     304             :     // this one is an unexpected problem, do not silent it
     305             :     SAL_WARN( "oox.xmlstream", "Expected end of element " << tokenToString( token ) << " not found." );
     306             : }
     307             : 
     308           0 : void XmlStream::handleUnexpectedTag()
     309             : {
     310           0 :     if( atEnd())
     311           0 :         return;
     312           0 :     if( currentToken() == CLOSING( currentToken()))
     313             :     {
     314             :         SAL_INFO( "oox.xmlstream", "Skipping unexpected tag " << tokenToString( currentToken()));
     315           0 :         moveToNextTag(); // just skip it
     316           0 :         return;
     317             :     }
     318           0 :     skipElementInternal( currentToken(), false ); // otherwise skip the entire element
     319             : }
     320             : 
     321             : 
     322           0 : void XmlStreamBuilder::appendOpeningTag( int token, const uno::Reference< xml::sax::XFastAttributeList >& attrs )
     323             : {
     324           0 :     tags.push_back( Tag( OPENING( token ), attrs ));
     325           0 : }
     326             : 
     327           0 : void XmlStreamBuilder::appendOpeningTag( int token, const AttributeList& attrs )
     328             : {
     329           0 :     tags.push_back( Tag( OPENING( token ), attrs ));
     330           0 : }
     331             : 
     332           0 : void XmlStreamBuilder::appendClosingTag( int token )
     333             : {
     334           0 :     tags.push_back( Tag( CLOSING( token )));
     335           0 : }
     336             : 
     337           0 : void XmlStreamBuilder::appendCharacters( const OUString& chars )
     338             : {
     339             :     assert( !tags.empty());
     340           0 :     tags.back().text += chars;
     341           0 : }
     342             : 
     343             : } // namespace
     344             : } // namespace
     345             : 
     346             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10