Meta Tags Generator — Java source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the Java implementation — the same logic the interactive tool runs, in a shareable, citable form.
// meta-tags-generator — build the ordered SEO head-tag set: title,
// description, canonical, robots, Open Graph (ogTitle/ogDescription/ogUrl
// falling back to the base fields, og:type website whenever the block is
// present), Twitter card, theme-color. Java port (canonical TS:
// src/lib/meta-tags.ts; Go twin:
// cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
// fallbacks, same emission order, same warnings (title > 60,
// description > 160) and the same hard errors (missing title/description,
// non-absolute og:image, non-hex theme-color suppress emission entirely).
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
// Package-private: display snippets keep the repo filename (java.java),
// which need not match the class name.
final class MetaTagsGenerator {
private static final Pattern HEX = Pattern.compile("^#[0-9a-fA-F]{6}$");
private MetaTagsGenerator() {}
/** The input field set; every field defaults to "" (all-optional shape). */
static final class Input {
String title = "";
String description = "";
String canonicalUrl = "";
String robots = "";
String ogTitle = "";
String ogDescription = "";
String ogImage = "";
String ogUrl = "";
String twitterCard = "";
String twitterSite = "";
String themeColor = "";
}
/** The ordered tag list plus diagnostics. */
static final class Result {
boolean ok;
List<String> errors = new ArrayList<>();
List<String> warnings = new ArrayList<>();
List<String> tags = new ArrayList<>();
}
private static String trim(String s) {
return s == null ? "" : s.trim();
}
/** Build the ordered head-tag set. Blank optional fields are skipped;
* hard errors suppress emission entirely; warnings never block. */
static Result buildMetaTags(Input in) {
Result r = new Result();
List<String> errors = new ArrayList<>();
String title = trim(in.title);
String description = trim(in.description);
String canonical = trim(in.canonicalUrl);
String ogTitleIn = trim(in.ogTitle);
String ogTitle = !ogTitleIn.isEmpty() ? ogTitleIn : title;
String ogDescriptionIn = trim(in.ogDescription);
String ogDescription = !ogDescriptionIn.isEmpty() ? ogDescriptionIn : description;
String ogImage = trim(in.ogImage);
String ogUrlIn = trim(in.ogUrl);
String ogUrl = !ogUrlIn.isEmpty() ? ogUrlIn : canonical;
String themeColor = trim(in.themeColor);
if (title.isEmpty()) errors.add("Title is required.");
if (description.isEmpty()) errors.add("Description is required.");
if (!ogImage.isEmpty() && !ogImage.startsWith("http://") && !ogImage.startsWith("https://")) {
errors.add("og:image must be an absolute http(s) URL.");
}
if (!themeColor.isEmpty() && !HEX.matcher(themeColor).matches()) {
errors.add("theme-color must be a #rrggbb hex value.");
}
if (!errors.isEmpty()) {
r.ok = false;
r.errors = errors;
return r;
}
if (title.length() > 60) {
r.warnings.add("Title is " + title.length() + " characters — search results truncate around 60.");
}
if (description.length() > 160) {
r.warnings.add("Description is " + description.length() + " characters — search results truncate around 160.");
}
List<String> tags = new ArrayList<>();
tags.add("<title>" + title + "</title>");
tags.add("<meta name=\"description\" content=\"" + description + "\" />");
if (!canonical.isEmpty()) tags.add("<link rel=\"canonical\" href=\"" + canonical + "\" />");
String robots = trim(in.robots);
if (!robots.isEmpty()) tags.add("<meta name=\"robots\" content=\"" + robots + "\" />");
boolean hasOg = !ogTitleIn.isEmpty() || !ogDescriptionIn.isEmpty() || !ogImage.isEmpty() || !ogUrl.isEmpty();
if (hasOg) {
tags.add("<meta property=\"og:title\" content=\"" + ogTitle + "\" />");
tags.add("<meta property=\"og:description\" content=\"" + ogDescription + "\" />");
if (!ogImage.isEmpty()) tags.add("<meta property=\"og:image\" content=\"" + ogImage + "\" />");
if (!ogUrl.isEmpty()) tags.add("<meta property=\"og:url\" content=\"" + ogUrl + "\" />");
tags.add("<meta property=\"og:type\" content=\"website\" />");
}
if (!in.twitterCard.isEmpty()) tags.add("<meta name=\"twitter:card\" content=\"" + in.twitterCard + "\" />");
String twitterSite = trim(in.twitterSite);
if (!twitterSite.isEmpty()) tags.add("<meta name=\"twitter:site\" content=\"" + twitterSite + "\" />");
if (!themeColor.isEmpty()) tags.add("<meta name=\"theme-color\" content=\"" + themeColor + "\" />");
r.ok = true;
r.tags = tags;
return r;
}
}
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 →