blob: bf544454e401ddbc1e58108ff086433080ce0552 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
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
17import org.clearsilver.HDF;
18import org.clearsilver.CS;
19import java.util.*;
20import java.io.*;
21import java.util.regex.Pattern;
22import java.util.regex.Matcher;
23
24
25public 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
Scott Main2fa99f12009-11-20 10:41:49 -080041 public void write(boolean offlineMode) {
The Android Open Source Project88b60792009-03-03 19:28:42 -080042 File f = new File(mSource);
43 if (!f.isDirectory()) {
44 System.out.println("-samplecode not a directory: " + mSource);
45 return;
46 }
Dirk Dougherty502c4982009-12-02 18:01:16 -080047 if (offlineMode) writeIndexOnly(f, mDest, offlineMode);
Scott Main2fa99f12009-11-20 10:41:49 -080048 else writeDirectory(f, mDest);
The Android Open Source Project88b60792009-03-03 19:28:42 -080049 }
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;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800103
Scott Main2fa99f12009-11-20 10:41:49 -0800104 HDF hdf = writeIndex(dir);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800105 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 }
Scott Main2fa99f12009-11-20 10:41:49 -0800117
118 ClearPage.write(hdf, "sampleindex.cs", relative + "/index" + DroidDoc.htmlExtension);
119 }
120
Dirk Dougherty502c4982009-12-02 18:01:16 -0800121 public void writeIndexOnly(File dir, String relative, Boolean offline) {
Scott Main2fa99f12009-11-20 10:41:49 -0800122 HDF hdf = writeIndex(dir);
Dirk Dougherty502c4982009-12-02 18:01:16 -0800123 if (!offline) relative = "/" + relative;
124 ClearPage.write(hdf, "sampleindex.cs", relative + "index" +
Scott Main2fa99f12009-11-20 10:41:49 -0800125 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
The Android Open Source Project88b60792009-03-03 19:28:42 -0800134 String filename = dir.getPath() + "/_index.html";
Scott Main2fa99f12009-11-20 10:41:49 -0800135 String summary = SampleTagInfo.readFile(new SourcePositionInfo(filename,
136 -1,-1), filename, "sample code", true, false, true);
137
The Android Open Source Project88b60792009-03-03 19:28:42 -0800138 if (summary == null) {
139 summary = "";
140 }
141 hdf.setValue("summary", summary);
Scott Main2fa99f12009-11-20 10:41:49 -0800142
143 return hdf;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800144 }
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}