1 /* 2 * Copyright (C) 2009 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 package com.android.layoutopt.cli; 18 19 import com.android.layoutopt.uix.LayoutAnalyzer; 20 import com.android.layoutopt.uix.LayoutAnalysis; 21 22 import java.io.File; 23 import java.util.List; 24 import java.util.ArrayList; 25 26 /** 27 * Command line utility for the uix library. 28 * 29 * This is a simple CLI front-end for the uix library, used to 30 * analyze and optimize Android layout files. 31 */ 32 public class Main { 33 /** 34 * Main entry point of the application. 35 * 36 * @param args One mandatory parameter, a path (absolute or relative) 37 * to an Android XML layout file 38 */ 39 public static void main(String[] args) { 40 Parameters p = checkParameters(args); 41 if (!p.valid) { 42 displayHelpMessage(); 43 exit(); 44 } 45 46 analyzeFiles(p.files); 47 } 48 49 private static void analyzeFiles(File[] files) { 50 LayoutAnalyzer analyzer = new LayoutAnalyzer(); 51 for (File file : files) { 52 if (file.isFile() && file.getName().endsWith(".xml")) { 53 analyze(analyzer, file); 54 } else if (file.isDirectory()) { 55 analyzeFiles(file.listFiles()); 56 } 57 } 58 } 59 60 private static void analyze(LayoutAnalyzer analyzer, File file) { 61 LayoutAnalysis analysis = analyzer.analyze(file); 62 System.out.println(analysis.getName()); 63 for (LayoutAnalysis.Issue issue : analysis.getIssues()) { 64 System.out.print(String.format("\t%d:%d ", issue.getStartLine(), issue.getEndLine())); 65 System.out.println(issue.getDescription()); 66 } 67 } 68 69 /** 70 * Exits the tool. 71 */ 72 private static void exit() { 73 System.exit(0); 74 } 75 76 /** 77 * Displays this tool's help message on the standard output. 78 */ 79 private static void displayHelpMessage() { 80 System.out.println("usage: layoutopt <directories/files to analyze>"); 81 } 82 83 /** 84 * Builds a valid Parameters object. Parses the paramters if necessary 85 * and checks for errors. 86 * 87 * @param args The parameters passed from the CLI. 88 */ 89 private static Parameters checkParameters(String[] args) { 90 Parameters p = new Parameters(); 91 92 if (args.length < 1) { 93 p.valid = false; 94 } else { 95 List<File> files = new ArrayList<File>(); 96 for (String path : args) { 97 File file = new File(path); 98 if (file.exists() && (file.isDirectory() || file.getName().endsWith(".xml"))) { 99 files.add(file); 100 } 101 } 102 p.files = files.toArray(new File[files.size()]); 103 p.valid = true; 104 } 105 106 return p; 107 } 108 109 /** 110 * Parameters parsed from the CLI. 111 */ 112 private static class Parameters { 113 /** 114 * True if this list of parameters is valid, false otherwise. 115 */ 116 boolean valid; 117 118 /** 119 * Paths (absolute or relative) to the files to be analyzed. 120 */ 121 File[] files; 122 } 123 } 124