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