OSDN Git Service

android-2.1_r1 snapshot
[android-x86/sdk.git] / sdkmanager / libs / sdklib / src / com / android / sdklib / internal / avd / HardwareProperties.java
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.HashMap;
28 import java.util.Map;
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
52         private String mValue;
53
54         ValueType(String value) {
55             mValue = value;
56         }
57
58         public String getValue() {
59             return mValue;
60         }
61
62         public static ValueType getEnum(String value) {
63             for (ValueType type : values()) {
64                 if (type.mValue.equals(value)) {
65                     return type;
66                 }
67             }
68
69             return null;
70         }
71     }
72
73     public static final class HardwareProperty {
74         private String mName;
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;
80
81         public String getName() {
82             return mName;
83         }
84
85         public ValueType getType() {
86             return mType;
87         }
88
89         public String getDefault() {
90             return mDefault;
91         }
92
93         public String getAbstract() {
94             return mAbstract;
95         }
96
97         public String getDescription() {
98             return mDescription;
99         }
100     }
101
102     /**
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.
107      */
108     public static Map<String, HardwareProperty> parseHardwareDefinitions(File file, ISdkLog log) {
109         try {
110             FileInputStream fis = new FileInputStream(file);
111             BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
112
113             Map<String, HardwareProperty> map = new HashMap<String, HardwareProperty>();
114
115             String line = null;
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);
120                     if (m.matches()) {
121                         String valueName = m.group(1);
122                         String value = m.group(2);
123
124                         if (HW_PROP_NAME.equals(valueName)) {
125                             prop = new HardwareProperty();
126                             prop.mName = value;
127                             map.put(prop.mName, prop);
128                         }
129
130                         if (prop == null) {
131                             log.warning("Error parsing '%1$s': missing '%2$s'",
132                                     file.getAbsolutePath(), HW_PROP_NAME);
133                             return null;
134                         }
135
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;
144                         }
145                     } else {
146                         log.warning("Error parsing '%1$s': \"%2$s\" is not a valid syntax",
147                                 file.getAbsolutePath(), line);
148                         return null;
149                     }
150                 }
151             }
152
153             return map;
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) {
159             if (log != null) {
160                 log.warning("Error parsing '%1$s': %2$s.", file.getAbsolutePath(),
161                         e.getMessage());
162             }
163         }
164
165         return null;
166     }
167
168     /**
169      * Returns the index of <var>value</var> in {@link #BOOLEAN_VALUES}.
170      */
171     public static int getBooleanValueIndex(String value) {
172         if (BOOLEAN_YES.equals(value)) {
173             return 0;
174         } else if (BOOLEAN_NO.equals(value)) {
175             return 1;
176         }
177
178         return -1;
179     }
180 }