Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2008 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 
     17 import java.io.IOException;
     18 import java.io.FileWriter;
     19 import java.util.regex.Pattern;
     20 import java.util.regex.Matcher;
     21 
     22 public class Proofread
     23 {
     24     static FileWriter out = null;
     25     static final Pattern WHITESPACE = Pattern.compile("\\r?\\n");
     26     static final String INDENT = "        ";
     27     static final String NEWLINE = "\n" + INDENT;
     28 
     29     public static void initProofread(String filename)
     30     {
     31         try {
     32             out = new FileWriter(filename);
     33             out.write("javadoc proofread file: " + filename + "\n");
     34         }
     35         catch (IOException e) {
     36             if (out != null) {
     37                 try {
     38                     out.close();
     39                 }
     40                 catch (IOException ex) {
     41                 }
     42                 out = null;
     43             }
     44             System.err.println("error opening file: " + filename);
     45         }
     46     }
     47 
     48     public static void finishProofread(String filename)
     49     {
     50         if (out == null) {
     51             return;
     52         }
     53 
     54         try {
     55             out.close();
     56         }
     57         catch (IOException e) {
     58         }
     59     }
     60 
     61     public static void write(String s)
     62     {
     63         if (out == null) {
     64             return ;
     65         }
     66         try {
     67             out.write(s);
     68         }
     69         catch (IOException e) {
     70         }
     71     }
     72 
     73     public static void writeIndented(String s)
     74     {
     75         s = s.trim();
     76         Matcher m = WHITESPACE.matcher(s);
     77         s = m.replaceAll(NEWLINE);
     78         write(INDENT);
     79         write(s);
     80         write("\n");
     81     }
     82 
     83     public static void writeFileHeader(String filename)
     84     {
     85         write("\n\n=== ");
     86         write(filename);
     87         write(" ===\n");
     88     }
     89 
     90     public static void writeTagList(TagInfo[] tags)
     91     {
     92         if (out == null) {
     93             return;
     94         }
     95 
     96         for (TagInfo t: tags) {
     97             String k = t.kind();
     98             if ("Text".equals(t.name())) {
     99                 writeIndented(t.text());
    100             }
    101             else if ("@more".equals(k)) {
    102                 writeIndented("");
    103             }
    104             else if ("@see".equals(k)) {
    105                 SeeTagInfo see = (SeeTagInfo)t;
    106                 String label = see.label();
    107                 if (label == null) {
    108                     label = "";
    109                 }
    110                 writeIndented("{" + see.name() + " ... " + label + "}");
    111             }
    112             else if ("@code".equals(k)) {
    113                 writeIndented(t.text());
    114             }
    115             else if ("@samplecode".equals(k)) {
    116                 writeIndented(t.text());
    117             }
    118             else {
    119                 writeIndented("{" + (t.name() != null ? t.name() : "") + "/" +
    120                         t.text() + "}");
    121             }
    122         }
    123     }
    124 
    125     public static void writePackages(String filename, TagInfo[] tags)
    126     {
    127         if (out == null) {
    128             return;
    129         }
    130 
    131         writeFileHeader(filename);
    132         writeTagList(tags);
    133     }
    134 
    135     public static void writePackage(String filename, TagInfo[] tags)
    136     {
    137         if (out == null) {
    138             return;
    139         }
    140 
    141         writeFileHeader(filename);
    142         writeTagList(tags);
    143     }
    144 
    145     public static void writeClass(String filename, ClassInfo cl)
    146     {
    147         if (out == null) {
    148             return;
    149         }
    150 
    151         writeFileHeader(filename);
    152         writeTagList(cl.inlineTags());
    153 
    154         // enum constants
    155         for (FieldInfo f: cl.enumConstants()) {
    156             write("ENUM: " + f.name() + "\n");
    157             writeTagList(f.inlineTags());
    158         }
    159 
    160         // fields
    161         for (FieldInfo f: cl.selfFields()) {
    162             write("FIELD: " + f.name() + "\n");
    163             writeTagList(f.inlineTags());
    164         }
    165 
    166         // constructors
    167         for (MethodInfo m: cl.constructors()) {
    168             write("CONSTRUCTOR: " + m.name() + "\n");
    169             writeTagList(m.inlineTags().tags());
    170         }
    171 
    172         // methods
    173         for (MethodInfo m: cl.selfMethods()) {
    174             write("METHOD: " + m.name() + "\n");
    175             writeTagList(m.inlineTags().tags());
    176         }
    177     }
    178 }
    179