xalan-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zong...@apache.org
Subject cvs commit: xml-xalan/java/src/org/apache/xalan/xsltc/dom SortSettings.java NodeSortRecord.java NodeSortRecordFactory.java
Date Fri, 27 Feb 2004 01:58:29 GMT
zongaro     2004/02/26 17:58:29

  Modified:    java/src/org/apache/xalan/xsltc/dom NodeSortRecord.java
                        NodeSortRecordFactory.java
  Added:       java/src/org/apache/xalan/xsltc/dom SortSettings.java
  Log:
  Restored old constructor of NodeSortRecordFactory and _collator field of
  NodeSortRecord for binary compatibility.  These changes allow translets
  compiled with an earlier version of XSLTC to continue to run with the new
  version.  They should not be used in newly compiled translets or in new code
  in XSLTC itself.
  
  Also introduced a SortSettings class that is used to bundle sort settings from
  NodeSortRecordFactory into instances of NodeSortRecord.  That reduces some of
  the space overhead in constructing a NodeSortRecord.
  
  Reviewed by Igor Hersht (igorh () ca ! ibm ! com)
  
  Revision  Changes    Path
  1.19      +57 -44    xml-xalan/java/src/org/apache/xalan/xsltc/dom/NodeSortRecord.java
  
  Index: NodeSortRecord.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/dom/NodeSortRecord.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- NodeSortRecord.java	23 Feb 2004 10:29:35 -0000	1.18
  +++ NodeSortRecord.java	27 Feb 2004 01:58:29 -0000	1.19
  @@ -31,10 +31,6 @@
   
   /**
    * Base class for sort records containing application specific sort keys 
  - * @author Jacek Ambroziak
  - * @author Santiago Pericas-Geertsen
  - * @author Morten Jorgensen
  - * @author W. Eliot Kimber (eliot@isogen.com)
    */
   public abstract class NodeSortRecord {
       public static final int COMPARE_STRING     = 0;
  @@ -44,22 +40,31 @@
       public static final int COMPARE_DESCENDING = 1;
   
       /**
  -     * A reference to a locales. 
  +     * A reference to a collator. May be updated by subclass if the stylesheet
  +     * specifies a different language (will be updated iff _locale is updated).
  +     * @deprecated This field continues to exist for binary compatibility.
  +     *             New code should not refer to it.
        */
  -    protected Locale[] _locale;
  +    private static final Collator DEFAULT_COLLATOR = Collator.getInstance();
   
       /**
  -     * A reference to a collators. 
  +     * A reference to the first Collator
  +     * @deprecated This field continues to exist for binary compatibility.
  +     *             New code should not refer to it.
        */
  -    protected Collator[] _collator ;
  -    protected CollatorFactory _collatorFactory;
  +    protected Collator _collator = DEFAULT_COLLATOR;
  +    protected Collator[] _collators;
   
  -    protected int   _levels = 1;
  -    protected int[] _compareType;
  -    protected int[] _sortOrder;
  -    protected String[] _case_order;
  +    /**
  +     * A locale field that might be set by an instance of a subclass.
  +     * @deprecated This field continues to exist for binary compatibility.
  +     *             New code should not refer to it.
  +     */
  +    protected Locale _locale;
  +
  +    protected CollatorFactory _collatorFactory;
   
  -    private AbstractTranslet _translet = null;
  +    protected SortSettings _settings;
   
       private DOM    _dom = null;
       private int    _node;           // The position in the current iterator
  @@ -87,21 +92,16 @@
        * to the default constructor.
        */
       public final void initialize(int node, int last, DOM dom,
  -	 AbstractTranslet translet, int[] order, int[] type, final Locale[] locale, final Collator[]
collator,
  -     final String[] case_order, NodeSortRecordFactory nsrFactory) throws TransletException
  +         SortSettings settings)
  +        throws TransletException
       {
   	_dom = dom;
   	_node = node;
   	_last = last;
  -	_translet = translet;
  -	_scanned = 0;
  +        _settings = settings;
   
  -	_levels = order.length;
  -	_sortOrder = order;
  -	_compareType = type;
  -
  -	_values = new Object[_levels];
  -   _locale = locale;
  +        int levels = settings.getSortOrders().length;
  +	_values = new Object[levels];
     
   	// -- W. Eliot Kimber (eliot@isogen.com)
           String colFactClassname = 
  @@ -112,19 +112,19 @@
                   Object candObj = ObjectFactory.findProviderClass(
                       colFactClassname, ObjectFactory.findClassLoader(), true);
                   _collatorFactory = (CollatorFactory)candObj;
  -            } 
  -	          catch (ClassNotFoundException e) {
  -		          throw new TransletException(e);
  +            } catch (ClassNotFoundException e) {
  +                throw new TransletException(e);
  +            }
  +            Locale[] locales = settings.getLocales();
  +            _collators = new Collator[levels];
  +            for (int i = 0; i < levels; i++){
  +                _collators[i] = _collatorFactory.getCollator(locales[i]);
               }
  -             for(int i = 0; i< _levels; i++){
  -                _collator[i] = _collatorFactory.getCollator(_locale[i]);
  -             }
  -        }else {
  -    	    _collator = collator;
  +            _collator = _collators[0];
  +        } else {
  +    	    _collators = settings.getCollators();
  +            _collator = _collators[0];
           }
  -     
  -     
  -     _case_order = case_order;
       }
   
       /**
  @@ -149,10 +149,17 @@
       private final Comparable stringValue(int level) {
       	// Get value from our array if possible
       	if (_scanned <= level) {
  +            AbstractTranslet translet = _settings.getTranslet();
  +            Locale[] locales = _settings.getLocales();
  +            String[] caseOrder = _settings.getCaseOrders();
  +
       	    // Get value from DOM if accessed for the first time
       	    final String str = extractValueFromDOM(_dom, _node, level,
  -    						   _translet, _last);
  -    	    final Comparable key = StringComparable.getComparator(str, _locale[level], _collator[level],
_case_order[level]);
  +    						   translet, _last);
  +    	    final Comparable key =
  +                StringComparable.getComparator(str, locales[level],
  +                                               _collators[level],
  +                                               caseOrder[level]);
       	    _values[_scanned++] = key;
       	    return(key);
       	}
  @@ -162,9 +169,11 @@
       private final Double numericValue(int level) {
   	// Get value from our vector if possible
   	if (_scanned <= level) {
  +            AbstractTranslet translet = _settings.getTranslet();
  +
   	    // Get value from DOM if accessed for the first time
   	    final String str = extractValueFromDOM(_dom, _node, level,
  -						   _translet, _last);
  +						   translet, _last);
   	    Double num;
   	    try {
   		num = new Double(str);
  @@ -188,9 +197,13 @@
        */
       public int compareTo(NodeSortRecord other) {
   	int cmp, level;
  -	for (level = 0; level < _levels; level++) {
  +        int[] sortOrder = _settings.getSortOrders();
  +        int levels = _settings.getSortOrders().length;
  +        int[] compareTypes = _settings.getTypes();
  +
  +	for (level = 0; level < levels; level++) {
   	    // Compare the two nodes either as numeric or text values
  -	    if (_compareType[level] == COMPARE_NUMERIC) {
  +	    if (compareTypes[level] == COMPARE_NUMERIC) {
   		final Double our = numericValue(level);
   		final Double their = other.numericValue(level);
   		cmp = our.compareTo(their);
  @@ -203,7 +216,7 @@
   	    
   	    // Return inverse compare value if inverse sort order
   	    if (cmp != 0) {
  -		return _sortOrder[level] == COMPARE_DESCENDING ? 0 - cmp : cmp;
  +		return sortOrder[level] == COMPARE_DESCENDING ? 0 - cmp : cmp;
   	    }
   	}
   	// Compare based on document order if all sort keys are equal
  @@ -211,11 +224,11 @@
       }
   
       /**
  -     * Returns the Collator used for text comparisons in this object.
  +     * Returns the array of Collators used for text comparisons in this object.
        * May be overridden by inheriting classes
        */
       public Collator[] getCollator() {
  -	return _collator;
  +	return _collators;
       }
   
       /**
  
  
  
  1.14      +87 -49    xml-xalan/java/src/org/apache/xalan/xsltc/dom/NodeSortRecordFactory.java
  
  Index: NodeSortRecordFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/dom/NodeSortRecordFactory.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- NodeSortRecordFactory.java	23 Feb 2004 10:29:35 -0000	1.13
  +++ NodeSortRecordFactory.java	27 Feb 2004 01:58:29 -0000	1.14
  @@ -27,11 +27,6 @@
   import java.util.Locale;
   import java.text.Collator;
   
  -/**
  - * @author Jacek Ambroziak
  - * @author Santiago Pericas-Geertsen
  - * @author Morten Jorgensen
  - */
   public class NodeSortRecordFactory {
   
       private static int DESCENDING = "descending".length();
  @@ -40,12 +35,29 @@
       private final DOM      _dom;
       private final String   _className;
       private Class _class;
  -    private int   _order[];
  -    private int   _type[];
  -    private Locale _locale[];
  -    private Collator _collator[];
  -    private String _case_order[];
  -    private final AbstractTranslet _translet;
  +    private SortSettings _sortSettings;
  +
  +    /**
  +     *
  +     */
  +    protected Collator _collator;
  +
  +    /**
  +     * Creates a NodeSortRecord producing object. The DOM specifies which tree
  +     * to get the nodes to sort from, the class name specifies what auxillary
  +     * class to use to sort the nodes (this class is generated by the Sort
  +     * class), and the translet parameter is needed for methods called by
  +     * this object.
  +     *
  +     * @deprecated This constructor is no longer used in generated code.  It
  +     *             exists only for backwards compatibility.
  +     */
  +     public NodeSortRecordFactory(DOM dom, String className, Translet translet,
  +                 String order[], String type[])
  +         throws TransletException
  +     {
  +         this(dom, className, translet, order, type, null, null);
  +     }
   
       /**
        * Creates a NodeSortRecord producing object. The DOM specifies which tree
  @@ -55,35 +67,69 @@
        * this object.
        */
        public NodeSortRecordFactory(DOM dom, String className, Translet translet,
  -                 String order[], String type[], String lang[], String case_order[])
  -    throws TransletException {
  -    try {
  -        _dom = dom;
  -        _className = className;
  -        // This should return a Class definition if using TrAX
  -        _class = translet.getAuxiliaryClass(className);
  -        // This code is only run when the native API is used
  -        if (_class == null) {
  -                _class = ObjectFactory.findProviderClass(
  -                    className, ObjectFactory.findClassLoader(), true);
  -            } 
  -        _translet = (AbstractTranslet)translet;
  -
  -        int levels = order.length;
  -        _order = new int[levels];
  -        _type = new int[levels];
  -        for (int i = 0; i < levels; i++) {
  -        if (order[i].length() == DESCENDING)
  -            _order[i] = NodeSortRecord.COMPARE_DESCENDING;
  -        if (type[i].length() == NUMBER)
  -            _type[i] = NodeSortRecord.COMPARE_NUMERIC;
  +                 String order[], String type[], String lang[],
  +                 String caseOrder[])
  +         throws TransletException
  +     {
  +         try {
  +             _dom = dom;
  +             _className = className;
  +             // This should return a Class definition if using TrAX
  +             _class = translet.getAuxiliaryClass(className);
  +             // This code is only run when the native API is used
  +             if (_class == null) {
  +                 _class = ObjectFactory.findProviderClass(
  +                      className, ObjectFactory.findClassLoader(), true);
  +             } 
  +
  +             int levels = order.length;
  +             int[] iOrder = new int[levels];
  +             int[] iType = new int[levels];
  +             for (int i = 0; i < levels; i++) {
  +                  if (order[i].length() == DESCENDING) {
  +                      iOrder[i] = NodeSortRecord.COMPARE_DESCENDING;
  +                  }
  +                  if (type[i].length() == NUMBER) {
  +                      iType[i] = NodeSortRecord.COMPARE_NUMERIC;
  +                  }
  +             }
  +
  +             // Old NodeSortRecordFactory constructor had no lang or case_order
  +             // arguments.  Provide default values in that case for binary
  +             // compatibility.
  +             String[] emptyStringArray = null;
  +             if (lang == null || caseOrder == null) {
  +                 int numSortKeys = order.length;
  +                 emptyStringArray = new String[numSortKeys];
  +
  +                 // Set up array of zero-length strings as default values
  +                 // of lang and case_order
  +                 for (int i = 0; i < numSortKeys; i++) {
  +                     emptyStringArray[i] = "";
  +                 }
  +             }
  +
  +             if (lang == null) {
  +                 lang = emptyStringArray;
  +             }
  +             if (caseOrder == null) {
  +                 caseOrder = emptyStringArray;
  +             }
  +
  +             final int length = lang.length;
  +             Locale[] locales = new Locale[length];
  +             Collator[] collators = new Collator[length];
  +             for (int i = 0; i< length; i++){
  +                 locales[i] = LocaleUtility.langToLocale(lang[i]);
  +                 collators[i] = Collator.getInstance(locales[i]);
  +             }
  +
  +             _sortSettings = new SortSettings((AbstractTranslet) translet,
  +                                              iOrder, iType, locales, collators,
  +                                              caseOrder);
  +        } catch (ClassNotFoundException e) {
  +            throw new TransletException(e);
           }
  -         setLang(lang);
  -        _case_order = case_order;
  -    }
  -    catch (ClassNotFoundException e) {
  -        throw new TransletException(e);
  -    }
       }
       
       
  @@ -102,8 +148,7 @@
   
   	final NodeSortRecord sortRecord =
   	    (NodeSortRecord)_class.newInstance();
  -	sortRecord.initialize(node, last, _dom, _translet, _order, 
  -                         _type, _locale,  _collator, _case_order, this);
  +	sortRecord.initialize(node, last, _dom, _sortSettings);
   	return sortRecord;
       }
   
  @@ -113,12 +158,5 @@
       
      private final void setLang(final String lang[]){
           
  -      final int length = lang.length;
  -      _locale = new Locale[length];
  -      _collator = new Collator[length];
  -      for(int i = 0; i< length; i++){
  -        _locale[i] = LocaleUtility.langToLocale(lang[i]);
  -        _collator[i] = Collator.getInstance(_locale[i] );
  -      }
       }
   }
  
  
  
  1.1                  xml-xalan/java/src/org/apache/xalan/xsltc/dom/SortSettings.java
  
  Index: SortSettings.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  /*
   * $Id: SortSettings.java,v 1.1 2004/02/27 01:58:29 zongaro Exp $
   */
  
  package org.apache.xalan.xsltc.dom;
  
  import java.text.Collator;
  import java.util.Locale;
  
  import org.apache.xalan.xsltc.runtime.AbstractTranslet;
  
  /**
   * Class for carrying settings that are to be used for a particular set
   * of <code>xsl:sort</code> elements.
   */
  final class SortSettings {
      /**
       * A reference to the translet object for the transformation.
       */
      private AbstractTranslet _translet;
  
      /**
       * The sort order (ascending or descending) for each level of
       * <code>xsl:sort</code>
       */
      private int[] _sortOrders;
  
      /**
       * The type of comparison (text or number) for each level of
       * <code>xsl:sort</code>
       */
      private int[] _types;
  
      /**
       * The Locale for each level of <code>xsl:sort</code>, based on any lang
       * attribute or the default Locale.
       */
      private Locale[] _locales;
  
      /**
       * The Collator object in effect for each level of <code>xsl:sort</code>
       */
      private Collator[] _collators;
  
      /**
       * Case ordering for each level of <code>xsl:sort</code>.
       */
      private String[] _caseOrders;
  
      /**
       * Create an instance of <code>SortSettings</code>.
       * @param translet {@link org.apache.xalan.xsltc.runtime.AbstractTranslet}
       *                 object for the transformation
       * @param sortOrders an array specifying the sort order for each sort level
       * @param types an array specifying the type of comparison for each sort
       *              level (text or number)
       * @param locales an array specifying the Locale for each sort level
       * @param collators an array specifying the Collation in effect for each
       *                  sort level
       * @param caseOrders an array specifying whether upper-case, lower-case
       *                   or neither is to take precedence for each sort level.
       *                   The value of each element is equal to one of
       *                   <code>"upper-first", "lower-first", or ""</code>.
       */
      SortSettings(AbstractTranslet translet, int[] sortOrders, int[] types,
                   Locale[] locales, Collator[] collators, String[] caseOrders) {
          _translet = translet;
          _sortOrders = sortOrders;
          _types = types;
          _locales = locales;
          _collators = collators;
          _caseOrders = caseOrders;
      }
  
      /**
       * @return A reference to the translet object for the transformation.
       */
      AbstractTranslet getTranslet() {
          return _translet;
      }
  
      /**
       * @return An array containing the sort order (ascending or descending)
       *         for each level of <code>xsl:sort</code>
       */
      int[] getSortOrders() {
          return _sortOrders;
      }
  
      /**
       * @return An array containing the type of comparison (text or number)
       *         to perform for each level of <code>xsl:sort</code>
       */
      int[] getTypes() {
          return _types;
      }
  
      /**
       * @return An array containing the Locale object in effect for each level
       *         of <code>xsl:sort</code>
       */
      Locale[] getLocales() {
          return _locales;
      }
  
      /**
       * @return An array containing the Collator object in effect for each level
       *         of <code>xsl:sort</code>
       */
      Collator[] getCollators() {
          return _collators;
      }
  
      /**
       * @return An array specifying the case ordering for each level of
       *         <code>xsl:sort</code>.
       */
      String[] getCaseOrders() {
          return _caseOrders;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org


Mime
View raw message