Home | History | Annotate | Download | only in srcgen
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.icu4j.srcgen;
     17 
     18 import com.google.currysrc.api.process.Reporter;
     19 import com.google.currysrc.api.process.ast.AstNodes;
     20 import com.google.currysrc.processors.BaseTagElementNodeScanner;
     21 
     22 import org.eclipse.jdt.core.dom.AST;
     23 import org.eclipse.jdt.core.dom.IDocElement;
     24 import org.eclipse.jdt.core.dom.TagElement;
     25 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
     26 
     27 import java.util.List;
     28 
     29 /**
     30  * Replaces {@icu}, {@icuenhanced} and {@icunote} with text.
     31  */
     32 public class ReplaceIcuTags extends BaseTagElementNodeScanner {
     33 
     34   @Override
     35   protected boolean visitTagElement(Reporter reporter, ASTRewrite rewrite, TagElement tag) {
     36     String tagName = tag.getTagName();
     37     if (tagName != null) {
     38       AST ast = tag.getAST();
     39       List<IDocElement> fragments = tag.fragments();
     40       if (tagName.equalsIgnoreCase("@icu")) {
     41         // ICU replaces {@icu __usage__} with "Methods, fields, and other functionality specific to
     42         // ICU are labeled '[icu]'"
     43         // ICU replaces {@icu} with [icu]
     44         if (fragments.size() == 0) {
     45           rewrite.replace(tag, createIcuMarker(ast), null /* editGroup */);
     46         } else {
     47           IDocElement element = fragments.get(0);
     48           if (element.toString().trim().equalsIgnoreCase("_usage_")) {
     49             rewrite.replace(tag, createIcuUsageText(ast), null /* editGroup */);
     50           } else {
     51             throw new AssertionError("Unknown Javadoc tag: " + tag);
     52           }
     53         }
     54         return false;
     55       } else if (tagName.equalsIgnoreCase("@icuenhanced")) {
     56         // ICU replaces {@icuenhanced <classname>} with "[icu enhancement] ICU's replacement for
     57         // <classname>"
     58         IDocElement element = fragments.get(0);
     59         rewrite.replace(tag, createIcuEnhancementText(ast, element), null /* editGroup */);
     60         return false;
     61       } else if (tagName.equalsIgnoreCase("@icunote")) {
     62         // ICU replaces {@icunote} with "[icu] Note:"
     63         rewrite.replace(tag, createIcuNoteText(ast), null /* editGroup */);
     64         return false;
     65       } else if (tagName.equalsIgnoreCase("@discouraged")) {
     66         // ICU replaces {@discouraged} with "Discouraged:"
     67         IDocElement element = fragments.get(0);
     68         rewrite.replace(tag, createDiscouragedText(ast, element), null /* editGroup */);
     69         return false;
     70       }
     71     }
     72     return true;
     73   }
     74 
     75   @Override
     76   public String toString() {
     77     return "ReplaceIcuTags{}";
     78   }
     79 
     80   private static TagElement createIcuEnhancementText(AST ast, IDocElement fragment) {
     81     return AstNodes.createTextTagElement(ast,
     82         "<strong>[icu enhancement]</strong> ICU's replacement for {@link" + fragment.toString()
     83             + "}");
     84   }
     85 
     86   private static TagElement createIcuUsageText(AST ast) {
     87     // Use of &nbsp; is a hacky way to preserve whitespace.
     88     return AstNodes.createTextTagElement(ast,
     89         "&nbsp;Methods, fields, and other functionality specific to ICU are labeled"
     90             + " '<strong>[icu]</strong>'.");
     91   }
     92 
     93   private static TagElement createIcuNoteText(AST ast) {
     94     return AstNodes.createTextTagElement(ast, "<strong>[icu] Note:</strong>");
     95   }
     96 
     97   private static TagElement createIcuMarker(AST ast) {
     98     return AstNodes.createTextTagElement(ast, "<strong>[icu]</strong>");
     99   }
    100 
    101   private static TagElement createDiscouragedText(AST ast, IDocElement fragment) {
    102     return AstNodes.createTextTagElement(
    103         ast, "@apiNote <strong>Discouraged:</strong>" + fragment.toString());
    104   }
    105 }
    106