-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from 1c-syntax/fix/brackets-in-uri
- Loading branch information
Showing
4 changed files
with
57 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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) { | ||
|
@@ -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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/com/github/_1c_syntax/utils/CaseInsensitivePattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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("]"); | ||
} | ||
} |