1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: URIHelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_scripting.hxx"
34 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
35 #include <rtl/ustrbuf.hxx>
36 #include "URIHelper.hxx"
38 #define PRTSTR(x) ::rtl::OUStringToOString(x, RTL_TEXTENCODING_ASCII_US).pData->buffer
40 namespace func_provider
43 using ::rtl::OUString
;
44 namespace uno
= ::com::sun::star::uno
;
45 namespace ucb
= ::com::sun::star::ucb
;
46 namespace lang
= ::com::sun::star::lang
;
47 namespace uri
= ::com::sun::star::uri
;
48 namespace script
= ::com::sun::star::script
;
50 static const char SHARE
[] = "share";
51 static const char SHARE_URI
[] =
52 "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::BaseInstallation}";
54 static const char SHARE_UNO_PACKAGES
[] = "share:uno_packages";
55 static const char SHARE_UNO_PACKAGES_URI
[] =
56 "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
58 static const char USER
[] = "user";
59 static const char USER_URI
[] =
60 "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
62 static const char USER_UNO_PACKAGES
[] = "user:uno_packages";
63 static const char USER_UNO_PACKAGES_DIR
[] =
64 "/user/uno_packages/cache";
66 static const char DOCUMENT
[] = "document";
67 static const char TDOC_SCHEME
[] = "vnd.sun.star.tdoc";
69 ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
70 const uno::Reference
< uno::XComponentContext
>& xContext
)
71 throw( uno::RuntimeException
)
75 m_xSimpleFileAccess
= uno::Reference
< ucb::XSimpleFileAccess
>(
76 xContext
->getServiceManager()->createInstanceWithContext(
77 OUString::createFromAscii(
78 "com.sun.star.ucb.SimpleFileAccess"),
79 xContext
), uno::UNO_QUERY_THROW
);
81 catch (uno::Exception
&)
84 "Scripting Framework error initialising XSimpleFileAccess");
89 m_xUriReferenceFactory
= uno::Reference
< uri::XUriReferenceFactory
>(
90 xContext
->getServiceManager()->createInstanceWithContext(
91 OUString::createFromAscii(
92 "com.sun.star.uri.UriReferenceFactory"),
93 xContext
), uno::UNO_QUERY_THROW
);
95 catch (uno::Exception
&)
98 "Scripting Framework error initialising XUriReferenceFactory");
102 ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
104 // currently does nothing
108 ScriptingFrameworkURIHelper::initialize(
109 const uno::Sequence
< uno::Any
>& args
)
110 throw ( uno::Exception
, uno::RuntimeException
)
112 if ( args
.getLength() != 2 ||
113 args
[0].getValueType() != ::getCppuType((const OUString
*)NULL
) ||
114 args
[1].getValueType() != ::getCppuType((const OUString
*)NULL
) )
116 throw uno::RuntimeException( OUString::createFromAscii(
117 "ScriptingFrameworkURIHelper got invalid argument list" ),
118 uno::Reference
< uno::XInterface
>() );
121 if ( (args
[0] >>= m_sLanguage
) == sal_False
||
122 (args
[1] >>= m_sLocation
) == sal_False
)
124 throw uno::RuntimeException( OUString::createFromAscii(
125 "ScriptingFrameworkURIHelper error parsing args" ),
126 uno::Reference
< uno::XInterface
>() );
129 SCRIPTS_PART
= OUString::createFromAscii( "/Scripts/" );
130 SCRIPTS_PART
= SCRIPTS_PART
.concat( m_sLanguage
.toAsciiLowerCase() );
132 if ( !initBaseURI() )
134 throw uno::RuntimeException( OUString::createFromAscii(
135 "ScriptingFrameworkURIHelper cannot find script directory"),
136 uno::Reference
< uno::XInterface
>() );
141 ScriptingFrameworkURIHelper::initBaseURI()
144 bool bAppendScriptsPart
= false;
146 if ( m_sLocation
.equalsAscii(USER
))
148 test
= OUString::createFromAscii(USER
);
149 uri
= OUString::createFromAscii(USER_URI
);
150 bAppendScriptsPart
= true;
152 else if ( m_sLocation
.equalsAscii(USER_UNO_PACKAGES
))
154 test
= OUString::createFromAscii("uno_packages");
155 uri
= OUString::createFromAscii(USER_URI
);
156 uri
= uri
.concat(OUString::createFromAscii(USER_UNO_PACKAGES_DIR
));
158 else if (m_sLocation
.equalsAscii(SHARE
))
160 test
= OUString::createFromAscii(SHARE
);
161 uri
= OUString::createFromAscii(SHARE_URI
);
162 bAppendScriptsPart
= true;
164 else if (m_sLocation
.equalsAscii(SHARE_UNO_PACKAGES
))
166 test
= OUString::createFromAscii("uno_packages");
167 uri
= OUString::createFromAscii(SHARE_UNO_PACKAGES_URI
);
169 else if (m_sLocation
.indexOf(OUString::createFromAscii(TDOC_SCHEME
)) == 0)
171 m_sBaseURI
= m_sLocation
.concat( SCRIPTS_PART
);
172 m_sLocation
= OUString::createFromAscii( DOCUMENT
);
180 if ( !m_xSimpleFileAccess
->exists( uri
) ||
181 !m_xSimpleFileAccess
->isFolder( uri
) )
186 uno::Sequence
< OUString
> children
=
187 m_xSimpleFileAccess
->getFolderContents( uri
, true );
189 for ( sal_Int32 i
= 0; i
< children
.getLength(); i
++ )
191 OUString child
= children
[i
];
192 sal_Int32 idx
= child
.lastIndexOf(test
);
194 // OSL_TRACE("Trying: %s", PRTSTR(child));
195 // OSL_TRACE("idx=%d, testlen=%d, children=%d",
196 // idx, test.getLength(), child.getLength());
198 if ( idx
!= -1 && (idx
+ test
.getLength()) == child
.getLength() )
200 // OSL_TRACE("FOUND PATH: %s", PRTSTR(child));
201 if ( bAppendScriptsPart
)
203 m_sBaseURI
= child
.concat( SCRIPTS_PART
);
216 ScriptingFrameworkURIHelper::getLanguagePart(const OUString
& rStorageURI
)
220 sal_Int32 idx
= rStorageURI
.indexOf(m_sBaseURI
);
221 sal_Int32 len
= m_sBaseURI
.getLength() + 1;
225 result
= rStorageURI
.copy(idx
+ len
);
226 result
= result
.replace('/', '|');
232 ScriptingFrameworkURIHelper::getLanguagePath(const OUString
& rLanguagePart
)
235 result
= rLanguagePart
.replace('|', '/');
240 ScriptingFrameworkURIHelper::getScriptURI(const OUString
& rStorageURI
)
241 throw( lang::IllegalArgumentException
, uno::RuntimeException
)
243 ::rtl::OUStringBuffer
buf(120);
245 buf
.appendAscii("vnd.sun.star.script:");
246 buf
.append(getLanguagePart(rStorageURI
));
247 buf
.appendAscii("?language=");
248 buf
.append(m_sLanguage
);
249 buf
.appendAscii("&location=");
250 buf
.append(m_sLocation
);
252 return buf
.makeStringAndClear();
256 ScriptingFrameworkURIHelper::getStorageURI(const OUString
& rScriptURI
)
257 throw( lang::IllegalArgumentException
, uno::RuntimeException
)
259 OUString sLanguagePart
;
262 uno::Reference
< uri::XVndSunStarScriptUrl
> xURI(
263 m_xUriReferenceFactory
->parse( rScriptURI
), uno::UNO_QUERY_THROW
);
264 sLanguagePart
= xURI
->getName();
266 catch ( uno::Exception
& )
268 throw lang::IllegalArgumentException(
269 OUString::createFromAscii( "Script URI not valid" ),
270 uno::Reference
< uno::XInterface
>(), 1 );
273 ::rtl::OUStringBuffer
buf(120);
274 buf
.append(m_sBaseURI
);
275 buf
.append(OUString::createFromAscii("/"));
276 buf
.append(getLanguagePath(sLanguagePart
));
278 OUString result
= buf
.makeStringAndClear();
284 ScriptingFrameworkURIHelper::getRootStorageURI()
285 throw( uno::RuntimeException
)
291 ScriptingFrameworkURIHelper::getImplementationName()
292 throw( uno::RuntimeException
)
294 return OUString::createFromAscii(
295 "com.sun.star.script.provider.ScriptURIHelper" );
299 ScriptingFrameworkURIHelper::supportsService( const OUString
& serviceName
)
300 throw( uno::RuntimeException
)
302 OUString m_sServiceName
= OUString::createFromAscii(
303 "com.sun.star.script.provider.ScriptURIHelper" );
305 if ( serviceName
.equals( m_sServiceName
) )
312 uno::Sequence
< ::rtl::OUString
> SAL_CALL
313 ScriptingFrameworkURIHelper::getSupportedServiceNames()
314 throw( uno::RuntimeException
)
316 ::rtl::OUString serviceNameList
[] = {
317 ::rtl::OUString::createFromAscii(
318 "com.sun.star.script.provider.ScriptURIHelper" ) };
320 uno::Sequence
< ::rtl::OUString
> serviceNames
= uno::Sequence
<
321 ::rtl::OUString
> ( serviceNameList
, 1 );