1 /* 2 * Copyright (c) 2017, 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 import java.util.regex.Matcher; 32 import java.util.regex.Pattern; 33 import javax.lang.model.element.Element; 34 35 import com.sun.source.doctree.DocTree; 36 import com.sun.source.doctree.UnknownInlineTagTree; 37 import jdk.javadoc.doclet.Taglet; 38 39 import static com.sun.source.doctree.DocTree.Kind.*; 40 41 /** 42 * An inline tag to conveniently insert an external link. 43 * The tag can be used as follows: 44 * {@extLink name description}. 45 * <p> 46 * Using the tag as follows: 47 * <br> 48 * {@code Please see {@extLink Borealis a spectacular} sight.} 49 * <br> 50 * the taglet will produce the following html: 51 * <br><br> 52 * {@code Please see <cite>a spectacular</cite> sight.} 53 * <br><br> 54 * If the property {@code build.tools.taglet.ExtLink.LINK} is set, using 55 * the above example, the taglet will produce the following html: 56 * <br><br> 57 * {@code 58 * Please see <a href="https://www.oracle.com/pls/topic/lookup?ctx=javase9&id=Borealis">a spectacular</a> sight. 59 * } 60 */ 61 public class ExtLink implements Taglet { 62 63 static final String TAG_NAME = "extLink"; 64 65 static final String URL = "https://www.oracle.com/pls/topic/lookup?ctx=javase9&id="; 66 67 static final Pattern TAG_PATTERN = Pattern.compile("(\\s*)(?<name>\\w+)(\\s+)(?<desc>.*)"); 68 69 static final boolean LINK = Boolean.getBoolean("build.tools.taglet.ExtLink.LINK"); 70 71 /** 72 * Returns the set of locations in which the tag may be used. 73 */ 74 @Override 75 public Set<Location> getAllowedLocations() { 76 return EnumSet.allOf(jdk.javadoc.doclet.Taglet.Location.class); 77 } 78 79 @Override 80 public boolean isInlineTag() { 81 return true; 82 } 83 84 @Override 85 public String getName() { 86 return TAG_NAME; 87 } 88 89 @Override 90 public String toString(List<? extends DocTree> tags, Element elem) { 91 92 if (tags.isEmpty()) 93 return ""; 94 95 DocTree tag = tags.get(0); 96 if (tag.getKind() != UNKNOWN_INLINE_TAG) 97 return ""; 98 99 UnknownInlineTagTree uitree = (UnknownInlineTagTree) tag; 100 if (uitree.getContent().isEmpty()) 101 return ""; 102 103 String tagText = uitree.getContent().get(0).toString(); 104 Matcher m = TAG_PATTERN.matcher(tagText); 105 if (!m.find()) 106 return ""; 107 108 StringBuilder sb = new StringBuilder(); 109 if (LINK) { 110 sb.append("<a href=\"") 111 .append(URL) 112 .append(m.group("name")) 113 .append("\">") 114 .append(m.group("desc")) 115 .append("</a>"); 116 } else { 117 sb.append("<cite>") 118 .append(m.group("desc")) 119 .append("</cite>"); 120 } 121 return sb.toString(); 122 } 123 }