1 /* 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 17 import org.clearsilver.HDF; 18 import org.clearsilver.CS; 19 import java.util.*; 20 import java.io.*; 21 import java.util.regex.Pattern; 22 import java.util.regex.Matcher; 23 24 25 public 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 readFile(String filename)31 public static String readFile(String filename) 32 { 33 try { 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]; 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 writePage(String docfile, String relative, String outfile)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); 119 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); 129 } else { 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); 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); 140 } else if (outfile.indexOf("resources/") != -1) { 141 hdf.setValue("resources", "true"); 142 ClearPage.write(hdf, "docpage.cs", outfile); 143 } else { 144 ClearPage.write(hdf, "nosidenavpage.cs", outfile); 145 } 146 } 147 } //writePage 148 } 149