1 /* 2 * Copyright (C) 2020 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.build.config; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 import java.util.regex.Pattern; 22 import java.util.regex.Matcher; 23 24 /** 25 * Class to hold the two types of variables we support, strings and lists of strings. 26 */ 27 public class Value { 28 private static final Pattern SPACES = Pattern.compile("\\s+"); 29 30 private final VarType mVarType; 31 private final Str mStr; 32 private final ArrayList<Str> mList; 33 34 /** 35 * Construct an appropriately typed empty value. 36 */ Value(VarType varType)37 public Value(VarType varType) { 38 mVarType = varType; 39 if (varType == VarType.LIST) { 40 mStr = null; 41 mList = new ArrayList(); 42 mList.add(new Str("")); 43 } else { 44 mStr = new Str(""); 45 mList = null; 46 } 47 } 48 Value(VarType varType, Str str)49 public Value(VarType varType, Str str) { 50 mVarType = varType; 51 mStr = str; 52 mList = null; 53 } 54 Value(List<Str> list)55 public Value(List<Str> list) { 56 mVarType = VarType.LIST; 57 mStr = null; 58 mList = new ArrayList(list); 59 } 60 getVarType()61 public VarType getVarType() { 62 return mVarType; 63 } 64 getStr()65 public Str getStr() { 66 return mStr; 67 } 68 getList()69 public List<Str> getList() { 70 return mList; 71 } 72 73 /** 74 * Normalize a string that is behaving as a list. 75 */ normalize(String str)76 public static String normalize(String str) { 77 if (str == null) { 78 return null; 79 } 80 return SPACES.matcher(str.trim()).replaceAll(" ").trim(); 81 } 82 83 /** 84 * Normalize a string that is behaving as a list. 85 */ normalize(Str str)86 public static Str normalize(Str str) { 87 if (str == null) { 88 return null; 89 } 90 return new Str(str.getPosition(), normalize(str.toString())); 91 } 92 93 /** 94 * Normalize a this Value into the same format as normalize(Str). 95 */ normalize(Value val)96 public static Str normalize(Value val) { 97 if (val == null) { 98 return null; 99 } 100 if (val.mStr != null) { 101 return normalize(val.mStr); 102 } 103 104 if (val.mList.size() == 0) { 105 return new Str(""); 106 } 107 108 StringBuilder result = new StringBuilder(); 109 final int size = val.mList.size(); 110 boolean first = true; 111 for (int i = 0; i < size; i++) { 112 String s = val.mList.get(i).toString().trim(); 113 if (s.length() > 0) { 114 if (!first) { 115 result.append(" "); 116 } else { 117 first = false; 118 } 119 result.append(s); 120 } 121 } 122 123 // Just use the first item's position. 124 return new Str(val.mList.get(0).getPosition(), result.toString()); 125 } 126 127 /** 128 * Put each word in 'str' on its own line in make format. If 'val' is null, 129 * 'nullValue' is returned. 130 */ oneLinePerWord(Value val, String nullValue)131 public static String oneLinePerWord(Value val, String nullValue) { 132 if (val == null) { 133 return nullValue; 134 } 135 final String s = normalize(val).toString(); 136 final Matcher m = SPACES.matcher(s); 137 final StringBuilder result = new StringBuilder(); 138 if (s.length() > 0 && (val.mVarType == VarType.LIST || m.find())) { 139 result.append("\\\n "); 140 } 141 result.append(m.replaceAll(" \\\\\n ")); 142 return result.toString(); 143 } 144 145 /** 146 * Put each word in 'str' on its own line in make format. If 'str' is null, 147 * nullValue is returned. 148 */ oneLinePerWord(Str str, String nullValue)149 public static String oneLinePerWord(Str str, String nullValue) { 150 if (str == null) { 151 return nullValue; 152 } 153 final Matcher m = SPACES.matcher(normalize(str.toString())); 154 final StringBuilder result = new StringBuilder(); 155 if (m.find()) { 156 result.append("\\\n "); 157 } 158 result.append(m.replaceAll(" \\\\\n ")); 159 return result.toString(); 160 } 161 162 /** 163 * Return a string representing this value with detailed debugging information. 164 */ debugString(Value val)165 public static String debugString(Value val) { 166 if (val == null) { 167 return "null"; 168 } 169 170 final StringBuilder str = new StringBuilder("Value("); 171 if (val.mStr != null) { 172 str.append("mStr="); 173 str.append("\""); 174 str.append(val.mStr.toString()); 175 str.append("\""); 176 if (false) { 177 str.append(" ("); 178 str.append(val.mStr.getPosition().toString()); 179 str.append(")"); 180 } 181 } 182 if (val.mList != null) { 183 str.append("mList="); 184 str.append("["); 185 for (Str s: val.mList) { 186 str.append(" \""); 187 str.append(s.toString()); 188 if (false) { 189 str.append("\" ("); 190 str.append(s.getPosition().toString()); 191 str.append(")"); 192 } else { 193 str.append("\""); 194 } 195 } 196 str.append(" ]"); 197 } 198 str.append(")"); 199 return str.toString(); 200 } 201 202 /** 203 * Get the Positions of all of the parts of this Value. 204 */ getPositions()205 public List<Position> getPositions() { 206 List<Position> result = new ArrayList(); 207 if (mStr != null) { 208 result.add(mStr.getPosition()); 209 } 210 if (mList != null) { 211 for (Str str: mList) { 212 result.add(str.getPosition()); 213 } 214 } 215 return result; 216 } 217 } 218 219