19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: NodeSortKey.java 468645 2006-10-28 06:57:24Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xalan.transformer;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.text.Collator;
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.util.Locale;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xalan.res.XSLTErrorResources;
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPath;
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Data structure for use by the NodeSorter class.
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage internal
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonclass NodeSortKey
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Select pattern for this sort key          */
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  XPath m_selectPat;
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Flag indicating whether to treat thee result as a number     */
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  boolean m_treatAsNumbers;
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Flag indicating whether to sort in descending order      */
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  boolean m_descending;
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Flag indicating by case          */
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  boolean m_caseOrderUpper;
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Collator instance          */
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  Collator m_col;
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Locale we're in          */
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  Locale m_locale;
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Prefix resolver to use          */
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  org.apache.xml.utils.PrefixResolver m_namespaceContext;
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Transformer instance          */
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  TransformerImpl m_processor;  // needed for error reporting.
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Constructor NodeSortKey
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param transformer non null transformer instance
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param selectPat Select pattern for this key
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param treatAsNumbers Flag indicating whether the result will be a number
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param descending Flag indicating whether to sort in descending order
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param langValue Lang value to use to get locale
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param caseOrderUpper Flag indicating whether case is relevant
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param namespaceContext Prefix resolver
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws javax.xml.transform.TransformerException
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  NodeSortKey(
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          TransformerImpl transformer, XPath selectPat, boolean treatAsNumbers,
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          boolean descending, String langValue, boolean caseOrderUpper,
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          org.apache.xml.utils.PrefixResolver namespaceContext)
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            throws javax.xml.transform.TransformerException
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_processor = transformer;
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_namespaceContext = namespaceContext;
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_selectPat = selectPat;
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_treatAsNumbers = treatAsNumbers;
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_descending = descending;
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_caseOrderUpper = caseOrderUpper;
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (null != langValue && m_treatAsNumbers == false)
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // See http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2851
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // The constructor of Locale is defined as
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      //   public Locale(String language, String country)
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // with
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      //   language - lowercase two-letter ISO-639 code
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      //   country - uppercase two-letter ISO-3166 code
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // a) language must be provided as a lower-case ISO-code
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      //    instead of an upper-case code
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // b) country must be provided as an ISO-code
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      //    instead of a full localized country name (e.g. "France")
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_locale = new Locale(langValue.toLowerCase(),
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                  Locale.getDefault().getCountry());
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // (old, before bug report 2851).
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      //  m_locale = new Locale(langValue.toUpperCase(),
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      //                        Locale.getDefault().getDisplayCountry());
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      if (null == m_locale)
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // m_processor.warn("Could not find locale for <sort xml:lang="+langValue);
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        m_locale = Locale.getDefault();
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    else
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_locale = Locale.getDefault();
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_col = Collator.getInstance(m_locale);
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (null == m_col)
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_processor.getMsgMgr().warn(null, XSLTErrorResources.WG_CANNOT_FIND_COLLATOR,
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                                   new Object[]{ langValue });  //"Could not find Collator for <sort xml:lang="+langValue);
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_col = Collator.getInstance();
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
130