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 import static jdk.javadoc.doclet.Taglet.Location.*; 41 42 /** 43 * An inline tag to conveniently insert an external link. 44 * The tag can be used as follows: 45 * {@extLink name description}, for example 46 * <p> 47 * {@code Please see {@extLink Borealis a spectacular} sight.} 48 * <p> 49 * will produce the following html 50 * <p> 51 * {@code 52 * Please see <a href="https://www.oracle.com/pls/topic/lookup?ctx=javase10&id=Borealis">a spectacular</a> sight. 53 * } 54 */ 55 public class ExtLink implements Taglet { 56 57 static final String TAG_NAME = "extLink"; 58 59 static final String URL = "https://www.oracle.com/pls/topic/lookup?ctx=javase10&id="; 60 61 static final Pattern TAG_PATTERN = Pattern.compile("(?s)(\\s*)(?<name>\\w+)(\\s+)(?<desc>.*)$"); 62 63 /** 64 * Returns the set of locations in which the tag may be used. 65 */ 66 @Override 67 public Set<Location> getAllowedLocations() { 68 return EnumSet.allOf(jdk.javadoc.doclet.Taglet.Location.class); 69 } 70 71 @Override 72 public boolean isInlineTag() { 73 return true; 74 } 75 76 @Override 77 public String getName() { 78 return TAG_NAME; 79 } 80 81 @Override 82 public String toString(List<? extends DocTree> tags, Element elem) { 83 84 if (tags.isEmpty()) 85 return ""; 86 87 DocTree tag = tags.get(0); 88 if (tag.getKind() != UNKNOWN_INLINE_TAG) 89 return ""; 90 91 UnknownInlineTagTree uitree = (UnknownInlineTagTree) tag; 92 if (uitree.getContent().isEmpty()) 93 return ""; 94 95 String tagText = uitree.getContent().get(0).toString(); 96 Matcher m = TAG_PATTERN.matcher(tagText); 97 if (!m.find()) 98 return ""; 99 100 StringBuilder sb = new StringBuilder("<a href=\""); 101 sb.append(URL) 102 .append(m.group("name")) 103 .append("\">") 104 .append(m.group("desc")) 105 .append("</a>"); 106 107 return sb.toString(); 108 } 109 }