View Javadoc
1   /*
2    * Copyright (c) 2011-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.w3c;
31  
32  import java.net.URI;
33  import lombok.EqualsAndHashCode;
34  import lombok.ToString;
35  
36  /**
37   * Builder of HTML and CSS validators.
38   *
39   * <p>This is your entry point to the module. Start with creating a new
40   * validator:
41   *
42   * <pre> HtmlValidator validator = new ValidatorBuilder().html();</pre>
43   *
44   * <p>Now you can use it in order to validate your HTML document against
45   * W3C rules:
46   *
47   * <pre> ValidationResponse response = validator.validate(
48   *   "&lt;html&gt;&lt;body&gt;...&lt;/body&gt;&lt;/html&gt;"
49   * );</pre>
50   *
51   * <p>The response contains all information provided by W3C server. You can
52   * work with details from {@link ValidationResponse} or just output it to
53   * console:
54   *
55   * <pre> System.out.println(response.toString());</pre>
56   *
57   * @see ValidationResponse
58   * @see Validator
59   * @see <a href="http://validator.w3.org/docs/api.html">W3C API</a>
60   * @since 0.1
61   * @checkstyle NonStaticMethodCheck (500 lines)
62   */
63  @ToString
64  @EqualsAndHashCode
65  public final class ValidatorBuilder {
66  
67      /**
68       * Static instance of HTML validator.
69       */
70      @SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
71      public static final Validator HTML = new DefaultHtmlValidator(
72          URI.create("https://validator.w3.org/nu/?out=xml&showsource=yes")
73      );
74  
75      /**
76       * Static instance of CSS validator.
77       */
78      @SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
79      public static final Validator CSS = new DefaultCssValidator(
80          URI.create("https://jigsaw.w3.org/css-validator/validator")
81      );
82  
83      /**
84       * Build HTML validator.
85       * @return The validator
86       */
87      public Validator html() {
88          return ValidatorBuilder.HTML;
89      }
90  
91      /**
92       * Build CSS validator.
93       * @return The validator
94       */
95      public Validator css() {
96          return ValidatorBuilder.CSS;
97      }
98  
99      /**
100      * Build HTML validator, pointing to the given URI of W3C engine.
101      * @param uri URI of validator
102      * @return The validator
103      */
104     public Validator html(final URI uri) {
105         return new DefaultHtmlValidator(uri);
106     }
107 
108     /**
109      * Build CSS validator, pointing to the given URI of W3C engine.
110      * @param uri URI of validator
111      * @return The validator
112      */
113     public Validator css(final URI uri) {
114         return new DefaultCssValidator(uri);
115     }
116 
117 }