1 /*************************************************************************
2 *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26 ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_scripting.hxx"
30 #include <osl/file.hxx>
31 #include <osl/time.h>
32 #include <cppuhelper/implementationentry.hxx>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/ucb/CommandAbortedException.hpp>
35 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
37 #include <com/sun/star/container/XNameAccess.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 
41 #include <util/util.hxx>
42 #include <rtl/uri.hxx>
43 
44 
45 #include "ScriptData.hxx"
46 #include "ScriptInfo.hxx"
47 #include "ScriptStorage.hxx"
48 #include "ScriptElement.hxx"
49 #include "ScriptMetadataImporter.hxx"
50 #include "ScriptURI.hxx"
51 
52 using namespace ::rtl;
53 using namespace ::cppu;
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
56 using namespace ::drafts::com::sun::star::script::framework;
57 
58 namespace scripting_impl
59 {
60 
61 ScriptLanguages_hash* ScriptStorage::mh_scriptLangs = NULL;
62 
63 const sal_Char* const SERVICE_NAME =
64     "drafts.com.sun.star.script.framework.storage.ScriptStorage";
65 const sal_Char* const IMPL_NAME =
66     "drafts.com.sun.star.script.framework.storage.ScriptStorage";
67 
68 const sal_Char * const SCRIPT_DIR = "/Scripts";
69 const sal_Char * const SCRIPT_PARCEL = "/parcel-descriptor.xml";
70 const sal_Char * const SCRIPT_PARCEL_NAME_ONLY = "parcel-descriptor";
71 
72 static OUString ss_implName = OUString::createFromAscii( IMPL_NAME );
73 static OUString ss_serviceName = OUString::createFromAscii( SERVICE_NAME );
74 static Sequence< OUString > ss_serviceNames =
75     Sequence< OUString >( &ss_serviceName, 1 );
76 
77 const sal_uInt16 NUMBER_STORAGE_INITIALIZE_ARGS = 3;
78 
79 //extern ::rtl_StandardModuleCount s_moduleCount;
80 
81 
82 
83 //*************************************************************************
84 ScriptStorage::ScriptStorage( const Reference <
85                               XComponentContext > & xContext )
86 throw ( RuntimeException )
87         : m_xContext( xContext, UNO_SET_THROW ), m_bInitialised( false )
88 {
89     OSL_TRACE( "< ScriptStorage ctor called >\n" );
90 
91     m_xMgr.set( m_xContext->getServiceManager(), UNO_SET_THROW );
92 
93     if( !mh_scriptLangs )
94     {
95         ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
96         if( !mh_scriptLangs )
97         {
98             mh_scriptLangs = new ScriptLanguages_hash();
99             Reference< lang::XMultiServiceFactory > xConfigProvFactory(
100                 m_xMgr->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.configuration.ConfigurationProvider" ), m_xContext ),
101                 UNO_QUERY_THROW );
102             // create an instance of the ConfigurationAccess for accessing the
103             // scripting runtime settings
104             beans::PropertyValue configPath;
105             configPath.Name = ::rtl::OUString::createFromAscii( "nodepath" );
106             configPath.Value <<= ::rtl::OUString::createFromAscii( "org.openoffice.Office.Scripting/ScriptRuntimes" );
107             Sequence < Any > aargs( 1 );
108             aargs[ 0 ] <<= configPath;
109 
110             Reference< container::XNameAccess > xNameAccess(
111                 xConfigProvFactory->createInstanceWithArguments(
112                     OUString::createFromAscii( "com.sun.star.configuration.ConfigurationAccess" ),
113                     aargs
114                 ),
115                 UNO_QUERY_THROW );
116 
117             Sequence< OUString > names = xNameAccess->getElementNames();
118             for( int i = 0 ; i < names.getLength() ; i++ )
119             {
120                 OSL_TRACE(  "Getting propertyset for Lang=%s",
121                     ::rtl::OUStringToOString( names[i], RTL_TEXTENCODING_ASCII_US ).pData->buffer );
122                 Reference< beans::XPropertySet > xPropSet( xNameAccess->getByName( names[i] ), UNO_QUERY_THROW );
123                 Any aProp = xPropSet->getPropertyValue(
124                         OUString::createFromAscii( "SupportedFileExtensions") );
125                 Sequence< OUString > extns;
126                 if( sal_False == ( aProp >>= extns ) )
127                 {
128                     throw RuntimeException(
129                         OUSTR( "ScriptStorage:ScriptStorage: can't get runtime extensions" ),
130                         Reference< XInterface > () );
131                 }
132                 for( int j = 0 ; j < extns.getLength() ; j++ )
133                 {
134                     OSL_TRACE(  "Adding Lang=%s, Extn=%s\n",
135                         ::rtl::OUStringToOString( names[i], RTL_TEXTENCODING_ASCII_US ).pData->buffer,
136                         ::rtl::OUStringToOString( extns[j], RTL_TEXTENCODING_ASCII_US ).pData->buffer );
137                     (*mh_scriptLangs)[ extns[j] ] =
138                         names[i];
139                 }
140             }
141         }
142     }
143 //    s_moduleCount.modCnt.acquire( &s_moduleCount.modCnt );
144 }
145 
146 //*************************************************************************
147 ScriptStorage::~ScriptStorage() SAL_THROW( () )
148 {
149     OSL_TRACE( "< ScriptStorage dtor called >\n" );
150 //    s_moduleCount.modCnt.release( &s_moduleCount.modCnt );
151 }
152 
153 //*************************************************************************
154 void
155 ScriptStorage::initialize( const Sequence <Any> & args )
156 throw ( RuntimeException, Exception )
157 {
158     OSL_TRACE( "Entering ScriptStorage::initialize\n" );
159 
160     // Should not be renitialised
161     if ( m_bInitialised )
162     {
163         throw RuntimeException(
164             OUSTR( "ScriptStorage::initalize already initialized" ),
165             Reference<XInterface> () );
166     }
167 
168     {   // Protect member variable writes
169         ::osl::Guard< osl::Mutex > aGuard( m_mutex );
170 
171         // Check args
172         if ( args.getLength() != NUMBER_STORAGE_INITIALIZE_ARGS )
173         {
174             OSL_TRACE( "ScriptStorage::initialize: got wrong number of args\n" );
175             throw RuntimeException(
176                 OUSTR( "Invalid number of arguments provided!" ),
177                 Reference< XInterface >() );
178         }
179 
180         if ( sal_False == ( args[ 0 ] >>= m_xSimpleFileAccess ) )
181         {
182             throw RuntimeException(
183                 OUSTR( "Invalid XSimpleFileAccess argument provided!" ),
184                 Reference< XInterface >() );
185         }
186 
187         if ( sal_False == ( args[ 1 ] >>= m_scriptStorageID ) )
188         {
189             throw RuntimeException(
190                 OUSTR( "Invalid ScriptStorage ID argument provided!" ),
191                 Reference< XInterface >() );
192 
193         }
194         if ( sal_False == ( args[ 2 ] >>= m_stringUri ) )
195         {
196             throw RuntimeException(
197                 OUSTR( "Invalid String Uri argument provided!" ),
198                 Reference< XInterface >() );
199         }
200     } // End - Protect member variable writes
201 
202     OSL_TRACE(  "uri: %s\n", ::rtl::OUStringToOString(
203         m_stringUri, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
204 
205     try
206     {
207         // need to check for what???
208         // what we have is a URI for the filesystem or document
209         // we need to check of the last element in the path has an
210         // extension that is associated with a script (eg. .bsh, .js etc)
211         OUString fileExtension = getFileExtension( m_stringUri );
212         // and see if this is in our scripts map
213         ScriptLanguages_hash::iterator h_it = mh_scriptLangs->find( fileExtension );
214         if ( h_it != mh_scriptLangs->end() )
215         {
216             createForFilesystem( fileExtension );
217         }
218         else
219         {
220             create();
221         }
222     }
223     catch ( RuntimeException & re )
224     {
225         OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::initialize" );
226         throw RuntimeException(
227             OUSTR( "ScriptStorage::initalize RuntimeException: " ).concat( re.Message ),
228             Reference< XInterface > () );
229     }
230     catch ( Exception & ue )
231     {
232         OSL_TRACE( "caught com::sun::star::uno::Exception in ScriptStorage::initialize" );
233         throw RuntimeException(
234             OUSTR( "ScriptStorage::initalize Exception: " ).concat( ue.Message ),
235             Reference< XInterface > () );
236     }
237 #ifdef _DEBUG
238     catch ( ... )
239     {
240         OSL_TRACE( "caught unknown Exception in ScriptStorage::initialize" );
241         throw RuntimeException(
242             OUSTR( "ScriptStorage::initalize unknown exception: " ),
243             Reference< XInterface > () );
244     }
245 #endif
246 
247     OSL_TRACE( "Parsed the XML\n" );
248 
249     m_bInitialised = true;
250 }
251 
252 void
253 ScriptStorage::create()
254 throw ( RuntimeException, Exception )
255 {
256     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
257     try
258     {
259         // clear existing hashmap - rebuilding from scratch to avoid having
260         // to search for deleted elements on refresh
261         mh_implementations.clear();
262 
263         OUString xStringUri(m_stringUri);
264 
265         ScriptMetadataImporter* SMI = new ScriptMetadataImporter( m_xContext );
266         Reference< xml::sax::XExtendedDocumentHandler > xSMI( SMI, UNO_SET_THROW );
267 
268         xStringUri = xStringUri.concat( ::rtl::OUString::createFromAscii(
269             SCRIPT_DIR ) );
270 
271        // No Scripts directory - just return
272        if ( ! m_xSimpleFileAccess->isFolder( xStringUri ) )
273        {
274             OSL_TRACE( "ScriptStorage::initialize: no Scripts dir for this storage - install problem\n" );
275            return;
276        }
277 
278         // get the list of language folders under the Scripts directory
279         Sequence< ::rtl::OUString > languageDirs =
280             m_xSimpleFileAccess->getFolderContents( xStringUri, true );
281 
282         Reference< io::XInputStream > xInput;
283         sal_Int32 languageDirsLength = languageDirs.getLength();
284         for ( sal_Int32 i = 0; i < languageDirsLength ; ++i )
285         {
286             OSL_TRACE(  "contains: %s\n", ::rtl::OUStringToOString(
287                 languageDirs[ i ], RTL_TEXTENCODING_ASCII_US ).pData->buffer );
288 
289             if ( ! m_xSimpleFileAccess->isFolder( languageDirs[ i ] ) )
290             {
291                 continue;
292             }
293 
294             //get the list of parcel folders for each language folder
295             // under Scripts
296             Sequence< ::rtl::OUString > parcelDirs =
297                 m_xSimpleFileAccess->getFolderContents( languageDirs[ i ], true );
298 
299             sal_Int32 parcelDirsLength = parcelDirs.getLength();
300             for ( sal_Int32 j = 0; j < parcelDirsLength ; ++j )
301             {
302                 OSL_TRACE(  "contains: %s\n",
303                     ::rtl::OUStringToOString( parcelDirs[ j ],
304                     RTL_TEXTENCODING_ASCII_US ).pData->buffer );
305 
306                 OUString parcelFile = parcelDirs[ j ].concat(
307                     ::rtl::OUString::createFromAscii( SCRIPT_PARCEL ) );
308 
309                 // Do not have a valid parcel.xml
310                 if ( !m_xSimpleFileAccess->exists( parcelFile ) ||
311                         m_xSimpleFileAccess->isFolder( parcelFile ) )
312                 {
313                     continue;
314                 }
315                 OSL_TRACE(  "parcel file: %s\n",
316                     ::rtl::OUStringToOString( parcelFile,
317                     RTL_TEXTENCODING_ASCII_US ).pData->buffer );
318 
319                 xInput = m_xSimpleFileAccess->openFileRead( parcelFile );
320                 // Failed to get input stream
321                 if ( !xInput.is() )
322                 {
323                     continue;
324                 }
325 
326                 OSL_TRACE( "Parse the metadata \n" );
327                 Datas_vec vScriptDatas;
328                 try
329                 {
330                     SMI->parseMetaData( xInput, parcelDirs[ j ], vScriptDatas );
331                 }
332                 catch ( xml::sax::SAXException & saxe )
333                 {
334                     if ( xInput.is() )
335                     {
336                         xInput->closeInput();
337                     }
338                     OSL_TRACE(
339                         "caught com::sun::star::xml::sax::SAXException in ScriptStorage::create %s",
340                         ::rtl::OUStringToOString( saxe.Message,
341                         RTL_TEXTENCODING_ASCII_US ).pData->buffer  );
342 
343                     continue;
344                 }
345                 catch ( io::IOException & ioe )
346                 {
347                     if ( xInput.is() )
348                     {
349                         xInput->closeInput();
350                     }
351                     OSL_TRACE(
352                         "caught com::sun::star::io::IOException in ScriptStorage::create" );
353                     continue;
354                 }
355                 xInput->closeInput();
356 
357                 updateMaps( vScriptDatas );
358             }
359         }
360     }
361     catch ( io::IOException & ioe )
362     {
363         //From ScriptMetadata Importer
364         OSL_TRACE( "caught com::sun::star::io::IOException in ScriptStorage::create" );
365         throw RuntimeException(
366             OUSTR( "ScriptStorage::create IOException: " ).concat( ioe.Message ),
367             Reference< XInterface > () );
368 
369     }
370     catch ( ucb::CommandAbortedException & cae )
371     {
372         OSL_TRACE( "caught com::sun::star::ucb::CommandAbortedException in ScriptStorage::create" );
373         throw RuntimeException(
374             OUSTR(
375                 "ScriptStorage::create CommandAbortedException: " ).concat( cae.Message ),
376             Reference< XInterface > () );
377     }
378     catch ( RuntimeException & re )
379     {
380         OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::create" );
381         throw RuntimeException(
382             OUSTR( "ScriptStorage::create RuntimeException: " ).concat( re.Message ),
383             Reference< XInterface > () );
384     }
385     catch ( Exception & ue )
386     {
387         OSL_TRACE( "caught com::sun::star::uno::Exception in ScriptStorage::create" );
388         throw RuntimeException(
389             OUSTR( "ScriptStorage::create Exception: " ).concat( ue.Message ),
390             Reference< XInterface > () );
391     }
392 #ifdef _DEBUG
393     catch ( ... )
394     {
395         OSL_TRACE( "caught unknown Exception in ScriptStorage::create" );
396         throw RuntimeException(
397             OUSTR( "ScriptStorage::initalize unknown exception: " ),
398             Reference< XInterface > () );
399     }
400 #endif
401 
402     OSL_TRACE( "Parsed the XML\n" );
403 
404     m_bInitialised = true;
405 }
406 
407 //*************************************************************************
408 // private method to create the usual data structures for scripts located
409 // on the filesystem.
410 // parcelURI = the path to the script
411 // functionName = the full filename with extension
412 // logicalName = the filename without the extension
413 void
414 ScriptStorage::createForFilesystem( const OUString & fileExtension )
415 throw ( RuntimeException, Exception )
416 {
417     // need to decode as file urls are encoded
418     OUString xStringUri = ::rtl::Uri::decode( m_stringUri,
419         rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ASCII_US );
420 
421     // no x-platform issues here as we are dealing with URLs
422     sal_Int32 lastFileSep = xStringUri.lastIndexOf( '/' );
423     // the char just after the filesep
424     lastFileSep += 1;
425     sal_Int32 lastFileExt = xStringUri.lastIndexOf( fileExtension );
426     OUString searchString = OUString::createFromAscii( "://" );
427     sal_Int32 searchStringLength = searchString.getLength();
428     sal_Int32 startPath = xStringUri.indexOf( searchString );
429     sal_Int32 uriLength = xStringUri.getLength();
430     OUString fileNameNoExt = xStringUri.copy( lastFileSep ,
431         lastFileExt - lastFileSep  - 1 );
432     OUString fileName = xStringUri.copy( lastFileSep, uriLength - lastFileSep );
433     OUString filePath = xStringUri.copy( startPath + searchStringLength,
434         lastFileSep - startPath - searchStringLength );
435     OUString filePathWithName = xStringUri.copy( startPath + searchStringLength,
436         uriLength - startPath - searchStringLength );
437 
438     ScriptData scriptData;
439     scriptData.language = mh_scriptLangs->find( fileExtension )->second;
440     OSL_TRACE( "\t language = %s", ::rtl::OUStringToOString(
441         scriptData.language, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
442 
443     // do we need to encode this?
444     scriptData.functionname = fileName;
445     OSL_TRACE( "\t functionName = %s", ::rtl::OUStringToOString(
446         scriptData.functionname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
447     //scriptData.functionname = ::rtl::Uri::encode( fileName,
448         //rtl_UriCharClassUricNoSlash, rtl_UriEncodeCheckEscapes,
449         //RTL_TEXTENCODING_ASCII_US );
450 
451     scriptData.parcelURI = filePath;
452     OSL_TRACE( "\t parcelURI = %s", ::rtl::OUStringToOString(
453         scriptData.parcelURI, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
454     scriptData.logicalname = fileNameNoExt;
455     OSL_TRACE( "\t logicalName = %s", ::rtl::OUStringToOString(
456         scriptData.logicalname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
457 
458     // and now push onto the usual structures
459     ScriptFunction_hash sfh;
460     sfh[ scriptData.functionname ] = scriptData;
461     mh_implementations[ scriptData.language ] = sfh;
462     m_bInitialised = true;
463 }
464 
465 //*************************************************************************
466 // private method to return the file extension, eg. bsh, js etc
467 OUString
468 ScriptStorage::getFileExtension( const OUString & stringUri )
469 {
470     OUString fileExtension;
471     sal_Int32 lastDot = stringUri.lastIndexOf( '.' );
472     if( lastDot > 0 ) {
473         sal_Int32 stringUriLength = stringUri.getLength();
474         fileExtension = stringUri.copy( lastDot +1 , stringUriLength - lastDot - 1 );
475     }
476     else
477     {
478         fileExtension = OUString::createFromAscii("");
479     }
480     return fileExtension;
481 }
482 
483 //*************************************************************************
484 // private method for updating hashmaps
485 void
486 ScriptStorage::updateMaps( const Datas_vec & vScriptDatas )
487 {
488 
489     Datas_vec::const_iterator it_end = vScriptDatas.end();
490     // step through the vector of ScripImplInfos returned from parse
491     for ( Datas_vec::const_iterator it = vScriptDatas.begin() ; it != it_end; ++it )
492     {
493         //find the Datas_vec for this logical name
494         ScriptData_hash::iterator h_it = mh_implementations.find( it->language );
495 
496         if ( h_it == mh_implementations.end() )
497         {
498             //if it's null, need to create a new Datas_vec
499             OSL_TRACE(
500                      "updateMaps: new language: %s\n", rtl::OUStringToOString(
501                          it->language, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
502             OSL_TRACE(
503                      "updateMaps: adding functionname: %s\n", rtl::OUStringToOString(
504                          it->functionname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
505 
506             ScriptFunction_hash sfh;
507             sfh[ it->functionname ] = *it;
508             mh_implementations[ it->language ] = sfh;
509         }
510         else
511         {
512             OSL_TRACE(
513                      "updateMaps: adding functionname: %s\n", rtl::OUStringToOString(
514                          it->functionname, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
515             OSL_TRACE(  "                    language name: %s\n",
516                 rtl::OUStringToOString( it->functionname,
517                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
518 
519             h_it->second[ it->functionname ] = *it;
520         }
521     }
522 }
523 
524 //*************************************************************************
525 // XScriptStorageExport::save
526 void
527 ScriptStorage::save()
528 throw ( RuntimeException )
529 {
530     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
531     Reference< io::XActiveDataSource > xSource;
532     Reference< io::XOutputStream > xOS;
533 
534     // xScriptInvocation = Reference<XScriptInvocation>(xx, UNO_QUERY_THROW);
535     Reference< xml::sax::XExtendedDocumentHandler > xHandler;
536 
537     OUString parcel_suffix = OUString::createFromAscii( SCRIPT_PARCEL );
538     OUString ou_parcel = OUString(
539         RTL_CONSTASCII_USTRINGPARAM( SCRIPT_PARCEL_NAME_ONLY ) );
540 
541     try
542     {
543         ScriptData_hash::iterator it_end = mh_implementations.end();
544         for ( ScriptData_hash::iterator it = mh_implementations.begin() ; it != it_end; ++it )
545         {
546             ::rtl::OUString logName = it->first;
547             ScriptFunction_hash::iterator it_sfh_end = it->second.end();
548             for ( ScriptFunction_hash::iterator it_sfh = it->second.begin();
549                     it_sfh != it_sfh_end ; ++it_sfh )
550             {
551                 ScriptOutput_hash::const_iterator it_parcels =
552                     mh_parcels.find( it_sfh->second.parcelURI );
553                 if ( it_parcels == mh_parcels.end() )
554                 {
555                     //create new outputstream
556                     OUString parcel_xml_path = it_sfh->second.parcelURI.concat(
557                         parcel_suffix );
558                     m_xSimpleFileAccess->kill( parcel_xml_path );
559                     xOS = m_xSimpleFileAccess->openFileWrite( parcel_xml_path );
560 
561                     OSL_TRACE(  "saving: %s\n", rtl::OUStringToOString(
562                         it_sfh->second.parcelURI.concat( OUString::createFromAscii(
563                         "/parcel.xml" ) ),
564                         RTL_TEXTENCODING_ASCII_US ).pData->buffer );
565 
566                     xHandler.set(
567                         m_xMgr->createInstanceWithContext(
568                             OUString::createFromAscii( "com.sun.star.xml.sax.Writer" ),
569                             m_xContext
570                         ),
571                         UNO_QUERY_THROW
572                     );
573                     xSource.set( xHandler, UNO_QUERY_THROW );
574                     xSource->setOutputStream( xOS );
575 
576                     writeMetadataHeader( xHandler );
577 
578                     mh_parcels[ it_sfh->second.parcelURI ] = xHandler;
579                 }
580                 else
581                 {
582                     xHandler = it_parcels->second;
583                 }
584 
585                 ScriptElement* pSE = new ScriptElement( it_sfh->second );
586                 // this is to get pSE released correctly
587                 Reference < xml::sax::XAttributeList > xal( pSE );
588                 pSE->dump( xHandler );
589             }
590         }
591 
592         ScriptOutput_hash::const_iterator out_it_end = mh_parcels.end();
593 
594         for ( ScriptOutput_hash::const_iterator out_it = mh_parcels.begin();
595                 out_it != out_it_end; ++out_it )
596         {
597             out_it->second->ignorableWhitespace( ::rtl::OUString() );
598             out_it->second->endDocument();
599             xSource.set( out_it->second, UNO_QUERY );
600             Reference< io::XOutputStream > xOS = xSource->getOutputStream();
601             xOS->closeOutput();
602 
603         }
604 
605         // clear the hash map, as all output streams have been closed.
606         // need to re-create on next save
607         mh_parcels.clear();
608     }
609     // *** TODO - other exception handling IO etc.
610     catch ( RuntimeException & re )
611     {
612         OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::save" );
613         throw RuntimeException(
614             OUSTR( "ScriptStorage::save RuntimeException: " ).concat(
615             re.Message ),
616             Reference< XInterface > () );
617     }
618 }
619 
620 //*************************************************************************
621 void
622 ScriptStorage::refresh()
623 throw (RuntimeException)
624 {
625     OSL_TRACE("** => ScriptStorage: in refresh()\n");
626 
627     // guard against concurrent refreshes
628     ::osl::Guard< ::osl::Mutex > aGuard( m_mutex );
629 
630     try
631     {
632         create();
633 
634     }
635     catch ( RuntimeException & re )
636     {
637         OSL_TRACE( "caught com::sun::star::uno::RuntimeException in ScriptStorage::refresh" );
638         throw RuntimeException(
639             OUSTR( "ScriptStorage::refresh RuntimeException: " ).concat( re.Message ),
640             Reference< XInterface > () );
641     }
642     catch ( Exception & ue )
643     {
644         OSL_TRACE( "caught com::sun::star::uno::Exception in ScriptStorage::refresh" );
645         throw RuntimeException(
646             OUSTR( "ScriptStorage::refresh Exception: " ).concat( ue.Message ),
647             Reference< XInterface > () );
648     }
649 }
650 
651 //*************************************************************************
652 void
653 ScriptStorage::writeMetadataHeader(
654     Reference <xml::sax::XExtendedDocumentHandler> & xHandler )
655 {
656     xHandler->startDocument();
657     OUString aDocTypeStr( RTL_CONSTASCII_USTRINGPARAM(
658         "<!DOCTYPE  parcel SYSTEM \"scripting.dtd\">" ) );
659     xHandler->unknown( aDocTypeStr );
660     xHandler->ignorableWhitespace( OUString() );
661 }
662 
663 
664 //*************************************************************************
665 Sequence< ::rtl::OUString >
666 ScriptStorage::getScriptLogicalNames()
667 throw ( RuntimeException )
668 {
669     Sequence< ::rtl::OUString  > results;
670     // comment out the rest, and ultimately remove method
671     /*ScriptInfo_hash::iterator h_it = mh_implementations.begin();
672     ScriptInfo_hash::iterator h_itEnd =  mh_implementations.end();
673     if ( h_it == h_itEnd  )
674     {
675         OSL_TRACE( "ScriptStorage::getImplementations: EMPTY STORAGE");
676         return results;
677     }
678     results.realloc( mh_implementations.size() );
679 
680     //find the implementations for the given logical name
681     try
682     {
683 
684         ::osl::Guard< osl::Mutex > aGuard( m_mutex );
685 
686         for ( sal_Int32 count = 0; h_it != h_itEnd ; ++h_it )
687         {
688             ::rtl::OUString logicalName = h_it->first;
689             OSL_TRACE( "Adding %s at index %d ", ::rtl::OUStringToOString(
690                 logicalName, RTL_TEXTENCODING_ASCII_US ).pData->buffer, count);
691             results[ count++ ] = logicalName;
692         }
693 
694     }
695     catch ( RuntimeException & re )
696     {
697         throw RuntimeException(
698             OUSTR( "ScriptStorage::getScriptLogicalNames RuntimeException: " ).concat( re.Message ),
699             Reference< XInterface > () );
700     }
701     catch ( Exception & e )
702     {
703         throw RuntimeException( OUSTR(
704             "ScriptStorage::getScriptLogicalNames Exception: " ).concat(
705             e.Message ), Reference< XInterface > () );
706     } */
707     return results;
708 }
709 
710 //*************************************************************************
711 Sequence< Reference< storage::XScriptInfo > >
712 ScriptStorage::getImplementations( const ::rtl::OUString & queryURI )
713 throw ( lang::IllegalArgumentException,
714         RuntimeException )
715 {
716     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
717 // format is script:://[function_name]?language=[languge]&location=[location]
718 // LogicalName is now not used anymore, further more the ScriptURI class
719 // will be retired also and a new UNO service will be used. Additionally the
720 // parcel-description will also need to be modified to remove logical name
721 // ScriprtMetaDataImporter has been modified to ignore the Logical name
722 // definined in the parcel-desc.xml. As an interim temp solution the  Datas_vec
723 // structure that is returned from ScriptMetDataImporter sets the logicalname
724 // to the function name. ScriptURI class has been changed in the same way.
725 //
726     Sequence< Reference< storage::XScriptInfo > > results;
727     ScriptURI scriptURI( queryURI );
728     OSL_TRACE( "getting impl for language %s, function name: %s",
729         ::rtl::OUStringToOString( scriptURI.getLanguage(),
730         RTL_TEXTENCODING_ASCII_US ).pData->buffer,
731         ::rtl::OUStringToOString( scriptURI.getFunctionName(),
732         RTL_TEXTENCODING_ASCII_US ).pData->buffer );
733     ScriptData_hash::iterator h_itEnd =  mh_implementations.end();
734     ScriptData_hash::iterator h_it = mh_implementations.begin();
735     if ( h_it == h_itEnd )
736     {
737         OSL_TRACE( "ScriptStorage::getImplementations: EMPTY STORAGE" );
738         return results;
739     }
740 
741     //find the implementations for the given language
742     h_it = mh_implementations.find( scriptURI.getLanguage() );
743 
744     if ( h_it == h_itEnd )
745     {
746         OSL_TRACE( "ScriptStorage::getImplementations: no impls found for %s",
747             ::rtl::OUStringToOString( scriptURI.getLanguage(),
748             RTL_TEXTENCODING_ASCII_US ).pData->buffer );
749         return results;
750     }
751 
752     //find the implementations for the given language
753     ScriptFunction_hash::const_iterator it_datas = h_it->second.find(
754         scriptURI.getLogicalName() );
755     ScriptFunction_hash::const_iterator it_datas_end = h_it->second.end();
756 
757     if ( it_datas == it_datas_end )
758     {
759         OSL_TRACE( "ScriptStorage::getImplementations: no impls found for %s",
760             ::rtl::OUStringToOString( scriptURI.getFunctionName(),
761             RTL_TEXTENCODING_ASCII_US ).pData->buffer );
762         return results;
763     }
764 
765     results.realloc( 1 );
766     ScriptData scriptData = it_datas->second;
767     OSL_TRACE( "ScriptStorage::getImplementations: impls found for %s",
768         ::rtl::OUStringToOString( scriptData.functionname,
769         RTL_TEXTENCODING_ASCII_US ).pData->buffer );
770     Reference< storage::XScriptInfo > xScriptInfo =
771         new ScriptInfo ( scriptData, m_scriptStorageID );
772     results[ 0 ] = xScriptInfo;
773 
774     return results;
775 }
776 
777 //*************************************************************************
778 Sequence< Reference< storage::XScriptInfo > > SAL_CALL
779 ScriptStorage::getAllImplementations() throw ( RuntimeException )
780 {
781     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
782     Sequence< Reference< storage::XScriptInfo > > results;
783     ScriptData_hash::iterator h_itEnd =  mh_implementations.end();
784     ScriptData_hash::iterator h_it = mh_implementations.begin();
785     if ( h_it == h_itEnd )
786     {
787         OSL_TRACE( "ScriptStorage::getImplementations: EMPTY STORAGE" );
788         return results;
789     }
790 
791 
792     //iterate through each logical name and gather each implementation
793     //for that name
794     for ( sal_Int32 count = 0; h_it !=  h_itEnd; ++h_it )
795     {
796         results.realloc( h_it->second.size() + count );
797         OSL_TRACE( "Adding implementations for %s",
798             ::rtl::OUStringToOString( h_it->first,
799                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
800         ScriptFunction_hash::const_iterator it_sfh = h_it->second.begin();
801         ScriptFunction_hash::const_iterator it_sfh_end = h_it->second.end();
802         OSL_TRACE( "Adding %d to sequence of impls ", h_it->second.size() );
803         for ( ; it_sfh != it_sfh_end ; ++it_sfh )
804         {
805             Reference< storage::XScriptInfo > xScriptInfo = new ScriptInfo (
806             it_sfh->second, m_scriptStorageID );
807 
808             results[ count++ ] = xScriptInfo;
809         }
810     }
811     return results;
812 
813 }
814 
815 //*************************************************************************
816 OUString SAL_CALL ScriptStorage::getImplementationName( )
817 throw( RuntimeException )
818 {
819     return ss_implName;
820 }
821 
822 //*************************************************************************
823 sal_Bool SAL_CALL ScriptStorage::supportsService( const OUString& serviceName )
824 throw( RuntimeException )
825 {
826     OUString const * pNames = ss_serviceNames.getConstArray();
827     for ( sal_Int32 nPos = ss_serviceNames.getLength(); nPos--; )
828     {
829         if ( serviceName.equals( pNames[ nPos ] ) )
830         {
831             return sal_True;
832         }
833     }
834     return sal_False;
835 }
836 
837 //*************************************************************************
838 Sequence<OUString> SAL_CALL ScriptStorage::getSupportedServiceNames( )
839 throw( RuntimeException )
840 {
841     return ss_serviceNames;
842 }
843 
844 } // namespace scripting_impl
845 
846 
847 namespace scripting_runtimemgr
848 {
849 
850 //*************************************************************************
851 Reference<XInterface> SAL_CALL ss_create(
852     const Reference< XComponentContext > & xCompC )
853 {
854     return ( cppu::OWeakObject * ) new ::scripting_impl::ScriptStorage( xCompC );
855 }
856 
857 //*************************************************************************
858 Sequence<OUString> ss_getSupportedServiceNames( )
859 SAL_THROW( () )
860 {
861     return ::scripting_impl::ss_serviceNames;
862 }
863 
864 //*************************************************************************
865 OUString ss_getImplementationName( )
866 SAL_THROW( () )
867 {
868     return ::scripting_impl::ss_implName;
869 }
870 }//end namespace
871