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