blob: 38ac55c4a71be01cc520b66ad223e763b279576f [file] [log] [blame]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001/*
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();
36 FileReader reader = new FileReader(f);
37 char[] buf = new char[length];
38 int index = 0;
39 int amt;
40 while (true) {
41 amt = reader.read(buf, index, length-index);
42
43 if (amt < 1) {
44 break;
45 }
46
47 index += amt;
48 }
49 return new String(buf, 0, index);
50 }
51 catch (IOException e) {
52 return null;
53 }
54 }
55
56 public static void writePage(String docfile, String relative,
57 String outfile)
58 {
59 HDF hdf = DroidDoc.makeHDF();
60
61 /*
62 System.out.println("docfile='" + docfile
63 + "' relative='" + relative + "'"
64 + "' outfile='" + outfile + "'");
65 */
66
67 String filedata = readFile(docfile);
68
69 // The document is properties up until the line "@jd:body".
70 // Any blank lines are ignored.
71 int start = -1;
72 int lineno = 1;
73 Matcher lines = LINE.matcher(filedata);
74 String line = null;
75 while (lines.find()) {
76 line = lines.group(1);
77 if (line.length() > 0) {
78 if (line.equals("@jd:body")) {
79 start = lines.end();
80 break;
81 }
82 Matcher prop = PROP.matcher(line);
83 if (prop.matches()) {
84 String key = prop.group(1);
85 String value = prop.group(2);
86 hdf.setValue(key, value);
87 } else {
88 break;
89 }
90 }
91 lineno++;
92 }
93 if (start < 0) {
94 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
95 if (line != null) {
96 System.err.println(docfile + ":" + lineno + ":" + line);
97 }
98 System.exit(1);
99 }
100
101 // if they asked to only be for a certain template, maybe skip it
102 String fromTemplate = hdf.getValue("template.which", "");
103 String fromPage = hdf.getValue("page.onlyfortemplate", "");
104 if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
105 return;
106 }
107
108 // and the actual text after that
109 String commentText = filedata.substring(start);
110
111 Comment comment = new Comment(commentText, null,
112 new SourcePositionInfo(docfile, lineno, 1));
113 TagInfo[] tags = comment.tags();
114
115 TagInfo.makeHDF(hdf, "root.descr", tags);
116
117 hdf.setValue("commentText", commentText);
118
119 if(outfile.indexOf("sdk/") != -1) {
120 hdf.setValue("sdk", "true");
121 if(outfile.indexOf("index.html") != -1) {
122 ClearPage.write(hdf, "sdkpage.cs", outfile);
123 }else{
124 ClearPage.write(hdf, "docpage.cs", outfile);
125 }
126 }else if(outfile.indexOf("guide/") != -1){
127 hdf.setValue("guide", "true");
128 ClearPage.write(hdf, "docpage.cs", outfile);
129 }else if(outfile.indexOf("publish/") != -1){
130 hdf.setValue("publish", "true");
131 ClearPage.write(hdf, "docpage.cs", outfile);
132 }else{
133 ClearPage.write(hdf, "nosidenavpage.cs", outfile);
134 }
135 }
136
137}