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