/[classpath]/classpath/java/text/RuleBasedCollator.java
ViewVC logotype

Diff of /classpath/java/text/RuleBasedCollator.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.14 by mkoch, Wed Oct 15 16:51:19 2003 UTC revision 1.15 by mkoch, Mon Oct 20 13:54:53 2003 UTC
# Line 143  public class RuleBasedCollator extends C Line 143  public class RuleBasedCollator extends C
143      short secondary;      short secondary;
144      short tertiary;      short tertiary;
145    
146      CollationElement(String char_seq, int primary, short secondary, short tertiary)      CollationElement (String char_seq, int primary, short secondary, short tertiary)
147      {      {
148        this.char_seq = char_seq;        this.char_seq = char_seq;
149        this.primary = primary;        this.primary = primary;
# Line 175  public class RuleBasedCollator extends C Line 175  public class RuleBasedCollator extends C
175     * @exception ParseException If the rule string contains syntax errors.     * @exception ParseException If the rule string contains syntax errors.
176     */     */
177    public RuleBasedCollator (String rules) throws ParseException    public RuleBasedCollator (String rules) throws ParseException
178    {    {
179        if (rules.equals (""))
180          throw new ParseException ("empty rule set", 0);
181        
182      this.rules = rules;      this.rules = rules;
183        Vector vec = new Vector();
     if (rules.equals(""))  
       throw new IllegalArgumentException("Empty rule set");  
   
     Vector v = new Vector();  
184      boolean ignore_chars = true;      boolean ignore_chars = true;
185      int primary_seq = 0;      int primary_seq = 0;
186      short secondary_seq = 0;      short secondary_seq = 0;
187      short tertiary_seq = 0;      short tertiary_seq = 0;
188      StringBuffer sb = new StringBuffer("");      StringBuffer argument = new StringBuffer();
189      int len = rules.length();      int len = rules.length();
190            
191      for (int index = 0; index < len; ++index)      for (int index = 0; index < len; ++index)
# Line 195  public class RuleBasedCollator extends C Line 194  public class RuleBasedCollator extends C
194    
195          // Just skip whitespace.          // Just skip whitespace.
196          if (Character.isWhitespace (c))          if (Character.isWhitespace (c))
197            continue;            continue;
198    
199          // Primary difference          // Primary difference.
200          if (c == '<')          if (c == '<')
201            {            {
202              ignore_chars = false;              ignore_chars = false;
203              CollationElement e = new CollationElement(sb.toString(), primary_seq,              CollationElement e = new CollationElement (argument.toString(), primary_seq,
204                                                        secondary_seq,                                                         secondary_seq,
205                                                        tertiary_seq);                                                         tertiary_seq);
206              secondary_seq = 0;              secondary_seq = 0;
207              tertiary_seq = 0;              tertiary_seq = 0;
208              ++primary_seq;              ++primary_seq;
209    
210              v.add(e);              vec.add (e);
211              sb.setLength(0);              argument.setLength (0);
212              continue;              continue;
213            }            }
214    
215          // Secondary difference          // Secondary difference.
216          if (c == ';')          if (c == ';')
217            {            {
218              if (primary_seq == 0)              if (primary_seq == 0)
219                throw new ParseException (rules, index);                throw new ParseException (rules, index);
220    
221              CollationElement e = new CollationElement(sb.toString(), primary_seq,              CollationElement e = new CollationElement (argument.toString(), primary_seq,
222                                                        secondary_seq,                                                         secondary_seq,
223                                                        tertiary_seq);                                                         tertiary_seq);
224              ++secondary_seq;              ++secondary_seq;
225              tertiary_seq = 0;              tertiary_seq = 0;
226    
227              v.add(e);              vec.add (e);
228              sb.setLength(0);              argument.setLength (0);
229              continue;              continue;
230            }            }
231    
232          // Tertiary difference          // Tertiary difference.
233          if (c == ',')          if (c == ',')
234            {            {
235              if (primary_seq == 0)              if (primary_seq == 0)
236                throw new ParseException (rules, index);                throw new ParseException (rules, index);
237    
238              CollationElement e = new CollationElement(sb.toString(), primary_seq,              CollationElement e = new CollationElement (argument.toString(),
239                                                        secondary_seq,                                                         primary_seq,
240                                                        tertiary_seq);                                                         secondary_seq,
241                                                           tertiary_seq);
242              ++tertiary_seq;              ++tertiary_seq;
243    
244              v.add(e);              vec.add (e);
245              sb.setLength(0);              argument.setLength (0);
246              continue;              continue;
247            }            }
248    
249          // Is equal to          // Is equal to.
250          if (c == '=')          if (c == '=')
251            {            {
252              if (primary_seq == 0)              if (primary_seq == 0)
253                throw new ParseException (rules, index);                throw new ParseException (rules, index);
254    
255              CollationElement e = new CollationElement(sb.toString(), primary_seq,              CollationElement e = new CollationElement (argument.toString(),
256                                                        secondary_seq,                                                         primary_seq,
257                                                        tertiary_seq);                                                         secondary_seq,
258              v.add(e);                                                         tertiary_seq);
259              sb.setLength(0);              vec.add (e);
260                argument.setLength (0);
261              continue;              continue;
262            }            }
263    
264          // Sort accents backwards          // Sort accents backwards.
265          if (c == '@')          if (c == '@')
266            {            {
267              throw new ParseException("French style accents not implemented yet", 0);              throw new ParseException ("French style accents not implemented yet", 0);
268            }            }
269    
270          // Reset command          // Reset command.
271          if (c == '&')          if (c == '&')
272            {            {
273              throw new ParseException("Reset not implemented yet", 0);              throw new ParseException ("Reset not implemented yet", 0);
274            }            }
275    
276          // See if we are still reading characters to skip          // See if we are still reading characters to skip.
277          if (ignore_chars == true)          if (ignore_chars == true)
278            {            {
279              CollationElement e = new CollationElement(c + "", 0, (short)0,              CollationElement e = new CollationElement (c + "", 0, (short) 0,
280                                                        (short)0);                                                         (short) 0);
281              v.add(e);              vec.add (e);
282              continue;              continue;
283            }            }
284    
285          sb.append(c);          argument.append (c);
286        }        }
287    
288      if (sb.length() > 0)      if (argument.length() > 0)
289        {        {
290          CollationElement e = new CollationElement (sb.toString(), primary_seq,          CollationElement e = new CollationElement (argument.toString(), primary_seq,
291                                                     secondary_seq, tertiary_seq);                                                     secondary_seq, tertiary_seq);
292          v.add (e);          vec.add (e);
293        }        }
294    
295      ce_table = v.toArray();      ce_table = vec.toArray();
296    }    }
297    
298    /**    /**
# Line 301  public class RuleBasedCollator extends C Line 302  public class RuleBasedCollator extends C
302     */     */
303    public Object clone()    public Object clone()
304    {    {
305      return super.clone();      RuleBasedCollator c = (RuleBasedCollator) super.clone ();
306        return c;
307    }    }
308    
309    /**    /**
# Line 318  public class RuleBasedCollator extends C Line 320  public class RuleBasedCollator extends C
320     */     */
321    public int compare (String source, String target)    public int compare (String source, String target)
322    {    {
323      CollationElementIterator cei1 = getCollationElementIterator (source);      CollationElementIterator cs = getCollationElementIterator (source);
324      CollationElementIterator cei2 = getCollationElementIterator (target);      CollationElementIterator ct = getCollationElementIterator (target);
325    
326      for(;;)      while (true)
327        {        {
328          int ord1 = cei1.next();          int os = cs.next();
329          int ord2 = cei2.next();          int ot = ct.next();
330      
331          // Check for end of string          // Check for end of string
332          if (ord1 == CollationElementIterator.NULLORDER)          if (os == CollationElementIterator.NULLORDER
333            if (ord2 == CollationElementIterator.NULLORDER)              && ot == CollationElementIterator.NULLORDER)
334              return(0);            {
335            else              // Source and target string are equal.
336              return(-1);              return 0;
337          else if (ord2 == CollationElementIterator.NULLORDER)            }
338            return(1);          else if (os == CollationElementIterator.NULLORDER)
339              {
340                // Source string is shorter, so return "less than".
341                return -1;
342              }
343            else if (ot == CollationElementIterator.NULLORDER)
344              {
345                // Target string is shorter, so return "greater than".
346                return 1;
347              }
348    
349          // We know chars are totally equal, so skip          // We know chars are totally equal, so skip
350          if (ord1 == ord2)          if (os == ot)
351            continue;            continue;
352    
353          // Check for primary strength differences          // Check for primary strength differences
354          int prim1 = cei1.primaryOrder(ord1);          int prim1 = cs.primaryOrder (os);
355          int prim2 = cei2.primaryOrder(ord2);          int prim2 = ct.primaryOrder (ot);
356    
357          if (prim1 < prim2)          if (prim1 < prim2)
358            return(-1);            return -1;
359          else if (prim1 > prim2)          else if (prim1 > prim2)
360            return(1);            return 1;
361          else if (getStrength() == PRIMARY)          else if (getStrength() == PRIMARY)
362            continue;            continue;
363    
364          // Check for secondary strength differences          // Check for secondary strength differences
365          int sec1 = cei1.secondaryOrder(ord1);          int sec1 = cs.secondaryOrder (os);
366          int sec2 = cei2.secondaryOrder(ord2);          int sec2 = ct.secondaryOrder (ot);
367    
368          if (sec1 < sec2)          if (sec1 < sec2)
369            return(-1);            return -1;
370          else if (sec1 > sec2)          else if (sec1 > sec2)
371            return(1);            return 1;
372          else if (getStrength() == SECONDARY)          else if (getStrength() == SECONDARY)
373            continue;            continue;
374    
375          // Check for tertiary differences          // Check for tertiary differences
376          int tert1 = cei1.tertiaryOrder(ord1);          int tert1 = cs.tertiaryOrder (os);
377          int tert2 = cei2.tertiaryOrder(ord1);          int tert2 = ct.tertiaryOrder (ot);
378    
379          if (tert1 < tert2)          if (tert1 < tert2)
380            return(-1);            return -1;
381          else if (tert1 > tert2)          else if (tert1 > tert2)
382            return(1);            return 1;
383        }        }
384    }    }
385    
# Line 384  public class RuleBasedCollator extends C Line 395  public class RuleBasedCollator extends C
395    public boolean equals (Object obj)    public boolean equals (Object obj)
396    {    {
397      if (obj == this)      if (obj == this)
398        return(true);        return true;
399      else  
400        return(false);      return false;
401    }    }
402    
403    /**    /**
# Line 401  public class RuleBasedCollator extends C Line 412  public class RuleBasedCollator extends C
412    public CollationElementIterator getCollationElementIterator (String source)    public CollationElementIterator getCollationElementIterator (String source)
413    {    {
414      return new CollationElementIterator (source, this);      return new CollationElementIterator (source, this);
415    }      }
416    
417    /**    /**
418     * This method returns an instance of <code>CollationElementIterator</code>     * This method returns an instance of <code>CollationElementIterator</code>
# Line 414  public class RuleBasedCollator extends C Line 425  public class RuleBasedCollator extends C
425     */     */
426    public CollationElementIterator getCollationElementIterator (CharacterIterator source)    public CollationElementIterator getCollationElementIterator (CharacterIterator source)
427    {    {
428      StringBuffer sb = new StringBuffer("");      StringBuffer sb = new StringBuffer();
429    
430      // Right now we assume that we will read from the beginning of the string.      // Right now we assume that we will read from the beginning of the string.
431      for (char c = source.first(); c != CharacterIterator.DONE; c = source.next())      for (char c = source.first(); c != CharacterIterator.DONE; c = source.next())
# Line 439  public class RuleBasedCollator extends C Line 450  public class RuleBasedCollator extends C
450    public CollationKey getCollationKey (String source)    public CollationKey getCollationKey (String source)
451    {    {
452      CollationElementIterator cei = getCollationElementIterator (source);      CollationElementIterator cei = getCollationElementIterator (source);
453      Vector vect = new Vector(25);      Vector vec = new Vector (25);
454    
455      int ord = cei.next();      int ord = cei.next();
456      cei.reset(); //set to start of string      cei.reset(); //set to start of string
# Line 449  public class RuleBasedCollator extends C Line 460  public class RuleBasedCollator extends C
460          switch (getStrength())          switch (getStrength())
461            {            {
462              case PRIMARY:              case PRIMARY:
463                 ord = cei.primaryOrder(ord);                 ord = cei.primaryOrder (ord);
464                 break;                 break;
465    
466              case SECONDARY:              case SECONDARY:
467                 ord = cei.secondaryOrder(ord);                 ord = cei.secondaryOrder (ord);
468    
469              default:              default:
470                 break;                 break;
471            }            }
472    
473          vect.add(new Integer(ord));          vec.add (new Integer (ord));
474          ord = cei.next(); //increment to next key          ord = cei.next(); //increment to next key
475        }        }
476    
477      Object[] objarr = vect.toArray();      Object[] objarr = vec.toArray();
478      byte[] key = new byte[objarr.length * 4];      byte[] key = new byte [objarr.length * 4];
479    
480      for (int i = 0; i < objarr.length; i++)      for (int i = 0; i < objarr.length; i++)
481        {        {
482          int j = ((Integer)objarr[i]).intValue();          int j = ((Integer) objarr [i]).intValue();
483          key [i * 4] = (byte)((j & 0xFF000000) >> 24);          key [i * 4] = (byte) ((j & 0xFF000000) >> 24);
484          key [i * 4 + 1] = (byte)((j & 0x00FF0000) >> 16);          key [i * 4 + 1] = (byte) ((j & 0x00FF0000) >> 16);
485          key [i * 4 + 2] = (byte)((j & 0x0000FF00) >> 8);          key [i * 4 + 2] = (byte) ((j & 0x0000FF00) >> 8);
486          key [i * 4 + 3] = (byte)(j & 0x000000FF);          key [i * 4 + 3] = (byte) (j & 0x000000FF);
487        }        }
488    
489      return new CollationKey (this, source, key);      return new CollationKey (this, source, key);

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26