• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
readFile(String filename)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 
54   public static String[] DEVSITE_VALID_LANGS = {"en", "es","ja", "ko", "ru", "zh-cn"};
55 
getPathRoot(String filename)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 
writePage(String docfile, String relative, String outfile)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 {
144       String filename = outfile;
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) {
149         hdf.setValue("design", "true");
150       } else if (filename.indexOf("develop") == 0) {
151         hdf.setValue("develop", "true");
152       } else if (filename.indexOf("guide") == 0) {
153         hdf.setValue("guide", "true");
154       } else if (filename.indexOf("training") == 0) {
155         hdf.setValue("training", "true");
156       } else if (filename.indexOf("more") == 0) {
157         hdf.setValue("more", "true");
158       } else if (filename.indexOf("google") == 0) {
159         hdf.setValue("google", "true");
160       } else if (filename.indexOf("distribute") == 0) {
161         hdf.setValue("distribute", "true");
162       } else if (filename.indexOf("about") == 0) {
163         hdf.setValue("about", "true");
164       } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) {
165         hdf.setValue("tools", "true");
166         fromTemplate = hdf.getValue("page.template", "");
167       }
168       if (fromTemplate.equals("sdk")) {
169         ClearPage.write(hdf, "sdkpage.cs", outfile);
170       } else {
171         ClearPage.write(hdf, "docpage.cs", outfile);
172       }
173     }
174   } // writePage
175 }
176