2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.sdklib.internal.avd;
19 import com.android.sdklib.ISdkLog;
21 import java.io.BufferedReader;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.util.HashMap;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 public class HardwareProperties {
33 private final static Pattern PATTERN_PROP = Pattern.compile(
34 "^([a-zA-Z0-9._-]+)\\s*=\\s*(.*)\\s*$");
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";
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");
47 public enum ValueType {
52 private String mValue;
54 ValueType(String value) {
58 public String getValue() {
62 public static ValueType getEnum(String value) {
63 for (ValueType type : values()) {
64 if (type.mValue.equals(value)) {
73 public static final class HardwareProperty {
75 private ValueType mType;
76 /** the string representation of the default value. can be null. */
77 private String mDefault;
78 private String mAbstract;
79 private String mDescription;
81 public String getName() {
85 public ValueType getType() {
89 public String getDefault() {
93 public String getAbstract() {
97 public String getDescription() {
103 * Parses the hardware definition file.
104 * @param file the property file to parse
105 * @param log the ISdkLog object receiving warning/error from the parsing.
106 * @return the map of (key,value) pairs, or null if the parsing failed.
108 public static Map<String, HardwareProperty> parseHardwareDefinitions(File file, ISdkLog log) {
110 FileInputStream fis = new FileInputStream(file);
111 BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
113 Map<String, HardwareProperty> map = new HashMap<String, HardwareProperty>();
116 HardwareProperty prop = null;
117 while ((line = reader.readLine()) != null) {
118 if (line.length() > 0 && line.charAt(0) != '#') {
119 Matcher m = PATTERN_PROP.matcher(line);
121 String valueName = m.group(1);
122 String value = m.group(2);
124 if (HW_PROP_NAME.equals(valueName)) {
125 prop = new HardwareProperty();
127 map.put(prop.mName, prop);
131 log.warning("Error parsing '%1$s': missing '%2$s'",
132 file.getAbsolutePath(), HW_PROP_NAME);
136 if (HW_PROP_TYPE.equals(valueName)) {
137 prop.mType = ValueType.getEnum(value);
138 } else if (HW_PROP_DEFAULT.equals(valueName)) {
139 prop.mDefault = value;
140 } else if (HW_PROP_ABSTRACT.equals(valueName)) {
141 prop.mAbstract = value;
142 } else if (HW_PROP_DESC.equals(valueName)) {
143 prop.mDescription = value;
146 log.warning("Error parsing '%1$s': \"%2$s\" is not a valid syntax",
147 file.getAbsolutePath(), line);
154 } catch (FileNotFoundException e) {
155 // this should not happen since we usually test the file existence before
156 // calling the method.
157 // Return null below.
158 } catch (IOException e) {
160 log.warning("Error parsing '%1$s': %2$s.", file.getAbsolutePath(),
169 * Returns the index of <var>value</var> in {@link #BOOLEAN_VALUES}.
171 public static int getBooleanValueIndex(String value) {
172 if (BOOLEAN_YES.equals(value)) {
174 } else if (BOOLEAN_NO.equals(value)) {