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 <config_features.h>
21 :
22 : #include <hintids.hxx>
23 : #include <comphelper/string.hxx>
24 : #include <rtl/strbuf.hxx>
25 : #include <sfx2/sfx.hrc>
26 : #include <basic/sbx.hxx>
27 : #include <basic/basmgr.hxx>
28 : #include <basic/sbmod.hxx>
29 : #include <sfx2/evntconf.hxx>
30 : #include <sfx2/app.hxx>
31 : #include <svtools/htmlout.hxx>
32 : #include <svtools/htmltokn.h>
33 : #include <svtools/htmlkywd.hxx>
34 :
35 : #include <com/sun/star/document/XEventsSupplier.hpp>
36 : #include <com/sun/star/uno/Reference.hxx>
37 :
38 : #include <fmtornt.hxx>
39 : #include <fmtfld.hxx>
40 :
41 : #include "doc.hxx"
42 : #include <IDocumentFieldsAccess.hxx>
43 : #include "docsh.hxx"
44 : #include "docufld.hxx"
45 : #include "wrthtml.hxx"
46 : #include "swhtml.hxx"
47 :
48 : using namespace ::com::sun::star;
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::container;
51 :
52 : static HTMLOutEvent aBodyEventTable[] =
53 : {
54 : { OOO_STRING_SVTOOLS_HTML_O_SDonload, OOO_STRING_SVTOOLS_HTML_O_onload, SFX_EVENT_OPENDOC },
55 : { OOO_STRING_SVTOOLS_HTML_O_SDonunload, OOO_STRING_SVTOOLS_HTML_O_onunload, SFX_EVENT_PREPARECLOSEDOC },
56 : { OOO_STRING_SVTOOLS_HTML_O_SDonfocus, OOO_STRING_SVTOOLS_HTML_O_onfocus, SFX_EVENT_ACTIVATEDOC },
57 : { OOO_STRING_SVTOOLS_HTML_O_SDonblur, OOO_STRING_SVTOOLS_HTML_O_onblur, SFX_EVENT_DEACTIVATEDOC },
58 : { 0, 0, 0 }
59 : };
60 :
61 0 : void SwHTMLParser::NewScript()
62 : {
63 : ParseScriptOptions( aScriptType, sBaseURL, eScriptLang, aScriptURL,
64 0 : aBasicLib, aBasicModule );
65 :
66 0 : if( !aScriptURL.isEmpty() )
67 : {
68 : // Den Inhalt des Script-Tags ignorieren
69 0 : bIgnoreRawData = true;
70 : }
71 0 : }
72 :
73 0 : void SwHTMLParser::EndScript()
74 : {
75 0 : bool bInsIntoBasic = false,
76 0 : bInsSrcIntoField = false;
77 :
78 0 : switch( eScriptLang )
79 : {
80 : case HTML_SL_STARBASIC:
81 0 : bInsIntoBasic = true;
82 0 : break;
83 : default:
84 0 : bInsSrcIntoField = true;
85 0 : break;
86 : }
87 :
88 0 : bIgnoreRawData = 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( bInsSrcIntoField && !bIgnoreHTMLComments )
94 : {
95 : SwScriptFieldType *pType =
96 0 : static_cast<SwScriptFieldType*>(pDoc->getIDocumentFieldsAccess().GetSysFieldType( RES_SCRIPTFLD ));
97 :
98 : SwScriptField aField( pType, aScriptType,
99 0 : !aScriptURL.isEmpty() ? aScriptURL : aScriptSource,
100 0 : !aScriptURL.isEmpty() );
101 0 : InsertAttr( SwFormatField( aField ) );
102 : }
103 :
104 0 : SwDocShell *pDocSh = pDoc->GetDocShell();
105 0 : if( !aScriptSource.isEmpty() && 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, true );
111 :
112 : // get library name
113 0 : OUString aLibName;
114 0 : if( !aBasicLib.isEmpty() )
115 0 : aLibName = aBasicLib;
116 : else
117 0 : aLibName = "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.isEmpty() )
140 : {
141 : // create module name
142 0 : bool bFound = true;
143 0 : while( bFound )
144 : {
145 0 : aBasicModule = "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.clear();
176 0 : aScriptType.clear();
177 0 : aScriptURL.clear();
178 :
179 0 : aBasicLib.clear();
180 0 : aBasicModule.clear();
181 0 : }
182 :
183 0 : void SwHTMLParser::AddScriptSource()
184 : {
185 : // Hier merken wir und nur ein par Strings
186 0 : if( aToken.getLength() > 2 &&
187 0 : (HTML_SL_STARBASIC==eScriptLang && aToken[ 0 ] == '\'') )
188 : {
189 0 : sal_Int32 nPos = -1;
190 0 : if( aBasicLib.isEmpty() )
191 : {
192 0 : nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_library );
193 0 : if( nPos != -1 )
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.isEmpty() && nPos == -1 )
202 : {
203 0 : nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_module );
204 0 : if( nPos != -1 )
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 == -1 )
213 : {
214 0 : if( !aScriptSource.isEmpty() )
215 0 : aScriptSource += "\n";
216 0 : aScriptSource += aToken;
217 : }
218 : }
219 0 : else if( !aScriptSource.isEmpty() || !aToken.isEmpty() )
220 : {
221 : // Leerzeilen am Anfang werden ignoriert
222 0 : if( !aScriptSource.isEmpty() )
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( const OUString& aEvent, const OUString& rName,
236 : ScriptType eScrType,
237 : const OUString& rScrType )
238 : {
239 : OSL_ENSURE( !rName.isEmpty(), "InsertBasicDocEvent() ohne Macro gerufen" );
240 0 : if( rName.isEmpty() )
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 : OUString sEvent(convertLineEnd(rName, GetSystemLineEnd()));
249 0 : OUString 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 14 : void SwHTMLWriter::OutBasic()
258 : {
259 : #if HAVE_FEATURE_SCRIPTING
260 14 : if( !bCfgStarBasic )
261 14 : 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 == SfxApplication::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 OUString& 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 &rModule = dynamic_cast<const SbModule&>(*pModules->Get(j));
282 :
283 0 : OUString sLang(SVX_MACRO_LANGUAGE_STARBASIC);
284 0 : ScriptType eType = STARBASIC;
285 :
286 0 : if( 0==i && 0==j )
287 : {
288 0 : OutNewLine();
289 0 : OStringBuffer sOut;
290 0 : sOut.append('<').append(OOO_STRING_SVTOOLS_HTML_meta)
291 0 : .append(' ').append(OOO_STRING_SVTOOLS_HTML_O_httpequiv)
292 0 : .append("=\"")
293 0 : .append(OOO_STRING_SVTOOLS_HTML_META_content_script_type)
294 0 : .append("\" ").append(OOO_STRING_SVTOOLS_HTML_O_content)
295 0 : .append("=\"text/x-");
296 0 : Strm().WriteCharPtr( sOut.getStr() );
297 : // Entities aren't welcome here
298 0 : Strm().WriteCharPtr( OUStringToOString(sLang, eDestEnc).getStr() )
299 0 : .WriteCharPtr( "\">" );
300 : }
301 :
302 0 : const OUString& rModName = rModule.GetName();
303 0 : Strm().WriteCharPtr( SAL_NEWLINE_STRING ); // nicht einruecken!
304 0 : HTMLOutFuncs::OutScript( Strm(), GetBaseURL(), rModule.GetSource(),
305 : sLang, eType, aEmptyOUStr,
306 : &rLibName, &rModName,
307 0 : eDestEnc, &aNonConvertableCharacters );
308 0 : }
309 : }
310 : #endif
311 : }
312 :
313 : static const char* aEventNames[] =
314 : {
315 : "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
316 : };
317 :
318 14 : void SwHTMLWriter::OutBasicBodyEvents()
319 : {
320 14 : SwDocShell *pDocSh = pDoc->GetDocShell();
321 14 : if( !pDocSh )
322 14 : return;
323 :
324 14 : SvxMacroTableDtor aDocTable;
325 :
326 28 : uno::Reference< document::XEventsSupplier > xSup( pDocSh->GetModel(), uno::UNO_QUERY );
327 28 : uno::Reference < container::XNameReplace > xEvents = xSup->getEvents();
328 70 : for ( sal_Int32 i=0; i<4; i++ )
329 : {
330 56 : SvxMacro* pMacro = SfxEventConfiguration::ConvertToMacro( xEvents->getByName( OUString::createFromAscii(aEventNames[i]) ), pDocSh, true );
331 56 : if ( pMacro )
332 : {
333 0 : aDocTable.Insert( aBodyEventTable[i].nEvent, *pMacro );
334 0 : delete pMacro;
335 : }
336 : }
337 :
338 14 : if( !aDocTable.empty() )
339 0 : HTMLOutFuncs::Out_Events( Strm(), aDocTable, aBodyEventTable,
340 14 : bCfgStarBasic, eDestEnc, &aNonConvertableCharacters );
341 177 : }
342 :
343 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|