• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SampleCode(String source, String dest, String title)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 
write(boolean offlineMode)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);
48         else writeDirectory(f, mDest);
49     }
50 
convertExtension(String s, String ext)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 
inList(String s, String[] list)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 
writeDirectory(File dir, String relative)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 
writeIndexOnly(File dir, String relative)121     public void writeIndexOnly(File dir, String relative) {
122         HDF hdf = writeIndex(dir);
123         ClearPage.write(hdf, "sampleindex.cs", relative + "/index" +
124                         DroidDoc.htmlExtension);
125     }
126 
writeIndex(File dir)127     public HDF writeIndex(File dir) {
128         HDF hdf = DroidDoc.makeHDF();
129 
130         hdf.setValue("page.title", dir.getName() + " - " + mTitle);
131         hdf.setValue("projectTitle", mTitle);
132 
133         String filename = dir.getPath() + "/_index.html";
134         String summary = SampleTagInfo.readFile(new SourcePositionInfo(filename,
135                           -1,-1), filename, "sample code", true, false, true);
136 
137         if (summary == null) {
138             summary = "";
139         }
140         hdf.setValue("summary", summary);
141 
142         return hdf;
143     }
144 
writePage(File f, String out, String subdir)145     public void writePage(File f, String out, String subdir) {
146         String name = f.getName();
147 
148         String filename = f.getPath();
149         String data = SampleTagInfo.readFile(new SourcePositionInfo(filename, -1,-1), filename,
150                                                 "sample code", true, true, true);
151         data = DroidDoc.escape(data);
152 
153         HDF hdf = DroidDoc.makeHDF();
154 
155         hdf.setValue("page.title", name);
156         hdf.setValue("subdir", subdir);
157         hdf.setValue("realFile", name);
158         hdf.setValue("fileContents", data);
159 
160         ClearPage.write(hdf, "sample.cs", out);
161     }
162 
writeImagePage(File f, String out, String subdir)163     public void writeImagePage(File f, String out, String subdir) {
164         String name = f.getName();
165 
166         String data = "<img src=\"" + name + "\" title=\"" + name + "\" />";
167 
168         HDF hdf = DroidDoc.makeHDF();
169 
170         hdf.setValue("page.title", name);
171         hdf.setValue("subdir", subdir);
172         hdf.setValue("realFile", name);
173         hdf.setValue("fileContents", data);
174 
175         ClearPage.write(hdf, "sample.cs", out);
176     }
177 }
178