2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import org.clearsilver.HDF;
18 import org.clearsilver.CS;
21 import java.util.regex.Pattern;
22 import java.util.regex.Matcher;
27 private static final Pattern LINE = Pattern.compile("(.*)[\r]?\n",
29 private static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
31 public static String readFile(String filename)
34 File f = new File(filename);
35 int length = (int)f.length();
36 FileInputStream is = new FileInputStream(f);
37 InputStreamReader reader = new InputStreamReader(is, "UTF-8");
38 char[] buf = new char[length];
42 amt = reader.read(buf, index, length-index);
50 return new String(buf, 0, index);
52 catch (IOException e) {
57 public static void writePage(String docfile, String relative,
60 HDF hdf = DroidDoc.makeHDF();
63 System.out.println("docfile='" + docfile
64 + "' relative='" + relative + "'"
65 + "' outfile='" + outfile + "'");
68 String filedata = readFile(docfile);
70 // The document is properties up until the line "@jd:body".
71 // Any blank lines are ignored.
74 Matcher lines = LINE.matcher(filedata);
76 while (lines.find()) {
77 line = lines.group(1);
78 if (line.length() > 0) {
79 if (line.equals("@jd:body")) {
83 Matcher prop = PROP.matcher(line);
85 String key = prop.group(1);
86 String value = prop.group(2);
87 hdf.setValue(key, value);
95 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
97 System.err.println(docfile + ":" + lineno + ":" + line);
102 // if they asked to only be for a certain template, maybe skip it
103 String fromTemplate = hdf.getValue("template.which", "");
104 String fromPage = hdf.getValue("page.onlyfortemplate", "");
105 if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
109 // and the actual text after that
110 String commentText = filedata.substring(start);
112 Comment comment = new Comment(commentText, null,
113 new SourcePositionInfo(docfile, lineno, 1));
114 TagInfo[] tags = comment.tags();
116 TagInfo.makeHDF(hdf, "root.descr", tags);
118 hdf.setValue("commentText", commentText);
120 // write the page using the appropriate root template, based on the
121 // whichdoc value supplied by build
122 String fromWhichmodule = hdf.getValue("android.whichmodule", "");
123 if (fromWhichmodule.equals("online-pdk")) {
124 //leaving this in just for temporary compatibility with pdk doc
125 hdf.setValue("online-pdk", "true");
126 // add any conditional login for root template here (such as
127 // for custom left nav based on tab etc.
128 ClearPage.write(hdf, "docpage.cs", outfile);
130 if (outfile.indexOf("sdk/") != -1) {
131 hdf.setValue("sdk", "true");
132 if ((outfile.indexOf("index.html") != -1) && (outfile.indexOf("preview/") == -1)) {
133 ClearPage.write(hdf, "sdkpage.cs", outfile);
135 ClearPage.write(hdf, "docpage.cs", outfile);
137 } else if (outfile.indexOf("guide/") != -1) {
138 hdf.setValue("guide", "true");
139 ClearPage.write(hdf, "docpage.cs", outfile);
140 } else if (outfile.indexOf("resources/") != -1) {
141 hdf.setValue("resources", "true");
142 ClearPage.write(hdf, "docpage.cs", outfile);
144 ClearPage.write(hdf, "nosidenavpage.cs", outfile);