blob: cc7a8cfb8f0809ba62b4f4789c952b42a7179777 [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 DocFile
26{
27 private static final Pattern LINE = Pattern.compile("(.*)[\r]?\n",
28 Pattern.MULTILINE);
29 private static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
30
31 public static String readFile(String filename)
32 {
33 try {
34 File f = new File(filename);
35 int length = (int)f.length();
Dirk Dougherty4405a232009-07-07 17:43:27 -070036 FileInputStream is = new FileInputStream(f);
37 InputStreamReader reader = new InputStreamReader(is, "UTF-8");
The Android Open Source Project88b60792009-03-03 19:28:42 -080038 char[] buf = new char[length];
39 int index = 0;
40 int amt;
41 while (true) {
42 amt = reader.read(buf, index, length-index);
43
44 if (amt < 1) {
45 break;
46 }
47
48 index += amt;
49 }
50 return new String(buf, 0, index);
51 }
52 catch (IOException e) {
53 return null;
54 }
55 }
56
57 public static void writePage(String docfile, String relative,
58 String outfile)
59 {
60 HDF hdf = DroidDoc.makeHDF();
61
62 /*
63 System.out.println("docfile='" + docfile
64 + "' relative='" + relative + "'"
65 + "' outfile='" + outfile + "'");
66 */
67
68 String filedata = readFile(docfile);
69
70 // The document is properties up until the line "@jd:body".
71 // Any blank lines are ignored.
72 int start = -1;
73 int lineno = 1;
74 Matcher lines = LINE.matcher(filedata);
75 String line = null;
76 while (lines.find()) {
77 line = lines.group(1);
78 if (line.length() > 0) {
79 if (line.equals("@jd:body")) {
80 start = lines.end();
81 break;
82 }
83 Matcher prop = PROP.matcher(line);
84 if (prop.matches()) {
85 String key = prop.group(1);
86 String value = prop.group(2);
87 hdf.setValue(key, value);
88 } else {
89 break;
90 }
91 }
92 lineno++;
93 }
94 if (start < 0) {
95 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
96 if (line != null) {
97 System.err.println(docfile + ":" + lineno + ":" + line);
98 }
99 System.exit(1);
100 }
101
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)) {
106 return;
107 }
108
109 // and the actual text after that
110 String commentText = filedata.substring(start);
111
112 Comment comment = new Comment(commentText, null,
113 new SourcePositionInfo(docfile, lineno, 1));
114 TagInfo[] tags = comment.tags();
115
116 TagInfo.makeHDF(hdf, "root.descr", tags);
117
118 hdf.setValue("commentText", commentText);
Dirk Doughertyc521e262009-05-01 11:19:55 -0700119
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.
The Android Open Source Project88b60792009-03-03 19:28:42 -0800128 ClearPage.write(hdf, "docpage.cs", outfile);
129 } else {
Dirk Doughertyc521e262009-05-01 11:19:55 -0700130 if (outfile.indexOf("sdk/") != -1) {
131 hdf.setValue("sdk", "true");
Dirk Doughertyd8cfce02009-07-15 16:37:21 -0700132 if ((outfile.indexOf("index.html") != -1) || (outfile.indexOf("features.html") != -1)) {
Dirk Doughertyc521e262009-05-01 11:19:55 -0700133 ClearPage.write(hdf, "sdkpage.cs", outfile);
134 } else {
135 ClearPage.write(hdf, "docpage.cs", outfile);
136 }
137 } else if (outfile.indexOf("guide/") != -1) {
138 hdf.setValue("guide", "true");
139 ClearPage.write(hdf, "docpage.cs", outfile);
Dirk Dougherty502c4982009-12-02 18:01:16 -0800140 } else if (outfile.indexOf("resources/") != -1) {
141 hdf.setValue("resources", "true");
Scott Main36690722009-12-17 12:52:54 -0800142 ClearPage.write(hdf, "docpage.cs", outfile);
Dirk Doughertyc521e262009-05-01 11:19:55 -0700143 } else {
144 ClearPage.write(hdf, "nosidenavpage.cs", outfile);
145 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800146 }
Dirk Doughertyc521e262009-05-01 11:19:55 -0700147 } //writePage
The Android Open Source Project88b60792009-03-03 19:28:42 -0800148}