blob: c27c82ad3d453dea3ac2ad8849ff71860cf2ca25 [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) {
58 String[] stripStr = filename.split("\\/");
59 String outFrag = stripStr[0];
60 if (stripStr.length > 0) {
61 for (String t : DEVSITE_VALID_LANGS) {
62 if (stripStr[0].equals("intl")) {
63 if (stripStr[1].equals(t)) {
64 outFrag = stripStr[2];
65 break;
66 }
67 } else if (stripStr[0].equals(t)) {
68 outFrag = stripStr[1];
69 break;
70 }
71 }
72 }
73 return outFrag;
74 }
Dirk Doughertye9ab1b92014-02-22 00:49:44 +000075
Dirk Dougherty8a8f7912013-11-26 18:45:31 -080076 public static Data getPageMetadata (String docfile, Data hdf) {
77 //utility method for extracting metadata without generating file output.
78 if (hdf == null) {
79 hdf = Doclava.makeHDF();
80 }
81 String filedata = readFile(docfile);
82
83 // The document is properties up until the line "@jd:body".
84 // Any blank lines are ignored.
85 int start = -1;
86 int lineno = 1;
87 Matcher lines = LINE.matcher(filedata);
88 String line = null;
89 while (lines.find()) {
90 line = lines.group(1);
91 if (line.length() > 0) {
92 if (line.equals("@jd:body")) {
93 start = lines.end();
94 break;
95 }
96 Matcher prop = PROP.matcher(line);
97 if (prop.matches()) {
98 String key = prop.group(1);
99 String value = prop.group(2);
100 hdf.setValue(key, value);
101 } else {
102 break;
103 }
104 }
105 lineno++;
106 }
107 if (start < 0) {
108 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
109 if (line != null) {
110 System.err.println(docfile + ":" + lineno + ":" + line);
111 }
112 System.exit(1);
113 }
114 return hdf;
115 }
116
Dirk Doughertye52f6d82013-09-04 17:27:29 -0700117 public static void writePage(String docfile, String relative, String outfile, Data hdf) {
Ben Dodson920dbbb2010-08-04 15:21:06 -0700118
119 /*
120 * System.out.println("docfile='" + docfile + "' relative='" + relative + "'" + "' outfile='" +
121 * outfile + "'");
122 */
Dirk Doughertye52f6d82013-09-04 17:27:29 -0700123 if (hdf == null) {
124 hdf = Doclava.makeHDF();
125 }
Ben Dodson920dbbb2010-08-04 15:21:06 -0700126 String filedata = readFile(docfile);
127
128 // The document is properties up until the line "@jd:body".
129 // Any blank lines are ignored.
130 int start = -1;
131 int lineno = 1;
132 Matcher lines = LINE.matcher(filedata);
133 String line = null;
134 while (lines.find()) {
135 line = lines.group(1);
136 if (line.length() > 0) {
137 if (line.equals("@jd:body")) {
138 start = lines.end();
139 break;
140 }
141 Matcher prop = PROP.matcher(line);
142 if (prop.matches()) {
143 String key = prop.group(1);
144 String value = prop.group(2);
145 hdf.setValue(key, value);
146 } else {
147 break;
148 }
149 }
150 lineno++;
151 }
152 if (start < 0) {
153 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
154 if (line != null) {
155 System.err.println(docfile + ":" + lineno + ":" + line);
156 }
157 System.exit(1);
158 }
159
160 // if they asked to only be for a certain template, maybe skip it
161 String fromTemplate = hdf.getValue("template.which", "");
162 String fromPage = hdf.getValue("page.onlyfortemplate", "");
163 if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
164 return;
165 }
166
167 // and the actual text after that
168 String commentText = filedata.substring(start);
169
170 Comment comment = new Comment(commentText, null, new SourcePositionInfo(docfile, lineno, 1));
171 TagInfo[] tags = comment.tags();
172
173 TagInfo.makeHDF(hdf, "root.descr", tags);
174
175 hdf.setValue("commentText", commentText);
176
177 // write the page using the appropriate root template, based on the
178 // whichdoc value supplied by build
179 String fromWhichmodule = hdf.getValue("android.whichmodule", "");
180 if (fromWhichmodule.equals("online-pdk")) {
181 // leaving this in just for temporary compatibility with pdk doc
182 hdf.setValue("online-pdk", "true");
183 // add any conditional login for root template here (such as
184 // for custom left nav based on tab etc.
185 ClearPage.write(hdf, "docpage.cs", outfile);
186 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700187 String filename = outfile;
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800188 // Strip out the intl and lang id substr and get back just the
189 // guide, design, distribute, etc.
190 filename = getPathRoot(filename);
191 if (filename.indexOf("design") == 0) {
Roman Nurik33527062012-03-06 12:24:35 -0800192 hdf.setValue("design", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700193 hdf.setValue("page.type", "design");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800194 } else if (filename.indexOf("develop") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700195 hdf.setValue("develop", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800196 } else if (filename.indexOf("guide") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700197 hdf.setValue("guide", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700198 hdf.setValue("page.type", "guide");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800199 } else if (filename.indexOf("training") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700200 hdf.setValue("training", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700201 hdf.setValue("page.type", "training");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800202 } else if (filename.indexOf("more") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700203 hdf.setValue("more", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800204 } else if (filename.indexOf("google") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700205 hdf.setValue("google", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700206 hdf.setValue("page.type", "google");
Dirk Dougherty1b45d1d2010-08-17 17:25:36 -0700207 } else if (filename.indexOf("samples") == 0) {
208 hdf.setValue("samples", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700209 hdf.setValue("page.type", "samples");
Dirk Dougherty8a8f7912013-11-26 18:45:31 -0800210 if (Doclava.samplesNavTree != null) {
211 hdf.setValue("samples_toc_tree", Doclava.samplesNavTree.getValue("samples_toc_tree", ""));
212 }
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800213 } else if (filename.indexOf("distribute") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700214 hdf.setValue("distribute", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700215 hdf.setValue("page.type", "distribute");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800216 } else if (filename.indexOf("about") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700217 hdf.setValue("about", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700218 hdf.setValue("page.type", "about");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800219 } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) {
Scott Main3c925b42012-06-21 20:35:04 -0700220 hdf.setValue("tools", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700221 hdf.setValue("page.type", "tools");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800222 fromTemplate = hdf.getValue("page.template", "");
Robert Ly88c435b2013-02-01 11:55:04 -0800223 } else if (filename.indexOf("devices") == 0) {
224 hdf.setValue("devices", "true");
Dirk Doughertyf9d9cb02013-09-13 19:45:17 -0700225 hdf.setValue("page.type", "devices");
Robert Ly88c435b2013-02-01 11:55:04 -0800226 } else if (filename.indexOf("source") == 0) {
227 hdf.setValue("source", "true");
228 } else if (filename.indexOf("accessories") == 0) {
229 hdf.setValue("accessories", "true");
230 } else if (filename.indexOf("compatibility") == 0) {
231 hdf.setValue("compatibility", "true");
Scott Main16010db2014-03-27 10:00:10 -0700232 } else if (filename.indexOf("wear") == 0) {
233 hdf.setValue("wear", "true");
Scott Main3c925b42012-06-21 20:35:04 -0700234 }
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800235 if (fromTemplate.equals("sdk")) {
Scott Main3c925b42012-06-21 20:35:04 -0700236 ClearPage.write(hdf, "sdkpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700237 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700238 ClearPage.write(hdf, "docpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700239 }
240 }
241 } // writePage
242}