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 <hintids.hxx>
23 #include <comphelper/string.hxx>
24 #include <osl/diagnose.h>
25 #include <rtl/strbuf.hxx>
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 #include <com/sun/star/script/XLibraryContainer.hpp>
38 #include <com/sun/star/container/XNameContainer.hpp>
39 
40 #include <fmtornt.hxx>
41 #include <fmtfld.hxx>
42 
43 #include <doc.hxx>
45 #include <docsh.hxx>
46 #include <docufld.hxx>
47 #include "wrthtml.hxx"
48 #include "swhtml.hxx"
49 
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::container;
53 
54 static HTMLOutEvent const aBodyEventTable[] =
55 {
60  { nullptr, nullptr, SvMacroItemId::NONE }
61 };
62 
64 {
65  ParseScriptOptions( m_aScriptType, m_sBaseURL, m_eScriptLang, m_aScriptURL,
67 
68  if( !m_aScriptURL.isEmpty() )
69  {
70  // Ignore the script tag
71  m_bIgnoreRawData = true;
72  }
73 }
74 
76 {
77  bool bInsIntoBasic = false,
78  bInsSrcIntoField = false;
79 
80  switch( m_eScriptLang )
81  {
82  case HTMLScriptLanguage::StarBasic:
83  bInsIntoBasic = true;
84  break;
85  default:
86  bInsSrcIntoField = true;
87  break;
88  }
89 
90  m_bIgnoreRawData = false;
92 
93  // Except for StarBasic and unused JavaScript, save each script or module name in a field
94  if( bInsSrcIntoField && !m_bIgnoreHTMLComments )
95  {
96  SwScriptFieldType *pType =
97  static_cast<SwScriptFieldType*>(m_xDoc->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Script ));
98 
99  SwScriptField aField( pType, m_aScriptType,
101  !m_aScriptURL.isEmpty() );
102  InsertAttr( SwFormatField( aField ), false );
103  }
104 
105  SwDocShell *pDocSh = m_xDoc->GetDocShell();
106  if( !m_aScriptSource.isEmpty() && pDocSh &&
107  bInsIntoBasic && IsNewDoc() )
108  {
109  // Create a Basic module for javascript and StarBasic.
110 
111  // The Basic does still not remove SGML comments
112  RemoveSGMLComment( m_aScriptSource );
113 
114  // get library name
115  OUString aLibName;
116  if( !m_aBasicLib.isEmpty() )
117  aLibName = m_aBasicLib;
118  else
119  aLibName = "Standard";
120 
121  // get module library container
122  Reference< script::XLibraryContainer > xModLibContainer = pDocSh->GetBasicContainer();
123 
124  if ( xModLibContainer.is() )
125  {
127  if ( xModLibContainer->hasByName( aLibName ) )
128  {
129  // get module library
130  Any aElement = xModLibContainer->getByName( aLibName );
131  aElement >>= xModLib;
132  }
133  else
134  {
135  // create module library
136  xModLib = xModLibContainer->createLibrary( aLibName );
137  }
138 
139  if ( xModLib.is() )
140  {
141  if( m_aBasicModule.isEmpty() )
142  {
143  // create module name
144  bool bFound = true;
145  while( bFound )
146  {
147  m_aBasicModule = "Modul" + OUString::number( static_cast<sal_Int32>(++m_nSBModuleCnt) );
148  bFound = xModLib->hasByName( m_aBasicModule );
149  }
150  }
151 
152  // create module
153  OUString aModName( m_aBasicModule );
154  if ( !xModLib->hasByName( aModName ) )
155  {
156  Any aElement;
157  aElement <<= m_aScriptSource;
158  xModLib->insertByName( aModName , aElement );
159  }
160  }
161  }
162 
163  // get dialog library container
164  Reference< script::XLibraryContainer > xDlgLibContainer = pDocSh->GetDialogContainer();
165 
166  if ( xDlgLibContainer.is() )
167  {
168  if ( !xDlgLibContainer->hasByName( aLibName ) )
169  {
170  // create dialog library
171  xDlgLibContainer->createLibrary( aLibName );
172  }
173  }
174  }
175 
176  m_aScriptSource.clear();
177  m_aScriptType.clear();
178  m_aScriptURL.clear();
179 
180  m_aBasicLib.clear();
181  m_aBasicModule.clear();
182 }
183 
185 {
186  // We'll just remember a few strings here
187  if( aToken.getLength() > 2 &&
188  (HTMLScriptLanguage::StarBasic==m_eScriptLang && aToken[ 0 ] == '\'') )
189  {
190  sal_Int32 nPos = -1;
191  if( m_aBasicLib.isEmpty() )
192  {
193  nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_library );
194  if( nPos != -1 )
195  {
196  m_aBasicLib =
197  aToken.copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_library) - 1 );
199  }
200  }
201 
202  if( m_aBasicModule.isEmpty() && nPos == -1 )
203  {
204  nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_module );
205  if( nPos != -1 )
206  {
208  aToken.copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_module) - 1 );
210  }
211  }
212 
213  if( nPos == -1 )
214  {
215  if( !m_aScriptSource.isEmpty() )
216  m_aScriptSource += "\n";
217  m_aScriptSource += aToken;
218  }
219  }
220  else if( !m_aScriptSource.isEmpty() || !aToken.isEmpty() )
221  {
222  // Empty lines are ignored on the beginning
223  if( !m_aScriptSource.isEmpty() )
224  {
225  m_aScriptSource += "\n";
226  }
227  m_aScriptSource += aToken;
228  }
229 }
230 
231 void SwHTMLParser::InsertBasicDocEvent( const OUString& aEvent, const OUString& rName,
232  ScriptType eScrType,
233  const OUString& rScrType )
234 {
235  OSL_ENSURE( !rName.isEmpty(), "InsertBasicDocEvent() called without macro" );
236  if( rName.isEmpty() )
237  return;
238 
239  SwDocShell *pDocSh = m_xDoc->GetDocShell();
240  OSL_ENSURE( pDocSh, "Where is the DocShell?" );
241  if( !pDocSh )
242  return;
243 
244  OUString sEvent(convertLineEnd(rName, GetSystemLineEnd()));
245  OUString sScriptType;
246  if( EXTENDED_STYPE == eScrType )
247  sScriptType = rScrType;
248 
249  SfxEventConfiguration::ConfigureEvent( aEvent, SvxMacro( sEvent, sScriptType, eScrType ),
250  pDocSh );
251 }
252 
254 {
255 #if HAVE_FEATURE_SCRIPTING
256  if( !m_bCfgStarBasic )
257  return;
258 
259  BasicManager *pBasicMan = m_pDoc->GetDocShell()->GetBasicManager();
260  OSL_ENSURE( pBasicMan, "Where is the Basic-Manager?" );
261  // Only write DocumentBasic
262  if( !pBasicMan || pBasicMan == SfxApplication::GetBasicManager() )
263  {
264  return;
265  }
266 
267  bool bFirst=true;
268  // Now write all StarBasic and unused Javascript modules
269  for( sal_uInt16 i=0; i<pBasicMan->GetLibCount(); i++ )
270  {
271  StarBASIC *pBasic = pBasicMan->GetLib( i );
272  const OUString& rLibName = pBasic->GetName();
273  for( const auto& pModule: pBasic->GetModules() )
274  {
275  OUString sLang(SVX_MACRO_LANGUAGE_STARBASIC);
276 
277  if( bFirst )
278  {
279  bFirst = false;
280  OutNewLine();
281  OString sOut =
284  "=\""
287  "=\"text/x-";
288  Strm().WriteOString( sOut );
289  // Entities aren't welcome here
291  .WriteCharPtr( "\">" );
292  }
293 
294  const OUString& rModName = pModule->GetName();
295  Strm().WriteCharPtr( SAL_NEWLINE_STRING ); // don't indent!
296  HTMLOutFuncs::OutScript( Strm(), GetBaseURL(), pModule->GetSource(),
297  sLang, STARBASIC, OUString(),
298  &rLibName, &rModName,
300  }
301  }
302 #endif
303 }
304 
305 static const char* aEventNames[] =
306 {
307  "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
308 };
309 
311 {
312  SwDocShell *pDocSh = m_pDoc->GetDocShell();
313  if( !pDocSh )
314  return;
315 
316  SvxMacroTableDtor aDocTable;
317 
318  uno::Reference< document::XEventsSupplier > xSup( pDocSh->GetModel(), uno::UNO_QUERY );
319  uno::Reference < container::XNameReplace > xEvents = xSup->getEvents();
320  for ( sal_Int32 i=0; i<4; i++ )
321  {
322  std::unique_ptr<SvxMacro> pMacro = SfxEventConfiguration::ConvertToMacro( xEvents->getByName( OUString::createFromAscii(aEventNames[i]) ), pDocSh );
323  if ( pMacro )
324  {
325  aDocTable.Insert( aBodyEventTable[i].nEvent, *pMacro );
326  }
327  }
328 
329  if( !aDocTable.empty() )
332 }
333 
334 /* 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:54
SvxMacro & Insert(SvMacroItemId nEvent, const SvxMacro &rMacro)
const OUString & GetBaseURL() const
Definition: shellio.hxx:437
TOOLS_DLLPUBLIC OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
void OutBasicBodyEvents()
Definition: htmlbas.cxx:310
#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:1342
void EndScript()
Definition: htmlbas.cxx:75
#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:184
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:231
void OutNewLine(bool bCheck=false)
Definition: wrthtml.cxx:1418
static const char * aEventNames[]
Definition: htmlbas.cxx:305
LineEnd GetSystemLineEnd()
OString strip(const OString &rIn, sal_Char c)
void InsertAttr(const SfxPoolItem &rItem, bool bInsAtStart)
Definition: swhtml.cxx:3408
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
void OutBasic()
Definition: htmlbas.cxx:253
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
#define OOO_STRING_SVTOOLS_HTML_O_content
OUString m_aBasicLib
Definition: swhtml.hxx:341
void NewScript()
Definition: htmlbas.cxx:63
SvStream & Strm()
Definition: writer.cxx:218
#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
sal_Int32 nPos
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:401
css::uno::Reference< css::script::XLibraryContainer > GetBasicContainer()
StarBASIC * GetLib(sal_uInt16 nLib) const