OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [xml/] [dom/] [html2/] [DomHTMLCollection.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* DomHTMLCollection.java --
2
   Copyright (C) 2005 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.xml.dom.html2;
39
 
40
import gnu.xml.dom.DomDOMException;
41
import gnu.xml.dom.DomElement;
42
import java.util.ArrayList;
43
import java.util.Iterator;
44
import java.util.LinkedList;
45
import java.util.List;
46
import org.w3c.dom.DOMException;
47
import org.w3c.dom.NamedNodeMap;
48
import org.w3c.dom.Node;
49
import org.w3c.dom.NodeList;
50
import org.w3c.dom.html2.HTMLCollection;
51
import org.w3c.dom.html2.HTMLOptionsCollection;
52
import org.w3c.dom.traversal.NodeFilter;
53
import org.w3c.dom.traversal.NodeIterator;
54
 
55
/**
56
 * An HTML element collection.
57
 *
58
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
59
 */
60
class DomHTMLCollection
61
  implements HTMLCollection, HTMLOptionsCollection, NodeList, NodeFilter
62
{
63
 
64
  final DomHTMLDocument doc;
65
  final Node root;
66
  List nodeNames;
67
  List attributeNames;
68
  List results;
69
 
70
  DomHTMLCollection(DomHTMLDocument doc, Node root)
71
  {
72
    this.doc = doc;
73
    this.root = root;
74
  }
75
 
76
  // -- Node name and attribute filtering --
77
 
78
  void addNodeName(String name)
79
  {
80
    if (nodeNames == null)
81
      {
82
        nodeNames = new LinkedList();
83
      }
84
    nodeNames.add(name);
85
  }
86
 
87
  void addAttributeName(String name)
88
  {
89
    if (attributeNames == null)
90
      {
91
        attributeNames = new LinkedList();
92
      }
93
    attributeNames.add(name);
94
  }
95
 
96
  public short acceptNode(Node n)
97
  {
98
    if (n.getNodeType() != Node.ELEMENT_NODE)
99
      {
100
        return NodeFilter.FILTER_SKIP;
101
      }
102
    String localName = n.getLocalName();
103
    if (localName == null)
104
      {
105
        localName = n.getNodeName();
106
      }
107
    if (nodeNames != null && !acceptName(localName))
108
      {
109
        return NodeFilter.FILTER_SKIP;
110
      }
111
    if (attributeNames != null && !acceptAttributes(n.getAttributes()))
112
      {
113
        return NodeFilter.FILTER_SKIP;
114
      }
115
    return NodeFilter.FILTER_ACCEPT;
116
  }
117
 
118
  private boolean acceptName(String name)
119
  {
120
    for (Iterator i = nodeNames.iterator(); i.hasNext(); )
121
      {
122
        String nodeName = (String) i.next();
123
        if (nodeName.equalsIgnoreCase(name))
124
          {
125
            return true;
126
          }
127
      }
128
    return false;
129
  }
130
 
131
  private boolean acceptAttributes(NamedNodeMap attrs)
132
  {
133
    for (Iterator i = attributeNames.iterator(); i.hasNext(); )
134
      {
135
        String attributeName = (String) i.next();
136
        Node attr = getNamedItem(attrs, attributeName);
137
        if (attr != null)
138
          {
139
            // Check that attribute has a non-null value
140
            String nodeValue = attr.getNodeValue();
141
            if (nodeValue != null && nodeValue.length() > 0)
142
              {
143
                return true;
144
              }
145
          }
146
      }
147
    return false;
148
  }
149
 
150
  /**
151
   * Case-insensitive version of getNamedItem.
152
   */
153
  private Node getNamedItem(NamedNodeMap attrs, String name)
154
  {
155
    int len = attrs.getLength();
156
    for (int i = 0; i < len; i++)
157
      {
158
        Node attr = attrs.item(i);
159
        String attrName = attr.getLocalName();
160
        if (attrName == null)
161
          {
162
            attrName = attr.getNodeName();
163
          }
164
        if (name.equalsIgnoreCase(attrName))
165
          {
166
            return attr;
167
          }
168
      }
169
    return null;
170
  }
171
 
172
  // -- Perform query --
173
 
174
  void evaluate()
175
  {
176
    NodeIterator i = doc.createNodeIterator(root, NodeFilter.SHOW_ELEMENT,
177
                                            this, true);
178
    results = new ArrayList();
179
    for (Node node = i.nextNode(); node != null; node = i.nextNode())
180
      {
181
        results.add(node);
182
      }
183
  }
184
 
185
  // -- HTMLCollection/NodeList interface --
186
 
187
  public int getLength()
188
  {
189
    return results.size();
190
  }
191
 
192
  public void setLength(int length)
193
  {
194
    throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
195
  }
196
 
197
  public Node item(int index)
198
  {
199
    return (Node) results.get(index);
200
  }
201
 
202
  public Node namedItem(String name)
203
  {
204
    boolean xhtml = false; // FIXME detect XHTML document
205
    for (Iterator i = results.iterator(); i.hasNext(); )
206
      {
207
        Node node = (Node) i.next();
208
        NamedNodeMap attrs = node.getAttributes();
209
        Node attr = getNamedItem(attrs, "id");
210
        if (name.equals(attr.getTextContent()))
211
          {
212
            return node;
213
          }
214
        if (!xhtml)
215
          {
216
            attr = getNamedItem(attrs, "name");
217
            if (name.equals(attr.getTextContent()))
218
              {
219
                return node;
220
              }
221
          }
222
      }
223
    return null;
224
  }
225
 
226
}
227
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.