001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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
022/**
023 * Represents a single entry in the client-side search index.
024 *
025 * @param title display name
026 * @param url relative URL
027 * @param category category label
028 * @param type document type
029 * @param description short description
030 * @param keywords comma-separated keywords
031 */
032public record SearchIndexEntry(String title, String url, String category,
033                            String type, String description, String keywords) {
034
035    /** String literal for comma. */
036    private static final String COMMA = ",";
037
038    /** Maximum ASCII value for the lookup table. */
039    private static final int ASCII_MAX = 128;
040
041    /** Lookup table for JSON escape sequences for characters < 128 (ASCII range). */
042    private static final String[] JSON_ESCAPES;
043
044    static {
045        JSON_ESCAPES = new String[ASCII_MAX];
046        JSON_ESCAPES['\\'] = "\\\\";
047        JSON_ESCAPES['\"'] = "\\\"";
048        JSON_ESCAPES['\n'] = "\\n";
049        JSON_ESCAPES['\r'] = "\\r";
050        JSON_ESCAPES['\t'] = "\\t";
051    }
052
053    /**
054     * Serialises this entry to a compact JSON object string.
055     *
056     * @return JSON object string
057     */
058    public String toJson() {
059        return "{"
060            + "\"title\":" + jsonString(title) + COMMA
061            + "\"url\":" + jsonString(url) + COMMA
062            + "\"category\":" + jsonString(category) + COMMA
063            + "\"type\":" + jsonString(type) + COMMA
064            + "\"description\":" + jsonString(description) + COMMA
065            + "\"keywords\":" + jsonString(keywords)
066            + "}";
067    }
068
069    /**
070     * Wraps a string value in JSON double quotes and escapes special characters.
071     *
072     * @param value the raw string to encode
073     * @return JSON-safe quoted string, or {@code ""} if value is null
074     */
075    private static String jsonString(String value) {
076        String result = "\"\"";
077        if (value != null) {
078            final int length = value.length();
079            final StringBuilder sb = new StringBuilder(length + 2);
080            sb.append('\"');
081            for (int index = 0; index < length; index++) {
082                final char ch = value.charAt(index);
083                final String escape;
084                if (ch < ASCII_MAX) {
085                    escape = JSON_ESCAPES[ch];
086                }
087                else {
088                    escape = null;
089                }
090                if (escape == null) {
091                    sb.append(ch);
092                }
093                else {
094                    sb.append(escape);
095                }
096            }
097            sb.append('\"');
098            result = sb.toString();
099        }
100        return result;
101    }
102}