LibreOffice Module sw (master)  1
htmlbas.cxx
Go to the documentation of this file.
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 <comphelper/string.hxx>
23 #include <osl/diagnose.h>
24 #include <basic/basmgr.hxx>
25 #include <basic/sbmod.hxx>
26 #include <sfx2/evntconf.hxx>
27 #include <sfx2/app.hxx>
28 #include <svtools/htmlout.hxx>
29 #include <svtools/htmlkywd.hxx>
30 
31 #include <com/sun/star/document/XEventsSupplier.hpp>
32 #include <com/sun/star/uno/Reference.hxx>
33 #include <com/sun/star/script/XLibraryContainer.hpp>
34 #include <com/sun/star/container/XNameContainer.hpp>
35 
36 #include <fmtfld.hxx>
37 
38 #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 const aBodyEventTable[] =
50 {
55  { nullptr, nullptr, SvMacroItemId::NONE }
56 };
57 
59 {
60  ParseScriptOptions( m_aScriptType, m_sBaseURL, m_eScriptLang, m_aScriptURL,
62 
63  if( !m_aScriptURL.isEmpty() )
64  {
65  // Ignore the script tag
66  m_bIgnoreRawData = true;
67  }
68 }
69 
71 {
72  bool bInsIntoBasic = false,
73  bInsSrcIntoField = false;
74 
75  switch( m_eScriptLang )
76  {
77  case HTMLScriptLanguage::StarBasic:
78  bInsIntoBasic = true;
79  break;
80  default:
81  bInsSrcIntoField = true;
82  break;
83  }
84 
85  m_bIgnoreRawData = false;
87 
88  // Except for StarBasic and unused JavaScript, save each script or module name in a field
89  if( bInsSrcIntoField && !m_bIgnoreHTMLComments )
90  {
91  SwScriptFieldType *pType =
92  static_cast<SwScriptFieldType*>(m_xDoc->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Script ));
93 
94  SwScriptField aField( pType, m_aScriptType,
96  !m_aScriptURL.isEmpty() );
97  InsertAttr( SwFormatField( aField ), false );
98  }
99 
100  SwDocShell *pDocSh = m_xDoc->GetDocShell();
101  if( !m_aScriptSource.isEmpty() && pDocSh &&
102  bInsIntoBasic && IsNewDoc() )
103  {
104  // Create a Basic module for javascript and StarBasic.
105 
106  // The Basic does still not remove SGML comments
107  RemoveSGMLComment( m_aScriptSource );
108 
109  // get library name
110  OUString aLibName;
111  if( !m_aBasicLib.isEmpty() )
112  aLibName = m_aBasicLib;
113  else
114  aLibName = "Standard";
115 
116  // get module library container
117  Reference< script::XLibraryContainer > xModLibContainer = pDocSh->GetBasicContainer();
118 
119  if ( xModLibContainer.is() )
120  {
122  if ( xModLibContainer->hasByName( aLibName ) )
123  {
124  // get module library
125  Any aElement = xModLibContainer->getByName( aLibName );
126  aElement >>= xModLib;
127  }
128  else
129  {
130  // create module library
131  xModLib = xModLibContainer->createLibrary( aLibName );
132  }
133 
134  if ( xModLib.is() )
135  {
136  if( m_aBasicModule.isEmpty() )
137  {
138  // create module name
139  bool bFound = true;
140  while( bFound )
141  {
142  m_aBasicModule = "Modul" + OUString::number( static_cast<sal_Int32>(++m_nSBModuleCnt) );
143  bFound = xModLib->hasByName( m_aBasicModule );
144  }
145  }
146 
147  // create module
148  OUString aModName( m_aBasicModule );
149  if ( !xModLib->hasByName( aModName ) )
150  {
151  Any aElement;
152  aElement <<= m_aScriptSource;
153  xModLib->insertByName( aModName , aElement );
154  }
155  }
156  }
157 
158  // get dialog library container
159  Reference< script::XLibraryContainer > xDlgLibContainer = pDocSh->GetDialogContainer();
160 
161  if ( xDlgLibContainer.is() )
162  {
163  if ( !xDlgLibContainer->hasByName( aLibName ) )
164  {
165  // create dialog library
166  xDlgLibContainer->createLibrary( aLibName );
167  }
168  }
169  }
170 
171  m_aScriptSource.clear();
172  m_aScriptType.clear();
173  m_aScriptURL.clear();
174 
175  m_aBasicLib.clear();
176  m_aBasicModule.clear();
177 }
178 
180 {
181  // We'll just remember a few strings here
182  if( aToken.getLength() > 2 &&
183  (HTMLScriptLanguage::StarBasic==m_eScriptLang && aToken[ 0 ] == '\'') )
184  {
185  sal_Int32 nPos = -1;
186  if( m_aBasicLib.isEmpty() )
187  {
188  nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_library );
189  if( nPos != -1 )
190  {
191  m_aBasicLib =
192  aToken.copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_library) - 1 );
194  }
195  }
196 
197  if( m_aBasicModule.isEmpty() && nPos == -1 )
198  {
199  nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_module );
200  if( nPos != -1 )
201  {
203  aToken.copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_module) - 1 );
205  }
206  }
207 
208  if( nPos == -1 )
209  {
210  if( !m_aScriptSource.isEmpty() )
211  m_aScriptSource += "\n";
212  m_aScriptSource += aToken;
213  }
214  }
215  else if( !m_aScriptSource.isEmpty() || !aToken.isEmpty() )
216  {
217  // Empty lines are ignored on the beginning
218  if( !m_aScriptSource.isEmpty() )
219  {
220  m_aScriptSource += "\n";
221  }
222  m_aScriptSource += aToken;
223  }
224 }
225 
226 void SwHTMLParser::InsertBasicDocEvent( const OUString& aEvent, const OUString& rName,
227  ScriptType eScrType,
228  const OUString& rScrType )
229 {
230  OSL_ENSURE( !rName.isEmpty(), "InsertBasicDocEvent() called without macro" );
231  if( rName.isEmpty() )
232  return;
233 
234  SwDocShell *pDocSh = m_xDoc->GetDocShell();
235  OSL_ENSURE( pDocSh, "Where is the DocShell?" );
236  if( !pDocSh )
237  return;
238 
239  OUString sEvent(convertLineEnd(rName, GetSystemLineEnd()));
240  OUString sScriptType;
241  if( EXTENDED_STYPE == eScrType )
242  sScriptType = rScrType;
243 
244  SfxEventConfiguration::ConfigureEvent( aEvent, SvxMacro( sEvent, sScriptType, eScrType ),
245  pDocSh );
246 }
247 
249 {
250 #if !HAVE_FEATURE_SCRIPTING
251  (void) rHTMLWrt;
252 #else
253  if( !m_bCfgStarBasic )
254  return;
255 
256  BasicManager *pBasicMan = m_pDoc->GetDocShell()->GetBasicManager();
257  OSL_ENSURE( pBasicMan, "Where is the Basic-Manager?" );
258  // Only write DocumentBasic
259  if( !pBasicMan || pBasicMan == SfxApplication::GetBasicManager() )
260  {
261  return;
262  }
263 
264  bool bFirst=true;
265  // Now write all StarBasic and unused Javascript modules
266  for( sal_uInt16 i=0; i<pBasicMan->GetLibCount(); i++ )
267  {
268  StarBASIC *pBasic = pBasicMan->GetLib( i );
269  const OUString& rLibName = pBasic->GetName();
270  for( const auto& pModule: pBasic->GetModules() )
271  {
272  OUString sLang(SVX_MACRO_LANGUAGE_STARBASIC);
273 
274  if( bFirst )
275  {
276  bFirst = false;
277  OutNewLine();
278  OString sOut =
279  "<" + rHTMLWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_meta
281  "=\""
284  "=\"text/x-";
285  Strm().WriteOString( sOut );
286  // Entities aren't welcome here
288  .WriteCharPtr( "\">" );
289  }
290 
291  const OUString& rModName = pModule->GetName();
292  Strm().WriteCharPtr( SAL_NEWLINE_STRING ); // don't indent!
293  HTMLOutFuncs::OutScript( Strm(), GetBaseURL(), pModule->GetSource32(),
294  sLang, STARBASIC, OUString(),
295  &rLibName, &rModName,
297  }
298  }
299 #endif
300 }
301 
302 static const char* aEventNames[] =
303 {
304  "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
305 };
306 
308 {
309  SwDocShell *pDocSh = m_pDoc->GetDocShell();
310  if( !pDocSh )
311  return;
312 
313  SvxMacroTableDtor aDocTable;
314 
315  uno::Reference< document::XEventsSupplier > xSup( pDocSh->GetModel(), uno::UNO_QUERY );
316  uno::Reference < container::XNameReplace > xEvents = xSup->getEvents();
317  for ( sal_Int32 i=0; i<4; i++ )
318  {
319  std::unique_ptr<SvxMacro> pMacro = SfxEventConfiguration::ConvertToMacro( xEvents->getByName( OUString::createFromAscii(aEventNames[i]) ), pDocSh );
320  if ( pMacro )
321  {
322  aDocTable.Insert( aBodyEventTable[i].nEvent, *pMacro );
323  }
324  }
325 
326  if( !aDocTable.empty() )
329 }
330 
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define OOO_STRING_SVTOOLS_HTML_O_onload
EXTENDED_STYPE
static std::unique_ptr< SvxMacro > ConvertToMacro(const css::uno::Any &rElement, SfxObjectShell *pDoc)
static HTMLOutEvent const aBodyEventTable[]
Definition: htmlbas.cxx:49
SvxMacro & Insert(SvMacroItemId nEvent, const SvxMacro &rMacro)
const OUString & GetBaseURL() const
Definition: shellio.hxx:445
TOOLS_DLLPUBLIC OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
void OutBasicBodyEvents()
Definition: htmlbas.cxx:307
#define OOO_STRING_SVTOOLS_HTML_O_onblur
const OUString & GetName(SbxNameType=SbxNameType::NONE) const
bool m_bIgnoreRawData
Definition: swhtml.hxx:428
SwDocShell * GetDocShell()
Definition: doc.hxx:1348
void EndScript()
Definition: htmlbas.cxx:70
#define OOO_STRING_SVTOOLS_HTML_META_content_script_type
static SVT_DLLPUBLIC SvStream & Out_Events(SvStream &, const SvxMacroTableDtor &, const HTMLOutEvent *, bool bOutStarBasic, rtl_TextEncoding eDestEnc, OUString *pNonConvertableChars)
#define OOO_STRING_SVTOOLS_HTML_meta
sal_uInt16 GetLibCount() const
void AddScriptSource()
Definition: htmlbas.cxx:179
static void ConfigureEvent(const OUString &aName, const SvxMacro &, SfxObjectShell const *pObjSh)
OUString m_aNonConvertableCharacters
Definition: wrthtml.hxx:293
SvStream & WriteOString(const OString &rStr)
OUString m_sBaseURL
Definition: swhtml.hxx:340
css::uno::Reference< css::frame::XModel > GetModel() const
OUString m_aScriptURL
Definition: swhtml.hxx:345
SvStream & WriteCharPtr(const char *pBuf)
void InsertBasicDocEvent(const OUString &aEventName, const OUString &rName, ScriptType eScrType, const OUString &rScrType)
Definition: htmlbas.cxx:226
void OutNewLine(bool bCheck=false)
Definition: wrthtml.cxx:1426
static const char * aEventNames[]
Definition: htmlbas.cxx:302
LineEnd GetSystemLineEnd()
void InsertAttr(const SfxPoolItem &rItem, bool bInsAtStart)
Definition: swhtml.cxx:3424
void OutBasic(SwHTMLWriter &rHTMLWrt)
Definition: htmlbas.cxx:248
bool empty() const
css::uno::Reference< css::script::XLibraryContainer > GetDialogContainer()
bool m_bCfgStarBasic
Definition: wrthtml.hxx:342
rtl_TextEncoding m_eDestEnc
Definition: wrthtml.hxx:334
SvMacroItemId const nEvent
#define OOO_STRING_SVTOOLS_HTML_O_SDonunload
int i
OUString m_aBasicModule
Definition: swhtml.hxx:342
#define OOO_STRING_SVTOOLS_HTML_O_SDonblur
#define OOO_STRING_SVTOOLS_HTML_SB_module
HTMLScriptLanguage m_eScriptLang
Definition: swhtml.hxx:414
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
bool m_bIgnoreHTMLComments
Definition: swhtml.hxx:449
OUString m_aScriptType
Definition: swhtml.hxx:344
SbModules & GetModules()
static BasicManager * GetBasicManager()
static SVT_DLLPUBLIC SvStream & OutScript(SvStream &rStrm, const OUString &rBaseURL, const OUString &rSource, const OUString &rLanguage, ScriptType eScriptType, const OUString &rSrc, const OUString *pSBLibrary, const OUString *pSBModule, rtl_TextEncoding eDestEnc=RTL_TEXTENCODING_MS_1252, OUString *pNonConvertableChars=nullptr)
#define OOO_STRING_SVTOOLS_HTML_O_SDonfocus
#define OOO_STRING_SVTOOLS_HTML_SB_library
#define OOO_STRING_SVTOOLS_HTML_O_onunload
OUString m_aScriptSource
Definition: swhtml.hxx:343
OString strip(const OString &rIn, char c)
OString GetNamespace() const
Determines the prefix string needed to respect the requested namespace alias.
Definition: wrthtml.cxx:1475
#define OOO_STRING_SVTOOLS_HTML_O_content
OUString m_aBasicLib
Definition: swhtml.hxx:341
void NewScript()
Definition: htmlbas.cxx:58
SvStream & Strm()
Definition: writer.cxx:215
#define OOO_STRING_SVTOOLS_HTML_O_SDonload
sal_uInt16 m_nSBModuleCnt
Definition: swhtml.hxx:398
#define SVX_MACRO_LANGUAGE_STARBASIC
ScriptType
#define OOO_STRING_SVTOOLS_HTML_O_onfocus
rtl::Reference< SwDoc > m_xDoc
Definition: swhtml.hxx:375
STARBASIC
#define OOO_STRING_SVTOOLS_HTML_O_httpequiv
#define SAL_NEWLINE_STRING
BasicManager * GetBasicManager() const
SwDoc * m_pDoc
Definition: shellio.hxx:409
sal_uInt16 nPos
css::uno::Reference< css::script::XLibraryContainer > GetBasicContainer()
StarBASIC * GetLib(sal_uInt16 nLib) const
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo