cURL: follow redirects
[LibreOffice.git] / jvmaccess / source / classpath.cxx
blob8171945047a4085f781d3efc35842a18d763ec0b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include "jvmaccess/classpath.hxx"
24 #include <cassert>
25 #include <vector>
27 #include "com/sun/star/lang/IllegalArgumentException.hpp"
28 #include "com/sun/star/uno/Reference.hxx"
29 #include "com/sun/star/uno/RuntimeException.hpp"
30 #include "com/sun/star/uno/XComponentContext.hpp"
31 #include "com/sun/star/uno/XInterface.hpp"
32 #include "com/sun/star/uri/UriReferenceFactory.hpp"
33 #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
34 #include "com/sun/star/util/theMacroExpander.hpp"
35 #include "rtl/ustring.hxx"
36 #include "sal/types.h"
38 #include "jni.h"
40 jobjectArray jvmaccess::ClassPath::translateToUrls(
41 css::uno::Reference< css::uno::XComponentContext > const & context,
42 JNIEnv * environment, OUString const & classPath)
44 assert(context.is());
45 assert(environment != nullptr);
46 jclass classUrl(environment->FindClass("java/net/URL"));
47 if (classUrl == nullptr) {
48 return nullptr;
50 jmethodID ctorUrl(
51 environment->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
52 if (ctorUrl == nullptr) {
53 return nullptr;
55 ::std::vector< jobject > urls;
56 for (::sal_Int32 i = 0; i != -1;) {
57 OUString url(classPath.getToken(0, ' ', i));
58 if (!url.isEmpty()) {
59 css::uno::Reference< css::uri::XVndSunStarExpandUrlReference >
60 expUrl(
61 css::uri::UriReferenceFactory::create(context)->parse(url),
62 css::uno::UNO_QUERY);
63 if (expUrl.is()) {
64 css::uno::Reference< css::util::XMacroExpander > expander =
65 css::util::theMacroExpander::get(context);
66 try {
67 url = expUrl->expand( expander );
68 } catch (const css::lang::IllegalArgumentException & e) {
69 throw css::uno::RuntimeException(
70 "com.sun.star.lang.IllegalArgumentException: "
71 + e.Message);
74 jvalue arg;
75 arg.l = environment->NewString(
76 reinterpret_cast< jchar const * >(url.getStr()),
77 static_cast< jsize >(url.getLength()));
78 if (arg.l == nullptr) {
79 return nullptr;
81 jobject o(environment->NewObjectA(classUrl, ctorUrl, &arg));
82 if (o == nullptr) {
83 return nullptr;
85 urls.push_back(o);
88 jobjectArray result = environment->NewObjectArray(
89 static_cast< jsize >(urls.size()), classUrl, nullptr);
90 // static_cast is ok, as each element of urls occupied at least one
91 // character of the OUString classPath
92 if (result == nullptr) {
93 return nullptr;
95 jsize idx = 0;
96 for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i)
98 environment->SetObjectArrayElement(result, idx++, *i);
100 return result;
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */