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