File: AttrCompare.java

package info (click to toggle)
ruby-nokogiri 1.13.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,416 kB
  • sloc: ansic: 38,198; xml: 28,086; ruby: 22,271; java: 15,517; cpp: 7,037; yacc: 244; sh: 148; makefile: 136
file content (122 lines) | stat: -rw-r--r-- 4,003 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package nokogiri.internals.c14n;

import java.io.Serializable;
import java.util.Comparator;


import org.w3c.dom.Attr;

/**
 * Compares two attributes based on the C14n specification.
 *
 * <UL>
 * <LI>Namespace nodes have a lesser document order position than attribute
 *   nodes.
 * <LI> An element's namespace nodes are sorted lexicographically by
 *   local name (the default namespace node, if one exists, has no
 *   local name and is therefore lexicographically least).
 * <LI> An element's attribute nodes are sorted lexicographically with
 *   namespace URI as the primary key and local name as the secondary
 *   key (an empty namespace URI is lexicographically least).
 * </UL>
 *
 * @author Christian Geuer-Pollmann
 */
public class AttrCompare implements Comparator<Attr>, Serializable
{

  private static final long serialVersionUID = -7113259629930576230L;
  private static final int ATTR0_BEFORE_ATTR1 = -1;
  private static final int ATTR1_BEFORE_ATTR0 = 1;
  private static final String XMLNS = Constants.NamespaceSpecNS;

  /**
   * Compares two attributes based on the C14n specification.
   *
   * <UL>
   * <LI>Namespace nodes have a lesser document order position than
   *   attribute nodes.
   * <LI> An element's namespace nodes are sorted lexicographically by
   *   local name (the default namespace node, if one exists, has no
   *   local name and is therefore lexicographically least).
   * <LI> An element's attribute nodes are sorted lexicographically with
   *   namespace URI as the primary key and local name as the secondary
   *   key (an empty namespace URI is lexicographically least).
   * </UL>
   *
   * @param attr0
   * @param attr1
   * @return returns a negative integer, zero, or a positive integer as
   *   obj0 is less than, equal to, or greater than obj1
   *
   */
  public int
  compare(Attr attr0, Attr attr1)
  {
    String namespaceURI0 = attr0.getNamespaceURI();
    String namespaceURI1 = attr1.getNamespaceURI();

    boolean isNamespaceAttr0 = XMLNS.equals(namespaceURI0);
    boolean isNamespaceAttr1 = XMLNS.equals(namespaceURI1);

    if (isNamespaceAttr0) {
      if (isNamespaceAttr1) {
        // both are namespaces
        String localname0 = attr0.getLocalName();
        String localname1 = attr1.getLocalName();

        if ("xmlns".equals(localname0)) {
          localname0 = "";
        }

        if ("xmlns".equals(localname1)) {
          localname1 = "";
        }

        return localname0.compareTo(localname1);
      }
      // attr0 is a namespace, attr1 is not
      return ATTR0_BEFORE_ATTR1;
    } else if (isNamespaceAttr1) {
      // attr1 is a namespace, attr0 is not
      return ATTR1_BEFORE_ATTR0;
    }

    // none is a namespace
    if (namespaceURI0 == null) {
      if (namespaceURI1 == null) {
        String name0 = attr0.getName();
        String name1 = attr1.getName();
        return name0.compareTo(name1);
      }
      return ATTR0_BEFORE_ATTR1;
    } else if (namespaceURI1 == null) {
      return ATTR1_BEFORE_ATTR0;
    }

    int a = namespaceURI0.compareTo(namespaceURI1);
    if (a != 0) {
      return a;
    }

    return (attr0.getLocalName()).compareTo(attr1.getLocalName());
  }
}