1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: StringComparable.java,v 1.1.1.1 2004/02/26 11:40:18 vk112360 Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.util.Vector;
  21. import java.text.Collator;
  22. import java.text.RuleBasedCollator;
  23. import java.text.CollationElementIterator;
  24. import java.util.Locale;
  25. import java.text.CollationKey;
  26. /**
  27. * International friendly string comparison with case-order
  28. * @author Igor Hersht, igorh@ca.ibm.com
  29. */
  30. public class StringComparable implements Comparable {
  31. public final static int UNKNOWN_CASE = -1;
  32. public final static int UPPER_CASE = 1;
  33. public final static int LOWER_CASE = 2;
  34. private String m_text;
  35. private Locale m_locale;
  36. private RuleBasedCollator m_collator;
  37. private String m_caseOrder;
  38. private int m_mask = 0xFFFFFFFF;
  39. public StringComparable(final String text, final Locale locale, final Collator collator, final String caseOrder){
  40. m_text = text;
  41. m_locale = locale;
  42. m_collator = (RuleBasedCollator)collator;
  43. m_caseOrder = caseOrder;
  44. m_mask = getMask(m_collator.getStrength());
  45. }
  46. public final static Comparable getComparator( final String text, final Locale locale, final Collator collator, final String caseOrder){
  47. if((caseOrder == null) ||(caseOrder.length() == 0)){// no case-order specified
  48. return ((RuleBasedCollator)collator).getCollationKey(text);
  49. }else{
  50. return new StringComparable(text, locale, collator, caseOrder);
  51. }
  52. }
  53. public final String toString(){return m_text;}
  54. public int compareTo(Object o) {
  55. final String pattern = ((StringComparable)o).toString();
  56. if(m_text.equals(pattern)){//Code-point equals
  57. return 0;
  58. }
  59. final int savedStrength = m_collator.getStrength();
  60. int comp = 0;
  61. // Is there difference more significant than case-order?
  62. if(((savedStrength == Collator.PRIMARY) || (savedStrength == Collator.SECONDARY))){
  63. comp = m_collator.compare(m_text, pattern );
  64. }else{// more than SECONDARY
  65. m_collator.setStrength(Collator.SECONDARY);
  66. comp = m_collator.compare(m_text, pattern );
  67. m_collator.setStrength(savedStrength);
  68. }
  69. if(comp != 0){//Difference more significant than case-order
  70. return comp ;
  71. }
  72. // No difference more significant than case-order.
  73. // Find case difference
  74. comp = getCaseDiff(m_text, pattern);
  75. if(comp != 0){
  76. return comp;
  77. }else{// No case differences. Less significant difference could exist
  78. return m_collator.compare(m_text, pattern );
  79. }
  80. }
  81. private final int getCaseDiff (final String text, final String pattern){
  82. final int savedStrength = m_collator.getStrength();
  83. final int savedDecomposition = m_collator.getDecomposition();
  84. m_collator.setStrength(Collator.TERTIARY);// not to ignore case
  85. m_collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION );// corresponds NDF
  86. final int diff[] =getFirstCaseDiff (text, pattern, m_locale);
  87. m_collator.setStrength(savedStrength);// restore
  88. m_collator.setDecomposition(savedDecomposition); //restore
  89. if(diff != null){
  90. if((m_caseOrder).equals("upper-first")){
  91. if(diff[0] == UPPER_CASE){
  92. return -1;
  93. }else{
  94. return 1;
  95. }
  96. }else{// lower-first
  97. if(diff[0] == LOWER_CASE){
  98. return -1;
  99. }else{
  100. return 1;
  101. }
  102. }
  103. }else{// No case differences
  104. return 0;
  105. }
  106. }
  107. private final int[] getFirstCaseDiff(final String text, final String pattern, final Locale locale){
  108. final CollationElementIterator targIter = m_collator.getCollationElementIterator(text);
  109. final CollationElementIterator patIter = m_collator.getCollationElementIterator(pattern);
  110. int startTarg = -1;
  111. int endTarg = -1;
  112. int startPatt = -1;
  113. int endPatt = -1;
  114. final int done = getElement(CollationElementIterator.NULLORDER);
  115. int patternElement = 0, targetElement = 0;
  116. boolean getPattern = true, getTarget = true;
  117. while (true) {
  118. if (getPattern){
  119. startPatt = patIter.getOffset();
  120. patternElement = getElement(patIter.next());
  121. endPatt = patIter.getOffset();
  122. }
  123. if ((getTarget)){
  124. startTarg = targIter.getOffset();
  125. targetElement = getElement(targIter.next());
  126. endTarg = targIter.getOffset();
  127. }
  128. getTarget = getPattern = true;
  129. if ((patternElement == done) ||( targetElement == done)) {
  130. return null;
  131. } else if (targetElement == 0) {
  132. getPattern = false;
  133. } else if (patternElement == 0) {
  134. getTarget = false;
  135. } else if (targetElement != patternElement) {// mismatch
  136. if((startPatt < endPatt) && (startTarg < endTarg)){
  137. final String subText = text.substring(startTarg, endTarg);
  138. final String subPatt = pattern.substring(startPatt, endPatt);
  139. final String subTextUp = subText.toUpperCase(locale);
  140. final String subPattUp = subPatt.toUpperCase(locale);
  141. if(m_collator.compare(subTextUp, subPattUp) != 0){ // not case diffference
  142. continue;
  143. }
  144. int diff[] = {UNKNOWN_CASE, UNKNOWN_CASE};
  145. if(m_collator.compare(subText, subTextUp) == 0){
  146. diff[0] = UPPER_CASE;
  147. }else if(m_collator.compare(subText, subText.toLowerCase(locale)) == 0){
  148. diff[0] = LOWER_CASE;
  149. }
  150. if(m_collator.compare(subPatt, subPattUp) == 0){
  151. diff[1] = UPPER_CASE;
  152. }else if(m_collator.compare(subPatt, subPatt.toLowerCase(locale)) == 0){
  153. diff[1] = LOWER_CASE;
  154. }
  155. if(((diff[0] == UPPER_CASE) && ( diff[1] == LOWER_CASE)) ||
  156. ((diff[0] == LOWER_CASE) && ( diff[1] == UPPER_CASE))){
  157. return diff;
  158. }else{// not case diff
  159. continue;
  160. }
  161. }else{
  162. continue;
  163. }
  164. }
  165. }
  166. }
  167. // Return a mask for the part of the order we're interested in
  168. private static final int getMask(final int strength) {
  169. switch (strength) {
  170. case Collator.PRIMARY:
  171. return 0xFFFF0000;
  172. case Collator.SECONDARY:
  173. return 0xFFFFFF00;
  174. default:
  175. return 0xFFFFFFFF;
  176. }
  177. }
  178. //get collation element with given strength
  179. // from the element with max strength
  180. private final int getElement(int maxStrengthElement){
  181. return (maxStrengthElement & m_mask);
  182. }
  183. }//StringComparable