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: NodeSortKey.java 468645 2006-10-28 06:57:24Z minchau $
020     */
021    package org.apache.xalan.transformer;
022    
023    import java.text.Collator;
024    import java.util.Locale;
025    
026    import org.apache.xalan.res.XSLTErrorResources;
027    import org.apache.xpath.XPath;
028    
029    /**
030     * Data structure for use by the NodeSorter class.
031     * @xsl.usage internal
032     */
033    class NodeSortKey
034    {
035    
036      /** Select pattern for this sort key          */
037      XPath m_selectPat;
038    
039      /** Flag indicating whether to treat thee result as a number     */
040      boolean m_treatAsNumbers;
041    
042      /** Flag indicating whether to sort in descending order      */
043      boolean m_descending;
044    
045      /** Flag indicating by case          */
046      boolean m_caseOrderUpper;
047    
048      /** Collator instance          */
049      Collator m_col;
050    
051      /** Locale we're in          */
052      Locale m_locale;
053    
054      /** Prefix resolver to use          */
055      org.apache.xml.utils.PrefixResolver m_namespaceContext;
056    
057      /** Transformer instance          */
058      TransformerImpl m_processor;  // needed for error reporting.
059    
060      /**
061       * Constructor NodeSortKey
062       *
063       *
064       * @param transformer non null transformer instance
065       * @param selectPat Select pattern for this key 
066       * @param treatAsNumbers Flag indicating whether the result will be a number
067       * @param descending Flag indicating whether to sort in descending order
068       * @param langValue Lang value to use to get locale
069       * @param caseOrderUpper Flag indicating whether case is relevant
070       * @param namespaceContext Prefix resolver
071       *
072       * @throws javax.xml.transform.TransformerException
073       */
074      NodeSortKey(
075              TransformerImpl transformer, XPath selectPat, boolean treatAsNumbers, 
076              boolean descending, String langValue, boolean caseOrderUpper, 
077              org.apache.xml.utils.PrefixResolver namespaceContext)
078                throws javax.xml.transform.TransformerException
079      {
080    
081        m_processor = transformer;
082        m_namespaceContext = namespaceContext;
083        m_selectPat = selectPat;
084        m_treatAsNumbers = treatAsNumbers;
085        m_descending = descending;
086        m_caseOrderUpper = caseOrderUpper;
087    
088        if (null != langValue && m_treatAsNumbers == false)
089        {
090          // See http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2851
091          // The constructor of Locale is defined as 
092          //   public Locale(String language, String country)
093          // with
094          //   language - lowercase two-letter ISO-639 code
095          //   country - uppercase two-letter ISO-3166 code
096          // a) language must be provided as a lower-case ISO-code 
097          //    instead of an upper-case code
098          // b) country must be provided as an ISO-code 
099          //    instead of a full localized country name (e.g. "France")
100          m_locale = new Locale(langValue.toLowerCase(), 
101                      Locale.getDefault().getCountry());
102                      
103          // (old, before bug report 2851).
104          //  m_locale = new Locale(langValue.toUpperCase(),
105          //                        Locale.getDefault().getDisplayCountry());                    
106    
107          if (null == m_locale)
108          {
109    
110            // m_processor.warn("Could not find locale for <sort xml:lang="+langValue);
111            m_locale = Locale.getDefault();
112          }
113        }
114        else
115        {
116          m_locale = Locale.getDefault();
117        }
118    
119        m_col = Collator.getInstance(m_locale);
120    
121        if (null == m_col)
122        {
123          m_processor.getMsgMgr().warn(null, XSLTErrorResources.WG_CANNOT_FIND_COLLATOR,
124                                       new Object[]{ langValue });  //"Could not find Collator for <sort xml:lang="+langValue);
125    
126          m_col = Collator.getInstance();
127        }
128      }
129    }