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=javase9&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=javase9&id="; 60 61 static final Pattern TAG_PATTERN = Pattern.compile("(\\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 } | 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 } |