blob: dd0a6bc769334756b70716c2cac686316013e2ef [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 {
27 private static final Pattern LINE = Pattern.compile("(.*)[\r]?\n", Pattern.MULTILINE);
28 private static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
29
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 Dougherty9b316c82013-01-28 10:53:33 -080054 public static String[] DEVSITE_VALID_LANGS = {"en", "es","ja", "ko", "ru", "zh-cn"};
55
56 public static String getPathRoot(String filename) {
57 String[] stripStr = filename.split("\\/");
58 String outFrag = stripStr[0];
59 if (stripStr.length > 0) {
60 for (String t : DEVSITE_VALID_LANGS) {
61 if (stripStr[0].equals("intl")) {
62 if (stripStr[1].equals(t)) {
63 outFrag = stripStr[2];
64 break;
65 }
66 } else if (stripStr[0].equals(t)) {
67 outFrag = stripStr[1];
68 break;
69 }
70 }
71 }
72 return outFrag;
73 }
74
Ben Dodson920dbbb2010-08-04 15:21:06 -070075 public static void writePage(String docfile, String relative, String outfile) {
76 Data hdf = Doclava.makeHDF();
77
78 /*
79 * System.out.println("docfile='" + docfile + "' relative='" + relative + "'" + "' outfile='" +
80 * outfile + "'");
81 */
82
83 String filedata = readFile(docfile);
84
85 // The document is properties up until the line "@jd:body".
86 // Any blank lines are ignored.
87 int start = -1;
88 int lineno = 1;
89 Matcher lines = LINE.matcher(filedata);
90 String line = null;
91 while (lines.find()) {
92 line = lines.group(1);
93 if (line.length() > 0) {
94 if (line.equals("@jd:body")) {
95 start = lines.end();
96 break;
97 }
98 Matcher prop = PROP.matcher(line);
99 if (prop.matches()) {
100 String key = prop.group(1);
101 String value = prop.group(2);
102 hdf.setValue(key, value);
103 } else {
104 break;
105 }
106 }
107 lineno++;
108 }
109 if (start < 0) {
110 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
111 if (line != null) {
112 System.err.println(docfile + ":" + lineno + ":" + line);
113 }
114 System.exit(1);
115 }
116
117 // if they asked to only be for a certain template, maybe skip it
118 String fromTemplate = hdf.getValue("template.which", "");
119 String fromPage = hdf.getValue("page.onlyfortemplate", "");
120 if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
121 return;
122 }
123
124 // and the actual text after that
125 String commentText = filedata.substring(start);
126
127 Comment comment = new Comment(commentText, null, new SourcePositionInfo(docfile, lineno, 1));
128 TagInfo[] tags = comment.tags();
129
130 TagInfo.makeHDF(hdf, "root.descr", tags);
131
132 hdf.setValue("commentText", commentText);
133
134 // write the page using the appropriate root template, based on the
135 // whichdoc value supplied by build
136 String fromWhichmodule = hdf.getValue("android.whichmodule", "");
137 if (fromWhichmodule.equals("online-pdk")) {
138 // leaving this in just for temporary compatibility with pdk doc
139 hdf.setValue("online-pdk", "true");
140 // add any conditional login for root template here (such as
141 // for custom left nav based on tab etc.
142 ClearPage.write(hdf, "docpage.cs", outfile);
143 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700144 String filename = outfile;
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800145 // Strip out the intl and lang id substr and get back just the
146 // guide, design, distribute, etc.
147 filename = getPathRoot(filename);
148 if (filename.indexOf("design") == 0) {
Roman Nurik33527062012-03-06 12:24:35 -0800149 hdf.setValue("design", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800150 } else if (filename.indexOf("develop") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700151 hdf.setValue("develop", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800152 } else if (filename.indexOf("guide") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700153 hdf.setValue("guide", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800154 } else if (filename.indexOf("training") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700155 hdf.setValue("training", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800156 } else if (filename.indexOf("more") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700157 hdf.setValue("more", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800158 } else if (filename.indexOf("google") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700159 hdf.setValue("google", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800160 } else if (filename.indexOf("distribute") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700161 hdf.setValue("distribute", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800162 } else if (filename.indexOf("about") == 0) {
Scott Main3c925b42012-06-21 20:35:04 -0700163 hdf.setValue("about", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800164 } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) {
Scott Main3c925b42012-06-21 20:35:04 -0700165 hdf.setValue("tools", "true");
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800166 fromTemplate = hdf.getValue("page.template", "");
Robert Ly88c435b2013-02-01 11:55:04 -0800167 } else if (filename.indexOf("devices") == 0) {
168 hdf.setValue("devices", "true");
169 } else if (filename.indexOf("source") == 0) {
170 hdf.setValue("source", "true");
171 } else if (filename.indexOf("accessories") == 0) {
172 hdf.setValue("accessories", "true");
173 } else if (filename.indexOf("compatibility") == 0) {
174 hdf.setValue("compatibility", "true");
Scott Main3c925b42012-06-21 20:35:04 -0700175 }
Dirk Dougherty9b316c82013-01-28 10:53:33 -0800176 if (fromTemplate.equals("sdk")) {
Scott Main3c925b42012-06-21 20:35:04 -0700177 ClearPage.write(hdf, "sdkpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700178 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700179 ClearPage.write(hdf, "docpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700180 }
181 }
182 } // writePage
183}