URI 引用包括最多三个部分:模式、模式特定部分和片段标识符。一般为:
模式:模式特定部分:片段
如果省略模式,这个URI引用则是相对的。如果省略片段标识符,这个URI引用就是一个纯URI。

URI是对URL的抽象,不仅包括统一资源定位符URL,还包括统一资源名URN。实际上使用的URI大多都是URL。在java中,URI使用java.net.URI类表示,URI类只能标识资源,和解析URI,而不能获取URI所标识的资源(URN是无法定位到资源的)。

构造

public URI(String str) throws URISyntaxException {
new Parser(str).parse(false);
} public URI(String scheme, String host, String path, String fragment)
throws URISyntaxException{
this(scheme, null, host, -1, path, null, fragment);
} public URI(String scheme,
String authority,
String path, String query, String fragment)
throws URISyntaxException{
....
} public URI(String scheme,
String userInfo, String host, int port,
String path, String query, String fragment)
throws URISyntaxException{
...
} public URI(String scheme, String ssp, String fragment) throws URISyntaxException {
new Parser(toString(scheme, ssp, null, null, null, -1, null, null, fragment)).parse(false);
}

URI类提供了5中构造方法

  1. 根据提供的一个uri字符串构造一个URI对象。
  2. 主要针对层次的URI。通过 模式、服务器地址、文件路径、片段标识构造URI。
  3. 主要针对层次的URI。通过 模式、授权机构、文件路径、查询条件、片段标识构造URI。
  4. 主要针对层次的URI。通过 模式、用户信息、服务器地址、端口、文件路径、查询条件、片段标识构造URI。
  5. 主要针对非层次URI。通过 模式、模式特定部分和片段标识创建URI。

create方法

public static URI create(String str) {
try {
return new URI(str);
} catch (URISyntaxException x) {
throw new IllegalArgumentException(x.getMessage(), x);
}
}

如果可以确认URI的格式正确,可使用create的工厂方法创建URI。因为该方法不会抛出URISyntaxException异常。

是否透明URL

URI通常情况下都是层次(带“/”)的,但是也有不透明(没有“/”)的,层次的URI包含模式,主机,站点等各个部分,当然可能某些部分不包含,但是不透明的URI只包含三个部分,Scheme,Scheme-specific-part,Fragment.
如:mailto:jijianshuai@infcn.com.cn

public boolean isOpaque() {
return path == null;
}

判断path是否为空,如果为空则是不透明的,说明URI中没有“/”。

 
Paste_Image.png

在URI构造器中解析URI,代码:new Parser(str).parse(false);

判断URI中是否存在“/”符号,如果存在就是有层次结构的URI。
如果存在“/”,则调用parseAuthority方法进行解析path。

URI获取各部分信息

  1. 获得模式
    public String getScheme();
  2. 获得模式特定部分
    public String getSchemeSpecificPart();
    public String getRawSchemeSpecificPart();
  3. 获得片段
    public String getFragment();
    public String getRawFragment();
  4. 获得授权机构
    public String getAuthority();
    public String getRawAuthority()
    授权机构包括:用户信息、服务器地址(域名或ip)、端口
    user:password@localhost:80
  5. 获取片段标识
    public String getFragment()
    public String getRawFragment()
  6. 获取服务器地址(域名或ip)
    public String getHost()
  7. 获取路径
    public String getPath()
    public String getRawPath()
    路径包括(目录结构和文件部分)。如:/dir/index.html
  8. 获取端口
    public int getPort()
    如果没有端口则返回-1;
  9. 获取URI的查询字符串
    public String getQuery()
    public String getRawQuery()
  10. 获取用户信息
    public String getUserInfo()
    public String getRawUserInfo()

如果URI是非透明只能获取到1~3个信息。
如果URI是层次结构则能获取所有信息。

方法中带Raw的,是获取编码后的URI部分信息。非ascii的字符需要进行编码,不带Raw的方法是解码后的信息。

getScheme、getHost、getPort这三个方法没有Raw方法,是因为这三部分不会出现非ascii的字符。

resolve 方法

resolve方法可以将相对URI转换成绝对URI。示例如下:

