用于从模式生成URL的Java库 [英] Java library to generate URLs from a pattern

查看:106
本文介绍了用于从模式生成URL的Java库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何免费的Java库来自动执行以下过程:
1)一个提供遵循特定模式的URL,例如

I want to know if there is any free Java library to automate the following process: 1) One provides an URL that follows a specific pattern, e.g.

http://www.asite.com/path/to/something/thischange/alsothischange-andthischangetoo

其中一个从上面的字符串中指定:

where one specify from the above string that:


  • thischange 是在[0-10]范围内定义的整数;

  • alsothischange 是一个月,然后是set {jan,....,dec};

  • andthischangetoo 是一个在[0-1000]范围内定义的整数;

  • thischange is an integer defined within range [0-10];
  • alsothischange is a month, then is in set {jan, ...., dec};
  • andthischangetoo is an integer defined in range [0-1000];

2)给定模式,库会生成所有可能的URL,例如

2) Given a pattern, the library generates all the possile URLs, e.g.

http://www.asite.com/path/to/something/0/jan-0
http://www.asite.com/path/to/something/1/jan-0
http://www.asite.com/path/to/something/2/jan-0
...

显然,我可以自己开发代码,但如果有的话可用会更好。

Obviously, I can develop the code by myself, but if there is something available would be better.

推荐答案

免责声明:我是作者,但是......

Disclaimer: I am the author, but...

您可以尝试此库。它是RFC 6570(URI模板)的实现。平心而论,我应该提到另一个实现,它具有更好的API但是更多依赖项(我的只依赖于Guava)。

You can try this library. It is an implementation of RFC 6570 (URI templates). In all fairness, I should mention that there exists another implementation, which has a nicer API but a lot more dependencies (mine only depends on Guava).

假设你有变量 int1 int2 ,您的模板将是:

Let's say you have variables int1, int2, month, your template would be:

http://www.asite.com/path/to/something/{int1}/{month}-{int2}

使用该库,您可以执行以下操作:

Using the library, you can do something like this:

// Since the lib depends on Guava, might as well use that
final List<String> months = ImmutableList.of("jan", "feb", "etc");

// Create the template
final URITemplate template 
    = new URITemplate("http://www.asite.com/path/to/something/{int1}/{month}-{int2}");

// Variable values
VariableValue int1, month, int2;

// Expansion data
Map<String, VariableValue> data;

// Build the strings
for (int i1 = 0; i1 <= 10; i1++)
    for (final String s: months)
        for (int i2 = 0; i2 <= 1000; i2++) {
            int1 = new ScalarValue(Integer.toString(i1));
            month = new ScalarValue(s);
            int2 = new ScalarValue(Integer.toString(i2));
            data = ImmutableMap.of("int1", int1, "month", month, "int2", int2);
            // Print the template
            System.out.println(template.expand(data));
        }

重要提示: .expand()方法返回 String ,而不是 URI URL 。原因是虽然RFC保证了扩展结果,但它不能保证结果字符串实际上是URI或URL。你必须把这个字符串变成你想要的东西。

IMPORTANT NOTE: the .expand() method returns a String, not a URI or a URL. The reason is that while the RFC guarantees expansion results, it cannot guarantee that the resulting string is actually a URI or URL. You'll have to turn that string into what you want by yourself.

这篇关于用于从模式生成URL的Java库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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