2
|
1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
|
|
2 |
// Use of this source code is governed by a BSD-style license that can be
|
|
3 |
// found in the LICENSE file.
|
|
4 |
|
|
5 |
package org.chromium.sdk.internal;
|
|
6 |
|
|
7 |
import java.util.EnumMap;
|
|
8 |
import java.util.HashMap;
|
|
9 |
import java.util.Map;
|
|
10 |
|
|
11 |
import org.chromium.sdk.JsValue.Type;
|
|
12 |
|
|
13 |
/**
|
|
14 |
* A utility that facilitates retrieval of {@link Type}s according to the
|
|
15 |
* JSON values received.
|
|
16 |
*/
|
|
17 |
public class JsDataTypeUtil {
|
|
18 |
|
|
19 |
private static Map<String, Type> jsonTypeToEnum = new HashMap<String, Type>();
|
|
20 |
|
|
21 |
private static Map<Type, String> enumToJsonType = new EnumMap<Type, String>(Type.class);
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Class name of an Array object (TYPE_ARRAY).
|
|
25 |
*/
|
|
26 |
public static final String CLASSNAME_ARRAY = "Array";
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Class name of a Date object (TYPE_DATE).
|
|
30 |
*/
|
|
31 |
private static final String CLASSNAME_DATE = "Date";
|
|
32 |
|
|
33 |
static {
|
|
34 |
put("object", Type.TYPE_OBJECT);
|
|
35 |
put("number", Type.TYPE_NUMBER);
|
|
36 |
put("string", Type.TYPE_STRING);
|
|
37 |
put("function", Type.TYPE_FUNCTION);
|
|
38 |
put("boolean", Type.TYPE_BOOLEAN);
|
|
39 |
put("undefined", Type.TYPE_UNDEFINED);
|
|
40 |
put("null", Type.TYPE_NULL);
|
|
41 |
put("error", Type.TYPE_ERROR);
|
|
42 |
put("array", Type.TYPE_ARRAY);
|
|
43 |
put("date", Type.TYPE_DATE);
|
|
44 |
put("regexp", Type.TYPE_REGEXP);
|
|
45 |
}
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Gets a JsDataType using a V8 JavaScript type and, optionally, a class name
|
|
49 |
* of the object. If {@code className} is {@code null}, only the 1:1 mapping
|
|
50 |
* shall be used.
|
|
51 |
*
|
|
52 |
* @param jsonType the JS type from a JSON response
|
|
53 |
* @param className a nullable class name of the object
|
|
54 |
* @return a JsDataType corresponding to {@code jsonType} and, possibly,
|
|
55 |
* modified according to {@code className}
|
|
56 |
*/
|
|
57 |
public static Type fromJsonTypeAndClassName(String jsonType, String className) {
|
|
58 |
if (jsonType == null) {
|
|
59 |
return null;
|
|
60 |
}
|
|
61 |
if (CLASSNAME_DATE.equals(className)) {
|
|
62 |
// hack to use the TYPE_DATE type even though its type in V8 is "object"
|
|
63 |
return Type.TYPE_DATE;
|
|
64 |
} else if (CLASSNAME_ARRAY.equals(className)) {
|
|
65 |
// hack to use the TYPE_ARRAY type even though its type in V8 is "object"
|
|
66 |
return Type.TYPE_ARRAY;
|
|
67 |
}
|
|
68 |
return jsonTypeToEnum.get(jsonType);
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Converts {@code type} to its JSON representation used in V8.
|
|
73 |
*
|
|
74 |
* @param type to convert
|
|
75 |
* @return a string name of the type understandable by V8
|
|
76 |
*/
|
|
77 |
public static String getJsonString(Type type) {
|
|
78 |
return enumToJsonType.get(type);
|
|
79 |
}
|
|
80 |
|
|
81 |
private static void put(String jsonString, Type type) {
|
|
82 |
jsonTypeToEnum.put(jsonString, type);
|
|
83 |
enumToJsonType.put(type, jsonString);
|
|
84 |
}
|
|
85 |
|
|
86 |
private JsDataTypeUtil() {
|
|
87 |
// not instantiable
|
|
88 |
}
|
|
89 |
}
|