URI a = URI.create("http://localhost:8080/index.html");
URI b = URI.create("user/userInfo.html");
URI c = a.resolve(b);
System.out.println(c);

根据a获取b的绝对路径

打印结果为:http://localhost:8080/user/userInfo.html

源码如下
public URI resolve(URI uri) {
return resolve(this, uri);
} private static URI resolve(URI base, URI child) {
// check if child if opaque first so that NPE is thrown
// if child is null.
if (child.isOpaque() || base.isOpaque())
return child; // 5.2 (2): Reference to current document (lone fragment)
if ((child.scheme == null) && (child.authority == null)
&& child.path.equals("") && (child.fragment != null)
&& (child.query == null)) {
if ((base.fragment != null) && child.fragment.equals(base.fragment)) {
return base;
}
URI ru = new URI();
ru.scheme = base.scheme;
ru.authority = base.authority;
ru.userInfo = base.userInfo;
ru.host = base.host;
ru.port = base.port;
ru.path = base.path;
ru.fragment = child.fragment;
ru.query = base.query;
return ru;
} // 5.2 (3): Child is absolute
if (child.scheme != null)
return child;
URI ru = new URI(); // Resolved URI
ru.scheme = base.scheme;
ru.query = child.query;
ru.fragment = child.fragment; // 5.2 (4): Authority
if (child.authority == null) {
ru.authority = base.authority;
ru.host = base.host;
ru.userInfo = base.userInfo;
ru.port = base.port; String cp = (child.path == null) ? "" : child.path;
if ((cp.length() > 0) && (cp.charAt(0) == '/')) {
// 5.2 (5): Child path is absolute
ru.path = child.path;
} else {
// 5.2 (6): Resolve relative path
ru.path = resolvePath(base.path, cp, base.isAbsolute());
}
} else {
ru.authority = child.authority;
ru.host = child.host;
ru.userInfo = child.userInfo;
ru.host = child.host;
ru.port = child.port;
ru.path = child.path;
} // 5.2 (7): Recombine (nothing to do here)
return ru;
}
  1. 是否是非透明URI,如果是,则直接返回child。
  2. 判断child是否只有fragment(片段标识)。如果child只有片段标识则执行2.1。否则执行3。
    2.1 如果child的fragment和base的片段标识一样,就直接返回base的url
    2.2 把base不包含fragment的部分和child的fragment构造一个新的URI返回。
  3. 判断child的scheme不为空则直接返回child。不为空说明他是绝对路径。
  4. 根据base的URI各部分构造child的绝对路径URI并返回。

relativize 方法

relativize 方法可以将绝对路径的URI转换成相对路径的URI。

URI a = URI.create("http://localhost:8080/");
URI b = URI.create("http://localhost:8080/index.html");
URI c = a.relativize(b);
System.out.println(c);

获取b相对a的相对路径。

打印的结果为:index.html

private static URI relativize(URI base, URI child) {
// check if child if opaque first so that NPE is thrown
// if child is null.
if (child.isOpaque() || base.isOpaque())
return child;
if (!equalIgnoringCase(base.scheme, child.scheme)
|| !equal(base.authority, child.authority))
return child; String bp = normalize(base.path);
String cp = normalize(child.path);
if (!bp.equals(cp)) {
if (!bp.endsWith("/"))
bp = bp + "/";
if (!cp.startsWith(bp))
return child;
} URI v = new URI();
v.path = cp.substring(bp.length());
v.query = child.query;
v.fragment = child.fragment;
return v;
}
  1. 判断child是否不是透明URI,如果不是则直接返回child。不是层次结构的uri是没有相对路径的。
  2. 判断两个URI的scheme和授权机构是否不同,如果不同则直接返回child。
  3. 判断base是否“/”结尾,如果不是则加上“/”
  4. 判断child是否以base开头,如果不是则直接返回child。
  5. 返回child中,不包含base的部分,构造一个新URI返回。

作者:jijs
链接:https://www.jianshu.com/p/24a2da876372
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

