| 1 |
/* |
| 2 |
* Copyright (C) 2011 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 |
|
| 20 |
import com.android.SdkConstants; |
| 21 |
import com.android.annotations.NonNull; |
| 22 |
import com.android.annotations.Nullable; |
| 23 |
import com.android.io.IAbstractFile; |
| 24 |
import com.android.io.IAbstractFolder; |
| 25 |
import com.android.resources.ResourceType; |
| 26 |
import com.android.utils.ILogger; |
| 27 |
import com.google.common.base.Charsets; |
| 28 |
|
| 29 |
import org.kxml2.io.KXmlParser; |
| 30 |
import org.xmlpull.v1.XmlPullParser; |
| 31 |
|
| 32 |
import java.io.BufferedReader; |
| 33 |
import java.io.IOException; |
| 34 |
import java.io.InputStreamReader; |
| 35 |
import java.io.Reader; |
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.Collection; |
| 38 |
import java.util.Collections; |
| 39 |
import java.util.EnumMap; |
| 40 |
import java.util.List; |
| 41 |
import java.util.Map; |
| 42 |
|
| 43 |
/** |
| 44 |
* Framework resources repository. |
| 45 |
* |
| 46 |
* This behaves the same as {@link ResourceRepository} except that it differentiates between |
| 47 |
* resources that are public and non public. |
| 48 |
* {@link #getResourceItemsOfType(ResourceType)} and {@link #hasResourcesOfType(ResourceType)} only return |
| 49 |
* public resources. This is typically used to display resource lists in the UI. |
| 50 |
* |
| 51 |
* {@link #getConfiguredResources(com.android.ide.common.resources.configuration.FolderConfiguration)} |
| 52 |
* returns all resources, even the non public ones so that this can be used for rendering. |
| 53 |
*/ |
| 54 |
public class FrameworkResources extends ResourceRepository { |
| 55 |
|
| 56 |
/** |
| 57 |
* Map of {@link ResourceType} to list of items. It is guaranteed to contain a list for all |
| 58 |
* possible values of ResourceType. |
| 59 |
*/ |
| 60 |
protected final Map<ResourceType, List<ResourceItem>> mPublicResourceMap = |
| 61 |
new EnumMap<ResourceType, List<ResourceItem>>(ResourceType.class); |
| 62 |
|
| 63 |
public FrameworkResources(@NonNull IAbstractFolder resFolder) { |
| 64 |
super(resFolder, true /*isFrameworkRepository*/); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns a {@link Collection} (always non null, but can be empty) of <b>public</b> |
| 69 |
* {@link ResourceItem} matching a given {@link ResourceType}. |
| 70 |
* |
| 71 |
* @param type the type of the resources to return |
| 72 |
* @return a collection of items, possibly empty. |
| 73 |
*/ |
| 74 |
@Override |
| 75 |
@NonNull |
| 76 |
public List<ResourceItem> getResourceItemsOfType(@NonNull ResourceType type) { |
| 77 |
return mPublicResourceMap.get(type); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns whether the repository has <b>public</b> resources of a given {@link ResourceType}. |
| 82 |
* @param type the type of resource to check. |
| 83 |
* @return true if the repository contains resources of the given type, false otherwise. |
| 84 |
*/ |
| 85 |
@Override |
| 86 |
public boolean hasResourcesOfType(@NonNull ResourceType type) { |
| 87 |
return !mPublicResourceMap.get(type).isEmpty(); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
@NonNull |
| 92 |
protected ResourceItem createResourceItem(@NonNull String name) { |
| 93 |
return new FrameworkResourceItem(name); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Reads the public.xml file in data/res/values/ for a given resource folder and builds up |
| 98 |
* a map of public resources. |
| 99 |
* |
| 100 |
* This map is a subset of the full resource map that only contains framework resources |
| 101 |
* that are public. |
| 102 |
* |
| 103 |
* @param logger a logger to report issues to |
| 104 |
*/ |
| 105 |
public void loadPublicResources(@Nullable ILogger logger) { |
| 106 |
IAbstractFolder valueFolder = getResFolder().getFolder(SdkConstants.FD_RES_VALUES); |
| 107 |
if (!valueFolder.exists()) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
IAbstractFile publicXmlFile = valueFolder.getFile("public.xml"); //$NON-NLS-1$ |
| 112 |
if (publicXmlFile.exists()) { |
| 113 |
Reader reader = null; |
| 114 |
try { |
| 115 |
reader = new BufferedReader(new InputStreamReader(publicXmlFile.getContents(), |
| 116 |
Charsets.UTF_8)); |
| 117 |
KXmlParser parser = new KXmlParser(); |
| 118 |
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); |
| 119 |
parser.setInput(reader); |
| 120 |
|
| 121 |
ResourceType lastType = null; |
| 122 |
String lastTypeName = ""; |
| 123 |
while (true) { |
| 124 |
int event = parser.next(); |
| 125 |
if (event == XmlPullParser.START_TAG) { |
| 126 |
// As of API 15 there are a number of "java-symbol" entries here |
| 127 |
if (!parser.getName().equals("public")) { //$NON-NLS-1$ |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
String name = null; |
| 132 |
String typeName = null; |
| 133 |
for (int i = 0, n = parser.getAttributeCount(); i < n; i++) { |
| 134 |
String attribute = parser.getAttributeName(i); |
| 135 |
|
| 136 |
if (attribute.equals("name")) { //$NON-NLS-1$ |
| 137 |
name = parser.getAttributeValue(i); |
| 138 |
if (typeName != null) { |
| 139 |
// Skip id attribute processing |
| 140 |
break; |
| 141 |
} |
| 142 |
} else if (attribute.equals("type")) { //$NON-NLS-1$ |
| 143 |
typeName = parser.getAttributeValue(i); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (name != null && typeName != null) { |
| 148 |
ResourceType type = null; |
| 149 |
if (typeName.equals(lastTypeName)) { |
| 150 |
type = lastType; |
| 151 |
} else { |
| 152 |
type = ResourceType.getEnum(typeName); |
| 153 |
lastType = type; |
| 154 |
lastTypeName = typeName; |
| 155 |
} |
| 156 |
if (type != null) { |
| 157 |
ResourceItem match = null; |
| 158 |
Map<String, ResourceItem> map = mResourceMap.get(type); |
| 159 |
if (map != null) { |
| 160 |
match = map.get(name); |
| 161 |
} |
| 162 |
|
| 163 |
if (match != null) { |
| 164 |
List<ResourceItem> publicList = mPublicResourceMap.get(type); |
| 165 |
if (publicList == null) { |
| 166 |
// Pick initial size for the list to hold the public |
| 167 |
// resources. We could just use map.size() here, |
| 168 |
// but they're usually much bigger; for example, |
| 169 |
// in one platform version, there are 1500 drawables |
| 170 |
// and 1200 strings but only 175 and 25 public ones |
| 171 |
// respectively. |
| 172 |
int size; |
| 173 |
switch (type) { |
| 174 |
case STYLE: size = 500; break; |
| 175 |
case ATTR: size = 1050; break; |
| 176 |
case DRAWABLE: size = 200; break; |
| 177 |
case ID: size = 50; break; |
| 178 |
case LAYOUT: |
| 179 |
case COLOR: |
| 180 |
case STRING: |
| 181 |
case ANIM: |
| 182 |
case INTERPOLATOR: |
| 183 |
size = 30; |
| 184 |
break; |
| 185 |
default: |
| 186 |
size = 10; |
| 187 |
break; |
| 188 |
} |
| 189 |
publicList = new ArrayList<ResourceItem>(size); |
| 190 |
mPublicResourceMap.put(type, publicList); |
| 191 |
} |
| 192 |
|
| 193 |
publicList.add(match); |
| 194 |
} else { |
| 195 |
// log that there's a public resource that doesn't actually |
| 196 |
// exist? |
| 197 |
} |
| 198 |
} else { |
| 199 |
// log that there was a reference to a typo that doesn't actually |
| 200 |
// exist? |
| 201 |
} |
| 202 |
} |
| 203 |
} else if (event == XmlPullParser.END_DOCUMENT) { |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
} catch (Exception e) { |
| 208 |
if (logger != null) { |
| 209 |
logger.error(e, "Can't read and parse public attribute list"); |
| 210 |
} |
| 211 |
} finally { |
| 212 |
if (reader != null) { |
| 213 |
try { |
| 214 |
reader.close(); |
| 215 |
} catch (IOException e) { |
| 216 |
// Nothing to be done here - we don't care if it closed or not. |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// put unmodifiable list for all res type in the public resource map |
| 223 |
// this will simplify access |
| 224 |
for (ResourceType type : ResourceType.values()) { |
| 225 |
List<ResourceItem> list = mPublicResourceMap.get(type); |
| 226 |
if (list == null) { |
| 227 |
list = Collections.emptyList(); |
| 228 |
} else { |
| 229 |
list = Collections.unmodifiableList(list); |
| 230 |
} |
| 231 |
|
| 232 |
// put the new list in the map |
| 233 |
mPublicResourceMap.put(type, list); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|