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