• 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.preferences;
17 
18 import static com.android.ide.eclipse.adt.internal.editors.descriptors.XmlnsAttributeDescriptor.XMLNS;
19 
20 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiAttributeNode;
21 
22 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
23 import org.w3c.dom.Attr;
24 
25 import java.util.Comparator;
26 
27 /** Order to use when sorting attributes */
28 @SuppressWarnings("restriction") // IndexedRegion
29 public enum AttributeSortOrder {
30     NO_SORTING("none"),     //$NON-NLS-1$
31     ALPHABETICAL("alpha"),  //$NON-NLS-1$
32     LOGICAL("logical");     //$NON-NLS-1$
33 
AttributeSortOrder(String key)34     AttributeSortOrder(String key) {
35         this.key = key;
36     }
37 
38     public final String key;
39 
40     /**
41      * @return a comparator for use by this attribute sort order
42      */
getAttributeComparator()43     public Comparator<Attr> getAttributeComparator() {
44         switch (this) {
45             case ALPHABETICAL:
46                 return ALPHABETICAL_COMPARATOR;
47             case NO_SORTING:
48                 return EXISTING_ORDER_COMPARATOR;
49             case LOGICAL:
50             default:
51                 return SORTED_ORDER_COMPARATOR;
52         }
53     }
54 
55     /** Comparator which can be used to sort attributes in the coding style priority order */
56     private static final Comparator<Attr> SORTED_ORDER_COMPARATOR = new Comparator<Attr>() {
57         @Override
58         public int compare(Attr attr1, Attr attr2) {
59             // Namespace declarations should always go first
60             if (XMLNS.equals(attr1.getPrefix())) {
61                 if (XMLNS.equals(attr2.getPrefix())) {
62                     return 0;
63                 }
64                 return -1;
65             } else if (XMLNS.equals(attr2.getPrefix())) {
66                 return 1;
67             }
68 
69             // Sort by preferred attribute order
70             return UiAttributeNode.compareAttributes(
71                     attr1.getPrefix(), attr1.getLocalName(),
72                     attr2.getPrefix(), attr2.getLocalName());
73         }
74     };
75 
76     /**
77      * Comparator which can be used to "sort" attributes into their existing source order
78      * (which is not the same as the node map iteration order in the DOM model)
79      */
80     private static final Comparator<Attr> EXISTING_ORDER_COMPARATOR = new Comparator<Attr>() {
81         @Override
82         public int compare(Attr attr1, Attr attr2) {
83             IndexedRegion region1 = (IndexedRegion) attr1;
84             IndexedRegion region2 = (IndexedRegion) attr2;
85 
86             return region1.getStartOffset() - region2.getStartOffset();
87         }
88     };
89 
90     /**
91      * Comparator which can be used to sort attributes into alphabetical order (but xmlns
92      * is always first)
93      */
94     private static final Comparator<Attr> ALPHABETICAL_COMPARATOR = new Comparator<Attr>() {
95         @Override
96         public int compare(Attr attr1, Attr attr2) {
97             // Namespace declarations should always go first
98             if (XMLNS.equals(attr1.getPrefix())) {
99                 if (XMLNS.equals(attr2.getPrefix())) {
100                     return 0;
101                 }
102                 return -1;
103             } else if (XMLNS.equals(attr2.getPrefix())) {
104                 return 1;
105             }
106 
107             // Sort by name rather than localname to ensure we sort by namespaces first,
108             // then by names.
109             return attr1.getName().compareTo(attr2.getName());
110         }
111     };
112 }