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 org.clearsilver.HDF;
     18 import org.clearsilver.CS;
     19 import java.util.*;
     20 import java.io.*;
     21 import java.util.regex.Pattern;
     22 import java.util.regex.Matcher;
     23 
     24 
     25 public class SampleCode {
     26     String mSource;
     27     String mDest;
     28     String mTitle;
     29 
     30     public SampleCode(String source, String dest, String title) {
     31         mSource = source;
     32         mTitle = title;
     33         int len = dest.length();
     34         if (len > 1 && dest.charAt(len-1) != '/') {
     35             mDest = dest + '/';
     36         } else {
     37             mDest = dest;
     38         }
     39     }
     40 
     41     public void write(boolean offlineMode) {
     42         File f = new File(mSource);
     43         if (!f.isDirectory()) {
     44             System.out.println("-samplecode not a directory: " + mSource);
     45             return;
     46         }
     47         if (offlineMode) writeIndexOnly(f, mDest, offlineMode);
     48         else writeDirectory(f, mDest);
     49     }
     50 
     51     public static String convertExtension(String s, String ext) {
     52         return s.substring(0, s.lastIndexOf('.')) + ext;
     53     }
     54 
     55     public static String[] IMAGES = { ".png", ".jpg", ".gif" };
     56     public static String[] TEMPLATED = { ".java", ".xml" };
     57 
     58     public static boolean inList(String s, String[] list) {
     59         for (String t: list) {
     60             if (s.endsWith(t)) {
     61                 return true;
     62             }
     63         }
     64         return false;
     65     }
     66 
     67     public void writeDirectory(File dir, String relative) {
     68         TreeSet<String> dirs = new TreeSet<String>();
     69         TreeSet<String> files = new TreeSet<String>();
     70 
     71         String subdir = relative; //.substring(mDest.length());
     72 
     73         for (File f: dir.listFiles()) {
     74             String name = f.getName();
     75             if (name.startsWith(".") || name.startsWith("_")) {
     76                 continue;
     77             }
     78             if (f.isFile()) {
     79                 String out = relative + name;
     80 
     81                 if (inList(out, IMAGES)) {
     82                     // copied directly
     83                     ClearPage.copyFile(f, out);
     84                     writeImagePage(f, convertExtension(out, DroidDoc.htmlExtension), subdir);
     85                     files.add(name);
     86                 }
     87                 if (inList(out, TEMPLATED)) {
     88                     // copied and goes through the template
     89                     ClearPage.copyFile(f, out);
     90                     writePage(f, convertExtension(out, DroidDoc.htmlExtension), subdir);
     91                     files.add(name);
     92                 }
     93                 // else ignored
     94             }
     95             else if (f.isDirectory()) {
     96                 writeDirectory(f, relative + name + "/");
     97                 dirs.add(name);
     98             }
     99         }
    100 
    101         // write the index page
    102         int i;
    103 
    104         HDF hdf = writeIndex(dir);
    105         hdf.setValue("subdir", subdir);
    106         i=0;
    107         for (String d: dirs) {
    108             hdf.setValue("subdirs." + i + ".name", d);
    109             i++;
    110         }
    111         i=0;
    112         for (String f: files) {
    113             hdf.setValue("files." + i + ".name", f);
    114             hdf.setValue("files." + i + ".href", convertExtension(f, ".html"));
    115             i++;
    116         }
    117 
    118         ClearPage.write(hdf, "sampleindex.cs", relative + "/index" + DroidDoc.htmlExtension);
    119     }
    120 
    121     public void writeIndexOnly(File dir, String relative, Boolean offline) {
    122         HDF hdf = writeIndex(dir);
    123         if (!offline) relative = "/" + relative;
    124         ClearPage.write(hdf, "sampleindex.cs", relative + "index" +
    125                         DroidDoc.htmlExtension);
    126     }
    127 
    128     public HDF writeIndex(File dir) {
    129         HDF hdf = DroidDoc.makeHDF();
    130 
    131         hdf.setValue("page.title", dir.getName() + " - " + mTitle);
    132         hdf.setValue("projectTitle", mTitle);
    133 
    134         String filename = dir.getPath() + "/_index.html";
    135         String summary = SampleTagInfo.readFile(new SourcePositionInfo(filename,
    136                           -1,-1), filename, "sample code", true, false, true);
    137 
    138         if (summary == null) {
    139             summary = "";
    140         }
    141         hdf.setValue("summary", summary);
    142 
    143         return hdf;
    144     }
    145 
    146     public void writePage(File f, String out, String subdir) {
    147         String name = f.getName();
    148 
    149         String filename = f.getPath();
    150         String data = SampleTagInfo.readFile(new SourcePositionInfo(filename, -1,-1), filename,
    151                                                 "sample code", true, true, true);
    152         data = DroidDoc.escape(data);
    153 
    154         HDF hdf = DroidDoc.makeHDF();
    155 
    156         hdf.setValue("page.title", name);
    157         hdf.setValue("subdir", subdir);
    158         hdf.setValue("realFile", name);
    159         hdf.setValue("fileContents", data);
    160 
    161         ClearPage.write(hdf, "sample.cs", out);
    162     }
    163 
    164     public void writeImagePage(File f, String out, String subdir) {
    165         String name = f.getName();
    166 
    167         String data = "<img src=\"" + name + "\" title=\"" + name + "\" />";
    168 
    169         HDF hdf = DroidDoc.makeHDF();
    170 
    171         hdf.setValue("page.title", name);
    172         hdf.setValue("subdir", subdir);
    173         hdf.setValue("realFile", name);
    174         hdf.setValue("fileContents", data);
    175 
    176         ClearPage.write(hdf, "sample.cs", out);
    177     }
    178 }
    179