1 /*
2 * Copyright (c) 2002-2025 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.source;
16
17 import static java.nio.charset.StandardCharsets.ISO_8859_1;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.io.FileUtils;
25
26 /**
27 * Patches utilities.
28 *
29 * @author Ahmed Ashour
30 */
31 public final class Patch {
32
33 private Patch() {
34 }
35
36 /**
37 * Checks the @author tag in the files touched by the specified patch.
38 *
39 * @param baseDir the root folder of HtmlUnit, this can be '.' if you are calling this methods from HtmlUnit code
40 * base. If you are calling this method from another project, this specifies HtmlUnit home folder.
41 * @param patchPath the path to the patch
42 * @param authorName the author name, e.g. "John Smith"
43 * @throws IOException if an exception occurs
44 */
45 public static void checkAuthor(final String baseDir, final String patchPath, final String authorName)
46 throws IOException {
47 final List<String> errors = new ArrayList<>();
48 final List<String> lines = FileUtils.readLines(new File(patchPath), ISO_8859_1);
49 for (final String line : lines) {
50 if (line.startsWith("+++")) {
51 final String fileName = line.substring(4, line.indexOf('\t', 4));
52 if (fileName.endsWith(".java")) {
53 final File file = new File(baseDir, fileName);
54 if (file.exists()) {
55 final List<String> fileLines = FileUtils.readLines(file, ISO_8859_1);
56 boolean authorFound = false;
57 for (final String fileLine : fileLines) {
58 if (fileLine.contains("@author " + authorName)) {
59 authorFound = true;
60 break;
61 }
62 }
63 if (!authorFound) {
64 System.out.println("No \"@author " + authorName + "\" in " + file.getAbsolutePath());
65 errors.add(file.getAbsolutePath());
66 }
67 }
68 else {
69 System.out.println("File does not exist: " + file);
70 }
71 }
72 }
73 }
74 if (!errors.isEmpty()) {
75 throw new RuntimeException("Total missing files: " + errors.size());
76 }
77 }
78
79 /**
80 * Prints to <tt>System.out</tt> the "String html = ..." string from the actual HTML file.
81 * @param htmlFile the path of the HTML file
82 * @throws IOException if an exception occurs
83 */
84 public static void generateHtmlString(final File htmlFile) throws IOException {
85 final List<String> lines = FileUtils.readLines(htmlFile, ISO_8859_1);
86 for (int i = 0; i < lines.size(); i++) {
87 final String line = lines.get(i).replace("\t", " ").replace("\\", "\\\\").replace("\"", "\\\"");
88 if (i == 0) {
89 System.out.println(" final String html = \"" + line + "\\n\"");
90 }
91 else {
92 System.out.print(" + \"" + line);
93 if (i == lines.size() - 1) {
94 System.out.print("\";");
95 }
96 else {
97 System.out.print("\\n\"");
98 }
99 System.out.println();
100 }
101 }
102 }
103 }