1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: NodeSortRecordFactory.java,v 1.14 2004/02/27 01:58:29 zongaro Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import com.sun.org.apache.xalan.internal.xsltc.DOM;
  21. import com.sun.org.apache.xalan.internal.xsltc.Translet;
  22. import com.sun.org.apache.xalan.internal.xsltc.TransletException;
  23. import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
  24. import com.sun.org.apache.xml.internal.utils.LocaleUtility;
  25. import java.util.Locale;
  26. import java.text.Collator;
  27. public class NodeSortRecordFactory {
  28. private static int DESCENDING = "descending".length();
  29. private static int NUMBER = "number".length();
  30. private final DOM _dom;
  31. private final String _className;
  32. private Class _class;
  33. private SortSettings _sortSettings;
  34. /**
  35. *
  36. */
  37. protected Collator _collator;
  38. /**
  39. * Creates a NodeSortRecord producing object. The DOM specifies which tree
  40. * to get the nodes to sort from, the class name specifies what auxillary
  41. * class to use to sort the nodes (this class is generated by the Sort
  42. * class), and the translet parameter is needed for methods called by
  43. * this object.
  44. *
  45. * @deprecated This constructor is no longer used in generated code. It
  46. * exists only for backwards compatibility.
  47. */
  48. public NodeSortRecordFactory(DOM dom, String className, Translet translet,
  49. String order[], String type[])
  50. throws TransletException
  51. {
  52. this(dom, className, translet, order, type, null, null);
  53. }
  54. /**
  55. * Creates a NodeSortRecord producing object. The DOM specifies which tree
  56. * to get the nodes to sort from, the class name specifies what auxillary
  57. * class to use to sort the nodes (this class is generated by the Sort
  58. * class), and the translet parameter is needed for methods called by
  59. * this object.
  60. */
  61. public NodeSortRecordFactory(DOM dom, String className, Translet translet,
  62. String order[], String type[], String lang[],
  63. String caseOrder[])
  64. throws TransletException
  65. {
  66. try {
  67. _dom = dom;
  68. _className = className;
  69. // This should return a Class definition if using TrAX
  70. _class = translet.getAuxiliaryClass(className);
  71. // This code is only run when the native API is used
  72. if (_class == null) {
  73. _class = ObjectFactory.findProviderClass(
  74. className, ObjectFactory.findClassLoader(), true);
  75. }
  76. int levels = order.length;
  77. int[] iOrder = new int[levels];
  78. int[] iType = new int[levels];
  79. for (int i = 0; i < levels; i++) {
  80. if (order[i].length() == DESCENDING) {
  81. iOrder[i] = NodeSortRecord.COMPARE_DESCENDING;
  82. }
  83. if (type[i].length() == NUMBER) {
  84. iType[i] = NodeSortRecord.COMPARE_NUMERIC;
  85. }
  86. }
  87. // Old NodeSortRecordFactory constructor had no lang or case_order
  88. // arguments. Provide default values in that case for binary
  89. // compatibility.
  90. String[] emptyStringArray = null;
  91. if (lang == null || caseOrder == null) {
  92. int numSortKeys = order.length;
  93. emptyStringArray = new String[numSortKeys];
  94. // Set up array of zero-length strings as default values
  95. // of lang and case_order
  96. for (int i = 0; i < numSortKeys; i++) {
  97. emptyStringArray[i] = "";
  98. }
  99. }
  100. if (lang == null) {
  101. lang = emptyStringArray;
  102. }
  103. if (caseOrder == null) {
  104. caseOrder = emptyStringArray;
  105. }
  106. final int length = lang.length;
  107. Locale[] locales = new Locale[length];
  108. Collator[] collators = new Collator[length];
  109. for (int i = 0; i< length; i++){
  110. locales[i] = LocaleUtility.langToLocale(lang[i]);
  111. collators[i] = Collator.getInstance(locales[i]);
  112. }
  113. _sortSettings = new SortSettings((AbstractTranslet) translet,
  114. iOrder, iType, locales, collators,
  115. caseOrder);
  116. } catch (ClassNotFoundException e) {
  117. throw new TransletException(e);
  118. }
  119. }
  120. /**
  121. * Create an instance of a sub-class of NodeSortRecord. The name of this
  122. * sub-class is passed to us in the constructor.
  123. */
  124. public NodeSortRecord makeNodeSortRecord(int node, int last)
  125. throws ExceptionInInitializerError,
  126. LinkageError,
  127. IllegalAccessException,
  128. InstantiationException,
  129. SecurityException,
  130. TransletException {
  131. final NodeSortRecord sortRecord =
  132. (NodeSortRecord)_class.newInstance();
  133. sortRecord.initialize(node, last, _dom, _sortSettings);
  134. return sortRecord;
  135. }
  136. public String getClassName() {
  137. return _className;
  138. }
  139. private final void setLang(final String lang[]){
  140. }
  141. }