Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.HTML for Java provides the com.aspose.html.accessibility package, which is for all web accessibility related manipulations and checks. The AccessibilityValidator class initializes an object that is used to validate the document against WCAG success criteria. The class does not have an internal constructor, therefore users cannot directly create an object of the class. To create an object, you need to use the WebAccessibility container.
To create a validator, initialize the object of the WebAccessibility class like this:
1 // Initialize webAccessibility container
2 WebAccessibility webAccessibility = new WebAccessibility();The webAccessibility object has several overloaded
createValidator() methods for creating an AccessibilityValidator instance with varying levels of customization. These methods allow validators to be created using all default rules, a specific rule, or a list of rules, and can be customized using a ValidationBuilder object. This flexible approach allows for both simple and highly customized accessibility checks.
The following Java examples demonstrate how to initialize an AccessibilityValidator for either all available rules or a specific subset based on a single accessibility principle:
This example shows how to initialize an AccessibilityValidator that includes all available accessibility rules using the default comprehensive settings provided by ValidationBuilder.getAll():
1// Initialize the webAccessibility container
2WebAccessibility webAccessibility = new WebAccessibility();
3
4// Create an accessibility validator with all rules using the default settings
5AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());In this example, a specific accessibility principle is selected using its code (e.g., “1”), and a validator is created only for the rules under that principle:
1// Retrieve a specific principle by its code
2IRule rule = webAccessibility.getRules().getPrinciple("1");
3
4// Create a validator for that principle using the same default settings
5AccessibilityValidator validatorRule = webAccessibility.createValidator(rule, ValidationBuilder.getAll());The
ValidationBuilder class defines a helper class that can be used to customize the list of rules to be checked by the validator. The ValidationBuilder defines methods and provides concrete implementations of the configuration steps.
The ValidationBuilder class offers three predefined sets of properties:
The class also provides methods for customizing the validation, such as selecting specific technologies (
useHTML(),
useCSS(),
useScript()), setting criteria levels (useLowestLevel(), useMiddleLevel(), useHighestLevel(), allLevels()), etc.
For example, the following Java code initializes AccessibilityValidator to perform accessibility checks based on user configuration. It starts by creating a WebAccessibility container, which serves as the main access point for the validation functions. Then, using ValidationBuilder.getNone(), it creates a validator with no predefined rules. From there, it explicitly enables checks for HTML and CSS technologies and limits validation to medium conformance level (usually WCAG Level AA). This setup allows for targeted, standards-based validation that targets only selected technologies and a specific conformance level.
1// Initialize the WebAccessibility container
2WebAccessibility webAccessibility = new WebAccessibility();
3
4// Create a validator with no predefined filters, enabling HTML and CSS technologies, and selecting the middle conformance levels
5AccessibilityValidator validatorHTML = webAccessibility.createValidator(
6 ValidationBuilder.getNone()
7 .useHTML()
8 .useCSS()
9 .useMiddleLevel()
10);The
ValidationResult class is the central component of web accessibility validation. It encapsulates the results of accessibility checks, providing a structured way to analyze, manage, and act on the results of validation operations. To use it, instantiate a validator object and invoke the
validate(document) method, passing in the document to be validated. The method returns a ValidationResult object containing the validation process results.
The ValidationResult class aggregates the results for all criteria defined in the AccessibilityRules object, serving as the main container for reporting validation outcomes. It includes two key properties:
To support results export and reporting, the class provides several methods: saveTo(TextWriter) and saveTo(TextWriter, ValidationResultSaveFormat) allow saving the results to the specified output stream, while saveToString() returns the validation results as a formatted String.
A more complete description can be found in the article Validation Results – Check Web Accessibility in Java.
To perform document validation, follow these steps:
validator object.document) method to validate the document against web accessibility rules.Here is an example of calling a document validation. The following Java code will write to the console which of the rules passed the test and which did not:
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}See Also
Aspose.HTML offers free online Web Accessibility Checker. This tool scans web pages, validates them for WCAG compliance, identifies problems, and suggests improvements. Get instant insights into your website’s compliance, allowing you to determine the scope of necessary corrections and the gap between the current state of your website or HTML document and WCAG requirements.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.