1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.site;
21
22 import java.io.PrintWriter;
23 import java.io.Writer;
24 import java.util.regex.Pattern;
25
26 import javax.swing.text.MutableAttributeSet;
27
28 import org.apache.maven.doxia.module.xdoc.XdocSink;
29 import org.apache.maven.doxia.sink.SinkEventAttributes;
30 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
31
32 /**
33 * A sink for Checkstyle's xdoc templates.
34 * This module will be removed once
35 * <a href="https://github.com/checkstyle/checkstyle/issues/13426">#13426</a> is resolved.
36 *
37 * @see <a href="https://maven.apache.org/doxia/doxia/doxia-sink-api">Doxia Sink API</a>
38 */
39 public class XdocsTemplateSink extends XdocSink {
40
41 /** Encoding of the writer. */
42 private final String encoding;
43
44 /**
45 * Create a new instance, initialize the Writer.
46 *
47 * @param writer not null writer to write the result.
48 * @param encoding encoding of the writer.
49 */
50 public XdocsTemplateSink(Writer writer, String encoding) {
51 super(new CustomPrintWriter(writer));
52 this.encoding = encoding;
53 }
54
55 /**
56 * Place the XML declaration at the top of the file.
57 */
58 @Override
59 public void body() {
60 write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
61 writeEOL();
62 }
63
64 /**
65 * Place a newline at the end of the file, flush the writer, and reset the sink.
66 */
67 @Override
68 public void body_() {
69 writeEOL();
70 flush();
71 init();
72 }
73
74 /**
75 * Write an external link. We override this method because the default implementation
76 * adds a {@code class="external-link"} attribute to the link which we don't want.
77 *
78 * @param href the link.
79 */
80 @Override
81 public void link(String href) {
82 final MutableAttributeSet attributes = new SinkEventAttributeSet();
83 attributes.addAttribute(SinkEventAttributes.HREF, href);
84 writeStartTag(A, attributes);
85 }
86
87 /**
88 * Write a table row tag. We override this method because the default implementation
89 * adds a {@code align="top"} attribute to the row which we don't want.
90 */
91 @Override
92 public void tableRow() {
93 writeStartTag(TR);
94 }
95
96 /**
97 * Write a table tag. We override this method because the default implementation
98 * adds different attributes which we don't want. We ignore the parameters
99 * because we don't need them, but the default implementation will take them
100 * into account once this class is removed.
101 *
102 * @param justification ignored
103 * @param grid ignored
104 */
105 @Override
106 public void tableRows(int[] justification, boolean grid) {
107 writeStartTag(TABLE);
108 }
109
110 /**
111 * A Custom writer that only prints Unix-style newline character.
112 */
113 private static final class CustomPrintWriter extends PrintWriter {
114
115 /** A Regex pattern to represent all kinds of newline character. */
116 private static final Pattern LINE_BREAK_ESCAPE = Pattern.compile("\\R");
117
118 /** Unix-Style newline character. */
119 private static final String NEWLINE = "\n";
120
121 /**
122 * Creates a new instance of this custom writer.
123 *
124 * @param writer not null writer to write the result
125 */
126 private CustomPrintWriter(Writer writer) {
127 super(writer);
128 }
129
130 /**
131 * Enforces Unix-style newline character.
132 */
133 @Override
134 public void println() {
135 write(NEWLINE, 0, NEWLINE.length());
136 }
137
138 /**
139 * Unifies all newline characters to Unix-Style Newline character.
140 *
141 * @param line text that is to be written in the output file.
142 * @param offset starting offset value for writing data.
143 * @param length total length of string to be written.
144 */
145 @Override
146 public void write(String line, int offset, int length) {
147 final String lineBreakReplacedLine =
148 LINE_BREAK_ESCAPE.matcher(line).replaceAll(NEWLINE);
149 super.write(lineBreakReplacedLine, 0, lineBreakReplacedLine.length());
150 }
151 }
152 }