libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / xml / transform / AbstractNumberNode.java
blobfb953e4f66b44fbe68486a61464724a3b031db37
1 /* AbstractNumberNode.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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 gnu.java.lang.CPStringBuilder;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45 import javax.xml.namespace.QName;
46 import javax.xml.transform.TransformerException;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.DocumentFragment;
49 import org.w3c.dom.Node;
50 import org.w3c.dom.Text;
51 import gnu.xml.xpath.Expr;
53 /**
54 * A template node representing the XSL <code>number</code> instruction.
56 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
58 abstract class AbstractNumberNode
59 extends TemplateNode
62 static final int ALPHABETIC = 0;
63 static final int TRADITIONAL = 1;
65 final TemplateNode format;
66 final String lang;
67 final int letterValue;
68 final String groupingSeparator;
69 final int groupingSize;
71 AbstractNumberNode(TemplateNode format, String lang,
72 int letterValue, String groupingSeparator,
73 int groupingSize)
75 this.format = format;
76 this.lang = lang;
77 this.letterValue = letterValue;
78 this.groupingSeparator = groupingSeparator;
79 this.groupingSize = groupingSize;
82 void doApply(Stylesheet stylesheet, QName mode,
83 Node context, int pos, int len,
84 Node parent, Node nextSibling)
85 throws TransformerException
87 Document doc = (parent instanceof Document) ? (Document) parent :
88 parent.getOwnerDocument();
89 DocumentFragment fragment = doc.createDocumentFragment();
90 format.apply(stylesheet, mode, context, pos, len, fragment, null);
91 String f = Expr._string(context, Collections.singleton(fragment));
92 String value = format(f, compute(stylesheet, context, pos, len));
93 Text text = doc.createTextNode(value);
94 if (nextSibling != null)
96 parent.insertBefore(text, nextSibling);
98 else
100 parent.appendChild(text);
102 // xsl:number doesn't process children
103 if (next != null)
105 next.apply(stylesheet, mode,
106 context, pos, len,
107 parent, nextSibling);
111 String format(String format, int[] number)
113 if (number.length == 0)
115 return "";
117 int start = 0, end = 0, len = format.length(); // region of format
118 // Tokenize
119 List tokens = new ArrayList((number.length * 2) + 1);
120 List types = new ArrayList(tokens.size());
121 while (end < len)
123 while (end < len && !isAlphanumeric(format.charAt(end)))
125 end++;
127 if (end > start)
129 tokens.add(format.substring(start, end));
130 types.add(Boolean.FALSE);
132 start = end;
133 while (end < len && isAlphanumeric(format.charAt(end)))
135 end++;
137 if (end > start)
139 tokens.add(format.substring(start, end));
140 types.add(Boolean.TRUE);
142 start = end;
144 // Process tokens
145 CPStringBuilder buf = new CPStringBuilder();
146 len = tokens.size();
147 int pos = 0;
148 for (int i = 0; i < len; i++)
150 String token = (i < 0) ? "." : (String) tokens.get(i);
151 boolean alpha = (i < 0) ? true :
152 ((Boolean) types.get(i)).booleanValue();
153 if (!alpha)
155 buf.append(token);
157 else
159 if (pos < number.length)
161 format(buf, number[pos++], token);
162 if (((i + 1 == len) || (i + 2 == len)) &&
163 (pos < number.length))
165 // More numbers than tokens, reuse last token
166 i -= 2;
169 if (pos == number.length && i < (len - 2))
171 // No more numbers. Skip to the end...
172 i = len - 2;
173 if (((Boolean) types.get(i + 1)).booleanValue())
175 // number formatting token, ignore
176 i++;
181 //System.err.println("format: '"+format+"' "+asList(number)+" = '"+buf.toString()+"'");
182 return buf.toString();
185 /*List asList(int[] number)
187 List l = new ArrayList();
188 for (int i = 0; i < number.length; i++)
189 l.add(new Integer(number[i]));
190 return l;
193 void format(CPStringBuilder buf, int number, String formatToken)
195 int len = formatToken.length();
196 char c = formatToken.charAt(len - 1);
197 if (Character.digit(c, 10) == 1)
199 // Check preceding characters
200 for (int i = len - 2; i >= 0; i--)
202 if (formatToken.charAt(i) != (c - 1))
204 format(buf, number, "1");
205 return;
208 // Decimal representation
209 String val = Integer.toString(number);
210 for (int d = len - val.length(); d > 0; d--)
212 buf.append('0');
214 buf.append(val);
216 else if ("A".equals(formatToken))
218 buf.append(alphabetic('@', number));
220 else if ("a".equals(formatToken))
222 buf.append(alphabetic('`', number));
224 else if ("i".equals(formatToken))
226 buf.append(roman(false, number));
228 else if ("I".equals(formatToken))
230 buf.append(roman(true, number));
232 else
234 // Unknown numbering sequence
235 format(buf, number, "1");
239 static final boolean isAlphanumeric(char c)
241 switch (Character.getType(c))
243 case Character.DECIMAL_DIGIT_NUMBER: // Nd
244 case Character.LETTER_NUMBER: // Nl
245 case Character.OTHER_NUMBER: // No
246 case Character.UPPERCASE_LETTER: // Lu
247 case Character.LOWERCASE_LETTER: // Ll
248 case Character.TITLECASE_LETTER: // Lt
249 case Character.MODIFIER_LETTER: // Lm
250 case Character.OTHER_LETTER: // Lo
251 return true;
252 default:
253 return false;
257 static final String alphabetic(char offset, int number)
259 CPStringBuilder buf = new CPStringBuilder();
260 while (number > 0)
262 int r = number % 26;
263 number = number / 26;
264 buf.insert(0, (char) (offset + r));
266 return buf.toString();
269 static final int[] roman_numbers = {1, 5, 10, 50, 100, 500, 1000};
270 static final char[] roman_chars = {'i', 'v', 'x', 'l', 'c', 'd', 'm'};
272 static final String roman(boolean upper, int number)
274 CPStringBuilder buf = new CPStringBuilder();
275 for (int pos = roman_numbers.length - 1; pos >= 0; pos -= 2)
277 int f = number / roman_numbers[pos];
278 if (f != 0)
280 number = number % (f * roman_numbers[pos]);
282 if (f > 4 && f < 9)
284 buf.append(roman_chars[pos + 1]);
285 f -= 5;
287 if (f == 4)
289 buf.append(roman_chars[pos]);
290 buf.append(roman_chars[pos + 1]);
292 else if (f == 9)
294 buf.append(roman_chars[pos]);
295 buf.append(roman_chars[pos + 2]);
297 else
299 for (; f > 0; f--)
301 buf.append(roman_chars[pos]);
305 return upper ? buf.toString().toUpperCase() : buf.toString();
308 abstract int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
309 throws TransformerException;
311 public boolean references(QName var)
313 if (format.references(var))
315 return true;
317 return super.references(var);
320 public String toString()
322 CPStringBuilder buf = new CPStringBuilder("number");
323 buf.append('[');
324 buf.append("format=");
325 buf.append(format);
326 buf.append(']');
327 return buf.toString();