J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * reserved comment block |
| 3 | * DO NOT REMOVE OR ALTER! |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright 1999-2004 The Apache Software Foundation. |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | */ |
| 22 | package com.sun.org.apache.xml.internal.security.c14n.helper; |
| 23 | |
| 24 | |
| 25 | |
| 26 | import com.sun.org.apache.xml.internal.security.utils.Constants; |
| 27 | import org.w3c.dom.Attr; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Compares two attributes based on the C14n specification. |
| 32 | * |
| 33 | * <UL> |
| 34 | * <LI>Namespace nodes have a lesser document order position than attribute nodes. |
| 35 | * <LI> An element's namespace nodes are sorted lexicographically by |
| 36 | * local name (the default namespace node, if one exists, has no |
| 37 | * local name and is therefore lexicographically least). |
| 38 | * <LI> An element's attribute nodes are sorted lexicographically with |
| 39 | * namespace URI as the primary key and local name as the secondary |
| 40 | * key (an empty namespace URI is lexicographically least). |
| 41 | * </UL> |
| 42 | * |
| 43 | * $todo$ Should we implement java.util.Comparator and import java.util.Arrays to use Arrays.sort(intarray); |
| 44 | * @author Christian Geuer-Pollmann |
| 45 | */ |
| 46 | public class AttrCompare implements java.util.Comparator { |
| 47 | |
| 48 | private final int ATTR0_BEFORE_ATTR1 = -1; |
| 49 | private final int ATTR1_BEFORE_ATTR0 = 1; |
| 50 | |
| 51 | private final static String XMLNS=Constants.NamespaceSpecNS; |
| 52 | /** |
| 53 | * Compares two attributes based on the C14n specification. |
| 54 | * |
| 55 | * <UL> |
| 56 | * <LI>Namespace nodes have a lesser document order position than attribute nodes. |
| 57 | * <LI> An element's namespace nodes are sorted lexicographically by |
| 58 | * local name (the default namespace node, if one exists, has no |
| 59 | * local name and is therefore lexicographically least). |
| 60 | * <LI> An element's attribute nodes are sorted lexicographically with |
| 61 | * namespace URI as the primary key and local name as the secondary |
| 62 | * key (an empty namespace URI is lexicographically least). |
| 63 | * </UL> |
| 64 | * |
| 65 | * @param obj0 casted Attr |
| 66 | * @param obj1 casted Attr |
| 67 | * @return returns a negative integer, zero, or a positive integer as obj0 is less than, equal to, or greater than obj1 |
| 68 | * |
| 69 | */ |
| 70 | public int compare(Object obj0, Object obj1) { |
| 71 | |
| 72 | Attr attr0 = (Attr) obj0; |
| 73 | Attr attr1 = (Attr) obj1; |
| 74 | String namespaceURI0 = attr0.getNamespaceURI(); |
| 75 | String namespaceURI1 = attr1.getNamespaceURI(); |
| 76 | |
| 77 | boolean isNamespaceAttr0 = |
| 78 | XMLNS.equals(namespaceURI0); |
| 79 | boolean isNamespaceAttr1 = |
| 80 | XMLNS.equals(namespaceURI1); |
| 81 | |
| 82 | if (isNamespaceAttr0) { |
| 83 | if (isNamespaceAttr1) { |
| 84 | |
| 85 | // both are namespaces |
| 86 | String localname0 = attr0.getLocalName(); |
| 87 | String localname1 = attr1.getLocalName(); |
| 88 | |
| 89 | if (localname0.equals("xmlns")) { |
| 90 | localname0 = ""; |
| 91 | } |
| 92 | |
| 93 | if (localname1.equals("xmlns")) { |
| 94 | localname1 = ""; |
| 95 | } |
| 96 | |
| 97 | return localname0.compareTo(localname1); |
| 98 | } |
| 99 | // attr0 is a namespace, attr1 is not |
| 100 | return ATTR0_BEFORE_ATTR1; |
| 101 | |
| 102 | } |
| 103 | if (isNamespaceAttr1) { |
| 104 | |
| 105 | // attr1 is a namespace, attr0 is not |
| 106 | return ATTR1_BEFORE_ATTR0; |
| 107 | } |
| 108 | |
| 109 | // none is a namespae |
| 110 | |
| 111 | if (namespaceURI0 == null) { |
| 112 | if (namespaceURI1 == null) { |
| 113 | /* |
| 114 | String localName0 = attr0.getLocalName(); |
| 115 | String localName1 = attr1.getLocalName(); |
| 116 | return localName0.compareTo(localName1); |
| 117 | */ |
| 118 | |
| 119 | String name0 = attr0.getName(); |
| 120 | String name1 = attr1.getName(); |
| 121 | return name0.compareTo(name1); |
| 122 | } |
| 123 | return ATTR0_BEFORE_ATTR1; |
| 124 | |
| 125 | } |
| 126 | if (namespaceURI1 == null) { |
| 127 | return ATTR1_BEFORE_ATTR0; |
| 128 | } |
| 129 | int a = namespaceURI0.compareTo(namespaceURI1); |
| 130 | |
| 131 | if (a != 0) { |
| 132 | return a; |
| 133 | } |
| 134 | /* |
| 135 | String localName0 = ; |
| 136 | String localName1 =;*/ |
| 137 | |
| 138 | return (attr0.getLocalName()) |
| 139 | .compareTo( attr1.getLocalName()); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | } |