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 com.google.clearsilver.jsilver.data.Data;
     20 
     21 import java.io.*;
     22 import java.util.regex.Pattern;
     23 import java.util.regex.Matcher;
     24 
     25 
     26 public class DocFile {
     27   private static final Pattern LINE = Pattern.compile("(.*)[\r]?\n", Pattern.MULTILINE);
     28   private static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
     29 
     30   public static String readFile(String filename) {
     31     try {
     32       File f = new File(filename);
     33       int length = (int) f.length();
     34       FileInputStream is = new FileInputStream(f);
     35       InputStreamReader reader = new InputStreamReader(is, "UTF-8");
     36       char[] buf = new char[length];
     37       int index = 0;
     38       int amt;
     39       while (true) {
     40         amt = reader.read(buf, index, length - index);
     41 
     42         if (amt < 1) {
     43           break;
     44         }
     45 
     46         index += amt;
     47       }
     48       return new String(buf, 0, index);
     49     } catch (IOException e) {
     50       return null;
     51     }
     52   }
     53 
     54   public static void writePage(String docfile, String relative, String outfile) {
     55     Data hdf = Doclava.makeHDF();
     56 
     57     /*
     58      * System.out.println("docfile='" + docfile + "' relative='" + relative + "'" + "' outfile='" +
     59      * outfile + "'");
     60      */
     61 
     62     String filedata = readFile(docfile);
     63 
     64     // The document is properties up until the line "@jd:body".
     65     // Any blank lines are ignored.
     66     int start = -1;
     67     int lineno = 1;
     68     Matcher lines = LINE.matcher(filedata);
     69     String line = null;
     70     while (lines.find()) {
     71       line = lines.group(1);
     72       if (line.length() > 0) {
     73         if (line.equals("@jd:body")) {
     74           start = lines.end();
     75           break;
     76         }
     77         Matcher prop = PROP.matcher(line);
     78         if (prop.matches()) {
     79           String key = prop.group(1);
     80           String value = prop.group(2);
     81           hdf.setValue(key, value);
     82         } else {
     83           break;
     84         }
     85       }
     86       lineno++;
     87     }
     88     if (start < 0) {
     89       System.err.println(docfile + ":" + lineno + ": error parsing docfile");
     90       if (line != null) {
     91         System.err.println(docfile + ":" + lineno + ":" + line);
     92       }
     93       System.exit(1);
     94     }
     95 
     96     // if they asked to only be for a certain template, maybe skip it
     97     String fromTemplate = hdf.getValue("template.which", "");
     98     String fromPage = hdf.getValue("page.onlyfortemplate", "");
     99     if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
    100       return;
    101     }
    102 
    103     // and the actual text after that
    104     String commentText = filedata.substring(start);
    105 
    106     Comment comment = new Comment(commentText, null, new SourcePositionInfo(docfile, lineno, 1));
    107     TagInfo[] tags = comment.tags();
    108 
    109     TagInfo.makeHDF(hdf, "root.descr", tags);
    110 
    111     hdf.setValue("commentText", commentText);
    112 
    113     // write the page using the appropriate root template, based on the
    114     // whichdoc value supplied by build
    115     String fromWhichmodule = hdf.getValue("android.whichmodule", "");
    116     if (fromWhichmodule.equals("online-pdk")) {
    117       // leaving this in just for temporary compatibility with pdk doc
    118       hdf.setValue("online-pdk", "true");
    119       // add any conditional login for root template here (such as
    120       // for custom left nav based on tab etc.
    121       ClearPage.write(hdf, "docpage.cs", outfile);
    122     } else {
    123       if (outfile.indexOf("sdk/") != -1) {
    124         hdf.setValue("sdk", "true");
    125         if ((outfile.indexOf("index.html") != -1) && (outfile.indexOf("preview/") == -1)) {
    126           ClearPage.write(hdf, "sdkpage.cs", outfile);
    127         } else {
    128           ClearPage.write(hdf, "docpage.cs", outfile);
    129         }
    130       } else if (outfile.indexOf("guide/") != -1) {
    131         hdf.setValue("guide", "true");
    132         ClearPage.write(hdf, "docpage.cs", outfile);
    133       } else if ((outfile.indexOf("resources/") != -1) || (outfile.indexOf("training/") != -1)) {
    134         hdf.setValue("resources", "true");
    135         ClearPage.write(hdf, "docpage.cs", outfile);
    136       } else {
    137         ClearPage.write(hdf, "nosidenavpage.cs", outfile);
    138       }
    139     }
    140   } // writePage
    141 }
    142