[转载]URL 源码分析的更多相关文章

  1. [转载]URI 源码分析

    需要提前了解下什么是URI,及URI和URL的区别: URI. URL 和 URN 的区别 URI 引用包括最多三个部分:模式.模式特定部分和片段标识符.一般为: 模式:模式特定部分:片段 如果省略模 ...

  2. [转载] Netty源码分析

    转载自http://blog.csdn.net/kobejayandy/article/details/11836813 Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高 ...

  3. ArrayList实现原理及源码分析之JDK8

    转载 ArrayList源码分析 一.ArrayList介绍 Java 集合框架主要包括两种类型的容器: 一种是集合(Collection),存储一个元素集合. 一种是图(Map),存储键/值对映射. ...

  4. Heritrix源码分析(九) Heritrix的二次抓取以及如何让Heritrix抓取你不想抓取的URL

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/644396       本博客已迁移到本人独立博客: http://www.yun5u ...

  5. EasyUI学习总结(三)——easyloader源码分析(转载)

    声明:这一篇文章是转载过来的,转载地址忘记了,原作者如果看到了,希望能够告知一声,我好加上去! easyloader模块是用来加载jquery easyui的js和css文件的,而且它可以分析模块的依 ...

  6. springMVC源码分析--AbstractHandlerMethodMapping注册url和HandlerMethod对应关系(十一)

    在上一篇博客springMVC源码分析--AbstractHandlerMethodMapping获取url和HandlerMethod对应关系(十)中我们简单地介绍了获取url和HandlerMet ...

  7. springMVC源码分析--AbstractHandlerMethodMapping获取url和HandlerMethod对应关系(十)

    在之前的博客springMVC源码分析--AbstractHandlerMapping(二)中我们介绍了AbstractHandlerMethodMapping的父类AbstractHandlerMa ...

  8. 转载-HashMap1.7源码分析

    原文地址-https://www.cnblogs.com/chengxiao/p/6059914.html HashMap实现原理及源码分析   哈希表(hash table)也叫散列表,是一种非常重 ...

  9. jQuery源码分析系列(转载来源Aaron.)

    声明:非本文原创文章,转载来源原文链接Aaron. 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://github.com/JsAa ...

随机推荐

  1. (51)Wangdao.com第七天_JavaScript 编写位置及输出语句

    JavaScript 编写位置 编写在html内部标签的属性中 不推荐使用,因为结构和行为耦合,不便于维护 主要有  <button onclick="alert('点我干哈!');& ...

  2. [LeetCode] Friends Of Appropriate Ages 适合年龄段的朋友

    Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ...

  3. poj 1375

    一道解析几何么,,, 其实就是求直线与圆的切线. 看到方法有很多,比如根据角度之类的. 这里主要用到了初中的几何知识. 考虑这幅图. 首先可以根据相似三角形知道b的长度,同时圆心与点的方向也知道. 那 ...

  4. sqlserver 表循环-游标、表变量、临时表

    SQL Server遍历表的几种方法 阅读目录 使用游标 使用表变量 使用临时表 在数据库开发过程中,我们经常会碰到要遍历数据表的情形,一提到遍历表,我们第一印象可能就想到使用游标,使用游标虽然直观易 ...

  5. App Store评论优化,让你的APP评论上涨

    App Store评论优化怎么做 App Store评论优化,让你的APP评论上涨 关于「ASO评论优化」,主要分为三块.换评论,买评论,引导用户写评论. 可能有些刚接触ASO的朋友会问,为什么要给A ...

  6. 简述移动端开发前端和app间的关系

    <p>前端页面嵌套进app内部,一般有时候会进行一些交互,类似于前端页面请求后台接口一样,通常会起一个前端开发人员和app开发人员会相互协定一个协议;双方就协议而言去进行请求接口和返回数据 ...

  7. BUAA-OO-第一单元表达式求导作业总结

    figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...

  8. [vue开发记录]全局loading组件

    上图不上种,菊花万人捅: loading.js: import './loading.css' let Loading = {} // 避免重复install,设立flag Loading.insta ...

  9. org.hibernate.HibernateException: Duplicate identifier in table for: Waa

    提示表的标识符重复,发现是数据库中的主键id重复了.因为是序列自动生成的. 我原本以为是因为我的序列的问题,序列.nextval()有问题,但是当我在数据库测试时,发现当前序列没有问题.但是当数据插入 ...

  10. Google Colab Free GPU Tutorial【转载】

    转自:https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d 1.Google Cola ...