View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.maven;
32  
33  import com.jcabi.xml.XML;
34  import com.jcabi.xml.XMLDocument;
35  import com.qulice.spi.Environment;
36  import com.qulice.spi.ValidationException;
37  import java.io.File;
38  import java.io.IOException;
39  import java.nio.charset.StandardCharsets;
40  import org.apache.commons.io.FileUtils;
41  
42  /**
43   * Check pom.xml with XPath validation queries.
44   *
45   * <p>Restrictions:
46   *
47   * <ol>
48   * <li>Each xpath component should contains namespace prefix pom:</li>
49   * <li>Each xpath query should end with /text()</li>
50   * </ol>
51   *
52   * @since 0.6
53   */
54  public final class PomXpathValidator implements MavenValidator {
55  
56      @Override
57      public void validate(final MavenEnvironment env)
58          throws ValidationException {
59          PomXpathValidator.validate(PomXpathValidator.pom(env), env.asserts());
60      }
61  
62      /**
63       * Validate pom against xpath queries.
64       * @param pom POM
65       * @param xpaths Xpath queries
66       * @throws ValidationException validation exception
67       */
68      private static void validate(final XML pom, final Iterable<String> xpaths)
69          throws ValidationException {
70          for (final String xpath : xpaths) {
71              if (pom.xpath(xpath).isEmpty()) {
72                  throw new ValidationException(
73                      "pom.xml don't match the xpath query [%s]", xpath
74                  );
75              }
76          }
77      }
78  
79      /**
80       * Parse pom.xml.
81       *
82       * @param env Environment
83       * @return XML instance
84       */
85      private static XML pom(final Environment env) {
86          try {
87              return new XMLDocument(
88                  FileUtils.readFileToString(
89                      new File(
90                          String.format(
91                              "%s%s%s",
92                              env.basedir(),
93                              File.separator,
94                              "pom.xml"
95                          )
96                      ),
97                      StandardCharsets.UTF_8
98                  )
99              ).registerNs("pom", "http://maven.apache.org/POM/4.0.0");
100         } catch (final IOException exc) {
101             throw new IllegalArgumentException(exc);
102         }
103     }
104 }