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.parser;
17 
18 public class CLToken extends CLElement {
19     int mIndex = 0;
20     Type mType = Type.UNKNOWN;
21 
22     // @TODO: add description
getBoolean()23     public boolean getBoolean() throws CLParsingException {
24         if (mType == Type.TRUE) {
25             return true;
26         }
27         if (mType == Type.FALSE) {
28             return false;
29         }
30         throw new CLParsingException("this token is not a boolean: <" + content() + ">", this);
31     }
32 
33     // @TODO: add description
isNull()34     public boolean isNull() throws CLParsingException {
35         if (mType == Type.NULL) {
36             return true;
37         }
38         throw new CLParsingException("this token is not a null: <" + content() + ">", this);
39     }
40 
41     enum Type {UNKNOWN, TRUE, FALSE, NULL}
42 
43     char[] mTokenTrue = "true".toCharArray();
44     char[] mTokenFalse = "false".toCharArray();
45     char[] mTokenNull = "null".toCharArray();
46 
CLToken(char[] content)47     public CLToken(char[] content) {
48         super(content);
49     }
50 
51     // @TODO: add description
allocate(char[] content)52     public static CLElement allocate(char[] content) {
53         return new CLToken(content);
54     }
55 
56     @Override
toJSON()57     protected String toJSON() {
58         if (CLParser.sDebug) {
59             return "<" + content() + ">";
60         } else {
61             return content();
62         }
63     }
64 
65     @Override
toFormattedJSON(int indent, int forceIndent)66     protected String toFormattedJSON(int indent, int forceIndent) {
67         StringBuilder json = new StringBuilder();
68         addIndent(json, indent);
69         json.append(content());
70         return json.toString();
71     }
72 
73     @SuppressWarnings("HiddenTypeParameter")
getType()74     public Type getType() {
75         return mType;
76     }
77 
78     // @TODO: add description
validate(char c, long position)79     public boolean validate(char c, long position) {
80         boolean isValid = false;
81         switch (mType) {
82             case TRUE: {
83                 isValid = (mTokenTrue[mIndex] == c);
84                 if (isValid && mIndex + 1 == mTokenTrue.length) {
85                     setEnd(position);
86                 }
87             }
88             break;
89             case FALSE: {
90                 isValid = (mTokenFalse[mIndex] == c);
91                 if (isValid && mIndex + 1 == mTokenFalse.length) {
92                     setEnd(position);
93                 }
94             }
95             break;
96             case NULL: {
97                 isValid = (mTokenNull[mIndex] == c);
98                 if (isValid && mIndex + 1 == mTokenNull.length) {
99                     setEnd(position);
100                 }
101             }
102             break;
103             case UNKNOWN: {
104                 if (mTokenTrue[mIndex] == c) {
105                     mType = Type.TRUE;
106                     isValid = true;
107                 } else if (mTokenFalse[mIndex] == c) {
108                     mType = Type.FALSE;
109                     isValid = true;
110                 } else if (mTokenNull[mIndex] == c) {
111                     mType = Type.NULL;
112                     isValid = true;
113                 }
114             }
115         }
116 
117         mIndex++;
118         return isValid;
119     }
120 
121 }
122