merged tag LIBREOFFICE_3_2_99_3
[LibreOffice.git] / scripting / source / storage / ScriptStorage.cxx
blob9575321858665a971fbfa33f8b4e73c465378a3d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_scripting.hxx"
31 #include <osl/file.hxx>
32 #include <osl/time.h>
33 #include <cppuhelper/implementationentry.hxx>
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <com/sun/star/ucb/CommandAbortedException.hpp>
36 #include <com/sun/star/io/XActiveDataSource.hpp>
37 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
38 #include <com/sun/star/container/XNameAccess.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
42 #include <util/util.hxx>
43 #include <rtl/uri.hxx>
46 #include "ScriptData.hxx"
47 #include "ScriptInfo.hxx"
48 #include "ScriptStorage.hxx"
49 #include "ScriptElement.hxx"
50 #include "ScriptMetadataImporter.hxx"
51 #include "ScriptURI.hxx"
53 using namespace ::rtl;
54 using namespace ::cppu;
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::uno;
57 using namespace ::drafts::com::sun::star::script::framework;
59 namespace scripting_impl
62 ScriptLanguages_hash* ScriptStorage::mh_scriptLangs = NULL;
64 const sal_Char* const SERVICE_NAME =
65 "drafts.com.sun.star.script.framework.storage.ScriptStorage";
66 const sal_Char* const IMPL_NAME =
67 "drafts.com.sun.star.script.framework.storage.ScriptStorage";
69 const sal_Char * const SCRIPT_DIR = "/Scripts";
70 const sal_Char * const SCRIPT_PARCEL = "/parcel-descriptor.xml";
71 const sal_Char * const SCRIPT_PARCEL_NAME_ONLY = "parcel-descriptor";
73 static OUString ss_implName = OUString::createFromAscii( IMPL_NAME );
74 static OUString ss_serviceName = OUString::createFromAscii( SERVICE_NAME );
75 static Sequence< OUString > ss_serviceNames =
76 Sequence< OUString >( &ss_serviceName, 1 );
78 const sal_uInt16 NUMBER_STORAGE_INITIALIZE_ARGS = 3;
80 //extern ::rtl_StandardModuleCount s_moduleCount;
84 //*************************************************************************
85 ScriptStorage::ScriptStorage( const Reference <
86 XComponentContext > & xContext )
87 throw ( RuntimeException )
88 : m_xContext( xContext ), m_bInitialised( false )
90 OSL_TRACE( "< ScriptStorage ctor called >\n" );
92 validateXRef( m_xContext,
93 "ScriptStorage::ScriptStorage : cannot get component context" );
95 m_xMgr = m_xContext->getServiceManager();
96 validateXRef( m_xMgr,
97 "ScriptStorage::ScriptStorage : cannot get service manager" );
99 if( !mh_scriptLangs )
101 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
102 if( !mh_scriptLangs )
104 mh_scriptLangs = new ScriptLanguages_hash();
105 Reference< XInterface > xInterface =
106 m_xMgr->createInstanceWithContext(
107 OUString::createFromAscii(
108 "com.sun.star.configuration.ConfigurationProvider" )
109 , m_xContext );
110 validateXRef( xInterface,
111 "ScriptStorage::ScriptStorage: cannot get ConfigurationProvider" );
112 // create an instance of the ConfigurationAccess for accessing the
113 // scripting runtime settings
114 Reference< lang::XMultiServiceFactory > xConfigProvFactory =
115 Reference < lang::XMultiServiceFactory >
116 ( xInterface, UNO_QUERY_THROW );
117 validateXRef( xConfigProvFactory,
118 "ScriptStorage::ScriptStorage: cannot get XMultiServiceFactory interface from ConfigurationProvider" );
119 beans::PropertyValue configPath;
120 configPath.Name = ::rtl::OUString::createFromAscii( "nodepath" );
121 configPath.Value <<= ::rtl::OUString::createFromAscii( "org.openoffice.Office.Scripting/ScriptRuntimes" );
122 Sequence < Any > aargs( 1 );
123 aargs[ 0 ] <<= configPath;
125 xInterface = xConfigProvFactory->createInstanceWithArguments(
126 OUString::createFromAscii(
127 "com.sun.star.configuration.ConfigurationAccess"),
128 aargs );
129 validateXRef( xInterface,
130 "ScriptStorage::ScriptStorage: cannot get ConfigurationAccess" );
131 Reference< container::XNameAccess > xNameAccess =
132 Reference < container::XNameAccess > ( xInterface,
133 UNO_QUERY_THROW );
134 validateXRef( xNameAccess,
135 "ScriptStorage::ScriptStorage: cannot get ConfigurationAccess" );
136 Sequence< OUString > names = xNameAccess->getElementNames();
137 for( int i = 0 ; i < names.getLength() ; i++ )
139 OSL_TRACE( "Getting propertyset for Lang=%s",
140 ::rtl::OUStringToOString( names[i], RTL_TEXTENCODING_ASCII_US ).pData->buffer );
141 Reference< beans::XPropertySet > xPropSet =
142 Reference< beans::XPropertySet >( xNameAccess->getByName(names[i]),
143 UNO_QUERY_THROW );
144 validateXRef( xPropSet,
145 "ScriptStorage::ScriptStorage: cannot get XPropertySet for name" );
146 Any aProp = xPropSet->getPropertyValue(
147 OUString::createFromAscii( "SupportedFileExtensions") );
148 Sequence< OUString > extns;
149 if( sal_False == ( aProp >>= extns ) )
151 throw RuntimeException(
152 OUSTR( "ScriptStorage:ScriptStorage: can't get runtime extensions" ),
153 Reference< XInterface > () );
155 for( int j = 0 ; j < extns.getLength() ; j++ )
157 OSL_TRACE( "Adding Lang=%s, Extn=%s\n",
158 ::rtl::OUStringToOString( names[i], RTL_TEXTENCODING_ASCII_US ).pData->buffer,
159 ::rtl::OUStringToOString( extns[j], RTL_TEXTENCODING_ASCII_US ).pData->buffer );
160 (*mh_scriptLangs)[ extns[j] ] =
161 names[i];
166 // s_moduleCount.modCnt.acquire( &s_moduleCount.modCnt );
169 //*************************************************************************
170 ScriptStorage::~ScriptStorage() SAL_THROW( () )
172 OSL_TRACE( "< ScriptStorage dtor called >\n" );
173 // s_moduleCount.modCnt.release( &s_moduleCount.modCnt );
176 //*************************************************************************
177 void
178 ScriptStorage::initialize( const Sequence <Any> & args )
179 throw ( RuntimeException, Exception )
181 OSL_TRACE( "Entering ScriptStorage::initialize\n" );
183 // Should not be renitialised
184 if ( m_bInitialised )
186 throw RuntimeException(
187 OUSTR( "ScriptStorage::initalize already initialized" ),
188 Reference<XInterface> () );
191 { // Protect member variable writes
192 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
194 // Check args
195 if ( args.getLength() != NUMBER_STORAGE_INITIALIZE_ARGS )
197 OSL_TRACE( "ScriptStorage::initialize: got wrong number of args\n" );
198 throw RuntimeException(
199 OUSTR( "Invalid number of arguments provided!" ),
200 Reference< XInterface >() );
203 if ( sal_False == ( args[ 0 ] >>= m_xSimpleFileAccess ) )
205 throw RuntimeException(
206 OUSTR( "Invalid XSimpleFileAccess argument provided!" ),
207 Reference< XInterface >() );
210 if ( sal_False == ( args[ 1 ] >>= m_scriptStorageID ) )
212 throw RuntimeException(
213 OUSTR( "Invalid ScriptStorage ID argument provided!" ),
214 Reference< XInterface >() );
217 if ( sal_False == ( args[ 2 ] >>= m_stringUri ) )
219 throw RuntimeException(
220 OUSTR( "Invalid String Uri argument provided!" ),
221 Reference< XInterface >() );
223 } // End - Protect member variable writes
225 OSL_TRACE( "uri: %s\n", ::rtl::OUStringToOString(
226 m_stringUri, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
230 // need to check for what???
231 // what we have is a URI for the filesystem or document
232 // we need to check of the last element in the path has an
233 // extension that is associated with a script (eg. .bsh, .js etc)
234 OUString fileExtension = getFileExtension( m_stringUri );
235 // and see if this is in our scripts map
236 ScriptLanguages_hash::iterator h_it = mh_scriptLangs->find( fileExtension );
237 if ( h_it != mh_scriptLangs->end() )
239 createForFilesystem( fileExtension );
241 else
243 create();
246 catch ( RuntimeException & re )
248 OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::initialize" );
249 throw RuntimeException(
250 OUSTR( "ScriptStorage::initalize RuntimeException: " ).concat( re.Message ),
251 Reference< XInterface > () );
253 catch ( Exception & ue )
255 OSL_TRACE( "caught com::sun::star::uno::Exception in ScriptStorage::initialize" );
256 throw RuntimeException(
257 OUSTR( "ScriptStorage::initalize Exception: " ).concat( ue.Message ),
258 Reference< XInterface > () );
260 #ifdef _DEBUG
261 catch ( ... )
263 OSL_TRACE( "caught unknown Exception in ScriptStorage::initialize" );
264 throw RuntimeException(
265 OUSTR( "ScriptStorage::initalize unknown exception: " ),
266 Reference< XInterface > () );
268 #endif
270 OSL_TRACE( "Parsed the XML\n" );
272 m_bInitialised = true;
275 void
276 ScriptStorage::create()
277 throw ( RuntimeException, Exception )
279 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
282 // clear existing hashmap - rebuilding from scratch to avoid having
283 // to search for deleted elements on refresh
284 mh_implementations.clear();
286 OUString xStringUri(m_stringUri);
288 ScriptMetadataImporter* SMI = new ScriptMetadataImporter( m_xContext );
289 Reference< xml::sax::XExtendedDocumentHandler > xSMI( SMI );
291 validateXRef( xSMI, "ScriptStorage::create: failed to obtain valid XExtendedDocumentHandler" );
293 xStringUri = xStringUri.concat( ::rtl::OUString::createFromAscii(
294 SCRIPT_DIR ) );
296 // No Scripts directory - just return
297 if ( ! m_xSimpleFileAccess->isFolder( xStringUri ) )
299 OSL_TRACE( "ScriptStorage::initialize: no Scripts dir for this storage - install problem\n" );
300 return;
303 // get the list of language folders under the Scripts directory
304 Sequence< ::rtl::OUString > languageDirs =
305 m_xSimpleFileAccess->getFolderContents( xStringUri, true );
307 Reference< io::XInputStream > xInput;
308 sal_Int32 languageDirsLength = languageDirs.getLength();
309 for ( sal_Int32 i = 0; i < languageDirsLength ; ++i )
311 OSL_TRACE( "contains: %s\n", ::rtl::OUStringToOString(
312 languageDirs[ i ], RTL_TEXTENCODING_ASCII_US ).pData->buffer );
314 if ( ! m_xSimpleFileAccess->isFolder( languageDirs[ i ] ) )
316 continue;
319 //get the list of parcel folders for each language folder
320 // under Scripts
321 Sequence< ::rtl::OUString > parcelDirs =
322 m_xSimpleFileAccess->getFolderContents( languageDirs[ i ], true );
324 sal_Int32 parcelDirsLength = parcelDirs.getLength();
325 for ( sal_Int32 j = 0; j < parcelDirsLength ; ++j )
327 OSL_TRACE( "contains: %s\n",
328 ::rtl::OUStringToOString( parcelDirs[ j ],
329 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
331 OUString parcelFile = parcelDirs[ j ].concat(
332 ::rtl::OUString::createFromAscii( SCRIPT_PARCEL ) );
334 // Do not have a valid parcel.xml
335 if ( !m_xSimpleFileAccess->exists( parcelFile ) ||
336 m_xSimpleFileAccess->isFolder( parcelFile ) )
338 continue;
340 OSL_TRACE( "parcel file: %s\n",
341 ::rtl::OUStringToOString( parcelFile,
342 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
344 xInput = m_xSimpleFileAccess->openFileRead( parcelFile );
345 // Failed to get input stream
346 if ( !xInput.is() )
348 continue;
351 OSL_TRACE( "Parse the metadata \n" );
352 Datas_vec vScriptDatas;
355 SMI->parseMetaData( xInput, parcelDirs[ j ], vScriptDatas );
357 catch ( xml::sax::SAXException & saxe )
359 if ( xInput.is() )
361 xInput->closeInput();
363 OSL_TRACE(
364 "caught com::sun::star::xml::sax::SAXException in ScriptStorage::create %s",
365 ::rtl::OUStringToOString( saxe.Message,
366 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
368 continue;
370 catch ( io::IOException & ioe )
372 if ( xInput.is() )
374 xInput->closeInput();
376 OSL_TRACE(
377 "caught com::sun::star::io::IOException in ScriptStorage::create" );
378 continue;
380 xInput->closeInput();
382 updateMaps( vScriptDatas );
386 catch ( io::IOException & ioe )
388 //From ScriptMetadata Importer
389 OSL_TRACE( "caught com::sun::star::io::IOException in ScriptStorage::create" );
390 throw RuntimeException(
391 OUSTR( "ScriptStorage::create IOException: " ).concat( ioe.Message ),
392 Reference< XInterface > () );
395 catch ( ucb::CommandAbortedException & cae )
397 OSL_TRACE( "caught com::sun::star::ucb::CommandAbortedException in ScriptStorage::create" );
398 throw RuntimeException(
399 OUSTR(
400 "ScriptStorage::create CommandAbortedException: " ).concat( cae.Message ),
401 Reference< XInterface > () );
403 catch ( RuntimeException & re )
405 OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::create" );
406 throw RuntimeException(
407 OUSTR( "ScriptStorage::create RuntimeException: " ).concat( re.Message ),
408 Reference< XInterface > () );
410 catch ( Exception & ue )
412 OSL_TRACE( "caught com::sun::star::uno::Exception in ScriptStorage::create" );
413 throw RuntimeException(
414 OUSTR( "ScriptStorage::create Exception: " ).concat( ue.Message ),
415 Reference< XInterface > () );
417 #ifdef _DEBUG
418 catch ( ... )
420 OSL_TRACE( "caught unknown Exception in ScriptStorage::create" );
421 throw RuntimeException(
422 OUSTR( "ScriptStorage::initalize unknown exception: " ),
423 Reference< XInterface > () );
425 #endif
427 OSL_TRACE( "Parsed the XML\n" );
429 m_bInitialised = true;
432 //*************************************************************************
433 // private method to create the usual data structures for scripts located
434 // on the filesystem.
435 // parcelURI = the path to the script
436 // functionName = the full filename with extension
437 // logicalName = the filename without the extension
438 void
439 ScriptStorage::createForFilesystem( const OUString & fileExtension )
440 throw ( RuntimeException, Exception )
442 // need to decode as file urls are encoded
443 OUString xStringUri = ::rtl::Uri::decode( m_stringUri,
444 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ASCII_US );
446 // no x-platform issues here as we are dealing with URLs
447 sal_Int32 lastFileSep = xStringUri.lastIndexOf( '/' );
448 // the char just after the filesep
449 lastFileSep += 1;
450 sal_Int32 lastFileExt = xStringUri.lastIndexOf( fileExtension );
451 OUString searchString = OUString::createFromAscii( "://" );
452 sal_Int32 searchStringLength = searchString.getLength();
453 sal_Int32 startPath = xStringUri.indexOf( searchString );
454 sal_Int32 uriLength = xStringUri.getLength();
455 OUString fileNameNoExt = xStringUri.copy( lastFileSep ,
456 lastFileExt - lastFileSep - 1 );
457 OUString fileName = xStringUri.copy( lastFileSep, uriLength - lastFileSep );
458 OUString filePath = xStringUri.copy( startPath + searchStringLength,
459 lastFileSep - startPath - searchStringLength );
460 OUString filePathWithName = xStringUri.copy( startPath + searchStringLength,
461 uriLength - startPath - searchStringLength );
463 ScriptData scriptData;
464 scriptData.language = mh_scriptLangs->find( fileExtension )->second;
465 OSL_TRACE( "\t language = %s", ::rtl::OUStringToOString(
466 scriptData.language, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
468 // do we need to encode this?
469 scriptData.functionname = fileName;
470 OSL_TRACE( "\t functionName = %s", ::rtl::OUStringToOString(
471 scriptData.functionname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
472 //scriptData.functionname = ::rtl::Uri::encode( fileName,
473 //rtl_UriCharClassUricNoSlash, rtl_UriEncodeCheckEscapes,
474 //RTL_TEXTENCODING_ASCII_US );
476 scriptData.parcelURI = filePath;
477 OSL_TRACE( "\t parcelURI = %s", ::rtl::OUStringToOString(
478 scriptData.parcelURI, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
479 scriptData.logicalname = fileNameNoExt;
480 OSL_TRACE( "\t logicalName = %s", ::rtl::OUStringToOString(
481 scriptData.logicalname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
483 // and now push onto the usual structures
484 ScriptFunction_hash sfh;
485 sfh[ scriptData.functionname ] = scriptData;
486 mh_implementations[ scriptData.language ] = sfh;
487 m_bInitialised = true;
490 //*************************************************************************
491 // private method to return the file extension, eg. bsh, js etc
492 OUString
493 ScriptStorage::getFileExtension( const OUString & stringUri )
495 OUString fileExtension;
496 sal_Int32 lastDot = stringUri.lastIndexOf( '.' );
497 if( lastDot > 0 ) {
498 sal_Int32 stringUriLength = stringUri.getLength();
499 fileExtension = stringUri.copy( lastDot +1 , stringUriLength - lastDot - 1 );
501 else
503 fileExtension = OUString::createFromAscii("");
505 return fileExtension;
508 //*************************************************************************
509 // private method for updating hashmaps
510 void
511 ScriptStorage::updateMaps( const Datas_vec & vScriptDatas )
514 Datas_vec::const_iterator it_end = vScriptDatas.end();
515 // step through the vector of ScripImplInfos returned from parse
516 for ( Datas_vec::const_iterator it = vScriptDatas.begin() ; it != it_end; ++it )
518 //find the Datas_vec for this logical name
519 ScriptData_hash::iterator h_it = mh_implementations.find( it->language );
521 if ( h_it == mh_implementations.end() )
523 //if it's null, need to create a new Datas_vec
524 OSL_TRACE(
525 "updateMaps: new language: %s\n", rtl::OUStringToOString(
526 it->language, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
527 OSL_TRACE(
528 "updateMaps: adding functionname: %s\n", rtl::OUStringToOString(
529 it->functionname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
531 ScriptFunction_hash sfh;
532 sfh[ it->functionname ] = *it;
533 mh_implementations[ it->language ] = sfh;
535 else
537 OSL_TRACE(
538 "updateMaps: adding functionname: %s\n", rtl::OUStringToOString(
539 it->functionname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
540 OSL_TRACE( " language name: %s\n",
541 rtl::OUStringToOString( it->functionname,
542 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
544 h_it->second[ it->functionname ] = *it;
549 //*************************************************************************
550 // XScriptStorageExport::save
551 void
552 ScriptStorage::save()
553 throw ( RuntimeException )
555 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
556 Reference< io::XActiveDataSource > xSource;
557 Reference< io::XOutputStream > xOS;
559 // xScriptInvocation = Reference<XScriptInvocation>(xx, UNO_QUERY_THROW);
560 Reference< xml::sax::XExtendedDocumentHandler > xHandler;
562 OUString parcel_suffix = OUString::createFromAscii( SCRIPT_PARCEL );
563 OUString ou_parcel = OUString(
564 RTL_CONSTASCII_USTRINGPARAM( SCRIPT_PARCEL_NAME_ONLY ) );
568 ScriptData_hash::iterator it_end = mh_implementations.end();
569 for ( ScriptData_hash::iterator it = mh_implementations.begin() ; it != it_end; ++it )
571 ::rtl::OUString logName = it->first;
572 ScriptFunction_hash::iterator it_sfh_end = it->second.end();
573 for ( ScriptFunction_hash::iterator it_sfh = it->second.begin();
574 it_sfh != it_sfh_end ; ++it_sfh )
576 ScriptOutput_hash::const_iterator it_parcels =
577 mh_parcels.find( it_sfh->second.parcelURI );
578 if ( it_parcels == mh_parcels.end() )
580 //create new outputstream
581 OUString parcel_xml_path = it_sfh->second.parcelURI.concat(
582 parcel_suffix );
583 m_xSimpleFileAccess->kill( parcel_xml_path );
584 xOS = m_xSimpleFileAccess->openFileWrite( parcel_xml_path );
586 OSL_TRACE( "saving: %s\n", rtl::OUStringToOString(
587 it_sfh->second.parcelURI.concat( OUString::createFromAscii(
588 "/parcel.xml" ) ),
589 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
591 Reference< XInterface > xInterface =
592 m_xMgr->createInstanceWithContext(
593 OUString::createFromAscii( "com.sun.star.xml.sax.Writer" ),
594 m_xContext );
595 validateXRef( xInterface, "ScriptStorage::save: cannot get sax.Writer" );
596 xHandler = Reference<xml::sax::XExtendedDocumentHandler>(
597 xInterface, UNO_QUERY_THROW );
598 xSource = Reference< io::XActiveDataSource >(
599 xHandler, UNO_QUERY_THROW );
600 xSource->setOutputStream( xOS );
602 writeMetadataHeader( xHandler );
604 mh_parcels[ it_sfh->second.parcelURI ] = xHandler;
606 else
608 xHandler = it_parcels->second;
611 ScriptElement* pSE = new ScriptElement( it_sfh->second );
612 // this is to get pSE released correctly
613 Reference < xml::sax::XAttributeList > xal( pSE );
614 pSE->dump( xHandler );
618 ScriptOutput_hash::const_iterator out_it_end = mh_parcels.end();
620 for ( ScriptOutput_hash::const_iterator out_it = mh_parcels.begin();
621 out_it != out_it_end; ++out_it )
623 out_it->second->ignorableWhitespace( ::rtl::OUString() );
624 out_it->second->endDocument();
625 xSource.set( out_it->second, UNO_QUERY );
626 Reference< io::XOutputStream > xOS = xSource->getOutputStream();
627 xOS->closeOutput();
631 // clear the hash map, as all output streams have been closed.
632 // need to re-create on next save
633 mh_parcels.clear();
635 // *** TODO - other exception handling IO etc.
636 catch ( RuntimeException & re )
638 OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::save" );
639 throw RuntimeException(
640 OUSTR( "ScriptStorage::save RuntimeException: " ).concat(
641 re.Message ),
642 Reference< XInterface > () );
646 //*************************************************************************
647 void
648 ScriptStorage::refresh()
649 throw (RuntimeException)
651 OSL_TRACE("** => ScriptStorage: in refresh()\n");
653 // guard against concurrent refreshes
654 ::osl::Guard< ::osl::Mutex > aGuard( m_mutex );
658 create();
661 catch ( RuntimeException & re )
663 OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::refresh" );
664 throw RuntimeException(
665 OUSTR( "ScriptStorage::refresh RuntimeException: " ).concat( re.Message ),
666 Reference< XInterface > () );
668 catch ( Exception & ue )
670 OSL_TRACE( "caught com::sun::star::uno::Exception in ScriptStorage::refresh" );
671 throw RuntimeException(
672 OUSTR( "ScriptStorage::refresh Exception: " ).concat( ue.Message ),
673 Reference< XInterface > () );
677 //*************************************************************************
678 void
679 ScriptStorage::writeMetadataHeader(
680 Reference <xml::sax::XExtendedDocumentHandler> & xHandler )
682 xHandler->startDocument();
683 OUString aDocTypeStr( RTL_CONSTASCII_USTRINGPARAM(
684 "<!DOCTYPE parcel SYSTEM \"scripting.dtd\">" ) );
685 xHandler->unknown( aDocTypeStr );
686 xHandler->ignorableWhitespace( OUString() );
690 //*************************************************************************
691 Sequence< ::rtl::OUString >
692 ScriptStorage::getScriptLogicalNames()
693 throw ( RuntimeException )
695 Sequence< ::rtl::OUString > results;
696 // comment out the rest, and ultimately remove method
697 /*ScriptInfo_hash::iterator h_it = mh_implementations.begin();
698 ScriptInfo_hash::iterator h_itEnd = mh_implementations.end();
699 if ( h_it == h_itEnd )
701 OSL_TRACE( "ScriptStorage::getImplementations: EMPTY STORAGE");
702 return results;
704 results.realloc( mh_implementations.size() );
706 //find the implementations for the given logical name
710 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
712 for ( sal_Int32 count = 0; h_it != h_itEnd ; ++h_it )
714 ::rtl::OUString logicalName = h_it->first;
715 OSL_TRACE( "Adding %s at index %d ", ::rtl::OUStringToOString(
716 logicalName, RTL_TEXTENCODING_ASCII_US ).pData->buffer, count);
717 results[ count++ ] = logicalName;
721 catch ( RuntimeException & re )
723 throw RuntimeException(
724 OUSTR( "ScriptStorage::getScriptLogicalNames RuntimeException: " ).concat( re.Message ),
725 Reference< XInterface > () );
727 catch ( Exception & e )
729 throw RuntimeException( OUSTR(
730 "ScriptStorage::getScriptLogicalNames Exception: " ).concat(
731 e.Message ), Reference< XInterface > () );
732 } */
733 return results;
736 //*************************************************************************
737 Sequence< Reference< storage::XScriptInfo > >
738 ScriptStorage::getImplementations( const ::rtl::OUString & queryURI )
739 throw ( lang::IllegalArgumentException,
740 RuntimeException )
742 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
743 // format is script:://[function_name]?language=[languge]&location=[location]
744 // LogicalName is now not used anymore, further more the ScriptURI class
745 // will be retired also and a new UNO service will be used. Additionally the
746 // parcel-description will also need to be modified to remove logical name
747 // ScriprtMetaDataImporter has been modified to ignore the Logical name
748 // definined in the parcel-desc.xml. As an interim temp solution the Datas_vec
749 // structure that is returned from ScriptMetDataImporter sets the logicalname
750 // to the function name. ScriptURI class has been changed in the same way.
752 Sequence< Reference< storage::XScriptInfo > > results;
753 ScriptURI scriptURI( queryURI );
754 OSL_TRACE( "getting impl for language %s, function name: %s",
755 ::rtl::OUStringToOString( scriptURI.getLanguage(),
756 RTL_TEXTENCODING_ASCII_US ).pData->buffer,
757 ::rtl::OUStringToOString( scriptURI.getFunctionName(),
758 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
759 ScriptData_hash::iterator h_itEnd = mh_implementations.end();
760 ScriptData_hash::iterator h_it = mh_implementations.begin();
761 if ( h_it == h_itEnd )
763 OSL_TRACE( "ScriptStorage::getImplementations: EMPTY STORAGE" );
764 return results;
767 //find the implementations for the given language
768 h_it = mh_implementations.find( scriptURI.getLanguage() );
770 if ( h_it == h_itEnd )
772 OSL_TRACE( "ScriptStorage::getImplementations: no impls found for %s",
773 ::rtl::OUStringToOString( scriptURI.getLanguage(),
774 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
775 return results;
778 //find the implementations for the given language
779 ScriptFunction_hash::const_iterator it_datas = h_it->second.find(
780 scriptURI.getLogicalName() );
781 ScriptFunction_hash::const_iterator it_datas_end = h_it->second.end();
783 if ( it_datas == it_datas_end )
785 OSL_TRACE( "ScriptStorage::getImplementations: no impls found for %s",
786 ::rtl::OUStringToOString( scriptURI.getFunctionName(),
787 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
788 return results;
791 results.realloc( 1 );
792 ScriptData scriptData = it_datas->second;
793 OSL_TRACE( "ScriptStorage::getImplementations: impls found for %s",
794 ::rtl::OUStringToOString( scriptData.functionname,
795 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
796 Reference< storage::XScriptInfo > xScriptInfo =
797 new ScriptInfo ( scriptData, m_scriptStorageID );
798 results[ 0 ] = xScriptInfo;
800 return results;
803 //*************************************************************************
804 Sequence< Reference< storage::XScriptInfo > > SAL_CALL
805 ScriptStorage::getAllImplementations() throw ( RuntimeException )
807 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
808 Sequence< Reference< storage::XScriptInfo > > results;
809 ScriptData_hash::iterator h_itEnd = mh_implementations.end();
810 ScriptData_hash::iterator h_it = mh_implementations.begin();
811 if ( h_it == h_itEnd )
813 OSL_TRACE( "ScriptStorage::getImplementations: EMPTY STORAGE" );
814 return results;
818 //iterate through each logical name and gather each implementation
819 //for that name
820 for ( sal_Int32 count = 0; h_it != h_itEnd; ++h_it )
822 results.realloc( h_it->second.size() + count );
823 OSL_TRACE( "Adding implementations for %s",
824 ::rtl::OUStringToOString( h_it->first,
825 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
826 ScriptFunction_hash::const_iterator it_sfh = h_it->second.begin();
827 ScriptFunction_hash::const_iterator it_sfh_end = h_it->second.end();
828 OSL_TRACE( "Adding %d to sequence of impls ", h_it->second.size() );
829 for ( ; it_sfh != it_sfh_end ; ++it_sfh )
831 Reference< storage::XScriptInfo > xScriptInfo = new ScriptInfo (
832 it_sfh->second, m_scriptStorageID );
834 results[ count++ ] = xScriptInfo;
837 return results;
841 //*************************************************************************
842 OUString SAL_CALL ScriptStorage::getImplementationName( )
843 throw( RuntimeException )
845 return ss_implName;
848 //*************************************************************************
849 sal_Bool SAL_CALL ScriptStorage::supportsService( const OUString& serviceName )
850 throw( RuntimeException )
852 OUString const * pNames = ss_serviceNames.getConstArray();
853 for ( sal_Int32 nPos = ss_serviceNames.getLength(); nPos--; )
855 if ( serviceName.equals( pNames[ nPos ] ) )
857 return sal_True;
860 return sal_False;
863 //*************************************************************************
864 Sequence<OUString> SAL_CALL ScriptStorage::getSupportedServiceNames( )
865 throw( RuntimeException )
867 return ss_serviceNames;
870 } // namespace scripting_impl
873 namespace scripting_runtimemgr
876 //*************************************************************************
877 Reference<XInterface> SAL_CALL ss_create(
878 const Reference< XComponentContext > & xCompC )
880 return ( cppu::OWeakObject * ) new ::scripting_impl::ScriptStorage( xCompC );
883 //*************************************************************************
884 Sequence<OUString> ss_getSupportedServiceNames( )
885 SAL_THROW( () )
887 return ::scripting_impl::ss_serviceNames;
890 //*************************************************************************
891 OUString ss_getImplementationName( )
892 SAL_THROW( () )
894 return ::scripting_impl::ss_implName;
896 }//end namespace
898 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */