001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 /* 019 * $Id: NodeSortRecordFactory.java 468651 2006-10-28 07:04:25Z minchau $ 020 */ 021 022 package org.apache.xalan.xsltc.dom; 023 024 import org.apache.xalan.xsltc.DOM; 025 import org.apache.xalan.xsltc.Translet; 026 import org.apache.xalan.xsltc.TransletException; 027 import org.apache.xalan.xsltc.runtime.AbstractTranslet; 028 import org.apache.xml.utils.LocaleUtility; 029 import java.util.Locale; 030 import java.text.Collator; 031 032 public class NodeSortRecordFactory { 033 034 private static int DESCENDING = "descending".length(); 035 private static int NUMBER = "number".length(); 036 037 private final DOM _dom; 038 private final String _className; 039 private Class _class; 040 private SortSettings _sortSettings; 041 042 /** 043 * 044 */ 045 protected Collator _collator; 046 047 /** 048 * Creates a NodeSortRecord producing object. The DOM specifies which tree 049 * to get the nodes to sort from, the class name specifies what auxillary 050 * class to use to sort the nodes (this class is generated by the Sort 051 * class), and the translet parameter is needed for methods called by 052 * this object. 053 * 054 * @deprecated This constructor is no longer used in generated code. It 055 * exists only for backwards compatibility. 056 */ 057 public NodeSortRecordFactory(DOM dom, String className, Translet translet, 058 String order[], String type[]) 059 throws TransletException 060 { 061 this(dom, className, translet, order, type, null, null); 062 } 063 064 /** 065 * Creates a NodeSortRecord producing object. The DOM specifies which tree 066 * to get the nodes to sort from, the class name specifies what auxillary 067 * class to use to sort the nodes (this class is generated by the Sort 068 * class), and the translet parameter is needed for methods called by 069 * this object. 070 */ 071 public NodeSortRecordFactory(DOM dom, String className, Translet translet, 072 String order[], String type[], String lang[], 073 String caseOrder[]) 074 throws TransletException 075 { 076 try { 077 _dom = dom; 078 _className = className; 079 // This should return a Class definition if using TrAX 080 _class = translet.getAuxiliaryClass(className); 081 // This code is only run when the native API is used 082 if (_class == null) { 083 _class = ObjectFactory.findProviderClass( 084 className, ObjectFactory.findClassLoader(), true); 085 } 086 087 int levels = order.length; 088 int[] iOrder = new int[levels]; 089 int[] iType = new int[levels]; 090 for (int i = 0; i < levels; i++) { 091 if (order[i].length() == DESCENDING) { 092 iOrder[i] = NodeSortRecord.COMPARE_DESCENDING; 093 } 094 if (type[i].length() == NUMBER) { 095 iType[i] = NodeSortRecord.COMPARE_NUMERIC; 096 } 097 } 098 099 // Old NodeSortRecordFactory constructor had no lang or case_order 100 // arguments. Provide default values in that case for binary 101 // compatibility. 102 String[] emptyStringArray = null; 103 if (lang == null || caseOrder == null) { 104 int numSortKeys = order.length; 105 emptyStringArray = new String[numSortKeys]; 106 107 // Set up array of zero-length strings as default values 108 // of lang and case_order 109 for (int i = 0; i < numSortKeys; i++) { 110 emptyStringArray[i] = ""; 111 } 112 } 113 114 if (lang == null) { 115 lang = emptyStringArray; 116 } 117 if (caseOrder == null) { 118 caseOrder = emptyStringArray; 119 } 120 121 final int length = lang.length; 122 Locale[] locales = new Locale[length]; 123 Collator[] collators = new Collator[length]; 124 for (int i = 0; i< length; i++){ 125 locales[i] = LocaleUtility.langToLocale(lang[i]); 126 collators[i] = Collator.getInstance(locales[i]); 127 } 128 129 _sortSettings = new SortSettings((AbstractTranslet) translet, 130 iOrder, iType, locales, collators, 131 caseOrder); 132 } catch (ClassNotFoundException e) { 133 throw new TransletException(e); 134 } 135 } 136 137 138 139 /** 140 * Create an instance of a sub-class of NodeSortRecord. The name of this 141 * sub-class is passed to us in the constructor. 142 */ 143 public NodeSortRecord makeNodeSortRecord(int node, int last) 144 throws ExceptionInInitializerError, 145 LinkageError, 146 IllegalAccessException, 147 InstantiationException, 148 SecurityException, 149 TransletException { 150 151 final NodeSortRecord sortRecord = 152 (NodeSortRecord)_class.newInstance(); 153 sortRecord.initialize(node, last, _dom, _sortSettings); 154 return sortRecord; 155 } 156 157 public String getClassName() { 158 return _className; 159 } 160 161 private final void setLang(final String lang[]){ 162 163 } 164 }