AbstractNumberNode.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* AbstractNumberNode.java --
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.transform;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.ArrayList;
  34. import java.util.Collections;
  35. import java.util.List;
  36. import javax.xml.namespace.QName;
  37. import javax.xml.transform.TransformerException;
  38. import org.w3c.dom.Document;
  39. import org.w3c.dom.DocumentFragment;
  40. import org.w3c.dom.Node;
  41. import org.w3c.dom.Text;
  42. import gnu.xml.xpath.Expr;
  43. /**
  44. * A template node representing the XSL <code>number</code> instruction.
  45. *
  46. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  47. */
  48. abstract class AbstractNumberNode
  49. extends TemplateNode
  50. {
  51. static final int ALPHABETIC = 0;
  52. static final int TRADITIONAL = 1;
  53. final TemplateNode format;
  54. final String lang;
  55. final int letterValue;
  56. final String groupingSeparator;
  57. final int groupingSize;
  58. AbstractNumberNode(TemplateNode format, String lang,
  59. int letterValue, String groupingSeparator,
  60. int groupingSize)
  61. {
  62. this.format = format;
  63. this.lang = lang;
  64. this.letterValue = letterValue;
  65. this.groupingSeparator = groupingSeparator;
  66. this.groupingSize = groupingSize;
  67. }
  68. void doApply(Stylesheet stylesheet, QName mode,
  69. Node context, int pos, int len,
  70. Node parent, Node nextSibling)
  71. throws TransformerException
  72. {
  73. Document doc = (parent instanceof Document) ? (Document) parent :
  74. parent.getOwnerDocument();
  75. DocumentFragment fragment = doc.createDocumentFragment();
  76. format.apply(stylesheet, mode, context, pos, len, fragment, null);
  77. String f = Expr._string(context, Collections.singleton(fragment));
  78. String value = format(f, compute(stylesheet, context, pos, len));
  79. Text text = doc.createTextNode(value);
  80. if (nextSibling != null)
  81. {
  82. parent.insertBefore(text, nextSibling);
  83. }
  84. else
  85. {
  86. parent.appendChild(text);
  87. }
  88. // xsl:number doesn't process children
  89. if (next != null)
  90. {
  91. next.apply(stylesheet, mode,
  92. context, pos, len,
  93. parent, nextSibling);
  94. }
  95. }
  96. String format(String format, int[] number)
  97. {
  98. if (number.length == 0)
  99. {
  100. return "";
  101. }
  102. int start = 0, end = 0, len = format.length(); // region of format
  103. // Tokenize
  104. List tokens = new ArrayList((number.length * 2) + 1);
  105. List types = new ArrayList(tokens.size());
  106. while (end < len)
  107. {
  108. while (end < len && !isAlphanumeric(format.charAt(end)))
  109. {
  110. end++;
  111. }
  112. if (end > start)
  113. {
  114. tokens.add(format.substring(start, end));
  115. types.add(Boolean.FALSE);
  116. }
  117. start = end;
  118. while (end < len && isAlphanumeric(format.charAt(end)))
  119. {
  120. end++;
  121. }
  122. if (end > start)
  123. {
  124. tokens.add(format.substring(start, end));
  125. types.add(Boolean.TRUE);
  126. }
  127. start = end;
  128. }
  129. // Process tokens
  130. CPStringBuilder buf = new CPStringBuilder();
  131. len = tokens.size();
  132. int pos = 0;
  133. for (int i = 0; i < len; i++)
  134. {
  135. String token = (i < 0) ? "." : (String) tokens.get(i);
  136. boolean alpha = (i < 0) ? true :
  137. ((Boolean) types.get(i)).booleanValue();
  138. if (!alpha)
  139. {
  140. buf.append(token);
  141. }
  142. else
  143. {
  144. if (pos < number.length)
  145. {
  146. format(buf, number[pos++], token);
  147. if (((i + 1 == len) || (i + 2 == len)) &&
  148. (pos < number.length))
  149. {
  150. // More numbers than tokens, reuse last token
  151. i -= 2;
  152. }
  153. }
  154. if (pos == number.length && i < (len - 2))
  155. {
  156. // No more numbers. Skip to the end...
  157. i = len - 2;
  158. if (((Boolean) types.get(i + 1)).booleanValue())
  159. {
  160. // number formatting token, ignore
  161. i++;
  162. }
  163. }
  164. }
  165. }
  166. //System.err.println("format: '"+format+"' "+asList(number)+" = '"+buf.toString()+"'");
  167. return buf.toString();
  168. }
  169. /*List asList(int[] number)
  170. {
  171. List l = new ArrayList();
  172. for (int i = 0; i < number.length; i++)
  173. l.add(new Integer(number[i]));
  174. return l;
  175. }*/
  176. void format(CPStringBuilder buf, int number, String formatToken)
  177. {
  178. int len = formatToken.length();
  179. char c = formatToken.charAt(len - 1);
  180. if (Character.digit(c, 10) == 1)
  181. {
  182. // Check preceding characters
  183. for (int i = len - 2; i >= 0; i--)
  184. {
  185. if (formatToken.charAt(i) != (c - 1))
  186. {
  187. format(buf, number, "1");
  188. return;
  189. }
  190. }
  191. // Decimal representation
  192. String val = Integer.toString(number);
  193. for (int d = len - val.length(); d > 0; d--)
  194. {
  195. buf.append('0');
  196. }
  197. buf.append(val);
  198. }
  199. else if ("A".equals(formatToken))
  200. {
  201. buf.append(alphabetic('@', number));
  202. }
  203. else if ("a".equals(formatToken))
  204. {
  205. buf.append(alphabetic('`', number));
  206. }
  207. else if ("i".equals(formatToken))
  208. {
  209. buf.append(roman(false, number));
  210. }
  211. else if ("I".equals(formatToken))
  212. {
  213. buf.append(roman(true, number));
  214. }
  215. else
  216. {
  217. // Unknown numbering sequence
  218. format(buf, number, "1");
  219. }
  220. }
  221. static final boolean isAlphanumeric(char c)
  222. {
  223. switch (Character.getType(c))
  224. {
  225. case Character.DECIMAL_DIGIT_NUMBER: // Nd
  226. case Character.LETTER_NUMBER: // Nl
  227. case Character.OTHER_NUMBER: // No
  228. case Character.UPPERCASE_LETTER: // Lu
  229. case Character.LOWERCASE_LETTER: // Ll
  230. case Character.TITLECASE_LETTER: // Lt
  231. case Character.MODIFIER_LETTER: // Lm
  232. case Character.OTHER_LETTER: // Lo
  233. return true;
  234. default:
  235. return false;
  236. }
  237. }
  238. static final String alphabetic(char offset, int number)
  239. {
  240. CPStringBuilder buf = new CPStringBuilder();
  241. while (number > 0)
  242. {
  243. int r = number % 26;
  244. number = number / 26;
  245. buf.insert(0, (char) (offset + r));
  246. }
  247. return buf.toString();
  248. }
  249. static final int[] roman_numbers = {1, 5, 10, 50, 100, 500, 1000};
  250. static final char[] roman_chars = {'i', 'v', 'x', 'l', 'c', 'd', 'm'};
  251. static final String roman(boolean upper, int number)
  252. {
  253. CPStringBuilder buf = new CPStringBuilder();
  254. for (int pos = roman_numbers.length - 1; pos >= 0; pos -= 2)
  255. {
  256. int f = number / roman_numbers[pos];
  257. if (f != 0)
  258. {
  259. number = number % (f * roman_numbers[pos]);
  260. }
  261. if (f > 4 && f < 9)
  262. {
  263. buf.append(roman_chars[pos + 1]);
  264. f -= 5;
  265. }
  266. if (f == 4)
  267. {
  268. buf.append(roman_chars[pos]);
  269. buf.append(roman_chars[pos + 1]);
  270. }
  271. else if (f == 9)
  272. {
  273. buf.append(roman_chars[pos]);
  274. buf.append(roman_chars[pos + 2]);
  275. }
  276. else
  277. {
  278. for (; f > 0; f--)
  279. {
  280. buf.append(roman_chars[pos]);
  281. }
  282. }
  283. }
  284. return upper ? buf.toString().toUpperCase() : buf.toString();
  285. }
  286. abstract int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
  287. throws TransformerException;
  288. public boolean references(QName var)
  289. {
  290. if (format.references(var))
  291. {
  292. return true;
  293. }
  294. return super.references(var);
  295. }
  296. public String toString()
  297. {
  298. CPStringBuilder buf = new CPStringBuilder("number");
  299. buf.append('[');
  300. buf.append("format=");
  301. buf.append(format);
  302. buf.append(']');
  303. return buf.toString();
  304. }
  305. }