Debian Codegraph @

sdk-common/src/main/java/com/android/ide/common/resources/ValueResourceParser.java

raw
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.ide.common.resources;
18
19 import static com.android.SdkConstants.ANDROID_NS_NAME_PREFIX;
20 import static com.android.SdkConstants.ANDROID_NS_NAME_PREFIX_LEN;
21 import static com.android.SdkConstants.ATTR_NAME;
22 import static com.android.SdkConstants.ATTR_PARENT;
23 import static com.android.SdkConstants.ATTR_TYPE;
24 import static com.android.SdkConstants.ATTR_VALUE;
25 import static com.android.SdkConstants.TAG_ITEM;
26 import static com.android.SdkConstants.TAG_RESOURCES;
27
28 import com.android.ide.common.rendering.api.ArrayResourceValue;
29 import com.android.ide.common.rendering.api.AttrResourceValue;
30 import com.android.ide.common.rendering.api.DeclareStyleableResourceValue;
31 import com.android.ide.common.rendering.api.ItemResourceValue;
32 import com.android.ide.common.rendering.api.ResourceValue;
33 import com.android.ide.common.rendering.api.StyleResourceValue;
34 import com.android.ide.common.res2.ValueXmlHelper;
35 import com.android.resources.ResourceType;
36
37 import org.xml.sax.Attributes;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.helpers.DefaultHandler;
40
41 /**
42 * SAX handler to parse value resource files.
43 */
44 public final class ValueResourceParser extends DefaultHandler {
45
46 public interface IValueResourceRepository {
47 void addResourceValue(ResourceValue value);
48 boolean hasResourceValue(ResourceType type, String name);
49 }
50
51 private boolean inResources;
52 private int mDepth;
53 private ResourceValue mCurrentValue;
54 private ArrayResourceValue mArrayResourceValue;
55 private StyleResourceValue mCurrentStyle;
56 private DeclareStyleableResourceValue mCurrentDeclareStyleable;
57 private AttrResourceValue mCurrentAttr;
58 private IValueResourceRepository mRepository;
59 private final boolean mIsFramework;
60 private final String mLibraryName;
61
62 public ValueResourceParser(IValueResourceRepository repository, boolean isFramework, String libraryName) {
63 mRepository = repository;
64 mIsFramework = isFramework;
65 mLibraryName = libraryName;
66 }
67
68 @Override
69 public void endElement(String uri, String localName, String qName) throws SAXException {
70 if (mCurrentValue != null) {
71 String value = mCurrentValue.getValue();
72 value = value == null ? "" : ValueXmlHelper.unescapeResourceString(value, false, true);
73 mCurrentValue.setValue(value);
74 }
75
76 if (inResources && qName.equals(TAG_RESOURCES)) {
77 inResources = false;
78 } else if (mDepth == 2) {
79 mCurrentValue = null;
80 mCurrentStyle = null;
81 mCurrentDeclareStyleable = null;
82 mCurrentAttr = null;
83 mArrayResourceValue = null;
84 } else if (mDepth == 3) {
85 if (mArrayResourceValue != null && mCurrentValue != null) {
86 mArrayResourceValue.addElement(mCurrentValue.getValue());
87 }
88 mCurrentValue = null;
89 //noinspection VariableNotUsedInsideIf
90 if (mCurrentDeclareStyleable != null) {
91 mCurrentAttr = null;
92 }
93 }
94
95 mDepth--;
96 super.endElement(uri, localName, qName);
97 }
98
99 @Override
100 public void startElement(String uri, String localName, String qName, Attributes attributes)
101 throws SAXException {
102 try {
103 mDepth++;
104 if (!inResources && mDepth == 1) {
105 if (qName.equals(TAG_RESOURCES)) {
106 inResources = true;
107 }
108 } else if (mDepth == 2 && inResources) {
109 ResourceType type = getType(qName, attributes);
110
111 if (type != null) {
112 // get the resource name
113 String name = attributes.getValue(ATTR_NAME);
114 if (name != null) {
115 switch (type) {
116 case STYLE:
117 String parent = attributes.getValue(ATTR_PARENT);
118 mCurrentStyle = new StyleResourceValue(type, name, parent,
119 mIsFramework, mLibraryName);
120 mRepository.addResourceValue(mCurrentStyle);
121 break;
122 case DECLARE_STYLEABLE:
123 mCurrentDeclareStyleable = new DeclareStyleableResourceValue(
124 type, name, mIsFramework, mLibraryName);
125 mRepository.addResourceValue(mCurrentDeclareStyleable);
126 break;
127 case ATTR:
128 mCurrentAttr = new AttrResourceValue(type, name, mIsFramework, mLibraryName);
129 mRepository.addResourceValue(mCurrentAttr);
130 break;
131 case ARRAY:
132 mArrayResourceValue = new ArrayResourceValue(name, mIsFramework, mLibraryName);
133 mRepository.addResourceValue(mArrayResourceValue);
134 break;
135 default:
136 mCurrentValue = new ResourceValue(type, name, mIsFramework, mLibraryName);
137 mRepository.addResourceValue(mCurrentValue);
138 break;
139 }
140 }
141 }
142 } else if (mDepth == 3) {
143 // get the resource name
144 String name = attributes.getValue(ATTR_NAME);
145 if (name != null) {
146
147 if (mCurrentStyle != null) {
148 // is the attribute in the android namespace?
149 boolean isFrameworkAttr = mIsFramework;
150 if (name.startsWith(ANDROID_NS_NAME_PREFIX)) {
151 name = name.substring(ANDROID_NS_NAME_PREFIX_LEN);
152 isFrameworkAttr = true;
153 }
154
155 mCurrentValue = new ItemResourceValue(name, isFrameworkAttr, mIsFramework, mLibraryName);
156 mCurrentStyle.addItem((ItemResourceValue)mCurrentValue);
157 } else if (mCurrentDeclareStyleable != null) {
158 // is the attribute in the android namespace?
159 boolean isFramework = mIsFramework;
160 if (name.startsWith(ANDROID_NS_NAME_PREFIX)) {
161 name = name.substring(ANDROID_NS_NAME_PREFIX_LEN);
162 isFramework = true;
163 }
164
165 mCurrentAttr = new AttrResourceValue(ResourceType.ATTR, name, isFramework, mLibraryName);
166 mCurrentDeclareStyleable.addValue(mCurrentAttr);
167
168 // also add it to the repository.
169 mRepository.addResourceValue(mCurrentAttr);
170
171 } else if (mCurrentAttr != null) {
172 // get the enum/flag value
173 String value = attributes.getValue(ATTR_VALUE);
174
175 try {
176 // Integer.decode/parseInt can't deal with hex value > 0x7FFFFFFF so we
177 // use Long.decode instead.
178 mCurrentAttr.addValue(name, (int)(long)Long.decode(value));
179 } catch (NumberFormatException e) {
180 // pass, we'll just ignore this value
181 }
182 }
183 } else //noinspection VariableNotUsedInsideIf
184 if (mArrayResourceValue != null) {
185 // Create a temporary resource value to hold the item's value. The value is
186 // not added to the repository, since it's just a holder.
187 mCurrentValue = new ResourceValue(null, null, mIsFramework, mLibraryName);
188 }
189 } else if (mDepth == 4 && mCurrentAttr != null) {
190 // get the enum/flag name
191 String name = attributes.getValue(ATTR_NAME);
192 String value = attributes.getValue(ATTR_VALUE);
193
194 try {
195 // Integer.decode/parseInt can't deal with hex value > 0x7FFFFFFF so we
196 // use Long.decode instead.
197 mCurrentAttr.addValue(name, (int)(long)Long.decode(value));
198 } catch (NumberFormatException e) {
199 // pass, we'll just ignore this value
200 }
201 }
202 } finally {
203 super.startElement(uri, localName, qName, attributes);
204 }
205 }
206
207 private static ResourceType getType(String qName, Attributes attributes) {
208 String typeValue;
209
210 // if the node is <item>, we get the type from the attribute "type"
211 if (TAG_ITEM.equals(qName)) {
212 typeValue = attributes.getValue(ATTR_TYPE);
213 } else {
214 // the type is the name of the node.
215 typeValue = qName;
216 }
217
218 return ResourceType.getEnum(typeValue);
219 }
220
221 @Override
222 public void characters(char[] ch, int start, int length) throws SAXException {
223 if (mCurrentValue != null) {
224 String value = mCurrentValue.getValue();
225 if (value == null) {
226 mCurrentValue.setValue(new String(ch, start, length));
227 } else {
228 mCurrentValue.setValue(value + new String(ch, start, length));
229 }
230 }
231 }
232 }