Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.google.doclava; |
| 18 | |
| 19 | import com.google.clearsilver.jsilver.data.Data; |
| 20 | |
| 21 | import java.io.*; |
| 22 | import java.util.regex.Pattern; |
| 23 | import java.util.regex.Matcher; |
| 24 | |
| 25 | |
| 26 | public 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 Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 54 | 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 Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 75 | 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 Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 144 | String filename = outfile; |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 145 | // 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 Nurik | 3352706 | 2012-03-06 12:24:35 -0800 | [diff] [blame] | 149 | hdf.setValue("design", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 150 | } else if (filename.indexOf("develop") == 0) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 151 | hdf.setValue("develop", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 152 | } else if (filename.indexOf("guide") == 0) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 153 | hdf.setValue("guide", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 154 | } else if (filename.indexOf("training") == 0) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 155 | hdf.setValue("training", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 156 | } else if (filename.indexOf("more") == 0) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 157 | hdf.setValue("more", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 158 | } else if (filename.indexOf("google") == 0) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 159 | hdf.setValue("google", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 160 | } else if (filename.indexOf("distribute") == 0) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 161 | hdf.setValue("distribute", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 162 | } else if (filename.indexOf("about") == 0) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 163 | hdf.setValue("about", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 164 | } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 165 | hdf.setValue("tools", "true"); |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 166 | fromTemplate = hdf.getValue("page.template", ""); |
Robert Ly | 88c435b | 2013-02-01 11:55:04 -0800 | [diff] [blame^] | 167 | } 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 Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 175 | } |
Dirk Dougherty | 9b316c8 | 2013-01-28 10:53:33 -0800 | [diff] [blame] | 176 | if (fromTemplate.equals("sdk")) { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 177 | ClearPage.write(hdf, "sdkpage.cs", outfile); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 178 | } else { |
Scott Main | 3c925b4 | 2012-06-21 20:35:04 -0700 | [diff] [blame] | 179 | ClearPage.write(hdf, "docpage.cs", outfile); |
Ben Dodson | 920dbbb | 2010-08-04 15:21:06 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } // writePage |
| 183 | } |