1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package javadoc.tester;
25 
26 import java.io.PrintStream;
27 import java.nio.file.Path;
28 import java.util.Map;
29 import java.util.function.Function;
30 
31 public class ShowHeadings extends HtmlChecker {
32 
33     private int currLevel;
34 
35     private boolean copyContent;
36 
ShowHeadings(PrintStream out, Function<Path,String> fileReader)37     ShowHeadings(PrintStream out, Function<Path,String> fileReader) {
38         super(out, fileReader);
39     }
40 
41     @Override
report()42     public void report() {
43     }
44 
45     @Override
startFile(Path path)46     public void startFile(Path path) {
47         out.println("Headings: " + path);
48         currLevel = 0;
49     }
50 
51     @Override
endFile()52     public void endFile() {
53     }
54 
55     @Override
docType(String doctype)56     public void docType(String doctype) {
57     }
58 
59     @Override
startElement(String name, Map<String,String> attrs, boolean selfClosing)60     public void startElement(String name, Map<String,String> attrs, boolean selfClosing) {
61         switch (name) {
62             case "h1": case "h2": case "h3": case "h4": case "h5": case "h6":
63                 startHeading(name);
64                 break;
65         }
66     }
67 
68     @Override
endElement(String name)69     public void endElement(String name) {
70         switch (name) {
71             case "h1": case "h2": case "h3": case "h4": case "h5": case "h6":
72                 out.println();
73                 copyContent = false;
74                 break;
75         }
76 
77     }
78 
startHeading(String h)79     private void startHeading(String h) {
80         int level = Character.digit(h.charAt(1), 10);
81         while (level > currLevel + 1) {
82             currLevel++;
83             out.println("*  ".repeat(currLevel - 1) + "H" + currLevel + ": *** MISSING ***");
84         }
85         currLevel = level;
86         out.print("*  ".repeat(currLevel - 1) + "H" + currLevel + ": ");
87         copyContent = true;
88     }
89 
90     @Override
content(String s)91     public void content(String s) {
92         if (copyContent) {
93             out.print(s.replace("&nbsp;", " ")
94                 .replace("&lt;", "<")
95                 .replace("&gt;", ">")
96                 .replace("&amp;", "&")
97                 .replaceAll("\\s+", " ")
98                 );
99         }
100     }
101 }
102 
103