Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.HTML for Java 包含 com.aspose.html.accessibility 软件包,用于执行和管理网络辅助功能检查。
本文介绍了如何根据 WCAG 标准查看验证结果、报告测试标准并详细说明验证过程中遇到的具体问题。
要了解如何以编程方式检查网站的可访问性并获得分步指南,请阅读文章 Accessibility Validator – Website Accessibility Check in Java。
com.aspose.html.accessibility.results 包包含描述网络辅助功能规则验证结果的类。 ValidationResult 类是一个主类,它包含从 AccessibilityRules 对象检查所有标准的结果。
下面是一个网络可访问性检查的 Java 代码片段。它使用特定设置设置了一个验证器,验证 HTML 文档,并将结果输出到控制台,让您可以识别并解决 HTML 文档中的特定问题:
1// Validate HTML against WCAG rules using Java
2
3// Initialize a webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Create an accessibility validator with static instance for all rules
7// from repository that match the builder settings
8AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());
9
10// Prepare a path to a source HTML file
11String documentPath = "input.html";
12
13// Initialize an object of the HTMLDocument class
14final HTMLDocument document = new HTMLDocument(documentPath);
15ValidationResult validationResult = validator.validate(document);
16
17// Checking for success
18if (!validationResult.getSuccess()) {
19 // Get a list of RuleValidationResult Details
20 for (RuleValidationResult detail : validationResult.getDetails()) {
21 System.out.println(String.format("%s: %s = %s",
22 detail.getRule().getCode(),
23 detail.getRule().getDescription(),
24 detail.getSuccess()));
25 }
26}RuleValidationResult 类表示特定无障碍规则的验证结果。它提供了有关如何评估规则的详细信息,包括一个 ITechniqueResult 元素集合,每个元素都代表用于满足成功标准的方法。
该类揭示了几个关键属性(方法):
以下代码会遍历一个 validationResult 对象的 Details 属性中的 RuleValidationResult 对象列表。每个 RuleValidationResult 表示验证过程中特定可访问性规则检查的结果:
1 // Get a list of RuleValidationResult Details
2 for (RuleValidationResult result : validationResult.getDetails())
3 {
4 System.out.println(String.format("%s: %s = %s",
5 result.getRule().getCode(), result.getRule().getDescription(), result.getSuccess()));
6 }假设 Details 包含两条规则的结果 – H37 和 H67。输出将是
1H37:Check alt attributes for images = True
2H67:Check that all forms have labels = FalseITechniqueResult是一个公共接口,它包含了检查用于满足规则成功标准的特定无障碍技术的结果信息。它包括一个用于识别相关规则的 getRule() 方法、一个用于指示该技术是否通过的 getSuccess() 方法,以及一个返回 IError() 对象的 getError() 方法,该对象包含在检查过程中遇到的任何问题的详细信息。该接口有助于将规则检查分解为单个技术结果,以便进行更详细的分析。
下面是未通过成功标准测试的结果输出示例。我们将向控制台打印标准的代码说明,以及这些标准方法的所有执行细节,包括成功和错误:
1// Validate HTML accessibility using Java and get detailed failed rule results
2
3// Initialize a webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Create an accessibility validator with static instance for all rules
7// from repository that match the builder settings
8AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());
9
10String documentPath = "input.html";
11
12// Initialize an object of the HTMLDocument class
13final HTMLDocument document = new HTMLDocument(documentPath);
14ValidationResult validationResult = validator.validate(document);
15
16// Take a list of rules results
17for (RuleValidationResult ruleResult : validationResult.getDetails()) {
18 // List only unsuccessful rule
19 if (!ruleResult.getSuccess()) {
20 // Print the code and description of the rule
21 System.out.println(String.format("%s: %s",
22 ruleResult.getRule().getCode(),
23 ruleResult.getRule().getDescription()
24 ));
25
26 // Print the results of all methods
27 for (ITechniqueResult ruleDetail : ruleResult.getResults()) {
28 // Print the code and description of the criterions
29 StringBuilder str = new StringBuilder(String.format("\n{0}: {1} - {2}",
30 ruleDetail.getRule().getCode(),
31 ruleDetail.getSuccess(),
32 ruleDetail.getRule().getDescription()
33 ));
34 System.out.println(str);
35 }
36 }
37}验证过程完成后,您可以保存验证结果,以便进一步分析、记录和报告。Aspose.HTML for Java 库允许您将验证结果保存到 “TextWriter “对象中,其中的 ValidationResultSaveFormat类型参数指定了文本的保存格式。
有三种主要格式可用于保存网络可访问性验证结果:
将验证结果保存为字符串时,使用 saveToString() 方法:
1// Validate HTML for accessibility in Java and export all errors and warnings as a string
2
3String htmlPath = "input.html";
4
5final HTMLDocument document = new HTMLDocument(htmlPath);
6AccessibilityValidator validator = new WebAccessibility().createValidator();
7ValidationResult validationresult = validator.validate(document);
8
9// get rules errors in string format
10String content = validationresult.saveToString();
11
12// SaveToString - return only errors and warnings
13// if everything is ok, it will return "validationResult:true"
14System.out.println(content);输出结果以简单的文本格式显示,明确指出检查结果,并提供带有注释的错误详细信息:
1validationResult:False;
2%%
3technique: H35;
4criterion: 1.1.1;
5type: Error;
6description: Check that the applet element contains an alt attribute with a text alternative for the applet. ;
7source: <applet code="tictactoe.class" width="250" height="250">tic-tac-toe game</applet>;
8%%
9technique: H37;
10criterion: 1.1.1;
11type: Error;
12description: Img element missing an alt attribute. The value of this attribute is referred to as "alt text".;
13source: <img src="image1.jpeg">;
14%%
15
16...其中标明检查结果validationResult以及错误和注释列表:
对于那些更喜欢结构化和机器可读格式的人来说,以 XML 格式存储验证结果是一个合适的选择。让我们看看如何使用
saveTo(writer, format) 方法将结果保存为 XML 格式。该方法需要一个 TextWriter 对象和所需的
ValidationResultSaveFormat 方法(本例中为 XML)。
1// Validate HTML for accessibility in Java and export all errors and warnings as an XML
2
3String htmlPath = "input.html";
4
5final HTMLDocument document = new HTMLDocument(htmlPath);
6AccessibilityValidator validator = new WebAccessibility().createValidator();
7ValidationResult validationresult = validator.validate(document);
8
9final java.io.StringWriter sw = new java.io.StringWriter();
10validationresult.saveTo(sw, ValidationResultSaveFormat.XML);
11
12String xml = sw.toString();
13System.out.println(xml);
14
15DocumentBuilderFactory documentBuildFactory = DocumentBuilderFactory.newInstance();
16DocumentBuilder documentBuilder = documentBuildFactory.newDocumentBuilder();
17documentBuilder.parse(new java.io.ByteArrayInputStream(xml.getBytes()));由此产生的 XML 表示格式条理清晰,便于分析和进一步处理:
1<validationResult>
2<isValid>false</isValid>
3<details>
4 <techniqueResult>
5 <technique>H35</technique>
6 <criterion>1.1.1</criterion>
7 <type>Error</type>
8 <description>Check that the applet element contains an alt attribute with a text alternative for the applet. </description>
9 <source><![CDATA[<applet code="tictactoe.class" width="250" height="250">tic-tac-toe game</applet>]]>
10 </source>
11 </techniqueResult>
12 <techniqueResult>
13 <technique>H37</technique>
14 <criterion>1.1.1</criterion>
15 <type>Error</type>
16 <description>Img element missing an alt attribute. The value of this attribute is referred to as "alt text".</description>
17 <source><![CDATA[<img src="image1.jpeg">]]>
18 </source>
19 </techniqueResult>
20
21 ...
22
23 </details>
24</validationResult>另见
Aspose.HTML 提供免费的在线 网页可访问性检查器。该工具可扫描网页、验证网页是否符合 WCAG 标准、发现问题并提出改进建议。您可以即时了解网站的合规性,确定必要的修正范围,以及网站或 HTML 文档的当前状态与 WCAG 要求之间的差距。
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.