1 /* 2 * Copyright (C) 2008 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.sdklib.internal.avd; 18 19 import com.android.sdklib.ISdkLog; 20 21 import java.io.BufferedReader; 22 import java.io.File; 23 import java.io.FileInputStream; 24 import java.io.FileNotFoundException; 25 import java.io.IOException; 26 import java.io.InputStreamReader; 27 import java.util.Map; 28 import java.util.TreeMap; 29 import java.util.regex.Matcher; 30 import java.util.regex.Pattern; 31 32 public class HardwareProperties { 33 private final static Pattern PATTERN_PROP = Pattern.compile( 34 "^([a-zA-Z0-9._-]+)\\s*=\\s*(.*)\\s*$"); 35 36 private final static String HW_PROP_NAME = "name"; 37 private final static String HW_PROP_TYPE = "type"; 38 private final static String HW_PROP_DEFAULT = "default"; 39 private final static String HW_PROP_ABSTRACT = "abstract"; 40 private final static String HW_PROP_DESC = "description"; 41 42 private final static String BOOLEAN_YES = "yes"; 43 private final static String BOOLEAN_NO = "no"; 44 public final static String[] BOOLEAN_VALUES = new String[] { BOOLEAN_YES, BOOLEAN_NO }; 45 public final static Pattern DISKSIZE_PATTERN = Pattern.compile("\\d+[MK]B"); 46 47 public enum ValueType { 48 INTEGER("integer"), 49 BOOLEAN("boolean"), 50 DISKSIZE("diskSize"), 51 STRING("string"); 52 53 private String mValue; 54 55 ValueType(String value) { 56 mValue = value; 57 } 58 59 public String getValue() { 60 return mValue; 61 } 62 63 public static ValueType getEnum(String value) { 64 for (ValueType type : values()) { 65 if (type.mValue.equals(value)) { 66 return type; 67 } 68 } 69 70 return null; 71 } 72 } 73 74 public static final class HardwareProperty { 75 private String mName; 76 private ValueType mType; 77 /** the string representation of the default value. can be null. */ 78 private String mDefault; 79 private String mAbstract; 80 private String mDescription; 81 82 public HardwareProperty() { 83 // initialize strings to sane defaults, as not all properties will be set from 84 // the ini file 85 mName = ""; 86 mDefault = ""; 87 mAbstract = ""; 88 mDescription = ""; 89 } 90 91 public String getName() { 92 return mName; 93 } 94 95 public ValueType getType() { 96 return mType; 97 } 98 99 public String getDefault() { 100 return mDefault; 101 } 102 103 public String getAbstract() { 104 return mAbstract; 105 } 106 107 public String getDescription() { 108 return mDescription; 109 } 110 111 public boolean isValidForUi() { 112 // don't show display string type for now. 113 return mType != ValueType.STRING; 114 } 115 } 116 117 /** 118 * Parses the hardware definition file. 119 * @param file the property file to parse 120 * @param log the ISdkLog object receiving warning/error from the parsing. Cannot be null. 121 * @return the map of (key,value) pairs, or null if the parsing failed. 122 */ 123 public static Map<String, HardwareProperty> parseHardwareDefinitions(File file, ISdkLog log) { 124 try { 125 FileInputStream fis = new FileInputStream(file); 126 BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 127 128 Map<String, HardwareProperty> map = new TreeMap<String, HardwareProperty>(); 129 130 String line = null; 131 HardwareProperty prop = null; 132 while ((line = reader.readLine()) != null) { 133 if (line.length() > 0 && line.charAt(0) != '#') { 134 Matcher m = PATTERN_PROP.matcher(line); 135 if (m.matches()) { 136 String valueName = m.group(1); 137 String value = m.group(2); 138 139 if (HW_PROP_NAME.equals(valueName)) { 140 prop = new HardwareProperty(); 141 prop.mName = value; 142 map.put(prop.mName, prop); 143 } 144 145 if (prop == null) { 146 log.warning("Error parsing '%1$s': missing '%2$s'", 147 file.getAbsolutePath(), HW_PROP_NAME); 148 return null; 149 } 150 151 if (HW_PROP_TYPE.equals(valueName)) { 152 prop.mType = ValueType.getEnum(value); 153 } else if (HW_PROP_DEFAULT.equals(valueName)) { 154 prop.mDefault = value; 155 } else if (HW_PROP_ABSTRACT.equals(valueName)) { 156 prop.mAbstract = value; 157 } else if (HW_PROP_DESC.equals(valueName)) { 158 prop.mDescription = value; 159 } 160 } else { 161 log.warning("Error parsing '%1$s': \"%2$s\" is not a valid syntax", 162 file.getAbsolutePath(), line); 163 return null; 164 } 165 } 166 } 167 168 return map; 169 } catch (FileNotFoundException e) { 170 // this should not happen since we usually test the file existence before 171 // calling the method. 172 // Return null below. 173 } catch (IOException e) { 174 log.warning("Error parsing '%1$s': %2$s.", file.getAbsolutePath(), 175 e.getMessage()); 176 } 177 178 return null; 179 } 180 181 /** 182 * Returns the index of <var>value</var> in {@link #BOOLEAN_VALUES}. 183 */ 184 public static int getBooleanValueIndex(String value) { 185 if (BOOLEAN_YES.equals(value)) { 186 return 0; 187 } else if (BOOLEAN_NO.equals(value)) { 188 return 1; 189 } 190 191 return -1; 192 } 193 } 194