1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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 com.android.ide.eclipse.adt.internal.editors.formatting; 17 18 import com.android.resources.ResourceFolderType; 19 import com.android.resources.ResourceType; 20 import com.android.sdklib.SdkConstants; 21 22 import org.eclipse.core.runtime.IPath; 23 24 /** 25 * Style to use when printing the XML. Different types of Android XML files use slightly 26 * different preferred formats. For example, in layout files there is typically always a 27 * newline between successive elements, whereas in a manifest file there is typically only 28 * newlines between different types of elements. As another example, in resource files, 29 * the format is typically much more compact: the text content of {@code <item>} tags is 30 * included on the same line whereas for other layout styles the children are typically 31 * placed on a line of their own. 32 */ 33 public enum XmlFormatStyle { 34 /** Layout formatting style: blank lines between elements, attributes on separate lines */ 35 LAYOUT, 36 37 /** Resource style: one line per complete element including text child content */ 38 RESOURCE, 39 40 /** 41 * Similar to layout style, but no newlines between related elements such as 42 * successive {@code <uses-permission>} declarations, and no newlines inside 43 * the second level elements (so an {@code <activity>} declaration appears as a 44 * single block with no whitespace within it) 45 */ 46 MANIFEST; 47 48 /** 49 * Returns the {@link XmlFormatStyle} to use for a resource of the given type 50 * 51 * @param resourceType the type of resource to be formatted 52 * @return the suitable format style to use 53 */ 54 public static XmlFormatStyle get(ResourceType resourceType) { 55 switch (resourceType) { 56 case ARRAY: 57 case ATTR: 58 case BOOL: 59 case DECLARE_STYLEABLE: 60 case DIMEN: 61 case FRACTION: 62 case ID: 63 case INTEGER: 64 case STRING: 65 case STYLE: 66 case STYLEABLE: 67 return RESOURCE; 68 69 default: 70 return LAYOUT; 71 } 72 } 73 74 /** 75 * Returns the {@link XmlFormatStyle} to use for resource files in the given resource 76 * folder 77 * 78 * @param folderType the type of folder containing the resource file 79 * @return the suitable format style to use 80 */ 81 public static XmlFormatStyle getForFolderType(ResourceFolderType folderType) { 82 if (folderType == ResourceFolderType.VALUES) { 83 return RESOURCE; 84 } else { 85 return LAYOUT; 86 } 87 } 88 89 /** 90 * Returns the {@link XmlFormatStyle} to use for resource files of the given path. 91 * 92 * @param path the path to the resource file 93 * @return the suitable format style to use 94 */ 95 public static XmlFormatStyle getForFile(IPath path) { 96 if (SdkConstants.FN_ANDROID_MANIFEST_XML.equals(path.lastSegment())) { 97 return MANIFEST; 98 } 99 100 String parentName = path.segment(path.segmentCount() - 1); 101 ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName); 102 return getForFolderType(folderType); 103 } 104 }