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