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