1 /*
2  * Copyright (C) 2021 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 package androidx.constraintlayout.core.motion.parse;
17 
18 import androidx.constraintlayout.core.motion.utils.TypedBundle;
19 import androidx.constraintlayout.core.motion.utils.TypedValues;
20 import androidx.constraintlayout.core.parser.CLElement;
21 import androidx.constraintlayout.core.parser.CLKey;
22 import androidx.constraintlayout.core.parser.CLObject;
23 import androidx.constraintlayout.core.parser.CLParser;
24 import androidx.constraintlayout.core.parser.CLParsingException;
25 
26 import java.util.Arrays;
27 
28 public class KeyParser {
29 
30     private interface Ids {
get(String str)31         int get(String str);
32     }
33 
34     private interface DataType {
get(int str)35         int get(int str);
36     }
37 
parse(String str, Ids table, DataType dtype)38     private static TypedBundle parse(String str, Ids table, DataType dtype) {
39         TypedBundle bundle = new TypedBundle();
40 
41         try {
42             CLObject parsedContent = CLParser.parse(str);
43             int n = parsedContent.size();
44             for (int i = 0; i < n; i++) {
45                 CLKey clkey = ((CLKey) parsedContent.get(i));
46                 String type = clkey.content();
47                 CLElement value = clkey.getValue();
48                 int id = table.get(type);
49                 if (id == -1) {
50                     System.err.println("unknown type " + type);
51                     continue;
52                 }
53                 switch (dtype.get(id)) {
54                     case TypedValues.FLOAT_MASK:
55                         bundle.add(id, value.getFloat());
56                         System.out.println("parse " + type + " FLOAT_MASK > " + value.getFloat());
57                         break;
58                     case TypedValues.STRING_MASK:
59                         bundle.add(id, value.content());
60                         System.out.println("parse " + type + " STRING_MASK > " + value.content());
61 
62                         break;
63                     case TypedValues.INT_MASK:
64                         bundle.add(id, value.getInt());
65                         System.out.println("parse " + type + " INT_MASK > " + value.getInt());
66                         break;
67                     case TypedValues.BOOLEAN_MASK:
68                         bundle.add(id, parsedContent.getBoolean(i));
69                         break;
70                 }
71             }
72         } catch (CLParsingException e) {
73             //TODO replace with something not equal to printStackTrace();
74             System.err.println(e.toString()+"\n"+ Arrays.toString(e.getStackTrace())
75                     .replace("[","   at ")
76                     .replace(",","\n   at")
77                     .replace("]",""));
78         }
79         return bundle;
80     }
81 
82     // @TODO: add description
parseAttributes(String str)83     public static TypedBundle parseAttributes(String str) {
84         return parse(str, TypedValues.AttributesType::getId, TypedValues.AttributesType::getType);
85     }
86 
87     // @TODO: add description
main(String[] args)88     public static void main(String[] args) {
89         String str = "{"
90                 + "frame:22,\n"
91                 + "target:'widget1',\n"
92                 + "easing:'easeIn',\n"
93                 + "curveFit:'spline',\n"
94                 + "progress:0.3,\n"
95                 + "alpha:0.2,\n"
96                 + "elevation:0.7,\n"
97                 + "rotationZ:23,\n"
98                 + "rotationX:25.0,\n"
99                 + "rotationY:27.0,\n"
100                 + "pivotX:15,\n"
101                 + "pivotY:17,\n"
102                 + "pivotTarget:'32',\n"
103                 + "pathRotate:23,\n"
104                 + "scaleX:0.5,\n"
105                 + "scaleY:0.7,\n"
106                 + "translationX:5,\n"
107                 + "translationY:7,\n"
108                 + "translationZ:11,\n"
109                 + "}";
110         parseAttributes(str);
111     }
112 }
113