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.uix; 18 19 import org.w3c.dom.Node; 20 21 import java.util.List; 22 import java.util.ArrayList; 23 24 import com.android.layoutopt.uix.groovy.LayoutAnalysisCategory; 25 26 /** 27 * Contains the results of a layout analysis. Instances of this class are 28 * generated by {@link com.android.layoutopt.uix.LayoutAnalyzer}. 29 * 30 * @see com.android.layoutopt.uix.LayoutAnalyzer 31 */ 32 public class LayoutAnalysis { 33 /** 34 * Default layout analysis used to describe a problem with the 35 * analysis process. 36 */ 37 static final LayoutAnalysis ERROR = new LayoutAnalysis(""); 38 static { 39 ERROR.mAnalyzed = false; 40 ERROR.addIssue("The layout could not be analyzed. Check if you specified a valid " 41 + "XML layout, if the specified file exists, etc."); 42 } 43 44 private final List<Issue> mIssues = new ArrayList<Issue>(); 45 private String mName; 46 private boolean mAnalyzed; 47 private Node mNode; 48 49 /** 50 * Creates a new analysis. An analysis is always considered invalid by default. 51 * 52 * @see #validate() 53 * @see #isValid() 54 */ 55 LayoutAnalysis(String name) { 56 mName = name; 57 } 58 59 /** 60 * Returns the name of this analysis. 61 */ 62 public String getName() { 63 return mName; 64 } 65 66 void setName(String name) { 67 mName = name; 68 } 69 70 /** 71 * Adds an issue to the layout analysis. 72 * 73 * @param issue The issue to add. 74 */ 75 public void addIssue(Issue issue) { 76 mIssues.add(issue); 77 } 78 79 /** 80 * Adds an issue to the layout analysis. 81 * 82 * @param description Description of the issue. 83 */ 84 public void addIssue(String description) { 85 mIssues.add(new Issue(mNode, description)); 86 } 87 88 /** 89 * Adds an issue to the layout analysis. 90 * 91 * @param node The node containing the issue. 92 * @param description Description of the issue. 93 */ 94 public void addIssue(Node node, String description) { 95 mIssues.add(new Issue(node, description)); 96 } 97 98 /** 99 * Returns the list of issues found during the analysis. 100 * 101 * @return A non-null array of {@link com.android.layoutopt.uix.LayoutAnalysis.Issue}. 102 */ 103 public Issue[] getIssues() { 104 return mIssues.toArray(new Issue[mIssues.size()]); 105 } 106 107 /** 108 * Indicates whether the layout was analyzed. If this method returns false, 109 * a probleme occured during the analysis (missing file, invalid document, etc.) 110 * 111 * @return True if the layout was analyzed, false otherwise. 112 */ 113 public boolean isValid() { 114 return mAnalyzed; 115 } 116 117 /** 118 * Validates the analysis. This must be call before this analysis can 119 * be considered valid. Calling this method resets the current node to null. 120 * 121 * @see #setCurrentNode(org.w3c.dom.Node) 122 */ 123 void validate() { 124 mAnalyzed = true; 125 mNode = null; 126 } 127 128 /** 129 * Sets the current node to be automatically added to created issues. 130 * 131 * @param node An XML node. 132 */ 133 void setCurrentNode(Node node) { 134 mNode = node; 135 } 136 137 /** 138 * Represents an issue discovered during the analysis process. 139 * An issue provides a human-readable description as well as optional solutions. 140 */ 141 public static class Issue { 142 private final String mDescription; 143 private final Node mNode; 144 145 /** 146 * Creates a new issue with the specified description. 147 * 148 * @param description The description of the issue. 149 */ 150 public Issue(String description) { 151 mNode = null; 152 if (description == null) { 153 throw new IllegalArgumentException("The description must be non-null"); 154 } 155 mDescription = description; 156 } 157 158 /** 159 * Creates a new issue with the specified description. 160 * 161 * @param node The node in which the issue was found. 162 * @param description The description of the issue. 163 */ 164 public Issue(Node node, String description) { 165 mNode = node; 166 if (description == null) { 167 throw new IllegalArgumentException("The description must be non-null"); 168 } 169 mDescription = description; 170 } 171 172 /** 173 * Describes this issue to the user. 174 * 175 * @return A String describing the issue, always non-null. 176 */ 177 public String getDescription() { 178 return mDescription; 179 } 180 181 182 /** 183 * Returns the start line of this node. 184 * 185 * @return The start line or -1 if the line is unknown. 186 */ 187 public int getStartLine() { 188 return LayoutAnalysisCategory.getStartLine(mNode); 189 } 190 191 /** 192 * Returns the end line of this node. 193 * 194 * @return The end line or -1 if the line is unknown. 195 */ 196 public int getEndLine() { 197 return LayoutAnalysisCategory.getEndLine(mNode); 198 } 199 } 200 } 201