java.net.URI.relativize不适用于JAR URI [英] java.net.URI.relativize doesn't work with JAR URIs

查看:226
本文介绍了java.net.URI.relativize不适用于JAR URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个URI对象。一个指向JAR文件中的文件夹,另一个指向同一JAR文件中的文件。该文件位于第一个URI指定的目录的子文件夹中。我喜欢创建一个相对URI,因此生成的URI只包含JAR中文件的相对路径。

I have two URI objects. One is pointing to a folder in a JAR file, and another is pointing to a file in the same JAR file. The file is in a subfolder of the directory specified by the first URI. I like to create a relative URI so the resulting URI only containing the relative path to the file in the JAR.


  • 文件夹URI

  • Folder URI

jar:file:/C:/Users/inagy/.m2/repository/hu/inagy/my-config-artifact/2.0-SNAPSHOT/my-config-artifact-2.0-SNAPSHOT.jar!/conf/


  • 资源URI

  • Resource URI

    jar:file:/C:/Users/inagy/.m2/repository/hu/inagy/my-config-artifact/2.0-SNAPSHOT/my-config-artifact-2.0-SNAPSHOT.jar!/conf/somesubpath/someconfig.xml
    


  • 在调用 folderUri.relativize(resourceURI)后,我希望得到以下URI:

  • After calling folderUri.relativize(resourceURI) i'm expecting the following URI as a result:

    somesubpath/someconfig.xml
    


  • 然而,根据 URI类的Javadoc 即JDK代码发现这两条路径彼此不相对。

    However i get resourceURI back which mean according to the URI class's Javadoc that the JDK code find this two paths non relative to each other.

    这是一个错误还是我做错了什么?

    Is this a bug or i'm doing something wrong?

    推荐答案

    我也对此感到有点恼火。答案是没有对JAR文件的语义做了什么 - 它与 jar: URI的语法有关。要进行相对化,URI必须分层且不透明。您将从JavaDoc for java.net.URI中注意到:

    I've been mildly annoyed by this too. The answer has nothing do to with the semantics of JAR files—it has to do with the syntax of jar: URIs. To be "relativized," a URI has to be hierarchical and not opaque. You will note from the JavaDoc for java.net.URI that:


    最高级别的URI引用(以下简称URI)字符串形式的语法

    At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax

    [scheme:]scheme-specific-part[#fragment]
    

    ...

    不透明的URI是绝对URI,其方案 - 特定部分不以斜杠字符('/')开头。不透明的URI不需要进一步解析。

    An opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character ('/'). Opaque URIs are not subject to further parsing.

    ...

    分层URI需要根据语法进一步解析

    A hierarchical URI is subject to further parsing according to the syntax

    [scheme:][//authority][path][?query][#fragment]
    


    JAR URI,如 jar:file:///home/me/foo.jar !/ conf / 被解析为:

    A JAR URI like jar:file:///home/me/foo.jar!/conf/ is parsed as:


    • scheme = jar

    • scheme-specific-part = file:///home/me/foo.jar!/ conf /

    • fragment =(none)

    因为特定于方案的部分不是以 / ,它不能被视为分层URI。 URI类不会特别处理 jar:(或任何)URI,因此它无法识别 file:// part是 层次结构的嵌套URI。

    Because the scheme-specific-part does not begin with "/," it cannot be considered a hierarchical URI. The URI class does not treat jar: (or any) URIs specially, so it cannot recognize that the file:// part is a nested URI that is hierarchical.

    因为 jar: URI是不透明且不分层,它们受 URI#relativize()

    Since jar: URIs are opaque and not hierarchical, they are subject to the behavior documented for URI#relativize():


    给定URI对此的相对化URI计算为
    如下:

    The relativization of the given URI against this URI is computed as follows:


    1. 如果此URI或给定的URI不透明,[...],则返回给定的URI。


    编辑:遗漏了一个关键部分关于不透明的URI。

    EDIT: Left out a crucial part about opaque URIs.

    这篇关于java.net.URI.relativize不适用于JAR URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