• 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 
getAttributeComparator()40     public Comparator<Attr> getAttributeComparator() {
41         switch (this) {
42             case ALPHABETICAL:
43                 return ALPHABETICAL_COMPARATOR;
44             case NO_SORTING:
45                 return EXISTING_ORDER_COMPARATOR;
46             case LOGICAL:
47             default:
48                 return SORTED_ORDER_COMPARATOR;
49         }
50     }
51 
52     /** Comparator which can be used to sort attributes in the coding style priority order */
53     private static final Comparator<Attr> SORTED_ORDER_COMPARATOR = new Comparator<Attr>() {
54         public int compare(Attr attr1, Attr attr2) {
55             // Namespace declarations should always go first
56             if (XMLNS.equals(attr1.getPrefix())) {
57                 if (XMLNS.equals(attr2.getPrefix())) {
58                     return 0;
59                 }
60                 return -1;
61             } else if (XMLNS.equals(attr2.getPrefix())) {
62                 return 1;
63             }
64 
65             // Sort by preferred attribute order
66             return UiAttributeNode.compareAttributes(attr1.getLocalName(),
67                     attr2.getLocalName());
68         }
69     };
70 
71     /**
72      * Comparator which can be used to "sort" attributes into their existing source order
73      * (which is not the same as the node map iteration order in the DOM model)
74      */
75     private static final Comparator<Attr> EXISTING_ORDER_COMPARATOR = new Comparator<Attr>() {
76         public int compare(Attr attr1, Attr attr2) {
77             IndexedRegion region1 = (IndexedRegion) attr1;
78             IndexedRegion region2 = (IndexedRegion) attr2;
79 
80             return region1.getStartOffset() - region2.getStartOffset();
81         }
82     };
83 
84     /**
85      * Comparator which can be used to sort attributes into alphabetical order (but xmlns
86      * is always first)
87      */
88     private static final Comparator<Attr> ALPHABETICAL_COMPARATOR = new Comparator<Attr>() {
89         public int compare(Attr attr1, Attr attr2) {
90             // Namespace declarations should always go first
91             if (XMLNS.equals(attr1.getPrefix())) {
92                 if (XMLNS.equals(attr2.getPrefix())) {
93                     return 0;
94                 }
95                 return -1;
96             } else if (XMLNS.equals(attr2.getPrefix())) {
97                 return 1;
98             }
99 
100             return attr1.getLocalName().compareTo(attr2.getLocalName());
101         }
102     };
103 }