001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.site;
021
022import java.io.Writer;
023
024import javax.swing.text.MutableAttributeSet;
025
026import org.apache.maven.doxia.markup.HtmlMarkup;
027import org.apache.maven.doxia.module.xdoc.XdocSink;
028import org.apache.maven.doxia.sink.SinkEventAttributes;
029import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
030
031/**
032 * A sink for Checkstyle's xdoc templates.
033 * This module will be removed once
034 * <a href="https://github.com/checkstyle/checkstyle/issues/13426">#13426</a> is resolved.
035 *
036 * @see <a href="https://maven.apache.org/doxia/doxia/doxia-sink-api">Doxia Sink API</a>
037 */
038public class XdocsTemplateSink extends XdocSink {
039
040    /** Encoding of the writer. */
041    private final String encoding;
042
043    /**
044     * Create a new instance, initialize the Writer.
045     *
046     * @param writer not null writer to write the result.
047     * @param encoding encoding of the writer.
048     */
049    public XdocsTemplateSink(Writer writer, String encoding) {
050        super(writer);
051        this.encoding = encoding;
052    }
053
054    /**
055     * Place the XML declaration at the top of the file.
056     */
057    @Override
058    public void body() {
059        write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
060        writeEOL();
061    }
062
063    /**
064     * Place a newline at the end of the file, flush the writer, and reset the sink.
065     */
066    @Override
067    public void body_() {
068        writeEOL();
069        flush();
070        init();
071    }
072
073    /**
074     * Write an external link. We override this method because the default implementation
075     * adds a {@code class="external-link"} attribute to the link which we don't want.
076     *
077     * @param href the link.
078     */
079    @Override
080    public void link(String href) {
081        final MutableAttributeSet attributes = new SinkEventAttributeSet();
082        attributes.addAttribute(SinkEventAttributes.HREF, href);
083        writeStartTag(HtmlMarkup.A, attributes);
084    }
085
086    /**
087     * Write a table row tag. We override this method because the default implementation
088     * adds a {@code align="top"} attribute to the row which we don't want.
089     */
090    @Override
091    public void tableRow() {
092        writeStartTag(TR);
093    }
094
095    /**
096     * Write a table tag. We override this method because the default implementation
097     * adds different attributes which we don't want. We ignore the parameters
098     * because we don't need them, but the default implementation will take them
099     * into account once this class is removed.
100     *
101     * @param justification ignored
102     * @param grid ignored
103     */
104    @Override
105    public void tableRows(int[] justification, boolean grid) {
106        writeStartTag(HtmlMarkup.TABLE);
107    }
108}