Branch data 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 : 2 : SfxHTMLParser::SfxHTMLParser( SvStream& rStream, sal_Bool bIsNewDoc,
66 : : SfxMedium *pMed ) :
67 : : HTMLParser( rStream, bIsNewDoc ),
68 [ + - ]: 2 : 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 : 2 : SetSwitchToUCS2( sal_True );
78 : 2 : }
79 : :
80 [ + - ]: 2 : SfxHTMLParser::~SfxHTMLParser()
81 : : {
82 : : DBG_ASSERT( !pDLMedium, "Here is a File Download that has got stuck" );
83 [ - + ][ # # ]: 2 : delete pDLMedium;
84 [ - + ]: 2 : }
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 aRec( aCoords[0], aCoords[1],
187 [ # # ][ # # ]: 0 : aCoords[2], aCoords[3] );
[ # # ]
188 : : IMapRectangleObject aMapRObj( aRec, 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 : String aStr;
240 : :
241 [ # # ][ # # ]: 0 : sal_Bool bOK = pDLMedium && pDLMedium->GetErrorCode()==0;
[ # # ]
242 [ # # ]: 0 : if( bOK )
243 : : {
244 [ # # ]: 0 : SvStream* pStream = pDLMedium->GetInStream();
245 : : DBG_ASSERT( pStream, "No In-Stream received from Medium" );
246 : :
247 [ # # ]: 0 : SvMemoryStream aStream;
248 [ # # ]: 0 : if( pStream )
249 [ # # ]: 0 : aStream << *pStream;
250 : :
251 [ # # ]: 0 : aStream.Seek( STREAM_SEEK_TO_END );
252 : : DBG_ASSERT( aStream.Tell() < STRING_MAXLEN,
253 : : "File too long for a string, cut off the end" );
254 : 0 : xub_StrLen nLen = aStream.Tell() < STRING_MAXLEN
255 : 0 : ? (xub_StrLen)aStream.Tell()
256 [ # # ]: 0 : : STRING_MAXLEN;
257 : :
258 [ # # ]: 0 : aStream.Seek( 0 );
259 [ # # ]: 0 : rtl::OString sBuffer = read_uInt8s_ToOString(aStream, nLen);
260 [ # # ][ # # ]: 0 : rStr = S2U(sBuffer);
[ # # ]
261 : : }
262 : :
263 [ # # ][ # # ]: 0 : delete pDLMedium;
264 : 0 : pDLMedium = 0;
265 : :
266 [ # # ]: 0 : return bOK;
267 : : }
268 : :
269 : 2 : void SfxHTMLParser::GetScriptType_Impl( SvKeyValueIterator *pHTTPHeader )
270 : : {
271 [ + - ]: 2 : aScriptType = DEFINE_CONST_UNICODE(SVX_MACRO_LANGUAGE_JAVASCRIPT);
272 : 2 : eScriptType = JAVASCRIPT;
273 [ + - ]: 2 : if( pHTTPHeader )
274 : : {
275 [ + - ]: 2 : SvKeyValue aKV;
276 [ + - ][ + - ]: 6 : for( sal_Bool bCont = pHTTPHeader->GetFirst( aKV ); bCont;
[ + + ]
277 : 4 : bCont = pHTTPHeader->GetNext( aKV ) )
278 : : {
279 [ - + ]: 8 : if( aKV.GetKey().EqualsIgnoreCaseAscii(
280 [ + - ]: 4 : OOO_STRING_SVTOOLS_HTML_META_content_script_type ) )
281 : : {
282 [ # # ]: 0 : if( aKV.GetValue().Len() )
283 : : {
284 [ # # ]: 0 : String aTmp( aKV.GetValue() );
285 [ # # ][ # # ]: 0 : if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_text, 0, 5 ) )
286 [ # # ]: 0 : aTmp.Erase( 0, 5 );
287 [ # # ]: 0 : else if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_application,
288 [ # # ]: 0 : 0, 12 ) )
289 [ # # ]: 0 : aTmp.Erase( 0, 12 );
290 : : else
291 : : break;
292 : :
293 [ # # ]: 0 : if( aTmp.EqualsIgnoreCaseAscii( sHTML_MIME_experimental, 0,
294 [ # # ]: 0 : 2 ) )
295 : : {
296 [ # # ]: 0 : aTmp.Erase( 0, 2 );
297 : : }
298 : :
299 [ # # ][ # # ]: 0 : if( aTmp.EqualsIgnoreCaseAscii( OOO_STRING_SVTOOLS_HTML_LG_starbasic ) )
300 : : {
301 : 0 : eScriptType = STARBASIC;
302 [ # # ][ # # ]: 0 : aScriptType = DEFINE_CONST_UNICODE(SVX_MACRO_LANGUAGE_STARBASIC);
[ # # ]
303 : : }
304 [ # # ][ # # ]: 0 : if( !aTmp.EqualsIgnoreCaseAscii( OOO_STRING_SVTOOLS_HTML_LG_javascript ) )
305 : : {
306 : 0 : eScriptType = EXTENDED_STYPE;
307 [ # # ]: 0 : aScriptType = aTmp;
308 [ # # ][ # # ]: 0 : }
309 : : }
310 : 0 : break;
311 : : }
312 [ + - ]: 2 : }
313 : : }
314 : 2 : }
315 : :
316 : 2 : ScriptType SfxHTMLParser::GetScriptType( SvKeyValueIterator *pHTTPHeader ) const
317 : : {
318 [ + - ]: 2 : if( !aScriptType.Len() )
319 : 2 : ((SfxHTMLParser *)this)->GetScriptType_Impl( pHTTPHeader );
320 : :
321 : 2 : return eScriptType;
322 : : }
323 : :
324 : 2 : const String& SfxHTMLParser::GetScriptTypeString(
325 : : SvKeyValueIterator *pHTTPHeader ) const
326 : : {
327 [ - + ]: 2 : if( !aScriptType.Len() )
328 : 0 : ((SfxHTMLParser *)this)->GetScriptType_Impl( pHTTPHeader );
329 : :
330 : 2 : return aScriptType;
331 : : }
332 : :
333 : 0 : double SfxHTMLParser::GetTableDataOptionsValNum( sal_uInt32& nNumForm,
334 : : LanguageType& eNumLang, const String& aValStr, const String& aNumStr,
335 : : SvNumberFormatter& rFormatter )
336 : : {
337 [ # # ]: 0 : LanguageType eParseLang = (LanguageType )aNumStr.ToInt32();
338 : : sal_uInt32 nParseForm =
339 [ # # ]: 0 : rFormatter.GetFormatForLanguageIfBuiltIn( 0, eParseLang );
340 : : double fVal;
341 [ # # ]: 0 : rFormatter.IsNumberFormat( aValStr, nParseForm, fVal );
342 [ # # ][ # # ]: 0 : if ( comphelper::string::getTokenCount(aNumStr, ';') > 2 )
[ # # ]
343 : : {
344 [ # # ][ # # ]: 0 : eNumLang = (LanguageType)aNumStr.GetToken( 1, ';' ).ToInt32();
[ # # ]
345 [ # # ]: 0 : xub_StrLen nPos = aNumStr.Search( ';' );
346 [ # # ]: 0 : nPos = aNumStr.Search( ';', nPos + 1 );
347 [ # # ]: 0 : String aFormat( aNumStr.Copy( nPos + 1 ) );
348 : : xub_StrLen nCheckPos;
349 : : short nType;
350 [ # # ]: 0 : if ( eNumLang != LANGUAGE_SYSTEM )
351 [ # # ]: 0 : rFormatter.PutEntry( aFormat, nCheckPos, nType, nNumForm, eNumLang );
352 : : else
353 : : rFormatter.PutandConvertEntry( aFormat, nCheckPos, nType, nNumForm,
354 [ # # ][ # # ]: 0 : eParseLang, eNumLang );
355 : : }
356 : : else
357 : : {
358 : 0 : eNumLang = LANGUAGE_SYSTEM;
359 [ # # ]: 0 : nNumForm = rFormatter.GetFormatForLanguageIfBuiltIn( 0, eNumLang );
360 : : }
361 : 0 : return fVal;
362 : : }
363 : :
364 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|