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/] [transform/] [NodeNumberNode.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* NodeNumberNode.java --
2
   Copyright (C) 2004 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.transform;
39
 
40
import java.util.ArrayList;
41
import java.util.Collection;
42
import java.util.Collections;
43
import java.util.Iterator;
44
import java.util.List;
45
import javax.xml.transform.TransformerException;
46
import org.w3c.dom.Node;
47
import gnu.xml.xpath.Expr;
48
import gnu.xml.xpath.Pattern;
49
import gnu.xml.xpath.Selector;
50
import gnu.xml.xpath.UnionExpr;
51
 
52
/**
53
 * A template node representing the XSL <code>number</code> instruction
54
 * with no <code>value</code> expression, i.e. the value is computed from
55
 * the document position of the context node.
56
 *
57
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
58
 */
59
final class NodeNumberNode
60
  extends AbstractNumberNode
61
{
62
 
63
  static final int SINGLE = 0;
64
  static final int MULTIPLE = 1;
65
  static final int ANY = 2;
66
 
67
  final int level;
68
  final Pattern count;
69
  final Pattern from;
70
 
71
  NodeNumberNode(int level, Pattern count, Pattern from,
72
                 TemplateNode format, String lang,
73
                 int letterValue, String groupingSeparator, int groupingSize)
74
  {
75
    super(format, lang, letterValue, groupingSeparator, groupingSize);
76
    this.level = level;
77
    this.count = count;
78
    this.from = from;
79
  }
80
 
81
  TemplateNode clone(Stylesheet stylesheet)
82
  {
83
    TemplateNode ret = new NodeNumberNode(level,
84
                                          (count == null) ? null :
85
                                          (Pattern) count.clone(stylesheet),
86
                                          (from == null) ? from :
87
                                          (Pattern) from.clone(stylesheet),
88
                                          format, lang, letterValue,
89
                                          groupingSeparator, groupingSize);
90
    if (children != null)
91
      {
92
        ret.children = children.clone(stylesheet);
93
      }
94
    if (next != null)
95
      {
96
        ret.next = next.clone(stylesheet);
97
      }
98
    return ret;
99
  }
100
 
101
  int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
102
    throws TransformerException
103
  {
104
    /*if (from != null)
105
      {
106
        Object ret = from.evaluate(context, pos, len);
107
        if (ret instanceof Collection)
108
          {
109
            Collection ns = (Collection) ret;
110
            if (ns.size() > 0)
111
              {
112
                List list = new ArrayList(ns);
113
                Collections.sort(list, documentOrderComparator);
114
                context = (Node) list.get(0);
115
              }
116
            else
117
              {
118
                return new int[0];
119
              }
120
          }
121
        else
122
          {
123
            return new int[0];
124
          }
125
      }*/
126
    Node current = context;
127
    switch (level)
128
      {
129
      case SINGLE:
130
        if (from == null)
131
          {
132
            while (context != null && !countMatches(current, context))
133
              {
134
                context = context.getParentNode();
135
              }
136
          }
137
        else
138
          {
139
            while (context != null && !countMatches(current, context) &&
140
                   !fromMatches(context))
141
              {
142
                context = context.getParentNode();
143
              }
144
          }
145
        return (context == null) ? new int[0] :
146
          new int[] { (context == current) ? pos : getIndex(current, context) };
147
      case MULTIPLE:
148
        List ancestors = new ArrayList();
149
        while (context != null)
150
          {
151
            if (countMatches(current, context))
152
              {
153
                if (from == null || fromMatches(context))
154
                  {
155
                    ancestors.add(context);
156
                  }
157
              }
158
            context = context.getParentNode();
159
          }
160
        Collections.sort(ancestors, documentOrderComparator);
161
        int[] ret = new int[ancestors.size()];
162
        for (int i = 0; i < ret.length; i++)
163
          {
164
            ret[i] = getIndex(current, (Node) ancestors.get(i));
165
          }
166
        return ret;
167
      case ANY:
168
        Expr preceding = new Selector(Selector.PRECEDING,
169
                                      Collections.EMPTY_LIST);
170
        Expr ancestorOrSelf = new Selector(Selector.ANCESTOR_OR_SELF,
171
                                           Collections.EMPTY_LIST);
172
        Expr any = new UnionExpr(preceding, ancestorOrSelf);
173
        Object eval = any.evaluate(context, pos, len);
174
        if (eval instanceof Collection)
175
          {
176
            Collection ns = (Collection) eval;
177
            List candidates = new ArrayList();
178
            for (Iterator i = ns.iterator(); i.hasNext(); )
179
              {
180
                Node candidate = (Node) i.next();
181
                if (countMatches(current, candidate))
182
                  {
183
                    candidates.add(candidate);
184
                    if (from != null && from.matches(candidate))
185
                      {
186
                        break;
187
                      }
188
                  }
189
              }
190
            return new int[] { candidates.size() };
191
          }
192
        return new int[0];
193
      default:
194
        throw new TransformerException("invalid level");
195
      }
196
  }
197
 
198
  boolean countMatches(Node current, Node node)
199
  {
200
    if (count == null)
201
      {
202
        int cnt = current.getNodeType();
203
        int nnt = node.getNodeType();
204
        if (cnt != nnt)
205
          {
206
            return false;
207
          }
208
        if (nnt == Node.ELEMENT_NODE || nnt == Node.ATTRIBUTE_NODE)
209
          {
210
            String curi = current.getNamespaceURI();
211
            String nuri = node.getNamespaceURI();
212
            if ((curi == null && nuri != null) ||
213
                (curi != null && !curi.equals(nuri)))
214
              {
215
                return false;
216
              }
217
            String cn = current.getLocalName();
218
            if (cn == null)
219
              {
220
                cn = current.getNodeName();
221
              }
222
            String nn = node.getLocalName();
223
            if (nn == null)
224
              {
225
                nn = node.getNodeName();
226
              }
227
            if (!cn.equals(nn))
228
              {
229
                return false;
230
              }
231
          }
232
        return true;
233
      }
234
    else
235
      {
236
        return count.matches(node);
237
      }
238
  }
239
 
240
  boolean fromMatches(Node node)
241
  {
242
    for (Node ctx = node.getParentNode(); ctx != null;
243
         ctx = ctx.getParentNode())
244
      {
245
        if (from.matches(ctx))
246
          {
247
            return true;
248
          }
249
      }
250
    return false;
251
  }
252
 
253
  int getIndex(Node current, Node node)
254
  {
255
    int index = 0;
256
    do
257
      {
258
        do
259
          {
260
            node = node.getPreviousSibling();
261
          }
262
        while (node != null && !countMatches(current, node));
263
        index++;
264
      }
265
    while (node != null);
266
    return index;
267
  }
268
 
269
}

powered by: WebSVN 2.1.0

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