LibreOffice Module jvmaccess (master) 1
classpath.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
23
24#include <cassert>
25#include <vector>
26
27#include <com/sun/star/lang/IllegalArgumentException.hpp>
28#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
29#include <com/sun/star/uno/Reference.hxx>
30#include <com/sun/star/uno/XInterface.hpp>
31#include <com/sun/star/uri/UriReferenceFactory.hpp>
32#include <com/sun/star/uri/XVndSunStarExpandUrlReference.hpp>
33#include <com/sun/star/util/theMacroExpander.hpp>
35#include <rtl/ustring.hxx>
36#include <sal/types.h>
37#include <o3tl/string_view.hxx>
38
39#include <jni.h>
40
41namespace com::sun::star::uno { class XComponentContext; }
42
44 css::uno::Reference< css::uno::XComponentContext > const & context,
45 JNIEnv * environment, std::u16string_view classPath)
46{
47 assert(context.is());
48 assert(environment != nullptr);
49 jclass classUrl(environment->FindClass("java/net/URL"));
50 if (classUrl == nullptr) {
51 return nullptr;
52 }
53 jmethodID ctorUrl(
54 environment->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
55 if (ctorUrl == nullptr) {
56 return nullptr;
57 }
58 ::std::vector< jobject > urls;
59 for (::sal_Int32 i = 0; i != -1;) {
60 OUString url(o3tl::getToken(classPath, 0, ' ', i));
61 if (!url.isEmpty()) {
62 css::uno::Reference< css::uri::XVndSunStarExpandUrlReference >
63 expUrl(
64 css::uri::UriReferenceFactory::create(context)->parse(url),
65 css::uno::UNO_QUERY);
66 if (expUrl.is()) {
67 css::uno::Reference< css::util::XMacroExpander > expander =
68 css::util::theMacroExpander::get(context);
69 try {
70 url = expUrl->expand( expander );
71 } catch (const css::lang::IllegalArgumentException & e) {
72 css::uno::Any anyEx = cppu::getCaughtException();
73 throw css::lang::WrappedTargetRuntimeException(
74 "com.sun.star.lang.IllegalArgumentException: "
75 + e.Message,
76 nullptr, anyEx );
77 }
78 }
79 jvalue arg;
80 arg.l = environment->NewString(
81 reinterpret_cast< jchar const * >(url.getStr()),
82 static_cast< jsize >(url.getLength()));
83 if (arg.l == nullptr) {
84 return nullptr;
85 }
86 jobject o(environment->NewObjectA(classUrl, ctorUrl, &arg));
87 if (o == nullptr) {
88 return nullptr;
89 }
90 urls.push_back(o);
91 }
92 }
93 jobjectArray result = environment->NewObjectArray(
94 static_cast< jsize >(urls.size()), classUrl, nullptr);
95 // static_cast is ok, as each element of urls occupied at least one
96 // character of the OUString classPath
97 if (result == nullptr) {
98 return nullptr;
99 }
100 jsize idx = 0;
101 for (auto const& url : urls)
102 {
103 environment->SetObjectArrayElement(result, idx++, url);
104 }
105 return result;
106}
107
108/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_uInt16 idx[]
Any SAL_CALL getCaughtException()
int i
JVMACCESS_DLLPUBLIC jobjectArray translateToUrls(css::uno::Reference< css::uno::XComponentContext > const &context, JNIEnv *environment, std::u16string_view classPath)
translates a class path into a java.net.URL[] instance.
Definition: classpath.cxx:43
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
bool parse(OUString const &uri, SourceProviderScannerData *data)
Any result