简体   繁体   English

如何获取Jsoup白名单的有效标签列表?

[英]How to get list of valid tags of Jsoup whitelist?

How do I get a list of all the valid tags of a given Jsoup Whitelist ? 如何获得给定Jsoup Whitelist名单的所有有效标签的列表?

I can't find such a function in the docs at Jsoup whitelist docs . 我在Jsoup白名单docs的docs中找不到这样的功能。 I use ColdFusion, but a java solution or hint would be fine. 我使用ColdFusion,但是可以使用Java解决方案或提示。 I guess I could translate it. 我想我可以翻译。

You can check here that, what you are asking for is the tagNames set. 您可以在此处检查,要求的是设置的tagNames The class doesn't provide any getter. 该类不提供任何获取方法。

What you can do is: 您可以做的是:

  1. Download the source code of jsoup and just edit the Whitelist class and add a getter. 下载jsoup的源代码,然后编辑Whitelist类并添加一个getter。 You can even make a pull request after that. 之后,您甚至可以提出拉取请求。
  2. Take the default tags of each whitelist category and keep them in variables that are accessible to you. 取得每个白名单类别的默认标签,并将其保留在您可以访问的变量中。
  3. The last alternative would be to use reflection in order to gain access to a private variable, but that's not a good practice, since there are other, cleaner ways to achieve what you want. 最后一种选择是使用反射来获得对私有变量的访问权限,但这不是一个好习惯,因为还有其他更清洁的方法可以实现所需的目标。

If you want to go the reflection route, you can do something like below which grabs access to the tagNames set, converts it to an array of org.jsoup.safety.Whitelist$TagName objects (which contain the tag names) and then appends the toString() values of those objects to another array. 如果您想进行反思,可以执行以下操作,以获取对tagNames集合的访问,将其转换为org.jsoup.safety.Whitelist$TagName对象(包含标签名称)的数组,然后附加将这些对象的toString()值添加到另一个数组。

<cfscript>

whitelist = createObject("java", "org.jsoup.safety.Whitelist");
collection = [];
tags = whitelist.getClass().getDeclaredField("tagNames");
tags.setAccessible(true);

// this portion uses the relaxed whitelist as an example
for (tag in tags.get(whitelist.relaxed()).toArray()) {
    arrayAppend(collection, tag.toString());
}

writeDump(collection);

</cfscript>

If you need the attributes and/or protocols fields, it's a similar approach but there is more to iterate through since they are maps. 如果您需要属性和/或协议字段,这是一种类似的方法,但是由于它们是映射,因此还有很多要遍历的地方。

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

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