1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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-";
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
},
65 SfxHTMLParser::SfxHTMLParser( SvStream
& rStream
, sal_Bool bIsNewDoc
,
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" );
86 bool SfxHTMLParser::ParseMapOptions(
87 ImageMap
* pImageMap
, const HTMLOptions
& rOptions
)
89 DBG_ASSERT( pImageMap
, "ParseMapOptions: No Image-Map" );
93 for (size_t i
= rOptions
.size(); i
; )
95 const HTMLOption
& aOption
= rOptions
[--i
];
96 switch( aOption
.GetToken() )
99 aName
= aOption
.GetString();
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() )
131 aName
= rOption
.GetString();
134 rOption
.GetEnum( nShape
, aAreaShapeOptEnums
);
137 rOption
.GetNumbers( aCoords
, true );
140 aHRef
= INetURLObject::GetAbsURL( rBaseURL
, rOption
.GetString() );
146 aAlt
= rOption
.GetString();
149 aTarget
= rOption
.GetString();
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
;
166 String
sTmp( rOption
.GetString() );
169 sTmp
= convertLineEnd(sTmp
, GetSystemLineEnd());
170 aMacroTbl
.Insert( nEvent
, SvxMacro( sTmp
, sEmpty
, eScrpType
));
180 sal_Bool bNewArea
= sal_True
;
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
,
190 if( !aMacroTbl
.empty() )
191 aMapRObj
.SetMacroTable( aMacroTbl
);
192 pImageMap
->InsertIMapObject( aMapRObj
);
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
);
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
,
215 if( !aMacroTbl
.empty() )
216 aMapPObj
.SetMacroTable( aMacroTbl
);
217 pImageMap
->InsertIMapObject( aMapPObj
);
221 bNewArea
= sal_False
;
227 void SfxHTMLParser::StartFileDownload(const OUString
& rURL
)
229 DBG_ASSERT( !pDLMedium
, "StartFileDownload when active Download" );
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;
242 SvStream
* pStream
= pDLMedium
->GetInStream();
243 DBG_ASSERT( pStream
, "No In-Stream received from Medium" );
245 SvMemoryStream aStream
;
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()
257 OString sBuffer
= read_uInt8s_ToOString(aStream
, nLen
);
267 void SfxHTMLParser::GetScriptType_Impl( SvKeyValueIterator
*pHTTPHeader
)
269 aScriptType
= SVX_MACRO_LANGUAGE_JAVASCRIPT
;
270 eScriptType
= JAVASCRIPT
;
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 ) )
285 else if( aTmp
.EqualsIgnoreCaseAscii( sHTML_MIME_application
,
291 if( aTmp
.EqualsIgnoreCaseAscii( sHTML_MIME_experimental
, 0,
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
;
314 ScriptType
SfxHTMLParser::GetScriptType( SvKeyValueIterator
*pHTTPHeader
) const
316 if( !aScriptType
.Len() )
317 ((SfxHTMLParser
*)this)->GetScriptType_Impl( pHTTPHeader
);
322 const String
& SfxHTMLParser::GetScriptTypeString(
323 SvKeyValueIterator
*pHTTPHeader
) const
325 if( !aScriptType
.Len() )
326 ((SfxHTMLParser
*)this)->GetScriptType_Impl( pHTTPHeader
);
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
);
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 ) );
347 if ( eNumLang
!= LANGUAGE_SYSTEM
)
348 rFormatter
.PutEntry( aFormat
, nCheckPos
, nType
, nNumForm
, eNumLang
);
350 rFormatter
.PutandConvertEntry( aFormat
, nCheckPos
, nType
, nNumForm
,
351 eParseLang
, eNumLang
);
355 eNumLang
= LANGUAGE_SYSTEM
;
356 nNumForm
= rFormatter
.GetFormatForLanguageIfBuiltIn( 0, eNumLang
);
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */