• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 package com.android.cts.javascanner;
17 
18 import java.io.File;
19 import java.io.FileFilter;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Scanner;
24 
25 class DocletRunner {
26 
27     private final File mSourceDir;
28     private final File mDocletPath;
29 
DocletRunner(File sourceDir, File docletPath)30     DocletRunner(File sourceDir, File docletPath) {
31         mSourceDir = sourceDir;
32         mDocletPath = docletPath;
33     }
34 
runJavaDoc()35     int runJavaDoc() throws IOException, InterruptedException {
36         List<String> args = new ArrayList<String>();
37         args.add("javadoc");
38         args.add("-doclet");
39         args.add("com.android.cts.javascannerdoclet.CtsJavaScannerDoclet");
40         args.add("-docletpath");
41         args.add(mDocletPath.toString());
42         args.add("-sourcepath");
43         args.add(getSourcePath(mSourceDir));
44         args.add("-classpath");
45         args.add(getClassPath());
46         args.addAll(getSourceFiles(mSourceDir));
47 
48         Process process = new ProcessBuilder(args).start();
49         Scanner scanner = null;
50         try {
51             scanner = new Scanner(process.getInputStream());
52             while (scanner.hasNextLine()) {
53                 System.out.println(scanner.nextLine());
54             }
55         } finally {
56             if (scanner != null) {
57                 scanner.close();
58             }
59         }
60 
61         return process.waitFor();
62     }
63 
getSourcePath(File sourceDir)64     private String getSourcePath(File sourceDir) {
65         List<String> sourcePath = new ArrayList<String>();
66         sourcePath.add("./frameworks/base/core/java");
67         sourcePath.add("./frameworks/base/test-runner/src");
68         sourcePath.add("./external/junit/src");
69         sourcePath.add("./development/tools/hosttestlib/src");
70         sourcePath.add("./libcore/dalvik/src/main/java");
71         sourcePath.add("./cts/tests/src");
72         // PTS adds PtsAndroidTestCase
73         sourcePath.add("./cts/suite/pts/deviceTests/ptsutil/src");
74         sourcePath.add("./cts/libs/util/src");
75         sourcePath.add("./frameworks/testing/uiautomator/library/testrunner-src");
76         sourcePath.add(sourceDir.toString());
77         return join(sourcePath, ":");
78     }
79 
getClassPath()80     private String getClassPath() {
81         List<String> classPath = new ArrayList<String>();
82         classPath.add("./prebuilts/misc/common/tradefed/tradefed-prebuilt.jar");
83         return join(classPath, ":");
84     }
85 
getSourceFiles(File sourceDir)86     private List<String> getSourceFiles(File sourceDir) {
87         List<String> sourceFiles = new ArrayList<String>();
88 
89         File[] files = sourceDir.listFiles(new FileFilter() {
90             @Override
91             public boolean accept(File pathname) {
92                 return pathname.isDirectory() || pathname.toString().endsWith(".java");
93             }
94         });
95 
96         for (File file : files) {
97             if (file.isDirectory()) {
98                 sourceFiles.addAll(getSourceFiles(file));
99             } else {
100                 sourceFiles.add(file.toString());
101             }
102         }
103 
104         return sourceFiles;
105     }
106 
join(List<String> options, String delimiter)107     private String join(List<String> options, String delimiter) {
108         StringBuilder builder = new StringBuilder();
109         int numOptions = options.size();
110         for (int i = 0; i < numOptions; i++) {
111             builder.append(options.get(i));
112             if (i + 1 < numOptions) {
113                 builder.append(delimiter);
114             }
115         }
116         return builder.toString();
117     }
118 }
119