简体   繁体   English

java.net.URI.relativize不适用于JAR URI

[英]java.net.URI.relativize doesn't work with JAR URIs

I have two URI objects. 我有两个URI对象。 One is pointing to a folder in a JAR file, and another is pointing to a file in the same JAR file. 一个指向JAR文件中的文件夹,另一个指向同一JAR文件中的文件。 The file is in a subfolder of the directory specified by the first URI. 该文件位于第一个URI指定的目录的子文件夹中。 I like to create a relative URI so the resulting URI only containing the relative path to the file in the JAR. 我喜欢创建一个相对URI,因此生成的URI只包含JAR中文件的相对路径。

  • Folder URI 文件夹URI

     jar:file:/C:/Users/inagy/.m2/repository/hu/inagy/my-config-artifact/2.0-SNAPSHOT/my-config-artifact-2.0-SNAPSHOT.jar!/conf/ 
  • Resource URI 资源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 
  • After calling folderUri.relativize(resourceURI) i'm expecting the following URI as a result: 在调用folderUri.relativize(resourceURI)我期待以下URI作为结果:

     somesubpath/someconfig.xml 

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. 但是我得到了resourceURI,这意味着根据URI类的Javadoc ,JDK代码发现这两条路径彼此不相对。

Is this a bug or i'm doing something wrong? 这是一个错误还是我做错了什么?

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. 答案与JAR文件的语义无关 - 它与jar: URIs的语法有关。 To be "relativized," a URI has to be hierarchical and not opaque . 要“相对化”,URI必须是分层的而不是不透明的 You will note from the JavaDoc for java.net.URI that: 您将从JavaDoc for java.net.URI中注意到:

At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax 在最高级别,字符串形式的URI引用(下文中简称为“URI”)具有语法

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

... ...

An opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character ('/'). 不透明URI是绝对URI,其特定于节点的部分不以斜杠字符('/')开头。 Opaque URIs are not subject to further parsing. 不透明的URI不需要进一步解析。

... ...

A hierarchical URI is subject to further parsing according to the syntax 分层URI根据语法进行进一步解析

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

A JAR URI like jar:file:///home/me/foo.jar!/conf/ is parsed as: jar:file:///home/me/foo.jar!/conf/这样的JAR URI被解析为:

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

Because the scheme-specific-part does not begin with " / ," it cannot be considered a hierarchical URI. 由于特定于方案的部分不以“ / ”开头,因此不能将其视为分层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. URI类没有把jar: (或)特别的URI,所以它无法识别该file://部分是一个嵌套的URI 分层的。

Since jar: URIs are opaque and not hierarchical, they are subject to the behavior documented for URI#relativize() : 由于jar: URI是不透明的而不是分层的,因此它们受URI#relativize()记录的行为的影响:

The relativization of the given URI against this URI is computed as follows: 给定URI对此URI的相对化计算如下:

  1. If either this URI or the given URI are opaque, [...], then the given URI is returned. 如果此URI或给定URI是不透明的,[...],则返回给定的URI。

EDIT : Left out a crucial part about opaque URIs. 编辑 :遗漏了关于不透明URI的关键部分。

somesubpath/someconfig.xml is not an URI; somesubpath/someconfig.xml不是URI; the protocol (like file: ) is missing. 协议(如file: :)缺失。 In fact only a full URL with jar:file:/C:/Users/inagy/.m2/repository/hu/inagy/my-config-artifact/2.0-SNAPSHOT/my-config-artifact-2.0-SNAPSHOT.jar!somesubpath/someconfig.xml would be imaginable, but the jar-protocol does not hande base documents/dirs, like /conf . 实际上只有一个带jar:file:/C:/Users/inagy/.m2/repository/hu/inagy/my-config-artifact/2.0-SNAPSHOT/my-config-artifact-2.0-SNAPSHOT.jar!somesubpath/someconfig.xml的完整URL jar:file:/C:/Users/inagy/.m2/repository/hu/inagy/my-config-artifact/2.0-SNAPSHOT/my-config-artifact-2.0-SNAPSHOT.jar!somesubpath/someconfig.xml是可以想象的,但是jar协议不会像/conf那样处理基础文档/目录。

So you can only use absolute paths. 所以你只能使用绝对路径。 A consequence of URI standing for Unique Resource Identifier. URI代表唯一资源标识符的结果。

There is no base document (.html) with relative documents (.js). 没有带有相关文档(.js)的基本文档(.html)。


An URI is to retrieve data. URI是检索数据。 How would one write a relative URI for a file inside a jar? 如何为jar中的文件写一个相对URI? Relative to where? 相对于哪里? As the "jar:" protocol is only used inside java code, there is no context of "current file". 由于“jar:”协议仅在java代码中使用,因此没有“当前文件”的上下文。 So relative URIs are not implemented. 所以没有实现相对URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM