| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DefaultHtmlValidator |
|
| 1.6666666666666667;1.667 | ||||
| DefaultHtmlValidator$AjcClosure1 |
|
| 1.6666666666666667;1.667 |
| 1 | 4 | /** |
| 2 | * Copyright (c) 2011-2013, ReXSL.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 ReXSL.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.rexsl.w3c; | |
| 31 | ||
| 32 | import com.jcabi.aspects.Loggable; | |
| 33 | import com.rexsl.test.TestResponse; | |
| 34 | import java.net.URI; | |
| 35 | import javax.validation.constraints.NotNull; | |
| 36 | import javax.ws.rs.core.MediaType; | |
| 37 | import lombok.EqualsAndHashCode; | |
| 38 | import lombok.ToString; | |
| 39 | ||
| 40 | /** | |
| 41 | * Implementation of (X)HTML validator. | |
| 42 | * | |
| 43 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
| 44 | * @version $Id$ | |
| 45 | * @see <a href="http://validator.w3.org/docs/api.html">W3C API</a> | |
| 46 | */ | |
| 47 | @ToString | |
| 48 | @EqualsAndHashCode(callSuper = false, of = "uri") | |
| 49 | @Loggable(Loggable.DEBUG) | |
| 50 | final class DefaultHtmlValidator extends BaseValidator implements Validator { | |
| 51 | ||
| 52 | /** | |
| 53 | * The URI to use in W3C. | |
| 54 | */ | |
| 55 | private final transient URI uri; | |
| 56 | ||
| 57 | /** | |
| 58 | * Public ctor with default entry point. | |
| 59 | */ | |
| 60 | public DefaultHtmlValidator() { | |
| 61 | 0 | this(URI.create("http://validator.w3.org/check")); |
| 62 | 0 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Public ctor. | |
| 66 | * @param entry Entry point to use | |
| 67 | */ | |
| 68 | public DefaultHtmlValidator(@NotNull final URI entry) { | |
| 69 | 2 | super(); |
| 70 | 2 | this.uri = entry; |
| 71 | 2 | } |
| 72 | ||
| 73 | /** | |
| 74 | * {@inheritDoc} | |
| 75 | */ | |
| 76 | @Override | |
| 77 | @NotNull | |
| 78 | @SuppressWarnings("PMD.AvoidCatchingThrowable") | |
| 79 | public ValidationResponse validate(@NotNull final String html) { | |
| 80 | DefaultValidationResponse response; | |
| 81 | try { | |
| 82 | 4 | final TestResponse soap = this |
| 83 | .send( | |
| 84 | this.uri, | |
| 85 | this.entity("uploaded_file", html, MediaType.TEXT_HTML) | |
| 86 | ) | |
| 87 | .registerNs("env", "http://www.w3.org/2003/05/soap-envelope") | |
| 88 | .registerNs("m", "http://www.w3.org/2005/10/markup-validator") | |
| 89 | .assertThat( | |
| 90 | new RetryPolicy( | |
| 91 | "/env:Envelope/env:Body/m:markupvalidationresponse" | |
| 92 | ) | |
| 93 | ) | |
| 94 | .assertXPath("//m:validity") | |
| 95 | .assertXPath("//m:checkedby") | |
| 96 | .assertXPath("//m:doctype") | |
| 97 | .assertXPath("//m:charset"); | |
| 98 | 1 | response = this.build(soap); |
| 99 | 0 | } catch (AssertionError ex) { |
| 100 | 0 | response = this.success(ex.getMessage()); |
| 101 | // @checkstyle IllegalCatchCheck (1 line) | |
| 102 | 1 | } catch (Throwable ex) { |
| 103 | 1 | response = this.failure(ex); |
| 104 | 1 | } |
| 105 | 2 | return response; |
| 106 | } | |
| 107 | ||
| 108 | } |