• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SdkConstants;
19 import com.android.resources.ResourceFolderType;
20 import com.android.resources.ResourceType;
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     /** Similar to layout formatting style, but no blank lines inside opening elements */
38     FILE,
39 
40     /** Resource style: one line per complete element including text child content */
41     RESOURCE,
42 
43     /**
44      * Similar to layout style, but no newlines between related elements such as
45      * successive {@code <uses-permission>} declarations, and no newlines inside
46      * the second level elements (so an {@code <activity>} declaration appears as a
47      * single block with no whitespace within it)
48      */
49     MANIFEST;
50 
51     /**
52      * Returns the {@link XmlFormatStyle} to use for a resource of the given type
53      *
54      * @param resourceType the type of resource to be formatted
55      * @return the suitable format style to use
56      */
get(ResourceType resourceType)57     public static XmlFormatStyle get(ResourceType resourceType) {
58         switch (resourceType) {
59             case ARRAY:
60             case ATTR:
61             case BOOL:
62             case DECLARE_STYLEABLE:
63             case DIMEN:
64             case FRACTION:
65             case ID:
66             case INTEGER:
67             case STRING:
68             case PLURALS:
69             case STYLE:
70             case STYLEABLE:
71             case COLOR:
72                 return RESOURCE;
73 
74             case LAYOUT:
75                 return LAYOUT;
76 
77             case DRAWABLE:
78             case MENU:
79             case ANIM:
80             case ANIMATOR:
81             case INTERPOLATOR:
82             default:
83                 return FILE;
84         }
85     }
86 
87     /**
88      * Returns the {@link XmlFormatStyle} to use for resource files in the given resource
89      * folder
90      *
91      * @param folderType the type of folder containing the resource file
92      * @return the suitable format style to use
93      */
getForFolderType(ResourceFolderType folderType)94     public static XmlFormatStyle getForFolderType(ResourceFolderType folderType) {
95         switch (folderType) {
96             case LAYOUT:
97                 return LAYOUT;
98             case COLOR:
99             case VALUES:
100                 return RESOURCE;
101             case ANIM:
102             case ANIMATOR:
103             case DRAWABLE:
104             case INTERPOLATOR:
105             case MENU:
106             default:
107                 return FILE;
108         }
109     }
110 
111     /**
112      * Returns the {@link XmlFormatStyle} to use for resource files of the given path.
113      *
114      * @param path the path to the resource file
115      * @return the suitable format style to use
116      */
getForFile(IPath path)117     public static XmlFormatStyle getForFile(IPath path) {
118         if (SdkConstants.FN_ANDROID_MANIFEST_XML.equals(path.lastSegment())) {
119             return MANIFEST;
120         }
121 
122         if (path.segmentCount() > 2) {
123             String parentName = path.segment(path.segmentCount() - 2);
124             ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
125             return getForFolderType(folderType);
126         }
127 
128         return FILE;
129     }
130 }