我是靠谱客的博主 甜甜紫菜,最近开发中收集的这篇文章主要介绍URLBuilder简单快速构建URL链接,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

URLBuilder快速构建一个URL结构,使用了Builder建造者模式,大大增加了代码的可读性,并且可以支持参数URL编码。


package com.kaishustory.quick.commons.text;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class URLBuilder {
	private static boolean[] eucIgnore = new boolean[256];

	protected StringBuilder builder = new StringBuilder();
	protected boolean firstParam = true;
	protected boolean hasPath = false;

	private static String percentify(String s) {
		StringBuilder sb = new StringBuilder();

		ByteBuffer bb = Charset.forName("utf-8").encode(s);
		int size = bb.limit();
		for (int i = 0; i < size; i++) {
			sb.append(String.format("%%%02x", new Object[] { Integer.valueOf(bb.get() & 0xFF) }));
		}

		return sb.toString();
	}

	public static String encodeURIComponent(String s) {
		if (s == null) {
			return null;
		}
		if ("".equals(s)) {
			return "";
		}
		StringBuilder sb = new StringBuilder();

		int _i = 0;
		int c = Character.codePointAt(s, _i);
		boolean ignore = (c < 256) && (eucIgnore[c]!=false);

		for (int i = 0; i < s.length(); i++) {
			c = Character.codePointAt(s, i);
			if (ignore != ((c < 256) && (eucIgnore[c] != false))) {
				if (ignore)
					sb.append(s.substring(_i, i));
				else
					sb.append(percentify(s.substring(_i, i)));
				ignore = !ignore;
				_i = i;
			}
		}

		if (ignore)
			sb.append(s.substring(_i, s.length()));
		else {
			sb.append(percentify(s.substring(_i, s.length())));
		}

		return sb.toString();
	}

	protected void appendParamPrefix() {
		if (this.firstParam) {
			this.firstParam = false;
			if (this.hasPath)
				this.builder.append('?');
		} else {
			this.builder.append('&');
		}
	}

	public URLBuilder appendPath(String path) {
		if (path == null) {
			return this;
		}
		if ((this.hasPath) || (!this.firstParam)) {
			throw new IllegalStateException("Missed the trick to set path.");
		}
		this.hasPath = true;

		this.builder.append(path);

		return this;
	}

	public URLBuilder appendParam(String key, String value) {
		if ((key != null) && (value != null)) {
			appendParamPrefix();
			this.builder.append(key).append('=');
			this.builder.append(value);
		}

		return this;
	}

	public URLBuilder appendParamEncode(String key, String value) {
		if ((key != null) && (value != null)) {
			appendParamPrefix();
			this.builder.append(key).append('=');
			this.builder.append(encodeURIComponent(value));
		}

		return this;
	}

	public URLBuilder appendParamEncode(String key, String value, String charset) {
		appendParamEncode(key, value);

		return this;
	}

	public URLBuilder appendLabel(String label) {
		this.builder.append('#').append(label);

		return this;
	}

	public URLBuilder append(String str) {
		this.builder.append(str);

		return this;
	}

	public String toString() {
		return this.builder.toString();
	}

	static {
		String ignore = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-_.!~*'()";
		for (int i = 0; i < ignore.length(); i++)
			eucIgnore[Character.codePointAt(ignore, i)] = true;
	}
	
	public static void main(String[] args){
		URLBuilder urlBuilder = new URLBuilder();
		urlBuilder.
			appendPath("https://weixin.kaishustory.com").
			appendParam("key1", "value1").
			appendParam("key2", "value2").appendParamEncode("ekey3", "{$#你好}").appendLabel("label");
		System.err.println(urlBuilder.toString());

	}
}

//https://weixin.kaishustory.com?key1=value1&key2=value2&ekey3=%7b%24%23%e4%bd%a0%e5%a5%bd%7d#label

最后

以上就是甜甜紫菜为你收集整理的URLBuilder简单快速构建URL链接的全部内容,希望文章能够帮你解决URLBuilder简单快速构建URL链接所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部