Logo Search packages:      
Sourcecode: javahelp2 version File versions  Download package

static boolean javax::help::HelpUtilities::isStringInString ( RuleBasedCollator  rbc,
String  source,
String  target 
) [inline, static]

Returns information about whether a string is contained in another string. Compares the character data stored in two different strings based on the collation rules.

Definition at line 550 of file HelpUtilities.java.

References debug.

                                                {
        // The basic algorithm here is that we use CollationElementIterators
        // to step through both the source and target strings.  We compare each
        // collation element in the source string against the corresponding one
        // in the target, checking for differences.
        //
        // If a difference is found, we set <result> to false.
        //
        // However, it's not that simple.  If we find a tertiary difference
        // (e.g. 'A' vs. 'a') near the beginning of a string, it can be
        // overridden by a primary difference (e.g. "A" vs. "B") later in
        // the string.  For example, "AA" < "aB", even though 'A' > 'a'.
        //
        // To keep track of this, we use strengthResult to keep track of the
        // strength of the most significant difference that has been found
        // so far.  When we find a difference whose strength is greater than
        // strengthResult, it overrides the last difference (if any) that
        // was found.

      debug("isStringInString source=" + source + " targe =" + target); 
          
      // Initial sanity checks
      if (source == null || target == null) {
          return false;
      }
      if (source.length() == 0 && target.length() == 0) {
          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
          // return true;
          return true;
      }
      return false;
    }


Generated by  Doxygen 1.6.0   Back to index