Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2007 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.DOT_XML;
20
21 import com.android.ide.common.rendering.api.DensityBasedResourceValue;
22 import com.android.ide.common.rendering.api.ResourceValue;
23 import com.android.ide.common.resources.configuration.DensityQualifier;
24 import com.android.ide.common.resources.configuration.ResourceQualifier;
25 import com.android.io.IAbstractFile;
26 import com.android.resources.FolderTypeRelationship;
27 import com.android.resources.ResourceType;
28 import com.android.utils.SdkUtils;
29
30 import java.util.Collection;
31 import java.util.List;
32
33 import javax.xml.parsers.SAXParserFactory;
34
35 /**
36 * Represents a resource file describing a single resource.
37 * <p>
38 * This is typically an XML file inside res/anim, res/layout, or res/menu or an image file
39 * under res/drawable.
40 */
41 public class SingleResourceFile extends ResourceFile {
42
43 private static final SAXParserFactory sParserFactory = SAXParserFactory.newInstance();
44 static {
45 sParserFactory.setNamespaceAware(true);
46 }
47
48 private final String mResourceName;
49 private final ResourceType mType;
50 private ResourceValue mValue;
51
52 public SingleResourceFile(IAbstractFile file, ResourceFolder folder) {
53 super(file, folder);
54
55 // we need to infer the type of the resource from the folder type.
56 // This is easy since this is a single Resource file.
57 List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folder.getType());
58 mType = types.get(0);
59
60 // compute the resource name
61 mResourceName = getResourceName(mType);
62
63 // test if there's a density qualifier associated with the resource
64 DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier();
65
66 if (!ResourceQualifier.isValid(qualifier)) {
67 mValue = new ResourceValue(mType, getResourceName(mType),
68 file.getOsLocation(), isFramework());
69 } else {
70 mValue = new DensityBasedResourceValue(
71 mType,
72 getResourceName(mType),
73 file.getOsLocation(),
74 qualifier.getValue(),
75 isFramework());
76 }
77 }
78
79 @Override
80 protected void load(ScanningContext context) {
81 // get a resource item matching the given type and name
82 ResourceItem item = getRepository().getResourceItem(mType, mResourceName);
83
84 // add this file to the list of files generating this resource item.
85 item.add(this);
86
87 // Ask for an ID refresh since we're adding an item that will generate an ID
88 context.requestFullAapt();
89 }
90
91 @Override
92 protected void update(ScanningContext context) {
93 // when this happens, nothing needs to be done since the file only generates
94 // a single resources that doesn't actually change (its content is the file path)
95
96 // However, we should check for newly introduced errors
97 // Parse the file and look for @+id/ entries
98 validateAttributes(context);
99 }
100
101 @Override
102 protected void dispose(ScanningContext context) {
103 // only remove this file from the existing ResourceItem.
104 getFolder().getRepository().removeFile(mType, this);
105
106 // Ask for an ID refresh since we're removing an item that previously generated an ID
107 context.requestFullAapt();
108
109 // don't need to touch the content, it'll get reclaimed as this objects disappear.
110 // In the mean time other objects may need to access it.
111 }
112
113 @Override
114 public Collection<ResourceType> getResourceTypes() {
115 return FolderTypeRelationship.getRelatedResourceTypes(getFolder().getType());
116 }
117
118 @Override
119 public boolean hasResources(ResourceType type) {
120 return FolderTypeRelationship.match(type, getFolder().getType());
121 }
122
123 /*
124 * (non-Javadoc)
125 * @see com.android.ide.eclipse.editors.resources.manager.ResourceFile#getValue(com.android.ide.eclipse.common.resources.ResourceType, java.lang.String)
126 *
127 * This particular implementation does not care about the type or name since a
128 * SingleResourceFile represents a file generating only one resource.
129 * The value returned is the full absolute path of the file in OS form.
130 */
131 @Override
132 public ResourceValue getValue(ResourceType type, String name) {
133 return mValue;
134 }
135
136 /**
137 * Returns the name of the resources.
138 */
139 private String getResourceName(ResourceType type) {
140 // get the name from the filename.
141 String name = getFile().getName();
142
143 int pos = name.indexOf('.');
144 if (pos != -1) {
145 name = name.substring(0, pos);
146 }
147
148 return name;
149 }
150
151 /**
152 * Validates the associated resource file to make sure the attribute references are valid
153 *
154 * @return true if parsing succeeds and false if it fails
155 */
156 private boolean validateAttributes(ScanningContext context) {
157 // We only need to check if it's a non-framework file (and an XML file; skip .png's)
158 if (!isFramework() && SdkUtils.endsWith(getFile().getName(), DOT_XML)) {
159 ValidatingResourceParser parser = new ValidatingResourceParser(context, false);
160 try {
161 IAbstractFile file = getFile();
162 return parser.parse(file.getOsLocation(), file.getContents());
163 } catch (Exception e) {
164 context.needsFullAapt();
165 }
166
167 return false;
168 }
169
170 return true;
171 }
172 }