1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * @LastModified: Oct 2017 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.xalan.internal.xsltc.dom; 23 24 import com.sun.org.apache.xalan.internal.utils.ObjectFactory; 25 import com.sun.org.apache.xalan.internal.xsltc.DOM; 26 import com.sun.org.apache.xalan.internal.xsltc.Translet; 27 import com.sun.org.apache.xalan.internal.xsltc.TransletException; 28 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; 29 import com.sun.org.apache.xml.internal.utils.LocaleUtility; 30 import java.lang.reflect.InvocationTargetException; 31 import java.text.Collator; 32 import java.util.Locale; 33 34 public class NodeSortRecordFactory { 35 36 private static int DESCENDING = "descending".length(); 37 private static int NUMBER = "number".length(); 38 39 private final DOM _dom; 40 private final String _className; 41 private Class<?> _class; 42 private SortSettings _sortSettings; 43 44 /** 45 * 46 */ 47 protected Collator _collator; 48 49 /** 50 * Creates a NodeSortRecord producing object. The DOM specifies which tree 51 * to get the nodes to sort from, the class name specifies what auxillary 52 * class to use to sort the nodes (this class is generated by the Sort 53 * class), and the translet parameter is needed for methods called by 54 * this object. 55 * 56 * @deprecated This constructor is no longer used in generated code. It 57 * exists only for backwards compatibility. 58 */ 59 @Deprecated 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 */ 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 */ 145 public NodeSortRecord makeNodeSortRecord(int node, int last) 146 throws ExceptionInInitializerError, 147 LinkageError, 148 IllegalAccessException, 149 InstantiationException, 150 SecurityException, 151 TransletException { 152 153 try { 154 final NodeSortRecord sortRecord; 155 //NodeSortRecord subclasses are generated with a public empty constructor 156 // refer to com.sun.org.apache.xalan.internal.xsltc.compiler.Sort::compileInit 157 sortRecord = (NodeSortRecord)_class.getConstructor().newInstance(); 158 sortRecord.initialize(node, last, _dom, _sortSettings); 159 return sortRecord; 160 } catch (NoSuchMethodException | IllegalArgumentException | 161 InvocationTargetException ex) { 162 throw new InstantiationException(ex.getMessage()); 163 } 164 } 165 166 public String getClassName() { 167 return _className; 168 } 169 170 private final void setLang(final String lang[]){ 171 172 } 173 }