KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > c14n > helper > AttrCompare


1
2 /*
3  * Copyright 1999-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package com.sun.org.apache.xml.internal.security.c14n.helper;
19
20
21
22 import com.sun.org.apache.xml.internal.security.utils.Constants;
23 import org.w3c.dom.Attr JavaDoc;
24
25
26 /**
27  * Compares two attributes based on the C14n specification.
28  *
29  * <UL>
30  * <LI>Namespace nodes have a lesser document order position than attribute nodes.
31  * <LI> An element's namespace nodes are sorted lexicographically by
32  * local name (the default namespace node, if one exists, has no
33  * local name and is therefore lexicographically least).
34  * <LI> An element's attribute nodes are sorted lexicographically with
35  * namespace URI as the primary key and local name as the secondary
36  * key (an empty namespace URI is lexicographically least).
37  * </UL>
38  *
39  * $todo$ Should we implement java.util.Comparator and import java.util.Arrays to use Arrays.sort(intarray);
40  * @author Christian Geuer-Pollmann
41  */

42 public class AttrCompare implements java.util.Comparator JavaDoc {
43
44    private final int ATTR0_BEFORE_ATTR1 = -1;
45    private final int ATTR1_BEFORE_ATTR0 = 1;
46
47    private final static String JavaDoc XMLNS=Constants.NamespaceSpecNS;
48    /**
49     * Compares two attributes based on the C14n specification.
50     *
51     * <UL>
52     * <LI>Namespace nodes have a lesser document order position than attribute nodes.
53     * <LI> An element's namespace nodes are sorted lexicographically by
54     * local name (the default namespace node, if one exists, has no
55     * local name and is therefore lexicographically least).
56     * <LI> An element's attribute nodes are sorted lexicographically with
57     * namespace URI as the primary key and local name as the secondary
58     * key (an empty namespace URI is lexicographically least).
59     * </UL>
60     *
61     * @param obj0 casted Attr
62     * @param obj1 casted Attr
63     * @return returns a negative integer, zero, or a positive integer as obj0 is less than, equal to, or greater than obj1
64     *
65     */

66    public int compare(Object JavaDoc obj0, Object JavaDoc obj1) {
67
68       Attr JavaDoc attr0 = (Attr JavaDoc) obj0;
69       Attr JavaDoc attr1 = (Attr JavaDoc) obj1;
70       String JavaDoc namespaceURI0 = attr0.getNamespaceURI();
71       String JavaDoc namespaceURI1 = attr1.getNamespaceURI();
72       
73       boolean isNamespaceAttr0 =
74         XMLNS.equals(namespaceURI0);
75       boolean isNamespaceAttr1 =
76         XMLNS.equals(namespaceURI1);
77
78       if (isNamespaceAttr0) {
79          if (isNamespaceAttr1) {
80
81             // both are namespaces
82
String JavaDoc localname0 = attr0.getLocalName();
83             String JavaDoc localname1 = attr1.getLocalName();
84
85             if (localname0.equals("xmlns")) {
86                localname0 = "";
87             }
88
89             if (localname1.equals("xmlns")) {
90                localname1 = "";
91             }
92
93             return localname0.compareTo(localname1);
94          }
95          // attr0 is a namespace, attr1 is not
96
return ATTR0_BEFORE_ATTR1;
97          
98       }
99       if (isNamespaceAttr1) {
100
101             // attr1 is a namespace, attr0 is not
102
return ATTR1_BEFORE_ATTR0;
103       }
104
105       // none is a namespae
106

107       if (namespaceURI0 == null) {
108         if (namespaceURI1 == null) {
109             /*
110              String localName0 = attr0.getLocalName();
111              String localName1 = attr1.getLocalName();
112              return localName0.compareTo(localName1);
113              */

114             
115             String JavaDoc name0 = attr0.getName();
116             String JavaDoc name1 = attr1.getName();
117             return name0.compareTo(name1);
118         }
119         return ATTR0_BEFORE_ATTR1;
120         
121       }
122       if (namespaceURI1 == null) {
123             return ATTR1_BEFORE_ATTR0;
124       }
125       int a = namespaceURI0.compareTo(namespaceURI1);
126             
127       if (a != 0) {
128         return a;
129       }
130       /*
131       String localName0 = ;
132       String localName1 =;*/

133       
134       return (attr0.getLocalName())
135                     .compareTo( attr1.getLocalName());
136          
137    }
138          
139 }
140
Popular Tags