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("./libcore/junit/src/main/java"); 69 sourcePath.add("./development/tools/hosttestlib/src"); 70 sourcePath.add("./libcore/dalvik/src/main/java"); 71 sourcePath.add("./cts/tests/src"); 72 sourcePath.add(sourceDir.toString()); 73 return join(sourcePath, ":"); 74 } 75 getClassPath()76 private String getClassPath() { 77 List<String> classPath = new ArrayList<String>(); 78 classPath.add("./prebuilt/common/tradefed/tradefed-prebuilt.jar"); 79 return join(classPath, ":"); 80 } 81 getSourceFiles(File sourceDir)82 private List<String> getSourceFiles(File sourceDir) { 83 List<String> sourceFiles = new ArrayList<String>(); 84 85 File[] files = sourceDir.listFiles(new FileFilter() { 86 @Override 87 public boolean accept(File pathname) { 88 return pathname.isDirectory() || pathname.toString().endsWith(".java"); 89 } 90 }); 91 92 for (File file : files) { 93 if (file.isDirectory()) { 94 sourceFiles.addAll(getSourceFiles(file)); 95 } else { 96 sourceFiles.add(file.toString()); 97 } 98 } 99 100 return sourceFiles; 101 } 102 join(List<String> options, String delimiter)103 private String join(List<String> options, String delimiter) { 104 StringBuilder builder = new StringBuilder(); 105 int numOptions = options.size(); 106 for (int i = 0; i < numOptions; i++) { 107 builder.append(options.get(i)); 108 if (i + 1 < numOptions) { 109 builder.append(delimiter); 110 } 111 } 112 return builder.toString(); 113 } 114 } 115