Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / bastyp / sfxhtml.cxx
blobbbbd5de96fadedf7bc377aef8511682dc6b53499
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <tools/urlobj.hxx>
23 #include <sfx2/objsh.hxx>
24 #include <sfx2/docfile.hxx>
25 #include "openflag.hxx"
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>
38 #include <sfx2/sfxhtml.hxx>
40 #include <com/sun/star/beans/XPropertyContainer.hpp>
41 #include <comphelper/string.hxx>
43 #include <vector>
46 using namespace ::com::sun::star;
49 sal_Char sHTML_MIME_text[] = "text/";
50 sal_Char sHTML_MIME_application[] = "application/";
51 sal_Char sHTML_MIME_experimental[] = "x-";
53 // <INPUT TYPE=xxx>
54 static HTMLOptionEnum const aAreaShapeOptEnums[] =
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 }
65 SfxHTMLParser::SfxHTMLParser( SvStream& rStream, sal_Bool bIsNewDoc,
66 SfxMedium *pMed ) :
67 HTMLParser( rStream, bIsNewDoc ),
68 pMedium( pMed ), pDLMedium( 0 )
70 DBG_ASSERT( RTL_TEXTENCODING_UTF8 == GetSrcEncoding( ),
71 "SfxHTMLParser::SfxHTMLParser: From where comes ZS?" );
73 DBG_ASSERT( !IsSwitchToUCS2(),
74 "SfxHTMLParser::SfxHTMLParser: Switch to UCS2?" );
76 // If the file starts with a BOM, switch to UCS2.
77 SetSwitchToUCS2( sal_True );
80 SfxHTMLParser::~SfxHTMLParser()
82 DBG_ASSERT( !pDLMedium, "Here is a File Download that has got stuck" );
83 delete pDLMedium;
86 bool SfxHTMLParser::ParseMapOptions(
87 ImageMap* pImageMap, const HTMLOptions& rOptions)
89 DBG_ASSERT( pImageMap, "ParseMapOptions: No Image-Map" );
91 String aName;
93 for (size_t i = rOptions.size(); i; )
95 const HTMLOption& aOption = rOptions[--i];
96 switch( aOption.GetToken() )
98 case HTML_O_NAME:
99 aName = aOption.GetString();
100 break;
104 if( aName.Len() )
105 pImageMap->SetName( aName );
107 return aName.Len() > 0;
110 bool SfxHTMLParser::ParseAreaOptions(ImageMap * pImageMap, const String& rBaseURL,
111 const HTMLOptions& rOptions,
112 sal_uInt16 nEventMouseOver,
113 sal_uInt16 nEventMouseOut )
115 DBG_ASSERT( pImageMap, "ParseAreaOptions: no Image-Map" );
117 sal_uInt16 nShape = IMAP_OBJ_RECTANGLE;
118 std::vector<sal_uInt32> aCoords;
119 String aName, aHRef, aAlt, aTarget, sEmpty;
120 sal_Bool bNoHRef = sal_False;
121 SvxMacroTableDtor aMacroTbl;
123 for (size_t i = rOptions.size(); i; )
125 sal_uInt16 nEvent = 0;
126 ScriptType eScrpType = STARBASIC;
127 const HTMLOption& rOption = rOptions[--i];
128 switch( rOption.GetToken() )
130 case HTML_O_NAME:
131 aName = rOption.GetString();
132 break;
133 case HTML_O_SHAPE:
134 rOption.GetEnum( nShape, aAreaShapeOptEnums );
135 break;
136 case HTML_O_COORDS:
137 rOption.GetNumbers( aCoords, true );
138 break;
139 case HTML_O_HREF:
140 aHRef = INetURLObject::GetAbsURL( rBaseURL, rOption.GetString() );
141 break;
142 case HTML_O_NOHREF:
143 bNoHRef = sal_True;
144 break;
145 case HTML_O_ALT:
146 aAlt = rOption.GetString();
147 break;
148 case HTML_O_TARGET:
149 aTarget = rOption.GetString();
150 break;
152 case HTML_O_ONMOUSEOVER:
153 eScrpType = JAVASCRIPT;
154 case HTML_O_SDONMOUSEOVER:
155 nEvent = nEventMouseOver;
156 goto IMAPOBJ_SETEVENT;
158 case HTML_O_ONMOUSEOUT:
159 eScrpType = JAVASCRIPT;
160 case HTML_O_SDONMOUSEOUT:
161 nEvent = nEventMouseOut;
162 goto IMAPOBJ_SETEVENT;
163 IMAPOBJ_SETEVENT:
164 if( nEvent )
166 String sTmp( rOption.GetString() );
167 if( sTmp.Len() )
169 sTmp = convertLineEnd(sTmp, GetSystemLineEnd());
170 aMacroTbl.Insert( nEvent, SvxMacro( sTmp, sEmpty, eScrpType ));
173 break;
177 if( bNoHRef )
178 aHRef.Erase();
180 sal_Bool bNewArea = sal_True;
181 switch( nShape )
183 case IMAP_OBJ_RECTANGLE:
184 if( aCoords.size() >=4 )
186 Rectangle aRect( aCoords[0], aCoords[1],
187 aCoords[2], aCoords[3] );
188 IMapRectangleObject aMapRObj( aRect, aHRef, aAlt, String(), aTarget, aName,
189 !bNoHRef );
190 if( !aMacroTbl.empty() )
191 aMapRObj.SetMacroTable( aMacroTbl );
192 pImageMap->InsertIMapObject( aMapRObj );
194 break;
195 case IMAP_OBJ_CIRCLE:
196 if( aCoords.size() >=3 )
198 Point aPoint( aCoords[0], aCoords[1] );
199 IMapCircleObject aMapCObj( aPoint, aCoords[2],aHRef, aAlt, String(),
200 aTarget, aName, !bNoHRef );
201 if( !aMacroTbl.empty() )
202 aMapCObj.SetMacroTable( aMacroTbl );
203 pImageMap->InsertIMapObject( aMapCObj );
205 break;
206 case IMAP_OBJ_POLYGON:
207 if( aCoords.size() >=6 )
209 sal_uInt16 nCount = aCoords.size() / 2;
210 Polygon aPoly( nCount );
211 for( sal_uInt16 i=0; i<nCount; i++ )
212 aPoly[i] = Point( aCoords[2*i], aCoords[2*i+1] );
213 IMapPolygonObject aMapPObj( aPoly, aHRef, aAlt, String(), aTarget, aName,
214 !bNoHRef );
215 if( !aMacroTbl.empty() )
216 aMapPObj.SetMacroTable( aMacroTbl );
217 pImageMap->InsertIMapObject( aMapPObj );
219 break;
220 default:
221 bNewArea = sal_False;
224 return bNewArea;
227 void SfxHTMLParser::StartFileDownload(const OUString& rURL)
229 DBG_ASSERT( !pDLMedium, "StartFileDownload when active Download" );
230 if( pDLMedium )
231 return;
233 pDLMedium = new SfxMedium( rURL, SFX_STREAM_READONLY );
234 pDLMedium->DownLoad();
237 sal_Bool SfxHTMLParser::FinishFileDownload( String& rStr )
239 sal_Bool bOK = pDLMedium && pDLMedium->GetErrorCode()==0;
240 if( bOK )
242 SvStream* pStream = pDLMedium->GetInStream();
243 DBG_ASSERT( pStream, "No In-Stream received from Medium" );
245 SvMemoryStream aStream;
246 if( pStream )
247 aStream << *pStream;
249 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 xub_StrLen nLen = aStream.Tell() < STRING_MAXLEN
253 ? (xub_StrLen)aStream.Tell()
254 : STRING_MAXLEN;
256 aStream.Seek( 0 );
257 OString sBuffer = read_uInt8s_ToOString(aStream, nLen);
258 rStr = S2U(sBuffer);
261 delete pDLMedium;
262 pDLMedium = 0;
264 return bOK;
267 void SfxHTMLParser::GetScriptType_Impl( SvKeyValueIterator *pHTTPHeader )
269 aScriptType = SVX_MACRO_LANGUAGE_JAVASCRIPT;
270 eScriptType = JAVASCRIPT;
271 if( pHTTPHeader )
273 SvKeyValue aKV;
274 for( sal_Bool bCont = pHTTPHeader->GetFirst( aKV ); bCont;
275 bCont = pHTTPHeader->GetNext( aKV ) )
277 if( aKV.GetKey().EqualsIgnoreCaseAscii(
278 OOO_STRING_SVTOOLS_HTML_META_content_script_type ) )
280 if( aKV.GetValue().Len() )
282 String aTmp( aKV.GetValue() );
283 if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_text, 0, 5 ) )
284 aTmp.Erase( 0, 5 );
285 else if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_application,
286 0, 12 ) )
287 aTmp.Erase( 0, 12 );
288 else
289 break;
291 if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_experimental, 0,
292 2 ) )
294 aTmp.Erase( 0, 2 );
297 if( aTmp.EqualsIgnoreCaseAscii( OOO_STRING_SVTOOLS_HTML_LG_starbasic ) )
299 eScriptType = STARBASIC;
300 aScriptType = SVX_MACRO_LANGUAGE_STARBASIC;
302 if( !aTmp.EqualsIgnoreCaseAscii( OOO_STRING_SVTOOLS_HTML_LG_javascript ) )
304 eScriptType = EXTENDED_STYPE;
305 aScriptType = aTmp;
308 break;
314 ScriptType SfxHTMLParser::GetScriptType( SvKeyValueIterator *pHTTPHeader ) const
316 if( !aScriptType.Len() )
317 ((SfxHTMLParser *)this)->GetScriptType_Impl( pHTTPHeader );
319 return eScriptType;
322 const String& SfxHTMLParser::GetScriptTypeString(
323 SvKeyValueIterator *pHTTPHeader ) const
325 if( !aScriptType.Len() )
326 ((SfxHTMLParser *)this)->GetScriptType_Impl( pHTTPHeader );
328 return aScriptType;
331 double SfxHTMLParser::GetTableDataOptionsValNum( sal_uInt32& nNumForm,
332 LanguageType& eNumLang, const String& aValStr, const String& aNumStr,
333 SvNumberFormatter& rFormatter )
335 LanguageType eParseLang = (LanguageType )aNumStr.ToInt32();
336 sal_uInt32 nParseForm = rFormatter.GetFormatForLanguageIfBuiltIn( 0, eParseLang );
337 double fVal;
338 rFormatter.IsNumberFormat( aValStr, nParseForm, fVal );
339 if ( comphelper::string::getTokenCount(aNumStr, ';') > 2 )
341 eNumLang = (LanguageType)aNumStr.GetToken( 1, ';' ).ToInt32();
342 xub_StrLen nPos = aNumStr.Search( ';' );
343 nPos = aNumStr.Search( ';', nPos + 1 );
344 OUString aFormat( aNumStr.Copy( nPos + 1 ) );
345 sal_Int32 nCheckPos;
346 short nType;
347 if ( eNumLang != LANGUAGE_SYSTEM )
348 rFormatter.PutEntry( aFormat, nCheckPos, nType, nNumForm, eNumLang );
349 else
350 rFormatter.PutandConvertEntry( aFormat, nCheckPos, nType, nNumForm,
351 eParseLang, eNumLang );
353 else
355 eNumLang = LANGUAGE_SYSTEM;
356 nNumForm = rFormatter.GetFormatForLanguageIfBuiltIn( 0, eNumLang );
358 return fVal;
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */