author | mullan |
Mon, 26 Sep 2011 17:20:45 -0700 | |
changeset 10694 | cf59e2badd14 |
parent 1337 | e8d6cef36199 |
child 18780 | f47b920867e7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
5 |
/* |
|
6 |
* Copyright 1999-2004 The Apache Software Foundation. |
|
7 |
* |
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9 |
* you may not use this file except in compliance with the License. |
|
10 |
* You may obtain a copy of the License at |
|
11 |
* |
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13 |
* |
|
14 |
* Unless required by applicable law or agreed to in writing, software |
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17 |
* See the License for the specific language governing permissions and |
|
18 |
* limitations under the License. |
|
19 |
* |
|
20 |
*/ |
|
21 |
package com.sun.org.apache.xml.internal.security.c14n.helper; |
|
22 |
||
23 |
import com.sun.org.apache.xml.internal.security.utils.Constants; |
|
24 |
import org.w3c.dom.Attr; |
|
1337 | 25 |
import java.io.Serializable; |
26 |
import java.util.Comparator; |
|
2 | 27 |
|
28 |
/** |
|
29 |
* Compares two attributes based on the C14n specification. |
|
30 |
* |
|
31 |
* <UL> |
|
1337 | 32 |
* <LI>Namespace nodes have a lesser document order position than attribute |
33 |
* nodes. |
|
2 | 34 |
* <LI> An element's namespace nodes are sorted lexicographically by |
35 |
* local name (the default namespace node, if one exists, has no |
|
36 |
* local name and is therefore lexicographically least). |
|
37 |
* <LI> An element's attribute nodes are sorted lexicographically with |
|
38 |
* namespace URI as the primary key and local name as the secondary |
|
39 |
* key (an empty namespace URI is lexicographically least). |
|
40 |
* </UL> |
|
41 |
* |
|
42 |
* @author Christian Geuer-Pollmann |
|
43 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
44 |
public class AttrCompare implements Comparator<Attr>, Serializable { |
2 | 45 |
|
1337 | 46 |
private final static long serialVersionUID = -7113259629930576230L; |
47 |
private final static int ATTR0_BEFORE_ATTR1 = -1; |
|
48 |
private final static int ATTR1_BEFORE_ATTR0 = 1; |
|
49 |
private final static String XMLNS=Constants.NamespaceSpecNS; |
|
2 | 50 |
|
1337 | 51 |
/** |
52 |
* Compares two attributes based on the C14n specification. |
|
53 |
* |
|
54 |
* <UL> |
|
55 |
* <LI>Namespace nodes have a lesser document order position than |
|
56 |
* 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 |
* |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
65 |
* @param attr0 |
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
66 |
* @param attr1 |
1337 | 67 |
* @return returns a negative integer, zero, or a positive integer as |
68 |
* obj0 is less than, equal to, or greater than obj1 |
|
69 |
* |
|
70 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
71 |
public int compare(Attr attr0, Attr attr1) { |
2 | 72 |
|
1337 | 73 |
String namespaceURI0 = attr0.getNamespaceURI(); |
74 |
String namespaceURI1 = attr1.getNamespaceURI(); |
|
2 | 75 |
|
1337 | 76 |
boolean isNamespaceAttr0 = XMLNS==namespaceURI0; |
77 |
boolean isNamespaceAttr1 = XMLNS==namespaceURI1; |
|
2 | 78 |
|
1337 | 79 |
if (isNamespaceAttr0) { |
80 |
if (isNamespaceAttr1) { |
|
81 |
// both are namespaces |
|
82 |
String localname0 = attr0.getLocalName(); |
|
83 |
String localname1 = attr1.getLocalName(); |
|
2 | 84 |
|
1337 | 85 |
if (localname0.equals("xmlns")) { |
86 |
localname0 = ""; |
|
87 |
} |
|
2 | 88 |
|
1337 | 89 |
if (localname1.equals("xmlns")) { |
90 |
localname1 = ""; |
|
91 |
} |
|
2 | 92 |
|
1337 | 93 |
return localname0.compareTo(localname1); |
94 |
} |
|
95 |
// attr0 is a namespace, attr1 is not |
|
96 |
return ATTR0_BEFORE_ATTR1; |
|
97 |
} |
|
2 | 98 |
|
1337 | 99 |
if (isNamespaceAttr1) { |
2 | 100 |
// attr1 is a namespace, attr0 is not |
101 |
return ATTR1_BEFORE_ATTR0; |
|
1337 | 102 |
} |
2 | 103 |
|
1337 | 104 |
// none is a namespace |
105 |
if (namespaceURI0 == null) { |
|
106 |
if (namespaceURI1 == null) { |
|
2 | 107 |
String name0 = attr0.getName(); |
108 |
String name1 = attr1.getName(); |
|
109 |
return name0.compareTo(name1); |
|
1337 | 110 |
} |
111 |
return ATTR0_BEFORE_ATTR1; |
|
2 | 112 |
} |
113 |
||
1337 | 114 |
if (namespaceURI1 == null) { |
115 |
return ATTR1_BEFORE_ATTR0; |
|
116 |
} |
|
2 | 117 |
|
1337 | 118 |
int a = namespaceURI0.compareTo(namespaceURI1); |
119 |
if (a != 0) { |
|
120 |
return a; |
|
121 |
} |
|
2 | 122 |
|
1337 | 123 |
return (attr0.getLocalName()).compareTo(attr1.getLocalName()); |
124 |
} |
|
2 | 125 |
} |