Package java.text

Examples of java.text.CollationElementIterator


      return true;
  }

  // on to the real code
        int strengthResult = Collator.IDENTICAL;
        CollationElementIterator sourceCursor, targetCursor;
  boolean isFrenchSec = false;

  // set up the RuleBasedCollator and extract
  // various attributes
  rbc.setDecomposition(Collator.FULL_DECOMPOSITION);
  String rules = rbc.getRules();
  if (rules.startsWith("@")) {
      // I suspect we are cheating here when multiple rules are merged
      // One would think you could get this attribute but you can't
      isFrenchSec = true;
  }

  // Get the CollationElementIterators for the source and target
  sourceCursor = rbc.getCollationElementIterator(source);
  targetCursor = rbc.getCollationElementIterator(target);
       
  // Various locale specific matching parameters
        int sOrder = 0, tOrder = 0;
  int pSOrder, pTOrder;
  short secSOrder, secTOrder;
  short terSOrder, terTOrder;
        boolean gets = true, gett = true;
  int toffset = 0;
        boolean initialCheckSecTer, checkSecTer, checkTertiary;

   startSearchForFirstMatch:
        while(true) {
      debug("while(true) toffset=" + toffset);
      // Set the starting point for this comparison
      try {
    sourceCursor.setOffset(0);
      } catch (NoSuchMethodError ex3) {
    // ingnore it
      }
      sOrder = sourceCursor.next();
      try {
    targetCursor.setOffset(toffset);
      } catch (NoSuchMethodError ex4) {
    // Can't really do anything here. Return false.
      } catch (Exception e) {
    // We can get an exception when the toffset is really the
    // end of string
    // Just catch it and return false;
    return false;
      }
      tOrder = targetCursor.next();

      if (tOrder == CollationElementIterator.NULLORDER) {
    break;
      }

      debug("sOrder=" + sOrder + " tOrder=" + tOrder);

      //Find the first character in the target that matches the first
      // character in the source. Look for a complete match of
      // primary, secondary and tertiary. Lacking that look for
      // a match of just primary.
      while (tOrder != CollationElementIterator.NULLORDER) {
    if (sOrder == tOrder) {
        try {
      toffset = targetCursor.getOffset();
        } catch (NoSuchMethodError ex) {
      //ignore it
        }
        break;
    }
    pSOrder = CollationElementIterator.primaryOrder(sOrder);
    pTOrder = CollationElementIterator.primaryOrder(tOrder);
    if (pSOrder == pTOrder) {
        try {
      toffset = targetCursor.getOffset();
        } catch (NoSuchMethodError ex2) {
      //ignore it
        }
        break;
    }
    tOrder = targetCursor.next();
    debug("next tOrder=" + tOrder);
      }

      // If at end there is no match
      if (tOrder == CollationElementIterator.NULLORDER) {
    return false;
      }

      gets = false;
      gett = false;
      initialCheckSecTer = rbc.getStrength() >= Collator.SECONDARY;
      checkSecTer = initialCheckSecTer;
      checkTertiary = rbc.getStrength() >= Collator.TERTIARY;
     
      while (true) {
    // Get the next collation element in each of the strings, unless
    // we've been requested to skip it.
    if (gets) {
        sOrder = sourceCursor.next();
    } else {
        gets = true;
    }
    if (gett) {
        tOrder = targetCursor.next();
    } else {
        gett = true;
    }

    // If we've hit the end of one of the strings, jump out of the loop
    if ((sOrder == CollationElementIterator.NULLORDER)||
        (tOrder == CollationElementIterator.NULLORDER)) {
        debug("One string at end");
        break;
    }

    pSOrder = CollationElementIterator.primaryOrder(sOrder);
    pTOrder = CollationElementIterator.primaryOrder(tOrder);

            // If there's no difference at this position, we can skip it
    if (sOrder == tOrder) {
        if (isFrenchSec && pSOrder != 0) {
      if (!checkSecTer) {
          // in french, a secondary difference more to the
          // right is stronger,
          // so accents have to be checked with each base
          // element
          checkSecTer = initialCheckSecTer;
          // but tertiary differences are less important
          // than the first
          // secondary difference, so checking tertiary
          // remains disabled
          checkTertiary = false;
      }
        }
        debug("No diff at this positon continue");
        continue;
    }

    // Compare primary differences first.
    if ( pSOrder != pTOrder ) {
        if (sOrder == 0) {
      // The entire source element is ignorable.
      // Skip to the next source element,
      // but don't fetch another target element.
      gett = false;
      continue;
        }
        if (tOrder == 0) {
      // The entire target element is ignorable.
      // Skip to the next target element,
      // but don't fetch another source element.
      gets = false;
      continue;
        }

        // The source and target elements aren't ignorable,
        // but it's still possible
        // for the primary component of one of the elements
        // to be ignorable....
       
        if (pSOrder == 0) {
      // The source's primary is ignorable,
      // but the target's isn't.  We treat ignorables
      // as a secondary differenc
      if (checkSecTer) {
          // (strength is SECONDARY)
          targetCursor.next();
          toffset = targetCursor.getOffset();
          debug("Strength is secondary pSOrder === 0");
          continue startSearchForFirstMatch;
      }
      // Skip to the next source element,
      // but don't fetch another target element.
      gett = false;
        } else if (pTOrder == 0) {
      // record differences - see the comment above.
      if (checkSecTer) {
          // (stength is SECONDARY)
          targetCursor.next();
          toffset = targetCursor.getOffset();
          debug("Strength is secondary - pTOrder == 0");
          continue startSearchForFirstMatch;
      }
      // Skip to the next source element,
      // but don't fetch another target element.
      gets = false;
        } else {
      // Neither of the orders is ignorable,
      // and we already know that the primary
      // orders are different because of the
      // (pSOrder != pTOrder) test above.
      // Record the difference and stop the
      // comparison.
      targetCursor.next();
      toffset = targetCursor.getOffset();
      debug("Order are ignorable");
      continue startSearchForFirstMatch;
        }
    } else { // else of if ( pSOrder != pTOrder )
        // primary order is the same, but complete order is
        // different. So there
        // are no base elements at this point, only
        // ignorables (Since the strings are
        // normalized)
       
        if (checkSecTer) {
      // a secondary or tertiary difference may
      // still matter
      secSOrder = CollationElementIterator.secondaryOrder(sOrder);
      secTOrder = CollationElementIterator.secondaryOrder(tOrder);
      if (secSOrder != secTOrder) {
          // there is a secondary difference
          targetCursor.next();
          toffset = targetCursor.getOffset();
          debug("Secondary Difference");
          continue startSearchForFirstMatch;
          // (strength is SECONDARY)
          // (even in french, only the first
          // secondary difference within
          // a base character matters)
      } else {
          if (checkTertiary) {
        // a tertiary difference may still matter
        terSOrder = CollationElementIterator.tertiaryOrder(sOrder);
        terTOrder = CollationElementIterator.tertiaryOrder(tOrder);
        if (terSOrder != terTOrder) {
            // there is a tertiary difference
            targetCursor.next();
            toffset = targetCursor.getOffset();
            debug("Tertiary difference");
            continue startSearchForFirstMatch;
            // (strength is TERTIARY)
        }
          }
      }
        } // if (checkSecTer)

    // if ( pSOrder != pTOrder )
      } // while()

      // There might be a match
      // Just do some final checking
      if (sOrder != CollationElementIterator.NULLORDER) {
    // (tOrder must be CollationElementIterator::NULLORDER,
    // since this point is only reached when sOrder or
    // tOrder is NULLORDER.)
    // The source string has more elements,
    // but the target string hasn't.
    do {
        if (CollationElementIterator.primaryOrder(sOrder) != 0) {
      // We found an additional non-ignorable base
      // character in the source string.
      // This is a primary difference,
      // so the source is greater
      targetCursor.next();
      toffset = targetCursor.getOffset();
      debug("Additional non-ignborable base character in source string - source is greater");
      continue startSearchForFirstMatch;
      // (strength is PRIMARY)
        }
        else if (CollationElementIterator.secondaryOrder(sOrder) != 0) {
      // Additional secondary elements mean the
      // source string is greater
      if (checkSecTer) {
          targetCursor.next();
          toffset = targetCursor.getOffset();
          debug("Additional secondary elements source is greater");
          continue startSearchForFirstMatch;
          // (strength is SECONDARY)
      }
        }
    } while ((sOrder = sourceCursor.next()) != CollationElementIterator.NULLORDER);
      }
      // Either the target string has more elements,
      // but the source string hasn't or neither the target string
      // or the source string have any more elemnts.
      // In either case the source was contained in target
View Full Code Here


    }

    Boolean result;

    // get the collation integer representing "_"
    CollationElementIterator cei =
                  collator.getCollationElementIterator("_");
    anyCharInts[0] = cei.next();
    {
      int nextInt;

      // There may be multiple ints representing this character
      while ((nextInt = cei.next()) != CollationElementIterator.NULLORDER)
      {
        int[] temp = new int[anyCharInts.length + 1];
        for (int index = 0; index < anyCharInts.length; index++)
        {
          temp[index] = anyCharInts[index];
        }
        temp[anyCharInts.length] = nextInt;
        anyCharInts = temp;
      }
    }
    // get the collation integer representing "%"
    cei = collator.getCollationElementIterator("%");
    anyStringInts[0] = cei.next();
    {
      int nextInt;

      // There may be multiple ints representing this character
      while ((nextInt = cei.next()) != CollationElementIterator.NULLORDER)
      {
        int[] temp = new int[anyStringInts.length + 1];
        for (int index = 0; index < anyStringInts.length; index++)
        {
          temp[index] = anyStringInts[index];
View Full Code Here

    }

    intArray = new int[getLength()];

    RuleBasedCollator rbc = getLocaleFinder().getCollator();
    CollationElementIterator cei = rbc.getCollationElementIterator(getString());
    int nextInt;
    while ((nextInt = cei.next()) != CollationElementIterator.NULLORDER)
    {
      /* Believe it or not, a String might have more
       * collation elements than characters.
       * So, we handle that case by increasing the int array
       * by 5 and copying array elements.
View Full Code Here

    coll = (RuleBasedCollator) Collator.getInstance(Locale.US);
  }

  public void testGetOffset() {
    String text = "abc";
    CollationElementIterator iterator = coll
        .getCollationElementIterator(text);
    int[] offsets = { 0, 1, 2, 3 };
    int offset = iterator.getOffset();
    int i = 0;
    assertEquals(offsets[i++], offset);
    while (offset != text.length()) {
      iterator.next();
      offset = iterator.getOffset();
      assertEquals(offsets[i++], offset);
    }
  }
View Full Code Here

    }
  }

  public void testNext() {
    String text = "abc";
    CollationElementIterator iterator = coll
        .getCollationElementIterator(text);
    int[] orders = new int[text.length()];
    int order = iterator.next();
    int i = 0;
    while (order != CollationElementIterator.NULLORDER) {
      orders[i++] = order;
      order = iterator.next();
    }

    int offset = iterator.getOffset();
    assertEquals(text.length(), offset);
    order = iterator.previous();

    while (order != CollationElementIterator.NULLORDER) {
      assertEquals(orders[--i], order);
      order = iterator.previous();
    }

    assertEquals(0, iterator.getOffset());
  }
View Full Code Here

    assertEquals(0, iterator.getOffset());
  }

  public void testReset() {
    String text = "abc";
    CollationElementIterator iterator = coll
        .getCollationElementIterator(text);
    int[] orders = new int[text.length()];
    int order = iterator.next();
    int i = 0;
    while (order != CollationElementIterator.NULLORDER) {
      orders[i++] = order;
      order = iterator.next();
    }

    int offset = iterator.getOffset();
    assertEquals(text.length(), offset);

    iterator.reset();
    assertEquals(0, iterator.getOffset());
  }
View Full Code Here

  public void testGetMaxExpansion() {
    String text = "cha";
    RuleBasedCollator rbColl = (RuleBasedCollator) Collator
        .getInstance(new Locale("es", "", "TRADITIONAL"));
    CollationElementIterator iterator = rbColl
        .getCollationElementIterator(text);
    int order = iterator.next();
    while (order != CollationElementIterator.NULLORDER) {
      assertEquals(1, iterator.getMaxExpansion(order));
      order = iterator.next();
    }

  }
View Full Code Here

  public void testPrimaryOrder() {
    RuleBasedCollator rbColl = (RuleBasedCollator) Collator
        .getInstance(new Locale("de", "DE"));
    String text = "\u00e6";
    CollationElementIterator iterator = rbColl
        .getCollationElementIterator(text);
    int order = iterator.next();
    int pOrder = CollationElementIterator.primaryOrder(order);
    CollationElementIterator iterator2 = rbColl
        .getCollationElementIterator("ae");
    int order2 = iterator2.next();
    int pOrder2 = CollationElementIterator.primaryOrder(order2);
    assertEquals(pOrder, pOrder2);
  }
View Full Code Here

  public void testSecondaryOrder() {
    RuleBasedCollator rbColl = (RuleBasedCollator) Collator
        .getInstance(new Locale("fr", "FR"));
    String text = "a\u00e0";
    CollationElementIterator iterator = rbColl
        .getCollationElementIterator(text);
    int order = iterator.next();
    int sOrder1 = CollationElementIterator.secondaryOrder(order);

    order = iterator.next();
    int sOrder2 = CollationElementIterator.secondaryOrder(order);

    assertEquals(sOrder1, sOrder2);
  }
View Full Code Here

  public void testTertiaryOrder() {
    RuleBasedCollator rbColl = (RuleBasedCollator) Collator
        .getInstance(new Locale("fr", "FR"));
    String text = "abAB";
    CollationElementIterator iterator = rbColl
        .getCollationElementIterator(text);
    int order = iterator.next();
    int tOrder1 = CollationElementIterator.tertiaryOrder(order);
    order = iterator.next();
    int tOrder2 = CollationElementIterator.tertiaryOrder(order);
    assertEquals(tOrder1, tOrder2);

    order = iterator.next();
    tOrder1 = CollationElementIterator.tertiaryOrder(order);
    order = iterator.next();
    tOrder2 = CollationElementIterator.tertiaryOrder(order);
    assertEquals(tOrder1, tOrder2);
  }
View Full Code Here

TOP

Related Classes of java.text.CollationElementIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.