Skip to content

Commit

Permalink
Merge pull request #6 from 1c-syntax/fix/brackets-in-uri
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 authored Jul 14, 2021
2 parents 9202a75 + f93795f commit 51afbf2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
49 changes: 41 additions & 8 deletions src/main/java/com/github/_1c_syntax/utils/Absolute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is a part of 1c-syntax utils.
*
* Copyright © 2018-2020
* Copyright © 2018-2021
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
Expand All @@ -25,24 +25,41 @@
import lombok.experimental.UtilityClass;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

@UtilityClass
public final class Absolute {

public static URI uri(String uri) {
return uri(URI.create(uri));
try {
URL url = new URL(uri);
var decodedPath = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8);
var decodedUri = new URI(
url.getProtocol(),
url.getUserInfo(),
url.getHost(),
url.getPort(),
decodedPath,
url.getQuery(),
url.getRef()
);

return checkFileAuthorityAndReturnURI(decodedUri);
} catch (MalformedURLException | URISyntaxException e) {
return uri(URI.create(uri));
}
}

public static URI uri(URI uri) {
var decodedUri = URI.create(uri.getScheme() + ":" + uri.getSchemeSpecificPart().replace(" ", "%20"));
var decodedUri = URI.create(uri.getScheme() + ":" + encodePath(uri.getSchemeSpecificPart()));

if ("file".equals(decodedUri.getScheme()) && decodedUri.getAuthority() == null) {
return path(new File(decodedUri)).toUri();
}

return decodedUri;
return checkFileAuthorityAndReturnURI(decodedUri);
}

public static URI uri(File file) {
Expand All @@ -65,4 +82,20 @@ public static Path path(Path path) {
public static Path path(File file) {
return file.getCanonicalFile().toPath().toAbsolutePath();
}

private static String encodePath(String path) {
return path
.replace(" ", "%25")
.replace("[", "%91")
.replace("]", "%93")
;
}

private static URI checkFileAuthorityAndReturnURI(URI uri) {
if ("file".equals(uri.getScheme()) && uri.getAuthority() == null) {
return path(new File(uri)).toUri();
}

return uri;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is a part of 1c-syntax utils.
*
* Copyright © 2018-2020
* Copyright © 2018-2021
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/_1c_syntax/utils/Lazy.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is a part of 1c-syntax utils.
*
* Copyright © 2018-2020
* Copyright © 2018-2021
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
Expand Down
15 changes: 14 additions & 1 deletion src/test/java/com/github/_1c_syntax/utils/AbsoluteTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is a part of 1c-syntax utils.
*
* Copyright © 2018-2020
* Copyright © 2018-2021
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
Expand Down Expand Up @@ -155,4 +155,17 @@ void testUriFromStringWithSpaces() {
// then
assertThat(uri.getPath()).endsWith("fake.bsl");
}

@Test
void testUriFromStringWithBrackets() {
// given
var uriString = "file://server/fake%20path_кириллица/[some]fake.bsl";

// when
var uri = Absolute.uri(uriString);

// then
assertThat(uri.toString()).doesNotContain("[");
assertThat(uri.toString()).doesNotContain("]");
}
}

0 comments on commit 51afbf2

Please sign in to comment.