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 /**
23 * Represents a single entry in the client-side search index.
24 *
25 * @param title display name
26 * @param url relative URL
27 * @param category category label
28 * @param type document type
29 * @param description short description
30 * @param keywords comma-separated keywords
31 */
32 public record SearchIndexEntry(String title, String url, String category,
33 String type, String description, String keywords) {
34
35 /** String literal for comma. */
36 private static final String COMMA = ",";
37
38 /** Maximum ASCII value for the lookup table. */
39 private static final int ASCII_MAX = 128;
40
41 /** Lookup table for JSON escape sequences for characters < 128 (ASCII range). */
42 private static final String[] JSON_ESCAPES;
43
44 static {
45 JSON_ESCAPES = new String[ASCII_MAX];
46 JSON_ESCAPES['\\'] = "\\\\";
47 JSON_ESCAPES['\"'] = "\\\"";
48 JSON_ESCAPES['\n'] = "\\n";
49 JSON_ESCAPES['\r'] = "\\r";
50 JSON_ESCAPES['\t'] = "\\t";
51 }
52
53 /**
54 * Serialises this entry to a compact JSON object string.
55 *
56 * @return JSON object string
57 */
58 public String toJson() {
59 return "{"
60 + "\"title\":" + jsonString(title) + COMMA
61 + "\"url\":" + jsonString(url) + COMMA
62 + "\"category\":" + jsonString(category) + COMMA
63 + "\"type\":" + jsonString(type) + COMMA
64 + "\"description\":" + jsonString(description) + COMMA
65 + "\"keywords\":" + jsonString(keywords)
66 + "}";
67 }
68
69 /**
70 * Wraps a string value in JSON double quotes and escapes special characters.
71 *
72 * @param value the raw string to encode
73 * @return JSON-safe quoted string, or {@code ""} if value is null
74 */
75 private static String jsonString(String value) {
76 String result = "\"\"";
77 if (value != null) {
78 final int length = value.length();
79 final StringBuilder sb = new StringBuilder(length + 2);
80 sb.append('\"');
81 for (int index = 0; index < length; index++) {
82 final char ch = value.charAt(index);
83 final String escape;
84 if (ch < ASCII_MAX) {
85 escape = JSON_ESCAPES[ch];
86 }
87 else {
88 escape = null;
89 }
90 if (escape == null) {
91 sb.append(ch);
92 }
93 else {
94 sb.append(escape);
95 }
96 }
97 sb.append('\"');
98 result = sb.toString();
99 }
100 return result;
101 }
102 }