build fix
[LibreOffice.git] / sw / source / filter / html / htmlbas.cxx
blob54ff3e252f2cbe86979ea6b021b98434d32ef240
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <config_features.h>
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>
35 #include <com/sun/star/document/XEventsSupplier.hpp>
36 #include <com/sun/star/uno/Reference.hxx>
38 #include <fmtornt.hxx>
39 #include <fmtfld.hxx>
41 #include "doc.hxx"
42 #include <IDocumentFieldsAccess.hxx>
43 #include "docsh.hxx"
44 #include "docufld.hxx"
45 #include "wrthtml.hxx"
46 #include "swhtml.hxx"
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::container;
52 static HTMLOutEvent aBodyEventTable[] =
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 { nullptr, nullptr, 0 }
61 void SwHTMLParser::NewScript()
63 ParseScriptOptions( m_aScriptType, m_sBaseURL, m_eScriptLang, m_aScriptURL,
64 m_aBasicLib, m_aBasicModule );
66 if( !m_aScriptURL.isEmpty() )
68 // Den Inhalt des Script-Tags ignorieren
69 m_bIgnoreRawData = true;
73 void SwHTMLParser::EndScript()
75 bool bInsIntoBasic = false,
76 bInsSrcIntoField = false;
78 switch( m_eScriptLang )
80 case HTML_SL_STARBASIC:
81 bInsIntoBasic = true;
82 break;
83 default:
84 bInsSrcIntoField = true;
85 break;
88 m_bIgnoreRawData = false;
89 m_aScriptSource = convertLineEnd(m_aScriptSource, GetSystemLineEnd());
91 // Ausser StarBasic und unbenutzem JavaScript jedes Script oder den
92 // Modulnamen in einem Feld merken merken
93 if( bInsSrcIntoField && !m_bIgnoreHTMLComments )
95 SwScriptFieldType *pType =
96 static_cast<SwScriptFieldType*>(m_pDoc->getIDocumentFieldsAccess().GetSysFieldType( RES_SCRIPTFLD ));
98 SwScriptField aField( pType, m_aScriptType,
99 !m_aScriptURL.isEmpty() ? m_aScriptURL : m_aScriptSource,
100 !m_aScriptURL.isEmpty() );
101 InsertAttr( SwFormatField( aField ), false );
104 SwDocShell *pDocSh = m_pDoc->GetDocShell();
105 if( !m_aScriptSource.isEmpty() && pDocSh &&
106 bInsIntoBasic && IsNewDoc() )
108 // Fuer JavaScript und StarBasic noch ein Basic-Modul anlegen
109 // Das Basic entfernt natuerlich weiterhin keine SGML-Kommentare
110 RemoveSGMLComment( m_aScriptSource, true );
112 // get library name
113 OUString aLibName;
114 if( !m_aBasicLib.isEmpty() )
115 aLibName = m_aBasicLib;
116 else
117 aLibName = "Standard";
119 // get module library container
120 Reference< script::XLibraryContainer > xModLibContainer( pDocSh->GetBasicContainer(), UNO_QUERY );
122 if ( xModLibContainer.is() )
124 Reference< container::XNameContainer > xModLib;
125 if ( xModLibContainer->hasByName( aLibName ) )
127 // get module library
128 Any aElement = xModLibContainer->getByName( aLibName );
129 aElement >>= xModLib;
131 else
133 // create module library
134 xModLib = xModLibContainer->createLibrary( aLibName );
137 if ( xModLib.is() )
139 if( m_aBasicModule.isEmpty() )
141 // create module name
142 bool bFound = true;
143 while( bFound )
145 m_aBasicModule = "Modul";
146 m_aBasicModule += OUString::number( (sal_Int32)(++m_nSBModuleCnt) );
147 bFound = xModLib->hasByName( m_aBasicModule );
151 // create module
152 OUString aModName( m_aBasicModule );
153 if ( !xModLib->hasByName( aModName ) )
155 Any aElement;
156 aElement <<= OUString( m_aScriptSource );
157 xModLib->insertByName( aModName , aElement );
162 // get dialog library container
163 Reference< script::XLibraryContainer > xDlgLibContainer( pDocSh->GetDialogContainer(), UNO_QUERY );
165 if ( xDlgLibContainer.is() )
167 if ( !xDlgLibContainer->hasByName( aLibName ) )
169 // create dialog library
170 xDlgLibContainer->createLibrary( aLibName );
175 m_aScriptSource.clear();
176 m_aScriptType.clear();
177 m_aScriptURL.clear();
179 m_aBasicLib.clear();
180 m_aBasicModule.clear();
183 void SwHTMLParser::AddScriptSource()
185 // Hier merken wir und nur ein par Strings
186 if( aToken.getLength() > 2 &&
187 (HTML_SL_STARBASIC==m_eScriptLang && aToken[ 0 ] == '\'') )
189 sal_Int32 nPos = -1;
190 if( m_aBasicLib.isEmpty() )
192 nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_library );
193 if( nPos != -1 )
195 m_aBasicLib =
196 aToken.copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_library) - 1 );
197 m_aBasicLib = comphelper::string::strip(m_aBasicLib, ' ');
201 if( m_aBasicModule.isEmpty() && nPos == -1 )
203 nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_module );
204 if( nPos != -1 )
206 m_aBasicModule =
207 aToken.copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_module) - 1 );
208 m_aBasicModule = comphelper::string::strip(m_aBasicModule, ' ');
212 if( nPos == -1 )
214 if( !m_aScriptSource.isEmpty() )
215 m_aScriptSource += "\n";
216 m_aScriptSource += aToken;
219 else if( !m_aScriptSource.isEmpty() || !aToken.isEmpty() )
221 // Leerzeilen am Anfang werden ignoriert
222 if( !m_aScriptSource.isEmpty() )
224 m_aScriptSource += "\n";
226 else
228 // Wir stehen hinter dem CR/LF der Zeile davor
229 m_nScriptStartLineNr = GetLineNr() - 1;
231 m_aScriptSource += aToken;
235 void SwHTMLParser::InsertBasicDocEvent( const OUString& aEvent, const OUString& rName,
236 ScriptType eScrType,
237 const OUString& rScrType )
239 OSL_ENSURE( !rName.isEmpty(), "InsertBasicDocEvent() ohne Macro gerufen" );
240 if( rName.isEmpty() )
241 return;
243 SwDocShell *pDocSh = m_pDoc->GetDocShell();
244 OSL_ENSURE( pDocSh, "Wo ist die DocShell?" );
245 if( !pDocSh )
246 return;
248 OUString sEvent(convertLineEnd(rName, GetSystemLineEnd()));
249 OUString sScriptType;
250 if( EXTENDED_STYPE == eScrType )
251 sScriptType = rScrType;
253 SfxEventConfiguration::ConfigureEvent( aEvent, SvxMacro( sEvent, sScriptType, eScrType ),
254 pDocSh );
257 void SwHTMLWriter::OutBasic()
259 #if HAVE_FEATURE_SCRIPTING
260 if( !m_bCfgStarBasic )
261 return;
263 BasicManager *pBasicMan = pDoc->GetDocShell()->GetBasicManager();
264 OSL_ENSURE( pBasicMan, "Wo ist der Basic-Manager?" );
265 // nur das DocumentBasic schreiben
266 if( !pBasicMan || pBasicMan == SfxApplication::GetBasicManager() )
268 return;
271 bool bFirst=true;
272 // und jetzt alle StarBasic-Module und alle unbenutzen JavaSrript-Module
273 // ausgeben
274 for( sal_uInt16 i=0; i<pBasicMan->GetLibCount(); i++ )
276 StarBASIC *pBasic = pBasicMan->GetLib( i );
277 const OUString& rLibName = pBasic->GetName();
278 for( const auto& pModule: pBasic->GetModules() )
280 OUString sLang(SVX_MACRO_LANGUAGE_STARBASIC);
281 ScriptType eType = STARBASIC;
283 if( bFirst )
285 bFirst = false;
286 OutNewLine();
287 OStringBuffer sOut;
288 sOut.append('<').append(OOO_STRING_SVTOOLS_HTML_meta)
289 .append(' ').append(OOO_STRING_SVTOOLS_HTML_O_httpequiv)
290 .append("=\"")
291 .append(OOO_STRING_SVTOOLS_HTML_META_content_script_type)
292 .append("\" ").append(OOO_STRING_SVTOOLS_HTML_O_content)
293 .append("=\"text/x-");
294 Strm().WriteCharPtr( sOut.getStr() );
295 // Entities aren't welcome here
296 Strm().WriteCharPtr( OUStringToOString(sLang, m_eDestEnc).getStr() )
297 .WriteCharPtr( "\">" );
300 const OUString& rModName = pModule->GetName();
301 Strm().WriteCharPtr( SAL_NEWLINE_STRING ); // nicht einruecken!
302 HTMLOutFuncs::OutScript( Strm(), GetBaseURL(), pModule->GetSource(),
303 sLang, eType, aEmptyOUStr,
304 &rLibName, &rModName,
305 m_eDestEnc, &m_aNonConvertableCharacters );
308 #endif
311 static const char* aEventNames[] =
313 "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
316 void SwHTMLWriter::OutBasicBodyEvents()
318 SwDocShell *pDocSh = pDoc->GetDocShell();
319 if( !pDocSh )
320 return;
322 SvxMacroTableDtor aDocTable;
324 uno::Reference< document::XEventsSupplier > xSup( pDocSh->GetModel(), uno::UNO_QUERY );
325 uno::Reference < container::XNameReplace > xEvents = xSup->getEvents();
326 for ( sal_Int32 i=0; i<4; i++ )
328 SvxMacro* pMacro = SfxEventConfiguration::ConvertToMacro( xEvents->getByName( OUString::createFromAscii(aEventNames[i]) ), pDocSh, true );
329 if ( pMacro )
331 aDocTable.Insert( aBodyEventTable[i].nEvent, *pMacro );
332 delete pMacro;
336 if( !aDocTable.empty() )
337 HTMLOutFuncs::Out_Events( Strm(), aDocTable, aBodyEventTable,
338 m_bCfgStarBasic, m_eDestEnc, &m_aNonConvertableCharacters );
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */