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 com.jcabi.log.Logger;
33  import java.net.URI;
34  import java.nio.charset.Charset;
35  import java.util.Collections;
36  import java.util.Set;
37  import java.util.concurrent.CopyOnWriteArraySet;
38  import lombok.EqualsAndHashCode;
39  
40  /**
41   * Default implementaiton of validation response.
42   *
43   * @since 0.1
44   */
45  @EqualsAndHashCode(
46      of = { "ivalid", "validator", "type", "encoding", "ierrors", "iwarnings" }
47  )
48  final class DefaultValidationResponse implements ValidationResponse {
49  
50      /**
51       * Is it valid?
52       */
53      private final transient boolean ivalid;
54  
55      /**
56       * Who validated it?
57       */
58      private final transient URI validator;
59  
60      /**
61       * DOCTYPE of the document.
62       */
63      private final transient String type;
64  
65      /**
66       * The encoding.
67       */
68      private final transient Charset encoding;
69  
70      /**
71       * Set of errors found.
72       */
73      private final transient Set<Defect> ierrors;
74  
75      /**
76       * Set of warnings found.
77       */
78      private final transient Set<Defect> iwarnings;
79  
80      /**
81       * Public ctor.
82       * @param val The document is valid?
83       * @param server Who validated it?
84       * @param tpe DOCTYPE of the document
85       * @param enc Charset of the document
86       * @checkstyle ParameterNumber (3 lines)
87       */
88      DefaultValidationResponse(final boolean val,
89          final URI server, final String tpe,
90          final Charset enc) {
91          this.ivalid = val;
92          this.validator = server;
93          this.type = tpe;
94          this.encoding = enc;
95          this.ierrors = new CopyOnWriteArraySet<>();
96          this.iwarnings = new CopyOnWriteArraySet<>();
97      }
98  
99      @Override
100     public String toString() {
101         return new StringBuilder(0)
102             .append(Logger.format("Validity: %B\n", this.ivalid))
103             .append(Logger.format("Validator: \"%s\"\n", this.validator))
104             .append(Logger.format("DOCTYPE: \"%s\"\n", this.type))
105             .append(Logger.format("Charset: \"%s\"\n", this.encoding))
106             .append("Errors:\n")
107             .append(DefaultValidationResponse.asText(this.ierrors))
108             .append("Warnings:\n")
109             .append(DefaultValidationResponse.asText(this.iwarnings))
110             .toString();
111     }
112 
113     @Override
114     public boolean valid() {
115         return this.ivalid;
116     }
117 
118     @Override
119     public URI checkedBy() {
120         return this.validator;
121     }
122 
123     @Override
124     public String doctype() {
125         return this.type;
126     }
127 
128     @Override
129     public Charset charset() {
130         return this.encoding;
131     }
132 
133     @Override
134     public Set<Defect> errors() {
135         return Collections.unmodifiableSet(this.ierrors);
136     }
137 
138     @Override
139     public Set<Defect> warnings() {
140         return Collections.unmodifiableSet(this.iwarnings);
141     }
142 
143     /**
144      * Add error.
145      * @param error The error to add
146      */
147     public void addError(final Defect error) {
148         this.ierrors.add(error);
149     }
150 
151     /**
152      * Add warning.
153      * @param warning The warning to add
154      */
155     public void addWarning(final Defect warning) {
156         this.iwarnings.add(warning);
157     }
158 
159     /**
160      * Convert list of defects into string.
161      * @param defects Set of them
162      * @return The text
163      */
164     private static String asText(final Set<Defect> defects) {
165         final StringBuilder text = new StringBuilder(0);
166         for (final Defect defect : defects) {
167             text.append("  ").append(defect.toString()).append('\n');
168         }
169         return text.toString();
170     }
171 
172 }