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 : #include <hintids.hxx>
21 : #include <comphelper/string.hxx>
22 : #include <rtl/strbuf.hxx>
23 : #include <sfx2/sfx.hrc>
24 : #include <basic/sbx.hxx>
25 : #include <basic/basmgr.hxx>
26 : #include <basic/sbmod.hxx>
27 : #include <sfx2/evntconf.hxx>
28 : #include <sfx2/app.hxx>
29 : #include <svtools/htmlout.hxx>
30 : #include <svtools/htmltokn.h>
31 : #include <svtools/htmlkywd.hxx>
32 :
33 : #include <com/sun/star/document/XEventsSupplier.hpp>
34 : #include <com/sun/star/uno/Reference.hxx>
35 :
36 : #include <fmtornt.hxx>
37 : #include <fmtfld.hxx>
38 :
39 : #include "doc.hxx"
40 : #include "docsh.hxx"
41 : #include "docufld.hxx"
42 : #include "wrthtml.hxx"
43 : #include "swhtml.hxx"
44 :
45 :
46 : using namespace ::com::sun::star;
47 : using namespace ::com::sun::star::uno;
48 : using namespace ::com::sun::star::container;
49 :
50 :
51 : static HTMLOutEvent aBodyEventTable[] =
52 : {
53 : { OOO_STRING_SVTOOLS_HTML_O_SDonload, OOO_STRING_SVTOOLS_HTML_O_onload, SFX_EVENT_OPENDOC },
54 : { OOO_STRING_SVTOOLS_HTML_O_SDonunload, OOO_STRING_SVTOOLS_HTML_O_onunload, SFX_EVENT_PREPARECLOSEDOC },
55 : { OOO_STRING_SVTOOLS_HTML_O_SDonfocus, OOO_STRING_SVTOOLS_HTML_O_onfocus, SFX_EVENT_ACTIVATEDOC },
56 : { OOO_STRING_SVTOOLS_HTML_O_SDonblur, OOO_STRING_SVTOOLS_HTML_O_onblur, SFX_EVENT_DEACTIVATEDOC },
57 : { 0, 0, 0 }
58 : };
59 :
60 :
61 0 : void SwHTMLParser::NewScript()
62 : {
63 : ParseScriptOptions( aScriptType, sBaseURL, eScriptLang, aScriptURL,
64 0 : aBasicLib, aBasicModule );
65 :
66 0 : if( aScriptURL.Len() )
67 : {
68 : // Den Inhalt des Script-Tags ignorieren
69 0 : bIgnoreRawData = sal_True;
70 : }
71 0 : }
72 :
73 0 : void SwHTMLParser::EndScript()
74 : {
75 0 : sal_Bool bInsIntoBasic = sal_False,
76 0 : bInsSrcIntoFld = sal_False;
77 :
78 0 : switch( eScriptLang )
79 : {
80 : case HTML_SL_STARBASIC:
81 0 : bInsIntoBasic = sal_True;
82 0 : break;
83 : default:
84 0 : bInsSrcIntoFld = sal_True;
85 0 : break;
86 : }
87 :
88 0 : bIgnoreRawData = sal_False;
89 0 : aScriptSource = convertLineEnd(aScriptSource, GetSystemLineEnd());
90 :
91 : // Ausser StarBasic und unbenutzem JavaScript jedes Script oder den
92 : // Modulnamen in einem Feld merken merken
93 0 : if( bInsSrcIntoFld && !bIgnoreHTMLComments )
94 : {
95 : SwScriptFieldType *pType =
96 0 : (SwScriptFieldType*)pDoc->GetSysFldType( RES_SCRIPTFLD );
97 :
98 : SwScriptField aFld( pType, aScriptType,
99 0 : aScriptURL.Len() ? aScriptURL : aScriptSource,
100 0 : aScriptURL.Len()!=0 );
101 0 : InsertAttr( SwFmtFld( aFld ) );
102 : }
103 :
104 0 : SwDocShell *pDocSh = pDoc->GetDocShell();
105 0 : if( aScriptSource.Len() && pDocSh &&
106 0 : bInsIntoBasic && IsNewDoc() )
107 : {
108 : // Fuer JavaScript und StarBasic noch ein Basic-Modul anlegen
109 : // Das Basic entfernt natuerlich weiterhin keine SGML-Kommentare
110 0 : RemoveSGMLComment( aScriptSource, sal_True );
111 :
112 : // get library name
113 0 : OUString aLibName;
114 0 : if( aBasicLib.Len() )
115 0 : aLibName = aBasicLib;
116 : else
117 0 : aLibName = OUString("Standard");
118 :
119 : // get module library container
120 0 : Reference< script::XLibraryContainer > xModLibContainer( pDocSh->GetBasicContainer(), UNO_QUERY );
121 :
122 0 : if ( xModLibContainer.is() )
123 : {
124 0 : Reference< container::XNameContainer > xModLib;
125 0 : if ( xModLibContainer->hasByName( aLibName ) )
126 : {
127 : // get module library
128 0 : Any aElement = xModLibContainer->getByName( aLibName );
129 0 : aElement >>= xModLib;
130 : }
131 : else
132 : {
133 : // create module library
134 0 : xModLib = xModLibContainer->createLibrary( aLibName );
135 : }
136 :
137 0 : if ( xModLib.is() )
138 : {
139 0 : if( !aBasicModule.Len() )
140 : {
141 : // create module name
142 0 : sal_Bool bFound = sal_True;
143 0 : while( bFound )
144 : {
145 0 : aBasicModule.AssignAscii( "Modul" );
146 0 : aBasicModule += OUString::number( (sal_Int32)(++nSBModuleCnt) );
147 0 : bFound = xModLib->hasByName( OUString( aBasicModule ) );
148 : }
149 : }
150 :
151 : // create module
152 0 : OUString aModName( aBasicModule );
153 0 : if ( !xModLib->hasByName( aModName ) )
154 : {
155 0 : Any aElement;
156 0 : aElement <<= OUString( aScriptSource );
157 0 : xModLib->insertByName( aModName , aElement );
158 0 : }
159 0 : }
160 : }
161 :
162 : // get dialog library container
163 0 : Reference< script::XLibraryContainer > xDlgLibContainer( pDocSh->GetDialogContainer(), UNO_QUERY );
164 :
165 0 : if ( xDlgLibContainer.is() )
166 : {
167 0 : if ( !xDlgLibContainer->hasByName( aLibName ) )
168 : {
169 : // create dialog library
170 0 : xDlgLibContainer->createLibrary( aLibName );
171 : }
172 0 : }
173 : }
174 :
175 0 : aScriptSource.Erase();
176 0 : aScriptType.Erase();
177 0 : aScriptURL.Erase();
178 :
179 0 : aBasicLib.Erase();
180 0 : aBasicModule.Erase();
181 0 : }
182 :
183 0 : void SwHTMLParser::AddScriptSource()
184 : {
185 : // Hier merken wir und nur ein par Strings
186 0 : if( aToken.Len() > 2 &&
187 0 : (HTML_SL_STARBASIC==eScriptLang && aToken.GetChar( 0 ) == '\'') )
188 : {
189 0 : xub_StrLen nPos = STRING_NOTFOUND;
190 0 : if( !aBasicLib.Len() )
191 : {
192 0 : nPos = aToken.SearchAscii( OOO_STRING_SVTOOLS_HTML_SB_library );
193 0 : if( nPos != STRING_NOTFOUND )
194 : {
195 0 : aBasicLib =
196 0 : aToken.Copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_library) - 1 );
197 0 : aBasicLib = comphelper::string::strip(aBasicLib, ' ');
198 : }
199 : }
200 :
201 0 : if( !aBasicModule.Len() && nPos==STRING_NOTFOUND )
202 : {
203 0 : nPos = aToken.SearchAscii( OOO_STRING_SVTOOLS_HTML_SB_module );
204 0 : if( nPos != STRING_NOTFOUND )
205 : {
206 0 : aBasicModule =
207 0 : aToken.Copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_module) - 1 );
208 0 : aBasicModule = comphelper::string::strip(aBasicModule, ' ');
209 : }
210 : }
211 :
212 0 : if( nPos==STRING_NOTFOUND )
213 : {
214 0 : if( aScriptSource.Len() )
215 0 : aScriptSource += '\n';
216 0 : (aScriptSource += aToken);
217 : }
218 : }
219 0 : else if( aScriptSource.Len() || aToken.Len() )
220 : {
221 : // Leerzeilen am Anfang werden ignoriert
222 0 : if( aScriptSource.Len() )
223 : {
224 0 : aScriptSource += '\n';
225 : }
226 : else
227 : {
228 : // Wir stehen hinter dem CR/LF der Zeile davor
229 0 : nScriptStartLineNr = GetLineNr() - 1;
230 : }
231 0 : aScriptSource += aToken;
232 : }
233 0 : }
234 :
235 0 : void SwHTMLParser::InsertBasicDocEvent( OUString aEvent, const String& rName,
236 : ScriptType eScrType,
237 : const String& rScrType )
238 : {
239 : OSL_ENSURE( rName.Len(), "InsertBasicDocEvent() ohne Macro gerufen" );
240 0 : if( !rName.Len() )
241 0 : return;
242 :
243 0 : SwDocShell *pDocSh = pDoc->GetDocShell();
244 : OSL_ENSURE( pDocSh, "Wo ist die DocShell?" );
245 0 : if( !pDocSh )
246 0 : return;
247 :
248 0 : String sEvent(convertLineEnd(rName, GetSystemLineEnd()));
249 0 : String sScriptType;
250 0 : if( EXTENDED_STYPE == eScrType )
251 0 : sScriptType = rScrType;
252 :
253 : SfxEventConfiguration::ConfigureEvent( aEvent, SvxMacro( sEvent, sScriptType, eScrType ),
254 0 : pDocSh );
255 : }
256 :
257 2 : void SwHTMLWriter::OutBasic()
258 : {
259 : #ifndef DISABLE_SCRIPTING
260 2 : if( !bCfgStarBasic )
261 2 : return;
262 :
263 0 : BasicManager *pBasicMan = pDoc->GetDocShell()->GetBasicManager();
264 : OSL_ENSURE( pBasicMan, "Wo ist der Basic-Manager?" );
265 : // nur das DocumentBasic schreiben
266 0 : if( !pBasicMan || pBasicMan == SFX_APP()->GetBasicManager() )
267 : {
268 0 : return;
269 : }
270 :
271 : // und jetzt alle StarBasic-Module und alle unbenutzen JavaSrript-Module
272 : // ausgeben
273 0 : for( sal_uInt16 i=0; i<pBasicMan->GetLibCount(); i++ )
274 : {
275 0 : StarBASIC *pBasic = pBasicMan->GetLib( i );
276 0 : const String& rLibName = pBasic->GetName();
277 :
278 0 : SbxArray *pModules = pBasic->GetModules();
279 0 : for( sal_uInt16 j=0; j<pModules->Count(); j++ )
280 : {
281 0 : const SbModule *pModule = PTR_CAST( SbModule, pModules->Get(j) );
282 : OSL_ENSURE( pModule, "Wo ist das Modul?" );
283 :
284 0 : OUString sLang(SVX_MACRO_LANGUAGE_STARBASIC);
285 0 : ScriptType eType = STARBASIC;
286 :
287 0 : if( 0==i && 0==j )
288 : {
289 0 : OutNewLine();
290 0 : OStringBuffer sOut;
291 0 : sOut.append('<').append(OOO_STRING_SVTOOLS_HTML_meta)
292 0 : .append(' ').append(OOO_STRING_SVTOOLS_HTML_O_httpequiv)
293 0 : .append("=\"")
294 0 : .append(OOO_STRING_SVTOOLS_HTML_META_content_script_type)
295 0 : .append("\" ").append(OOO_STRING_SVTOOLS_HTML_O_content)
296 0 : .append("=\"text/x-");
297 0 : Strm() << sOut.getStr();
298 : // Entities aren't welcome here
299 0 : Strm() << OUStringToOString(sLang, eDestEnc).getStr()
300 0 : << "\">";
301 : }
302 :
303 0 : const String& rModName = pModule->GetName();
304 0 : Strm() << SwHTMLWriter::sNewLine; // nicht einruecken!
305 0 : HTMLOutFuncs::OutScript( Strm(), GetBaseURL(), pModule->GetSource(),
306 : sLang, eType, aEmptyStr,
307 : &rLibName, &rModName,
308 0 : eDestEnc, &aNonConvertableCharacters );
309 0 : }
310 0 : }
311 : #endif
312 : }
313 :
314 : static const char* aEventNames[] =
315 : {
316 : "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
317 : };
318 :
319 2 : void SwHTMLWriter::OutBasicBodyEvents()
320 : {
321 2 : SwDocShell *pDocSh = pDoc->GetDocShell();
322 2 : if( !pDocSh )
323 2 : return;
324 :
325 2 : SvxMacroTableDtor *pDocTable = new SvxMacroTableDtor;
326 :
327 2 : uno::Reference< document::XEventsSupplier > xSup( pDocSh->GetModel(), uno::UNO_QUERY );
328 4 : uno::Reference < container::XNameReplace > xEvents = xSup->getEvents();
329 10 : for ( sal_Int32 i=0; i<4; i++ )
330 : {
331 8 : SvxMacro* pMacro = SfxEventConfiguration::ConvertToMacro( xEvents->getByName( OUString::createFromAscii(aEventNames[i]) ), pDocSh, sal_True );
332 8 : if ( pMacro )
333 : {
334 0 : pDocTable->Insert( aBodyEventTable[i].nEvent, *pMacro );
335 0 : delete pMacro;
336 : }
337 : }
338 :
339 2 : if( pDocTable && !pDocTable->empty() )
340 0 : HTMLOutFuncs::Out_Events( Strm(), *pDocTable, aBodyEventTable,
341 2 : bCfgStarBasic, eDestEnc, &aNonConvertableCharacters );
342 99 : }
343 :
344 :
345 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|