PWA Manifest Generator — Java source
Assemble a valid Web App Manifest — name, colors, display mode, icon sizes, maskable purpose — with a stable key order and instant JSON you can copy or download.
This is the Java implementation — the same logic the interactive tool runs, in a shareable, citable form.
// pwa-manifest — build a fixed-key-order manifest.json from form fields.
//
// Language: Java (17+), standard library only (this manifest builder is pure
// string work — no external packages exist to link)
// CosmoDev polyglot showcase port of the `pwa-manifest-generator` tool.
// Ported from src/lib/pwa-manifest.ts — display source, part of CosmoDev's
// polyglot tool pages. Same logic as this dir's typescript.ts and the live
// Go twin at cli/pwa-manifest-generator/pwa-manifest-generator.go.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** Fixed-key-order manifest.json builder ported from src/lib/pwa-manifest.ts. */
final class PwaManifest {
private PwaManifest() {}
/** Mirrors the ManifestInput interface in src/lib/pwa-manifest.ts. */
static final class ManifestInput {
String name = "";
String shortName = "";
String description = "";
String startUrl = "";
String display = ""; // "" means absent
String orientation = ""; // "" means absent
String backgroundColor = "";
String themeColor = "";
/** Null means "not provided" → default [192, 512]; an explicit empty
* list emits an empty icons array — the same distinction the TS lib
* draws with `??`. */
List<Long> iconSizes = null;
/** Marks every icon purpose "maskable" instead of "any". */
boolean maskable = false;
}
/** Mirrors the ManifestResult interface in src/lib/pwa-manifest.ts. */
static final class ManifestResult {
boolean ok;
final List<String> errors = new ArrayList<>();
final List<String> warnings = new ArrayList<>();
String json = "";
}
/** Same contract as TS HEX = /^#[0-9a-fA-F]{6}$/. */
private static boolean isHexColor(String s) {
if (s.length() != 7 || s.charAt(0) != '#') return false;
for (int i = 1; i < s.length(); i++) {
char c = s.charAt(i);
boolean hex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
if (!hex) return false;
}
return true;
}
private static String trim(String s) {
return s == null ? "" : s.trim();
}
/**
* Double-quote s exactly like JavaScript's JSON.stringify: escape ", \,
* and the control characters (short escapes for \b \f \n \r \t, and
* backslash-u00xx for the rest), leave everything else — including
* non-ASCII — untouched. (javac processes unicode escapes even inside
* comments, so this text never spells out the two-character prefix.)
*/
static String jsonString(String s) {
StringBuilder out = new StringBuilder(s.length() + 2);
out.append('"');
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '"': out.append("\\\""); break;
case '\\': out.append("\\\\"); break;
case '\b': out.append("\\b"); break;
case '\f': out.append("\\f"); break;
case '\n': out.append("\\n"); break;
case '\r': out.append("\\r"); break;
case '\t': out.append("\\t"); break;
default:
if (c < 0x20) out.append(String.format("\\u%04x", (int) c));
else out.append(c);
}
}
out.append('"');
return out.toString();
}
/** The Java twin of buildManifest() in src/lib/pwa-manifest.ts. */
static ManifestResult buildManifest(ManifestInput input) {
ManifestResult result = new ManifestResult();
String name = trim(input.name);
String shortName = trim(input.shortName);
String description = trim(input.description);
String startUrl = trim(input.startUrl);
String backgroundColor = trim(input.backgroundColor);
String themeColor = trim(input.themeColor);
List<Long> defaultSizes = List.of(192L, 512L);
List<Long> sizes = input.iconSizes != null ? input.iconSizes : defaultSizes;
if (name.isEmpty()) result.errors.add("name is required.");
if (!backgroundColor.isEmpty() && !isHexColor(backgroundColor))
result.errors.add("background_color must be a #rrggbb hex value.");
if (!themeColor.isEmpty() && !isHexColor(themeColor))
result.errors.add("theme_color must be a #rrggbb hex value.");
boolean badSize = false;
for (long s : sizes)
if (s <= 0) { badSize = true; break; }
if (badSize) result.errors.add("icon sizes must be positive integers.");
Set<Long> seenSizes = new HashSet<>();
for (long s : sizes) {
if (!seenSizes.add(s)) result.errors.add("icon size " + s + " is listed twice.");
}
if (!result.errors.isEmpty()) {
result.ok = false;
result.warnings.clear();
result.json = "";
return result;
}
int shortNameLen = shortName.codePointCount(0, shortName.length());
if (shortNameLen > 12) {
result.warnings.add("short_name is " + shortNameLen +
" characters — home-screen labels truncate around 12.");
}
List<String> lines = new ArrayList<>();
List<Long> finalSizes = sizes;
java.util.function.BiConsumer<String, String> add =
(key, value) -> lines.add(" " + jsonString(key) + ": " + jsonString(value));
add.accept("name", name);
if (!shortName.isEmpty()) add.accept("short_name", shortName);
if (!description.isEmpty()) add.accept("description", description);
if (!startUrl.isEmpty()) add.accept("start_url", startUrl);
if (!input.display.isEmpty()) add.accept("display", input.display);
if (!input.orientation.isEmpty()) add.accept("orientation", input.orientation);
if (!backgroundColor.isEmpty()) add.accept("background_color", backgroundColor);
if (!themeColor.isEmpty()) add.accept("theme_color", themeColor);
String purpose = input.maskable ? "maskable" : "any";
List<String> iconEntries = new ArrayList<>();
for (long s : finalSizes) {
iconEntries.add(" {\n"
+ " \"src\": " + jsonString("icons/icon-" + s + "x" + s + ".png") + ",\n"
+ " \"sizes\": " + jsonString(s + "x" + s) + ",\n"
+ " \"type\": \"image/png\",\n"
+ " \"purpose\": " + jsonString(purpose) + "\n"
+ " }");
}
StringBuilder doc = new StringBuilder();
doc.append("{\n");
for (String line : lines) doc.append(line).append(",\n");
if (iconEntries.isEmpty()) {
doc.append(" \"icons\": []"); // JSON.stringify renders an empty array inline
} else {
doc.append(" \"icons\": [\n");
doc.append(String.join(",\n", iconEntries));
doc.append("\n ]");
}
doc.append("\n}\n");
result.ok = true;
result.json = doc.toString();
return result;
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →