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