blob: e2283bd2dbfb9b215a3aa489e266c735d7693c80 [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
41 public void write() {
42 File f = new File(mSource);
43 if (!f.isDirectory()) {
44 System.out.println("-samplecode not a directory: " + mSource);
45 return;
46 }
47 writeDirectory(f, mDest);
48 }
49
50 public static String convertExtension(String s, String ext) {
51 return s.substring(0, s.lastIndexOf('.')) + ext;
52 }
53
54 public static String[] IMAGES = { ".png", ".jpg", ".gif" };
55 public static String[] TEMPLATED = { ".java", ".xml" };
56
57 public static boolean inList(String s, String[] list) {
58 for (String t: list) {
59 if (s.endsWith(t)) {
60 return true;
61 }
62 }
63 return false;
64 }
65
66 public void writeDirectory(File dir, String relative) {
67 TreeSet<String> dirs = new TreeSet<String>();
68 TreeSet<String> files = new TreeSet<String>();
69
70 String subdir = relative; //.substring(mDest.length());
71
72 for (File f: dir.listFiles()) {
73 String name = f.getName();
74 if (name.startsWith(".") || name.startsWith("_")) {
75 continue;
76 }
77 if (f.isFile()) {
78 String out = relative + name;
79
80 if (inList(out, IMAGES)) {
81 // copied directly
82 ClearPage.copyFile(f, out);
83 writeImagePage(f, convertExtension(out, DroidDoc.htmlExtension), subdir);
84 files.add(name);
85 }
86 if (inList(out, TEMPLATED)) {
87 // copied and goes through the template
88 ClearPage.copyFile(f, out);
89 writePage(f, convertExtension(out, DroidDoc.htmlExtension), subdir);
90 files.add(name);
91 }
92 // else ignored
93 }
94 else if (f.isDirectory()) {
95 writeDirectory(f, relative + name + "/");
96 dirs.add(name);
97 }
98 }
99
100 // write the index page
101 int i;
102 HDF hdf = DroidDoc.makeHDF();
103
104 hdf.setValue("page.title", dir.getName() + " - " + mTitle);
105 hdf.setValue("projectTitle", mTitle);
106 hdf.setValue("subdir", subdir);
107 i=0;
108 for (String d: dirs) {
109 hdf.setValue("subdirs." + i + ".name", d);
110 i++;
111 }
112 i=0;
113 for (String f: files) {
114 hdf.setValue("files." + i + ".name", f);
115 hdf.setValue("files." + i + ".href", convertExtension(f, ".html"));
116 i++;
117 }
118 String filename = dir.getPath() + "/_index.html";
119 String summary = SampleTagInfo.readFile(new SourcePositionInfo(filename, -1,-1), filename,
120 "sample code", true, false, true);
121 if (summary == null) {
122 summary = "";
123 }
124 hdf.setValue("summary", summary);
125
126 ClearPage.write(hdf, "sampleindex.cs", relative + "/index" + DroidDoc.htmlExtension);
127 }
128
129 public void writePage(File f, String out, String subdir) {
130 String name = f.getName();
131
132 String filename = f.getPath();
133 String data = SampleTagInfo.readFile(new SourcePositionInfo(filename, -1,-1), filename,
134 "sample code", true, true, true);
135 data = DroidDoc.escape(data);
136
137 HDF hdf = DroidDoc.makeHDF();
138
139 hdf.setValue("page.title", name);
140 hdf.setValue("subdir", subdir);
141 hdf.setValue("realFile", name);
142 hdf.setValue("fileContents", data);
143
144 ClearPage.write(hdf, "sample.cs", out);
145 }
146
147 public void writeImagePage(File f, String out, String subdir) {
148 String name = f.getName();
149
150 String data = "<img src=\"" + name + "\" title=\"" + name + "\" />";
151
152 HDF hdf = DroidDoc.makeHDF();
153
154 hdf.setValue("page.title", name);
155 hdf.setValue("subdir", subdir);
156 hdf.setValue("realFile", name);
157 hdf.setValue("fileContents", data);
158
159 ClearPage.write(hdf, "sample.cs", out);
160 }
161}