blob: c3ad2c1139a411c0ec7a8b809269b133f22aa706 [file] [log] [blame]
Ben Dodson920dbbb2010-08-04 15:21:06 -07001/*
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
17package com.google.doclava;
18
19import com.google.clearsilver.jsilver.data.Data;
20
21import java.io.*;
22import java.util.regex.Pattern;
23import java.util.regex.Matcher;
24
25
26public class DocFile {
Scott Main71bf8d22013-03-08 18:59:15 -080027 public static final Pattern LINE = Pattern.compile("(.*)[\r]?\n", Pattern.MULTILINE);
28 public static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
Ben Dodson920dbbb2010-08-04 15:21:06 -070029
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
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -070054 public static String[] DEVSITE_VALID_LANGS = {"en", "es","ja", "ko",
55 "ru", "zh-cn", "zh-tw", "pt-br"};
Dirk Dougherty9b316c82013-01-28 10:53:33 -080056
57 public static String getPathRoot(String filename) {
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -080058 //look for a valid lang string in the file path. If found,
59 //snip the intl/lang from the path.
60 for (String t : DEVSITE_VALID_LANGS) {
61 int langStart = filename.indexOf("/" + t + "/");
62 if (langStart > -1) {
63 int langEnd = filename.indexOf("/", langStart + 1);
64 filename = filename.substring(langEnd + 1);
65 break;
Dirk Dougherty9b316c82013-01-28 10:53:33 -080066 }
67 }
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -080068 return filename;
Dirk Dougherty9b316c82013-01-28 10:53:33 -080069 }
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -080070
Dirk Dougherty8a8f7912013-11-26 18:45:31 -080071 public static Data getPageMetadata (String docfile, Data hdf) {
72 //utility method for extracting metadata without generating file output.
73 if (hdf == null) {
74 hdf = Doclava.makeHDF();
75 }
76 String filedata = readFile(docfile);
77
78 // The document is properties up until the line "@jd:body".
79 // Any blank lines are ignored.
80 int start = -1;
81 int lineno = 1;
82 Matcher lines = LINE.matcher(filedata);
83 String line = null;
84 while (lines.find()) {
85 line = lines.group(1);
86 if (line.length() > 0) {
87 if (line.equals("@jd:body")) {
88 start = lines.end();
89 break;
90 }
91 Matcher prop = PROP.matcher(line);
92 if (prop.matches()) {
93 String key = prop.group(1);
94 String value = prop.group(2);
95 hdf.setValue(key, value);
96 } else {
97 break;
98 }
99 }
100 lineno++;
101 }
102 if (start < 0) {
103 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
104 if (line != null) {
105 System.err.println(docfile + ":" + lineno + ":" + line);
106 }
107 System.exit(1);
108 }
109 return hdf;
110 }
111
Dirk Doughertye52f6d82013-09-04 17:27:29 -0700112 public static void writePage(String docfile, String relative, String outfile, Data hdf) {
Ben Dodson920dbbb2010-08-04 15:21:06 -0700113
114 /*
115 * System.out.println("docfile='" + docfile + "' relative='" + relative + "'" + "' outfile='" +
116 * outfile + "'");
117 */
Dirk Doughertye52f6d82013-09-04 17:27:29 -0700118 if (hdf == null) {
smain@google.comfb768102015-04-10 19:31:56 -0700119 hdf = Doclava.makeHDF();
120 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700121 String filedata = readFile(docfile);
122
123 // The document is properties up until the line "@jd:body".
124 // Any blank lines are ignored.
125 int start = -1;
126 int lineno = 1;
127 Matcher lines = LINE.matcher(filedata);
128 String line = null;
129 while (lines.find()) {
130 line = lines.group(1);
131 if (line.length() > 0) {
132 if (line.equals("@jd:body")) {
133 start = lines.end();
134 break;
135 }
136 Matcher prop = PROP.matcher(line);
137 if (prop.matches()) {
138 String key = prop.group(1);
139 String value = prop.group(2);
140 hdf.setValue(key, value);
141 } else {
142 break;
143 }
144 }
145 lineno++;
146 }
147 if (start < 0) {
148 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
149 if (line != null) {
150 System.err.println(docfile + ":" + lineno + ":" + line);
151 }
152 System.exit(1);
153 }
154
155 // if they asked to only be for a certain template, maybe skip it
156 String fromTemplate = hdf.getValue("template.which", "");
157 String fromPage = hdf.getValue("page.onlyfortemplate", "");
158 if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
159 return;
160 }
161
162 // and the actual text after that
163 String commentText = filedata.substring(start);
164
165 Comment comment = new Comment(commentText, null, new SourcePositionInfo(docfile, lineno, 1));
166 TagInfo[] tags = comment.tags();
167
168 TagInfo.makeHDF(hdf, "root.descr", tags);
169
170 hdf.setValue("commentText", commentText);
171
172 // write the page using the appropriate root template, based on the
173 // whichdoc value supplied by build
174 String fromWhichmodule = hdf.getValue("android.whichmodule", "");
175 if (fromWhichmodule.equals("online-pdk")) {
176 // leaving this in just for temporary compatibility with pdk doc
177 hdf.setValue("online-pdk", "true");
178 // add any conditional login for root template here (such as
179 // for custom left nav based on tab etc.
180 ClearPage.write(hdf, "docpage.cs", outfile);
181 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700182 String filename = outfile;
smain@google.comfb768102015-04-10 19:31:56 -0700183 // Strip out the intl and lang id substr and get back just the
184 // guide, design, distribute, etc.
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800185 filename = getPathRoot(filename);
186 if (filename.indexOf("design") == 0) {
Roman Nurik33527062012-03-06 12:24:35 -0800187 hdf.setValue("design", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700188 hdf.setValue("page.type", "design");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800189 } else if (filename.indexOf("develop") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700190 hdf.setValue("develop", "true");
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -0800191 hdf.setValue("page.type", "develop");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800192 } else if (filename.indexOf("guide") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700193 hdf.setValue("guide", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700194 hdf.setValue("page.type", "guide");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800195 } else if (filename.indexOf("training") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700196 hdf.setValue("training", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700197 hdf.setValue("page.type", "training");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800198 } else if (filename.indexOf("more") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700199 hdf.setValue("more", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800200 } else if (filename.indexOf("google") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700201 hdf.setValue("google", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700202 hdf.setValue("page.type", "google");
Dirk Dougherty1b45d1d2010-08-17 17:25:36 -0700203 } else if (filename.indexOf("samples") == 0) {
204 hdf.setValue("samples", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700205 hdf.setValue("page.type", "samples");
Dirk Dougherty8a8f7912013-11-26 18:45:31 -0800206 if (Doclava.samplesNavTree != null) {
207 hdf.setValue("samples_toc_tree", Doclava.samplesNavTree.getValue("samples_toc_tree", ""));
208 }
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800209 } else if (filename.indexOf("distribute") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700210 hdf.setValue("distribute", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700211 hdf.setValue("page.type", "distribute");
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -0800212 if (filename.indexOf("distribute/googleplay") == 0) {
213 hdf.setValue("googleplay", "true");
214 } else if (filename.indexOf("distribute/essentials") == 0) {
215 hdf.setValue("essentials", "true");
216 } else if (filename.indexOf("distribute/users") == 0) {
217 hdf.setValue("users", "true");
218 } else if (filename.indexOf("distribute/engage") == 0) {
219 hdf.setValue("engage", "true");
220 } else if (filename.indexOf("distribute/monetize") == 0) {
221 hdf.setValue("monetize", "true");
Dirk Dougherty9bb67672015-01-08 13:40:19 -0800222 } else if (filename.indexOf("distribute/analyze") == 0) {
223 hdf.setValue("analyze", "true");
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -0800224 } else if (filename.indexOf("distribute/tools") == 0) {
Dirk Dougherty024e9b22015-05-26 14:46:19 -0700225 hdf.setValue("essentials", "true");
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -0800226 } else if (filename.indexOf("distribute/stories") == 0) {
227 hdf.setValue("stories", "true");
228 }
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800229 } else if (filename.indexOf("about") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700230 hdf.setValue("about", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700231 hdf.setValue("page.type", "about");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800232 } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) {
Scott Main3c925b42012-06-21 20:35:04 -0700233 hdf.setValue("tools", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700234 hdf.setValue("page.type", "tools");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800235 fromTemplate = hdf.getValue("page.template", "");
Robert Ly88c435b2013-02-01 11:55:04 -0800236 } else if (filename.indexOf("devices") == 0) {
237 hdf.setValue("devices", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700238 hdf.setValue("page.type", "devices");
Robert Ly88c435b2013-02-01 11:55:04 -0800239 } else if (filename.indexOf("source") == 0) {
240 hdf.setValue("source", "true");
241 } else if (filename.indexOf("accessories") == 0) {
242 hdf.setValue("accessories", "true");
243 } else if (filename.indexOf("compatibility") == 0) {
244 hdf.setValue("compatibility", "true");
Scott Main16010db2014-03-27 10:00:10 -0700245 } else if (filename.indexOf("wear") == 0) {
246 hdf.setValue("wear", "true");
Robert Ly32f2de22014-06-04 00:25:39 -0700247 } else if (filename.indexOf("preview") == 0) {
248 hdf.setValue("preview", "true");
Dirk Doughertye8cdc5b2015-05-06 21:40:45 -0700249 hdf.setValue("page.type", "preview");
Robert Ly32f2de22014-06-04 00:25:39 -0700250 } else if (filename.indexOf("auto") == 0) {
251 hdf.setValue("auto", "true");
252 } else if (filename.indexOf("tv") == 0) {
253 hdf.setValue("tv", "true");
smain@google.comfb768102015-04-10 19:31:56 -0700254 } else if (filename.indexOf("ndk") == 0) {
255 hdf.setValue("ndk", "true");
256 hdf.setValue("page.type", "ndk");
257 if (filename.indexOf("ndk/guides") == 0) {
smain@google.com26333ca2015-04-10 20:39:32 -0700258 hdf.setValue("guide", "true");
smain@google.comfb768102015-04-10 19:31:56 -0700259 } else if (filename.indexOf("ndk/reference") == 0) {
260 hdf.setValue("reference", "true");
smain@google.com26333ca2015-04-10 20:39:32 -0700261 } else if (filename.indexOf("ndk/samples") == 0) {
262 hdf.setValue("samples", "true");
263 } else if (filename.indexOf("ndk/downloads") == 0) {
264 hdf.setValue("downloads", "true");
David Friedman4d6512a2015-05-23 02:05:46 -0700265 fromTemplate = hdf.getValue("page.template", "");
266
smain@google.comfb768102015-04-10 19:31:56 -0700267 }
Scott Main3c925b42012-06-21 20:35:04 -0700268 }
Dirk Dougherty8bfc32b2014-02-07 20:04:27 -0800269 //set metadata for this file in jd_lists_unified
270 PageMetadata.setPageMetadata(docfile, relative, outfile, hdf, Doclava.sTaglist);
271
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800272 if (fromTemplate.equals("sdk")) {
Scott Main3c925b42012-06-21 20:35:04 -0700273 ClearPage.write(hdf, "sdkpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700274 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700275 ClearPage.write(hdf, "docpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700276 }
277 }
278 } // writePage
279}