aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TypedVariable.java
blob: 468a10c77a3a9d336e3f18ebbd8bd5de4373768c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.android.ide.eclipse.adt.internal.wizards.templates;

import java.util.Locale;

import org.xml.sax.Attributes;

public class TypedVariable {
	public enum Type {
		STRING,
		BOOLEAN,
		INTEGER;

		public static Type get(String name) {
			if (name == null) {
				return STRING;
			}
			try {
				return valueOf(name.toUpperCase(Locale.US));
			} catch (IllegalArgumentException e) {
				System.err.println("Unexpected global type '" + name + "'");
				System.err.println("Expected one of :");
				for (Type s : Type.values()) {
					System.err.println("  " + s.name().toLowerCase(Locale.US));
				}
			}

			return STRING;
		}
	}

	public static Object parseGlobal(Attributes attributes) {
		String value = attributes.getValue(TemplateHandler.ATTR_VALUE);
		Type type = Type.get(attributes.getValue(TemplateHandler.ATTR_TYPE));

		switch (type) {
		case STRING:
			return value;
		case BOOLEAN:
			return Boolean.parseBoolean(value);
		case INTEGER:
			try {
				return Integer.parseInt(value);
			} catch (NumberFormatException e) {
				return value;
			}
		}

		return value;
	}
}