|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package build.tools.taglet; |
| 27 | + |
| 28 | +import java.util.EnumSet; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | + |
| 33 | +import javax.lang.model.element.Element; |
| 34 | +import javax.tools.Diagnostic; |
| 35 | + |
| 36 | + |
| 37 | +import com.sun.source.doctree.DocTree; |
| 38 | +import com.sun.source.doctree.UnknownInlineTagTree; |
| 39 | +import jdk.javadoc.doclet.Doclet; |
| 40 | +import jdk.javadoc.doclet.DocletEnvironment; |
| 41 | +import jdk.javadoc.doclet.Reporter; |
| 42 | +import jdk.javadoc.doclet.StandardDoclet; |
| 43 | +import jdk.javadoc.doclet.Taglet; |
| 44 | + |
| 45 | +import static com.sun.source.doctree.DocTree.Kind.UNKNOWN_INLINE_TAG; |
| 46 | + |
| 47 | +/** |
| 48 | + * An inline tag to insert a note formatted as preview note. |
| 49 | + * The tag can be used as follows: |
| 50 | + * |
| 51 | + * <pre> |
| 52 | + * {@previewNote jep-number [Preview note heading]} |
| 53 | + * Preview note content |
| 54 | + * {@previewNote} |
| 55 | + * </pre> |
| 56 | + * |
| 57 | + */ |
| 58 | +public class PreviewNote implements Taglet { |
| 59 | + |
| 60 | + static final String TAG_NAME = "previewNote"; |
| 61 | + Reporter reporter = null; |
| 62 | + |
| 63 | + @Override |
| 64 | + public void init(DocletEnvironment env, Doclet doclet) { |
| 65 | + if (doclet instanceof StandardDoclet stdoclet) { |
| 66 | + reporter = stdoclet.getReporter(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the set of locations in which the tag may be used. |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public Set<Location> getAllowedLocations() { |
| 75 | + return EnumSet.allOf(Taglet.Location.class); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean isInlineTag() { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String getName() { |
| 85 | + return TAG_NAME; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String toString(List<? extends DocTree> tags, Element elem) { |
| 90 | + |
| 91 | + for (DocTree tag : tags) { |
| 92 | + if (tag.getKind() == UNKNOWN_INLINE_TAG) { |
| 93 | + UnknownInlineTagTree inlineTag = (UnknownInlineTagTree) tag; |
| 94 | + String[] content = inlineTag.getContent().toString().trim().split("\\s+", 2); |
| 95 | + if (!content[0].isBlank()) { |
| 96 | + StringBuilder sb = new StringBuilder(""" |
| 97 | + <div class="preview-block" style="margin-top:10px; display:block; max-width:max-content;"> |
| 98 | + """); |
| 99 | + if (content.length == 2) { |
| 100 | + sb.append(""" |
| 101 | + <div class="preview-label"> |
| 102 | + """) |
| 103 | + .append(content[1]) |
| 104 | + .append(""" |
| 105 | + </div> |
| 106 | + """); |
| 107 | + } |
| 108 | + sb.append(""" |
| 109 | + <div class="preview-comment"> |
| 110 | + """); |
| 111 | + return sb.toString(); |
| 112 | + } else { |
| 113 | + return """ |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + """; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (reporter == null) { |
| 122 | + throw new IllegalArgumentException("@" + TAG_NAME + " taglet content must be begin or end"); |
| 123 | + } |
| 124 | + reporter.print(Diagnostic.Kind.ERROR, "@" + TAG_NAME + " taglet content must be begin or end"); |
| 125 | + return ""; |
| 126 | + } |
| 127 | +} |
0 commit comments