Merge from the pain train
[official-gcc.git] / libjava / gnu / xml / transform / NodeNumberNode.java
blobaae86c6f2a3aa3dc0bf9ce65fd8ac33eed5a0f1b
1 /* NodeNumberNode.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
38 package gnu.xml.transform;
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;
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.
57 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
59 final class NodeNumberNode
60 extends AbstractNumberNode
63 static final int SINGLE = 0;
64 static final int MULTIPLE = 1;
65 static final int ANY = 2;
67 final int level;
68 final Pattern count;
69 final Pattern from;
71 NodeNumberNode(TemplateNode children, TemplateNode next,
72 int level, Pattern count, Pattern from,
73 TemplateNode format, String lang,
74 int letterValue, String groupingSeparator, int groupingSize)
76 super(children, next, format, lang, letterValue, groupingSeparator,
77 groupingSize);
78 this.level = level;
79 this.count = count;
80 this.from = from;
83 TemplateNode clone(Stylesheet stylesheet)
85 return new NodeNumberNode((children == null) ? null :
86 children.clone(stylesheet),
87 (next == null) ? null :
88 next.clone(stylesheet),
89 level,
90 (count == null) ? null :
91 (Pattern) count.clone(stylesheet),
92 (from == null) ? from :
93 (Pattern) from.clone(stylesheet),
94 format, lang, letterValue,
95 groupingSeparator, groupingSize);
98 int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
99 throws TransformerException
101 /*if (from != null)
103 Object ret = from.evaluate(context, pos, len);
104 if (ret instanceof Collection)
106 Collection ns = (Collection) ret;
107 if (ns.size() > 0)
109 List list = new ArrayList(ns);
110 Collections.sort(list, documentOrderComparator);
111 context = (Node) list.get(0);
113 else
115 return new int[0];
118 else
120 return new int[0];
123 Node current = context;
124 switch (level)
126 case SINGLE:
127 if (from == null)
129 while (context != null && !countMatches(current, context))
131 context = context.getParentNode();
134 else
136 while (context != null && !countMatches(current, context) &&
137 !fromMatches(context))
139 context = context.getParentNode();
142 return (context == null) ? new int[0] :
143 new int[] { (context == current) ? pos : getIndex(current, context) };
144 case MULTIPLE:
145 List ancestors = new ArrayList();
146 while (context != null)
148 if (countMatches(current, context))
150 if (from == null || fromMatches(context))
152 ancestors.add(context);
155 context = context.getParentNode();
157 Collections.sort(ancestors, documentOrderComparator);
158 int[] ret = new int[ancestors.size()];
159 for (int i = 0; i < ret.length; i++)
161 ret[i] = getIndex(current, (Node) ancestors.get(i));
163 return ret;
164 case ANY:
165 Expr preceding = new Selector(Selector.PRECEDING,
166 Collections.EMPTY_LIST);
167 Expr ancestorOrSelf = new Selector(Selector.ANCESTOR_OR_SELF,
168 Collections.EMPTY_LIST);
169 Expr any = new UnionExpr(preceding, ancestorOrSelf);
170 Object eval = any.evaluate(context, pos, len);
171 if (eval instanceof Collection)
173 Collection ns = (Collection) eval;
174 List candidates = new ArrayList();
175 for (Iterator i = ns.iterator(); i.hasNext(); )
177 Node candidate = (Node) i.next();
178 if (countMatches(current, candidate))
180 candidates.add(candidate);
181 if (from != null && from.matches(candidate))
183 break;
187 return new int[] { candidates.size() };
189 return new int[0];
190 default:
191 throw new TransformerException("invalid level");
195 boolean countMatches(Node current, Node node)
197 if (count == null)
199 int cnt = current.getNodeType();
200 int nnt = node.getNodeType();
201 if (cnt != nnt)
203 return false;
205 if (nnt == Node.ELEMENT_NODE || nnt == Node.ATTRIBUTE_NODE)
207 String curi = current.getNamespaceURI();
208 String nuri = node.getNamespaceURI();
209 if ((curi == null && nuri != null) ||
210 (curi != null && !curi.equals(nuri)))
212 return false;
214 String cn = current.getLocalName();
215 String nn = current.getLocalName();
216 if (!cn.equals(nn))
218 return false;
221 return true;
223 else
225 return count.matches(node);
229 boolean fromMatches(Node node)
231 for (Node ctx = node.getParentNode(); ctx != null;
232 ctx = ctx.getParentNode())
234 if (from.matches(ctx))
236 return true;
239 return false;
242 int getIndex(Node current, Node node)
244 int index = 0;
249 node = node.getPreviousSibling();
251 while (node != null && !countMatches(current, node));
252 index++;
254 while (node != null);
255 return index;