File: NokogiriNamespaceCache.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 (228 lines) | stat: -rw-r--r-- 5,128 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package nokogiri.internals;

import static nokogiri.internals.NokogiriHelpers.isNamespace;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import nokogiri.XmlNamespace;

import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * Cache of namespaces of each node. XmlDocument has one cache of this class.
 *
 * @author sergio
 * @author Yoko Harada <yokolet@gmail.com>
 */
public class NokogiriNamespaceCache
{

  private final Map<CacheKey, CacheEntry> cache;  // pair of the index of a given key and entry
  private XmlNamespace defaultNamespace = null;

  public
  NokogiriNamespaceCache()
  {
    this.cache = new LinkedHashMap<CacheKey, CacheEntry>(4);
  }

  public
  NokogiriNamespaceCache(NokogiriNamespaceCache cache)
  {
    this.cache = new LinkedHashMap<CacheKey, CacheEntry>(cache.size() + 2);
    this.cache.putAll(cache.cache);
  }

  public XmlNamespace
  getDefault()
  {
    return defaultNamespace;
  }

  public XmlNamespace
  get(String prefix, String href)
  {
    if (href == null) { return null; }

    CacheEntry value = cache.get(new CacheKey(prefix, href));
    return value == null ? null : value.namespace;
  }

  public XmlNamespace
  get(Node node, String prefix)
  {
    if (prefix == null) { return defaultNamespace; }
    for (Map.Entry<CacheKey, CacheEntry> entry : cache.entrySet()) {
      if (entry.getKey().prefix.equals(prefix)) {
        if (entry.getValue().isOwner(node)) {
          return entry.getValue().namespace;
        }
      }
    }
    return null;
  }

  public List<XmlNamespace>
  get(String prefix)
  {
    List<XmlNamespace> namespaces = new ArrayList<XmlNamespace>();
    if (prefix == null) {
      namespaces.add(defaultNamespace);
      return namespaces;
    }
    for (Map.Entry<CacheKey, CacheEntry> entry : cache.entrySet()) {
      if (entry.getKey().prefix.equals(prefix)) {
        namespaces.add(entry.getValue().namespace);
      }
    }
    return namespaces;
  }

  public List<XmlNamespace>
  get(Node node)
  {
    List<XmlNamespace> namespaces = new ArrayList<XmlNamespace>();
    for (Map.Entry<CacheKey, CacheEntry> entry : cache.entrySet()) {
      if (entry.getValue().isOwner(node)) {
        namespaces.add(entry.getValue().namespace);
      }
    }
    return namespaces;
  }

  public void
  put(XmlNamespace namespace, Node ownerNode)
  {
    String prefix = namespace.getPrefix();
    String href = namespace.getHref();
    if (href == null) { return; }

    CacheKey key = new CacheKey(prefix, href);
    if (cache.get(key) != null) { return; }
    cache.put(key, new CacheEntry(namespace, ownerNode));
    if ("".equals(prefix)) { defaultNamespace = namespace; }
  }

  public void
  remove(Node ownerNode)
  {
    String prefix = ownerNode.getPrefix();
    String href = ownerNode.getNamespaceURI();
    if (href == null) { return; }

    cache.remove(new CacheKey(prefix, href));
  }

  public int
  size()
  {
    return cache.size();
  }

  public void
  clear()
  {
    // removes namespace declarations from node
    for (CacheEntry entry : cache.values()) {
      NamedNodeMap attributes = entry.ownerNode.getAttributes();
      for (int j = 0; j < attributes.getLength(); j++) {
        String name = ((Attr) attributes.item(j)).getName();
        if (isNamespace(name)) {
          attributes.removeNamedItem(name);
        }
      }
    }
    cache.clear();
    defaultNamespace = null;
  }

  public void
  replaceNode(Node oldNode, Node newNode)
  {
    for (Map.Entry<CacheKey, CacheEntry> entry : cache.entrySet()) {
      if (entry.getValue().isOwner(oldNode)) {
        entry.getValue().replaceOwner(newNode);
      }
    }
  }

  @Override
  public String
  toString()
  {
    return getClass().getName() + '@' + Integer.toHexString(hashCode()) + '(' + cache + "default=" + defaultNamespace + ')';
  }

  private static class CacheKey
  {
    final String prefix;
    final String href;

    CacheKey(String prefix, String href)
    {
      this.prefix = prefix;
      this.href = href;
    }

    @Override
    public boolean
    equals(final Object obj)
    {
      if (obj instanceof CacheKey) {
        CacheKey that = (CacheKey) obj;
        return prefix == null ? that.prefix == null : prefix.equals(that.prefix) && href.equals(that.href);
      }
      return false;
    }

    @Override
    public int
    hashCode()
    {
      return (prefix == null ? 0 : prefix.hashCode()) + 37 * href.hashCode();
    }

    @Override
    public String
    toString()
    {
      return '[' + prefix + ']' + href;
    }
  }

  private static class CacheEntry
  {
    final XmlNamespace namespace;
    private Node ownerNode;

    CacheEntry(XmlNamespace namespace, Node ownerNode)
    {
      this.namespace = namespace;
      this.ownerNode = ownerNode;
    }

    boolean
    isOwner(Node node)
    {
      return ownerNode.isSameNode(node);
    }

    void
    replaceOwner(Node newNode)
    {
      this.ownerNode = newNode;
    }

    @Override
    public String
    toString()
    {
      return namespace.toString();
    }
  }
}