Skip to content

Commit

Permalink
8299080: Wrong default value of snippet lang attribute
Browse files Browse the repository at this point in the history
Reviewed-by: jjg
  • Loading branch information
pavelrappo committed Jul 17, 2024
1 parent 8713628 commit 6df7acb
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
package jdk.javadoc.internal.doclets.formats.html.taglets;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -65,41 +67,16 @@
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
import jdk.javadoc.internal.doclets.toolkit.util.Utils;

import static jdk.javadoc.internal.doclets.formats.html.taglets.SnippetTaglet.Language.*;

/**
* A taglet that represents the {@code @snippet} tag.
*/
public class SnippetTaglet extends BaseTaglet {

public enum Language {

JAVA("java"),
PROPERTIES("properties");

private static final Map<String, Language> languages;

static {
Map<String, Language> tmp = new HashMap<>();
for (var language : values()) {
String id = Objects.requireNonNull(language.identifier);
if (tmp.put(id, language) != null)
throw new IllegalStateException(); // 1-1 correspondence
}
languages = Map.copyOf(tmp);
}

Language(String id) {
identifier = id;
}

private final String identifier;

public static Optional<Language> of(String identifier) {
if (identifier == null)
return Optional.empty();
return Optional.ofNullable(languages.get(identifier));
}

public String getIdentifier() {return identifier;}
JAVA,
PROPERTIES;
}

SnippetTaglet(HtmlConfiguration config) {
Expand Down Expand Up @@ -361,18 +338,30 @@ private Content generateContent(Element holder, DocTree tag)
}
}

String lang = null;
String lang;
AttributeTree langAttr = attributes.get("lang");
if (langAttr != null) {

if (langAttr != null) { // the lang attribute overrides everything else
lang = stringValueOf(langAttr);
} else if (containsClass) {
} else if (inlineContent != null && externalContent == null) { // an inline snippet
lang = "java";
} else if (containsFile) {
lang = languageFromFileName(fileObject.getName());
} else if (externalContent != null) { // an external or a hybrid snippet
if (containsClass) { // the class attribute means Java
lang = "java";
} else {
var uri = fileObject.toUri();
var path = uri.getPath() != null ? uri.getPath() : "";
var fileName = path.substring(path.lastIndexOf('/') + 1);
lang = languageFromFileName(fileName);
}
} else {
throw new AssertionError();
}

Optional<Language> language = Language.of(lang);

var language = switch (lang) {
case "properties" -> PROPERTIES;
case null, default -> JAVA;
};

// TODO cache parsed external snippet (WeakHashMap)

Expand Down Expand Up @@ -482,7 +471,7 @@ private static String diff(String inline, String external) {
""".formatted(inline, external);
}

private StyledText parse(Resources resources, Diags diags, Optional<Language> language, String content) throws ParseException {
private StyledText parse(Resources resources, Diags diags, Language language, String content) throws ParseException {
Parser.Result result = new Parser(resources).parse(diags, language, content);
result.actions().forEach(Action::perform);
return result.text();
Expand All @@ -505,13 +494,15 @@ private static String stringValueOf(AttributeTree at) throws BadSnippetException
}

private String languageFromFileName(String fileName) {
// TODO: find a way to extend/customize the list of recognized file name extensions
if (fileName.endsWith(".java")) {
return "java";
} else if (fileName.endsWith(".properties")) {
return "properties";
// The assumption is simple: a file extension is the language
// identifier.
// Was about to use Path.getExtension introduced in 8057113, but then
// learned that it was removed in 8298303.
int lastPeriod = fileName.lastIndexOf('.');
if (lastPeriod <= 0) {
return null;
}
return null;
return (lastPeriod == fileName.length() - 1) ? null : fileName.substring(lastPeriod + 1);
}

private void error(TagletWriter writer, Element holder, DocTree tag, String key, Object... args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -89,9 +89,8 @@ public Parser(Resources resources) {
this.markupParser = new MarkupParser(resources);
}

public Result parse(SnippetTaglet.Diags diags, Optional<SnippetTaglet.Language> language, String source) throws ParseException {
SnippetTaglet.Language lang = language.orElse(SnippetTaglet.Language.JAVA);
var p = switch (lang) {
public Result parse(SnippetTaglet.Diags diags, SnippetTaglet.Language language, String source) throws ParseException {
var p = switch (language) {
case JAVA -> JAVA_COMMENT;
case PROPERTIES -> PROPERTIES_COMMENT;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void snippet_wrapped() { }
<div class="snippet-container"><button class="copy snippet-copy" aria-label="Copy snippet" \
onclick="copySnippet(this)"><span data-copied="Copied!">Copy</span>\
<img src="../resource-files/copy.svg" alt="Copy snippet"></button>
<pre class="snippet" id="snippet-snippet_standalone()1"><code> this is snippet_standalone
<pre class="snippet" id="snippet-snippet_standalone()1"><code class="language-java"> this is snippet_standalone
</code></pre>
</div>
Expand All @@ -131,7 +131,7 @@ public void snippet_wrapped() { }
<div class="block"><p>First sentence.</p>
<p>Before.</p>
<div class="snippet-container"><button class="copy snippet-copy" aria-label="Copy snippet" onclick="copySnippet(this)"><span data-copied="Copied!">Copy</span><img src="../resource-files/copy.svg" alt="Copy snippet"></button>
<pre class="snippet" id="snippet-snippet_wrapped()1"><code> this is a snippet_wrapped
<pre class="snippet" id="snippet-snippet_wrapped()1"><code class="language-java"> this is a snippet_wrapped
</code></pre>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -100,7 +100,7 @@ protected void checkOutputEither(Output out, String first, String... other) {

protected String getSnippetHtmlRepresentation(String pathToHtmlFile,
String content) {
return getSnippetHtmlRepresentation(pathToHtmlFile, content, Optional.empty(), Optional.empty());
return getSnippetHtmlRepresentation(pathToHtmlFile, content, Optional.of("java"), Optional.empty());
}

protected String getSnippetHtmlRepresentation(String pathToHtmlFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public class A {
<span class="element-name">case%s</span>()</div>
<div class="block">
%s
</div>""".formatted(index, getSnippetHtmlRepresentation("A.html", t.expectedOutput(), Optional.empty(), Optional.of("snippet-case" + index + "()2")));
</div>""".formatted(index, getSnippetHtmlRepresentation("A.html", t.expectedOutput(), Optional.of("java"), Optional.of("snippet-case" + index + "()2")));
checkOutput("A.html", true, html);
});
}
Expand Down
Loading

0 comments on commit 6df7acb

Please sign in to comment.