LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sfx2/source/bastyp - sfxhtml.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 26 163 16.0 %
Date: 2013-07-09 Functions: 7 13 53.8 %
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             : 
      21             : #include <tools/urlobj.hxx>
      22             : 
      23             : #include <sfx2/objsh.hxx>
      24             : #include <sfx2/docfile.hxx>
      25             : #include "openflag.hxx"
      26             : 
      27             : #include <svtools/htmlkywd.hxx>
      28             : #include <svtools/htmltokn.h>
      29             : #include <svtools/imap.hxx>
      30             : #include <svtools/imapcirc.hxx>
      31             : #include <svtools/imapobj.hxx>
      32             : #include <svtools/imappoly.hxx>
      33             : #include <svtools/imaprect.hxx>
      34             : #include <svl/zforlist.hxx>
      35             : #include <rtl/tencinfo.h>
      36             : #include <tools/tenccvt.hxx>
      37             : 
      38             : #include <sfx2/sfxhtml.hxx>
      39             : 
      40             : #include <com/sun/star/beans/XPropertyContainer.hpp>
      41             : #include <comphelper/string.hxx>
      42             : 
      43             : #include <vector>
      44             : 
      45             : 
      46             : using namespace ::com::sun::star;
      47             : 
      48             : 
      49             : sal_Char sHTML_MIME_text[] = "text/";
      50             : sal_Char sHTML_MIME_application[] = "application/";
      51             : sal_Char sHTML_MIME_experimental[] = "x-";
      52             : 
      53             : // <INPUT TYPE=xxx>
      54             : static HTMLOptionEnum const aAreaShapeOptEnums[] =
      55             : {
      56             :     { OOO_STRING_SVTOOLS_HTML_SH_rect,      IMAP_OBJ_RECTANGLE  },
      57             :     { OOO_STRING_SVTOOLS_HTML_SH_rectangle, IMAP_OBJ_RECTANGLE  },
      58             :     { OOO_STRING_SVTOOLS_HTML_SH_circ,      IMAP_OBJ_CIRCLE     },
      59             :     { OOO_STRING_SVTOOLS_HTML_SH_circle,    IMAP_OBJ_CIRCLE     },
      60             :     { OOO_STRING_SVTOOLS_HTML_SH_poly,          IMAP_OBJ_POLYGON    },
      61             :     { OOO_STRING_SVTOOLS_HTML_SH_polygon,       IMAP_OBJ_POLYGON    },
      62             :     { 0,                    0                   }
      63             : };
      64             : 
      65           1 : SfxHTMLParser::SfxHTMLParser( SvStream& rStream, sal_Bool bIsNewDoc,
      66             :                               SfxMedium *pMed ) :
      67             :     HTMLParser( rStream, bIsNewDoc ),
      68           1 :     pMedium( pMed ), pDLMedium( 0 )
      69             : {
      70             :     DBG_ASSERT( RTL_TEXTENCODING_UTF8 == GetSrcEncoding( ),
      71             :                 "SfxHTMLParser::SfxHTMLParser: From where comes ZS?" );
      72             : 
      73             :     DBG_ASSERT( !IsSwitchToUCS2(),
      74             :                 "SfxHTMLParser::SfxHTMLParser: Switch to UCS2?" );
      75             : 
      76             :     // If the file starts with a BOM, switch to UCS2.
      77           1 :     SetSwitchToUCS2( sal_True );
      78           1 : }
      79             : 
      80           2 : SfxHTMLParser::~SfxHTMLParser()
      81             : {
      82             :     DBG_ASSERT( !pDLMedium, "Here is a File Download that has got stuck" );
      83           1 :     delete pDLMedium;
      84           1 : }
      85             : 
      86           0 : bool SfxHTMLParser::ParseMapOptions(
      87             :     ImageMap* pImageMap, const HTMLOptions& rOptions)
      88             : {
      89             :     DBG_ASSERT( pImageMap, "ParseMapOptions: No Image-Map" );
      90             : 
      91           0 :     String aName;
      92             : 
      93           0 :     for (size_t i = rOptions.size(); i; )
      94             :     {
      95           0 :         const HTMLOption& aOption = rOptions[--i];
      96           0 :         switch( aOption.GetToken() )
      97             :         {
      98             :         case HTML_O_NAME:
      99           0 :             aName = aOption.GetString();
     100           0 :             break;
     101             :         }
     102             :     }
     103             : 
     104           0 :     if( aName.Len() )
     105           0 :         pImageMap->SetName( aName );
     106             : 
     107           0 :     return aName.Len() > 0;
     108             : }
     109             : 
     110           0 : bool SfxHTMLParser::ParseAreaOptions(ImageMap * pImageMap, const String& rBaseURL,
     111             :                                      const HTMLOptions& rOptions,
     112             :                                      sal_uInt16 nEventMouseOver,
     113             :                                      sal_uInt16 nEventMouseOut )
     114             : {
     115             :     DBG_ASSERT( pImageMap, "ParseAreaOptions: no Image-Map" );
     116             : 
     117           0 :     sal_uInt16 nShape = IMAP_OBJ_RECTANGLE;
     118           0 :     std::vector<sal_uInt32> aCoords;
     119           0 :     String aName, aHRef, aAlt, aTarget, sEmpty;
     120           0 :     sal_Bool bNoHRef = sal_False;
     121           0 :     SvxMacroTableDtor aMacroTbl;
     122             : 
     123           0 :     for (size_t i = rOptions.size(); i; )
     124             :     {
     125           0 :         sal_uInt16 nEvent = 0;
     126           0 :         ScriptType eScrpType = STARBASIC;
     127           0 :         const HTMLOption& rOption = rOptions[--i];
     128           0 :         switch( rOption.GetToken() )
     129             :         {
     130             :         case HTML_O_NAME:
     131           0 :             aName = rOption.GetString();
     132           0 :             break;
     133             :         case HTML_O_SHAPE:
     134           0 :             rOption.GetEnum( nShape, aAreaShapeOptEnums );
     135           0 :             break;
     136             :         case HTML_O_COORDS:
     137           0 :             rOption.GetNumbers( aCoords, true );
     138           0 :             break;
     139             :         case HTML_O_HREF:
     140           0 :             aHRef = INetURLObject::GetAbsURL( rBaseURL, rOption.GetString() );
     141           0 :             break;
     142             :         case HTML_O_NOHREF:
     143           0 :             bNoHRef = sal_True;
     144           0 :             break;
     145             :         case HTML_O_ALT:
     146           0 :             aAlt = rOption.GetString();
     147           0 :             break;
     148             :         case HTML_O_TARGET:
     149           0 :             aTarget = rOption.GetString();
     150           0 :             break;
     151             : 
     152             :         case HTML_O_ONMOUSEOVER:
     153           0 :             eScrpType = JAVASCRIPT;
     154             :         case HTML_O_SDONMOUSEOVER:
     155           0 :             nEvent = nEventMouseOver;
     156           0 :             goto IMAPOBJ_SETEVENT;
     157             : 
     158             :         case HTML_O_ONMOUSEOUT:
     159           0 :             eScrpType = JAVASCRIPT;
     160             :         case HTML_O_SDONMOUSEOUT:
     161           0 :             nEvent = nEventMouseOut;
     162           0 :             goto IMAPOBJ_SETEVENT;
     163             : IMAPOBJ_SETEVENT:
     164           0 :             if( nEvent )
     165             :             {
     166           0 :                 String sTmp( rOption.GetString() );
     167           0 :                 if( sTmp.Len() )
     168             :                 {
     169           0 :                     sTmp = convertLineEnd(sTmp, GetSystemLineEnd());
     170           0 :                     aMacroTbl.Insert( nEvent, SvxMacro( sTmp, sEmpty, eScrpType ));
     171           0 :                 }
     172             :             }
     173           0 :             break;
     174             :         }
     175             :     }
     176             : 
     177           0 :     if( bNoHRef )
     178           0 :         aHRef.Erase();
     179             : 
     180           0 :     sal_Bool bNewArea = sal_True;
     181           0 :     switch( nShape )
     182             :     {
     183             :     case IMAP_OBJ_RECTANGLE:
     184           0 :         if( aCoords.size() >=4 )
     185             :         {
     186           0 :             Rectangle aRect( aCoords[0], aCoords[1],
     187           0 :                              aCoords[2], aCoords[3] );
     188             :             IMapRectangleObject aMapRObj( aRect, aHRef, aAlt, String(), aTarget, aName,
     189           0 :                                           !bNoHRef );
     190           0 :             if( !aMacroTbl.empty() )
     191           0 :                 aMapRObj.SetMacroTable( aMacroTbl );
     192           0 :             pImageMap->InsertIMapObject( aMapRObj );
     193             :         }
     194           0 :         break;
     195             :     case IMAP_OBJ_CIRCLE:
     196           0 :         if( aCoords.size() >=3 )
     197             :         {
     198           0 :             Point aPoint( aCoords[0], aCoords[1] );
     199           0 :             IMapCircleObject aMapCObj( aPoint, aCoords[2],aHRef, aAlt, String(),
     200           0 :                                        aTarget, aName, !bNoHRef );
     201           0 :             if( !aMacroTbl.empty() )
     202           0 :                 aMapCObj.SetMacroTable( aMacroTbl );
     203           0 :             pImageMap->InsertIMapObject( aMapCObj );
     204             :         }
     205           0 :         break;
     206             :     case IMAP_OBJ_POLYGON:
     207           0 :         if( aCoords.size() >=6 )
     208             :         {
     209           0 :             sal_uInt16 nCount = aCoords.size() / 2;
     210           0 :             Polygon aPoly( nCount );
     211           0 :             for( sal_uInt16 i=0; i<nCount; i++ )
     212           0 :                 aPoly[i] = Point( aCoords[2*i], aCoords[2*i+1] );
     213             :             IMapPolygonObject aMapPObj( aPoly, aHRef, aAlt, String(), aTarget, aName,
     214           0 :                                         !bNoHRef );
     215           0 :             if( !aMacroTbl.empty() )
     216           0 :                 aMapPObj.SetMacroTable( aMacroTbl );
     217           0 :             pImageMap->InsertIMapObject( aMapPObj );
     218             :         }
     219           0 :         break;
     220             :     default:
     221           0 :         bNewArea = sal_False;
     222             :     }
     223             : 
     224           0 :     return bNewArea;
     225             : }
     226             : 
     227           0 : void SfxHTMLParser::StartFileDownload(const OUString& rURL)
     228             : {
     229             :     DBG_ASSERT( !pDLMedium, "StartFileDownload when active Download" );
     230           0 :     if( pDLMedium )
     231           0 :         return;
     232             : 
     233           0 :     pDLMedium = new SfxMedium( rURL, SFX_STREAM_READONLY );
     234           0 :     pDLMedium->DownLoad();
     235             : }
     236             : 
     237           0 : sal_Bool SfxHTMLParser::FinishFileDownload( String& rStr )
     238             : {
     239           0 :     sal_Bool bOK = pDLMedium && pDLMedium->GetErrorCode()==0;
     240           0 :     if( bOK )
     241             :     {
     242           0 :         SvStream* pStream = pDLMedium->GetInStream();
     243             :         DBG_ASSERT( pStream, "No In-Stream received from Medium" );
     244             : 
     245           0 :         SvMemoryStream aStream;
     246           0 :         if( pStream )
     247           0 :             aStream << *pStream;
     248             : 
     249           0 :         aStream.Seek( STREAM_SEEK_TO_END );
     250             :         DBG_ASSERT( aStream.Tell() < STRING_MAXLEN,
     251             :                     "File too long for a string, cut off the end" );
     252           0 :         xub_StrLen nLen = aStream.Tell() < STRING_MAXLEN
     253           0 :                         ? (xub_StrLen)aStream.Tell()
     254           0 :                         : STRING_MAXLEN;
     255             : 
     256           0 :         aStream.Seek( 0 );
     257           0 :         OString sBuffer = read_uInt8s_ToOString(aStream, nLen);
     258           0 :         rStr = S2U(sBuffer);
     259             :     }
     260             : 
     261           0 :     delete pDLMedium;
     262           0 :     pDLMedium = 0;
     263             : 
     264           0 :     return bOK;
     265             : }
     266             : 
     267           1 : void SfxHTMLParser::GetScriptType_Impl( SvKeyValueIterator *pHTTPHeader )
     268             : {
     269           1 :     aScriptType = SVX_MACRO_LANGUAGE_JAVASCRIPT;
     270           1 :     eScriptType = JAVASCRIPT;
     271           1 :     if( pHTTPHeader )
     272             :     {
     273           1 :         SvKeyValue aKV;
     274           3 :         for( sal_Bool bCont = pHTTPHeader->GetFirst( aKV ); bCont;
     275           2 :              bCont = pHTTPHeader->GetNext( aKV ) )
     276             :         {
     277           4 :             if( aKV.GetKey().EqualsIgnoreCaseAscii(
     278           2 :                                     OOO_STRING_SVTOOLS_HTML_META_content_script_type ) )
     279             :             {
     280           0 :                 if( aKV.GetValue().Len() )
     281             :                 {
     282           0 :                     String aTmp( aKV.GetValue() );
     283           0 :                     if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_text, 0, 5 ) )
     284           0 :                         aTmp.Erase( 0, 5 );
     285           0 :                     else if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_application,
     286           0 :                                                          0, 12 ) )
     287           0 :                         aTmp.Erase( 0, 12 );
     288             :                     else
     289           0 :                         break;
     290             : 
     291           0 :                     if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_experimental, 0,
     292           0 :                                                     2 ) )
     293             :                     {
     294           0 :                         aTmp.Erase( 0, 2 );
     295             :                     }
     296             : 
     297           0 :                     if( aTmp.EqualsIgnoreCaseAscii( OOO_STRING_SVTOOLS_HTML_LG_starbasic ) )
     298             :                     {
     299           0 :                         eScriptType = STARBASIC;
     300           0 :                         aScriptType = SVX_MACRO_LANGUAGE_STARBASIC;
     301             :                     }
     302           0 :                     if( !aTmp.EqualsIgnoreCaseAscii( OOO_STRING_SVTOOLS_HTML_LG_javascript ) )
     303             :                     {
     304           0 :                         eScriptType = EXTENDED_STYPE;
     305           0 :                         aScriptType = aTmp;
     306           0 :                     }
     307             :                 }
     308           0 :                 break;
     309             :             }
     310           1 :         }
     311             :     }
     312           1 : }
     313             : 
     314           1 : ScriptType SfxHTMLParser::GetScriptType( SvKeyValueIterator *pHTTPHeader ) const
     315             : {
     316           1 :     if( !aScriptType.Len() )
     317           1 :         ((SfxHTMLParser *)this)->GetScriptType_Impl( pHTTPHeader );
     318             : 
     319           1 :     return eScriptType;
     320             : }
     321             : 
     322           1 : const String& SfxHTMLParser::GetScriptTypeString(
     323             :                                     SvKeyValueIterator *pHTTPHeader ) const
     324             : {
     325           1 :     if( !aScriptType.Len() )
     326           0 :         ((SfxHTMLParser *)this)->GetScriptType_Impl( pHTTPHeader );
     327             : 
     328           1 :     return aScriptType;
     329             : }
     330             : 
     331           0 : double SfxHTMLParser::GetTableDataOptionsValNum( sal_uInt32& nNumForm,
     332             :         LanguageType& eNumLang, const String& aValStr, const String& aNumStr,
     333             :         SvNumberFormatter& rFormatter )
     334             : {
     335           0 :     LanguageType eParseLang = (LanguageType )aNumStr.ToInt32();
     336           0 :     sal_uInt32 nParseForm = rFormatter.GetFormatForLanguageIfBuiltIn( 0, eParseLang );
     337             :     double fVal;
     338           0 :     rFormatter.IsNumberFormat( aValStr, nParseForm, fVal );
     339           0 :     if ( comphelper::string::getTokenCount(aNumStr, ';') > 2 )
     340             :     {
     341           0 :         eNumLang = (LanguageType)aNumStr.GetToken( 1, ';' ).ToInt32();
     342           0 :         xub_StrLen nPos = aNumStr.Search( ';' );
     343           0 :         nPos = aNumStr.Search( ';', nPos + 1 );
     344           0 :         OUString aFormat( aNumStr.Copy( nPos + 1 ) );
     345             :         sal_Int32 nCheckPos;
     346             :         short nType;
     347           0 :         if ( eNumLang != LANGUAGE_SYSTEM )
     348           0 :             rFormatter.PutEntry( aFormat, nCheckPos, nType, nNumForm, eNumLang );
     349             :         else
     350             :             rFormatter.PutandConvertEntry( aFormat, nCheckPos, nType, nNumForm,
     351           0 :                                            eParseLang, eNumLang );
     352             :     }
     353             :     else
     354             :     {
     355           0 :         eNumLang = LANGUAGE_SYSTEM;
     356           0 :         nNumForm = rFormatter.GetFormatForLanguageIfBuiltIn( 0, eNumLang );
     357             :     }
     358           0 :     return fVal;
     359         408 : }
     360             : 
     361             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10