1 16 19 20 package org.apache.xml.utils; 21 22 import java.util.Vector ; 23 import java.text.Collator ; 24 import java.text.RuleBasedCollator ; 25 import java.text.CollationElementIterator ; 26 import java.util.Locale ; 27 import java.text.CollationKey ; 28 29 30 34 public class StringComparable implements Comparable { 35 36 public final static int UNKNOWN_CASE = -1; 37 public final static int UPPER_CASE = 1; 38 public final static int LOWER_CASE = 2; 39 40 private String m_text; 41 private Locale m_locale; 42 private RuleBasedCollator m_collator; 43 private String m_caseOrder; 44 private int m_mask = 0xFFFFFFFF; 45 46 public StringComparable(final String text, final Locale locale, final Collator collator, final String caseOrder){ 47 m_text = text; 48 m_locale = locale; 49 m_collator = (RuleBasedCollator )collator; 50 m_caseOrder = caseOrder; 51 m_mask = getMask(m_collator.getStrength()); 52 } 53 54 public final static Comparable getComparator( final String text, final Locale locale, final Collator collator, final String caseOrder){ 55 if((caseOrder == null) ||(caseOrder.length() == 0)){ return ((RuleBasedCollator )collator).getCollationKey(text); 57 }else{ 58 return new StringComparable(text, locale, collator, caseOrder); 59 } 60 } 61 62 public final String toString(){return m_text;} 63 64 public int compareTo(Object o) { 65 final String pattern = ((StringComparable)o).toString(); 66 if(m_text.equals(pattern)){ return 0; 68 } 69 final int savedStrength = m_collator.getStrength(); 70 int comp = 0; 71 if(((savedStrength == Collator.PRIMARY) || (savedStrength == Collator.SECONDARY))){ 73 comp = m_collator.compare(m_text, pattern ); 74 }else{ m_collator.setStrength(Collator.SECONDARY); 76 comp = m_collator.compare(m_text, pattern ); 77 m_collator.setStrength(savedStrength); 78 } 79 if(comp != 0){ return comp ; 81 } 82 83 comp = getCaseDiff(m_text, pattern); 86 if(comp != 0){ 87 return comp; 88 }else{ return m_collator.compare(m_text, pattern ); 90 } 91 } 92 93 94 private final int getCaseDiff (final String text, final String pattern){ 95 final int savedStrength = m_collator.getStrength(); 96 final int savedDecomposition = m_collator.getDecomposition(); 97 m_collator.setStrength(Collator.TERTIARY); m_collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION ); 100 final int diff[] =getFirstCaseDiff (text, pattern, m_locale); 101 m_collator.setStrength(savedStrength); m_collator.setDecomposition(savedDecomposition); if(diff != null){ 104 if((m_caseOrder).equals("upper-first")){ 105 if(diff[0] == UPPER_CASE){ 106 return -1; 107 }else{ 108 return 1; 109 } 110 }else{ if(diff[0] == LOWER_CASE){ 112 return -1; 113 }else{ 114 return 1; 115 } 116 } 117 }else{ return 0; 119 } 120 121 } 122 123 124 125 private final int[] getFirstCaseDiff(final String text, final String pattern, final Locale locale){ 126 127 final CollationElementIterator targIter = m_collator.getCollationElementIterator(text); 128 final CollationElementIterator patIter = m_collator.getCollationElementIterator(pattern); 129 int startTarg = -1; 130 int endTarg = -1; 131 int startPatt = -1; 132 int endPatt = -1; 133 final int done = getElement(CollationElementIterator.NULLORDER); 134 int patternElement = 0, targetElement = 0; 135 boolean getPattern = true, getTarget = true; 136 137 while (true) { 138 if (getPattern){ 139 startPatt = patIter.getOffset(); 140 patternElement = getElement(patIter.next()); 141 endPatt = patIter.getOffset(); 142 } 143 if ((getTarget)){ 144 startTarg = targIter.getOffset(); 145 targetElement = getElement(targIter.next()); 146 endTarg = targIter.getOffset(); 147 } 148 getTarget = getPattern = true; 149 if ((patternElement == done) ||( targetElement == done)) { 150 return null; 151 } else if (targetElement == 0) { 152 getPattern = false; 153 } else if (patternElement == 0) { 154 getTarget = false; 155 } else if (targetElement != patternElement) { if((startPatt < endPatt) && (startTarg < endTarg)){ 157 final String subText = text.substring(startTarg, endTarg); 158 final String subPatt = pattern.substring(startPatt, endPatt); 159 final String subTextUp = subText.toUpperCase(locale); 160 final String subPattUp = subPatt.toUpperCase(locale); 161 if(m_collator.compare(subTextUp, subPattUp) != 0){ continue; 163 } 164 165 int diff[] = {UNKNOWN_CASE, UNKNOWN_CASE}; 166 if(m_collator.compare(subText, subTextUp) == 0){ 167 diff[0] = UPPER_CASE; 168 }else if(m_collator.compare(subText, subText.toLowerCase(locale)) == 0){ 169 diff[0] = LOWER_CASE; 170 } 171 if(m_collator.compare(subPatt, subPattUp) == 0){ 172 diff[1] = UPPER_CASE; 173 }else if(m_collator.compare(subPatt, subPatt.toLowerCase(locale)) == 0){ 174 diff[1] = LOWER_CASE; 175 } 176 177 if(((diff[0] == UPPER_CASE) && ( diff[1] == LOWER_CASE)) || 178 ((diff[0] == LOWER_CASE) && ( diff[1] == UPPER_CASE))){ 179 return diff; 180 }else{ continue; 182 } 183 }else{ 184 continue; 185 } 186 187 } 188 } 189 190 } 191 192 193 private static final int getMask(final int strength) { 195 switch (strength) { 196 case Collator.PRIMARY: 197 return 0xFFFF0000; 198 case Collator.SECONDARY: 199 return 0xFFFFFF00; 200 default: 201 return 0xFFFFFFFF; 202 } 203 } 204 private final int getElement(int maxStrengthElement){ 207 208 return (maxStrengthElement & m_mask); 209 } 210 211 } 213 214 | Popular Tags |