Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Checking website accessibility helps identify HTML and CSS issues that may prevent people with disabilities from using web content. Aspose.HTML for Java can evaluate an HTMLDocument against selected accessibility rules based on
WCAG 2.0 and return structured validation results.
Use
WebAccessibility to create an AccessibilityValidator, load a local file or webpage into an HTMLDocument, and call
validate(). Inspect the returned ValidationResult to find failed rules, errors, warnings, and affected HTML or CSS objects.
The following Java example checks a remote webpage using all rules selected by ValidationBuilder.getAll() and prints the code, description, and status of each unsuccessful rule:
WebAccessibility instance and call
createValidator() with the required validation rules.AccessibilityValidator.validate().ValidationResult.getSuccess() and iterate through getDetails() when the result is unsuccessful. 1// Check website for WCAG compliance in Java
2
3// Create a WebAccessibility instance
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Create an accessibility validator with static instance
7// for all rules from repository that match the builder settings
8AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());
9
10// Initialize an HTMLDocument object
11final HTMLDocument document = new HTMLDocument("https://products.aspose.com/html/net/generators/video/");
12ValidationResult validationResult = validator.validate(document);
13
14// Checking for success
15if (!validationResult.getSuccess()) {
16 // Get a list of Details
17 for (RuleValidationResult detail : validationResult.getDetails()) {
18 System.out.println(String.format("%s: %s = %s",
19 detail.getRule().getCode(),
20 detail.getRule().getDescription(),
21 detail.getSuccess()
22 ));
23 }
24}Errors identify failed accessibility checks, while warnings provide information that can help improve the document without making the corresponding result unsuccessful. Both types include diagnostic data that developers can use to locate and review the reported issue.
The IError interface describes a problem reported during validation. Its methods provide the message, status, type, and source of the issue:
| Method | Returned information |
|---|---|
getErrorMessage() | Human-readable description of the issue. |
getSuccess() | Whether the associated check succeeded. |
getErrorType() | Numeric issue type. |
getErrorTypeName() | Type name, such as Error or Warning. |
getTarget() | HTML or CSS object associated with the issue, when available. |
Aspose.HTML uses the following result combinations:
ErrorType = 1 and Success = false indicates an Error and an unsuccessful check.ErrorType = 2 and Success = true indicates a Warning with informational guidance.The
Target class contains the HTML or CSS object associated with an error or warning. Use getItem() to retrieve the object and getTargetType() to determine its type before casting it. A validation issue does not always have a target, so check the returned Target for null.
The
TargetTypes enumeration identifies the type of object stored in a Target:
| Name | Value | Stored object |
|---|---|---|
HTMLElement | 0 | An HTML element, such as <img> or <button>. |
CSSStyleRule | 1 | A CSS style rule. |
CSSStyleSheet | 2 | A CSS stylesheet. |
The next example validates a local HTML document and traverses unsuccessful rule results, technique errors, diagnostic messages, and target objects:
HTMLDocument and validate it with an AccessibilityValidator.validationResult.getDetails().ruleResult.getErrors().IError type and message, then obtain its target with
getTarget(). 1// Check HTML for WCAG compliance and output failed rule codes and error messages
2
3// Create a WebAccessibility instance
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Create an accessibility validator with static instance
7// for all rules 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
16for (RuleValidationResult ruleResult : validationResult.getDetails()) {
17 // list only unsuccessful rule
18 if (!ruleResult.getSuccess()) {
19 // print the code and description of the rule
20 System.out.println(String.format("%s: %s = %s",
21 ruleResult.getRule().getCode(),
22 ruleResult.getRule().getDescription(),
23 ruleResult.getSuccess()
24 ));
25
26 // print the results of methods with errors
27 for (ITechniqueResult ruleDetail : ruleResult.getErrors()) {
28 // print the code and description of the method
29 StringBuilder str = new StringBuilder(String.format("\n%s: %s - %s",
30 ruleDetail.getRule().getCode(),
31 ruleDetail.getSuccess(),
32 ruleDetail.getRule().getDescription()
33 ));
34 // get an error object
35 IError error = ruleDetail.getError();
36 // get a target object
37 Target target = error.getTarget();
38 // get error type and message
39 str.append(String.format("\n\n\t%s : %s",
40 error.getErrorTypeName(),
41 error.getErrorMessage()
42 ));
43
44 if (target != null) {
45 // Checking the type of the contained object for casting and working with it
46 if (target.getTargetType() == TargetTypes.CSSStyleRule) {
47 ICSSStyleRule cssRule = (ICSSStyleRule) target.getItem();
48 str.append(String.format("\n\n\t%s",
49 cssRule.getCSSText()
50 ));
51 }
52 if (ruleDetail.getError().getTarget().getTargetType() == TargetTypes.CSSStyleSheet) {
53 str.append(String.format("\n\n\t%s",
54 ((ICSSStyleSheet) target.getItem()).getTitle()
55 ));
56 }
57 if (ruleDetail.getError().getTarget().getTargetType() == TargetTypes.HTMLElement) {
58 str.append(String.format("\n\n\t%s",
59 ((HTMLElement) target.getItem()).getOuterHTML()
60 ));
61 }
62 }
63 System.out.println(str);
64 }
65 }
66}For each unsuccessful rule, the example accesses its description, technique results, error messages, and available HTML or CSS targets. This diagnostic context can be used to build a developer-facing accessibility report. Automated results identify issues covered by the selected rules, but they do not replace manual accessibility testing.
Use the free online Web Accessibility Checker for a quick page scan, or the Color Contrast Checker to evaluate a foreground and background color pair.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.