Java Code Examples for java.text.BreakIterator#current()

The following examples show how to use java.text.BreakIterator#current() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelectionImpl.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void selectWord(int wordPositionInArea) {
    if(area.getLength() == 0) {
        return;
    }

    BreakIterator breakIterator = BreakIterator.getWordInstance( getArea().getLocale() );
    breakIterator.setText(area.getText());
    breakIterator.preceding(wordPositionInArea);
    breakIterator.next();
    int wordStart = breakIterator.current();

    breakIterator.following(wordPositionInArea);
    breakIterator.next();
    int wordEnd = breakIterator.current();

    selectRange(wordStart, wordEnd);
}
 
Example 2
Source File: BreakIteratorTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bug 4068137
 */
public void TestEndBehavior()
{
    String testString = "boo.";
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(testString);

    if (wb.first() != 0)
        errln("Didn't get break at beginning of string.");
    if (wb.next() != 3)
        errln("Didn't get break before period in \"boo.\"");
    if (wb.current() != 4 && wb.next() != 4)
        errln("Didn't get break at end of string.");
}
 
Example 3
Source File: BreakIteratorTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bug 4068137
 */
public void TestEndBehavior()
{
    String testString = "boo.";
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(testString);

    if (wb.first() != 0)
        errln("Didn't get break at beginning of string.");
    if (wb.next() != 3)
        errln("Didn't get break before period in \"boo.\"");
    if (wb.current() != 4 && wb.next() != 4)
        errln("Didn't get break at end of string.");
}
 
Example 4
Source File: BreakIteratorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bug 4068137
 */
public void TestEndBehavior()
{
    String testString = "boo.";
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(testString);

    if (wb.first() != 0)
        errln("Didn't get break at beginning of string.");
    if (wb.next() != 3)
        errln("Didn't get break before period in \"boo.\"");
    if (wb.current() != 4 && wb.next() != 4)
        errln("Didn't get break at end of string.");
}
 
Example 5
Source File: BreakIteratorTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bug 4068137
 */
public void TestEndBehavior()
{
    String testString = "boo.";
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(testString);

    if (wb.first() != 0)
        errln("Didn't get break at beginning of string.");
    if (wb.next() != 3)
        errln("Didn't get break before period in \"boo.\"");
    if (wb.current() != 4 && wb.next() != 4)
        errln("Didn't get break at end of string.");
}
 
Example 6
Source File: TestSplittingBreakIterator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void testFirstAndLast(BreakIterator bi, String text, String boundaries) {
  String message = "Text: " + text;
  int current = bi.current();
  assertEquals(message, boundaries.indexOf('^'), current);
  assertEquals(message, current, bi.first());
  assertEquals(message, current, bi.current());
  current = bi.last();
  assertEquals(boundaries.lastIndexOf('^'), current);
  assertEquals(message, current, bi.current());
}
 
Example 7
Source File: TestSplittingBreakIterator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a string comprised of spaces and '^' only at the boundaries.
 */
private String readBoundariesToString(BreakIterator bi, String text) {
  // init markers to spaces
  StringBuilder markers = new StringBuilder();
  markers.setLength(text.length() + 1);
  for (int k = 0; k < markers.length(); k++) {
    markers.setCharAt(k, ' ');
  }

  bi.setText(text);
  for (int boundary = bi.current(); boundary != BreakIterator.DONE; boundary = bi.next()) {
    markers.setCharAt(boundary, '^');
  }
  return markers.toString();
}
 
Example 8
Source File: CaretSelectionBindImpl.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Assumes that {@code getArea().getLength != 0} is true and {@link BreakIterator#setText(String)} has been called */
private int calculatePositionViaBreakingBackwards(int numOfBreaks, BreakIterator breakIterator, int position) {
    breakIterator.preceding(position);
    for (int i = 1; i < numOfBreaks; i++) {
        breakIterator.previous();
    }
    return breakIterator.current();
}
 
Example 9
Source File: BreakIteratorTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bug 4068137
 */
public void TestEndBehavior()
{
    String testString = "boo.";
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(testString);

    if (wb.first() != 0)
        errln("Didn't get break at beginning of string.");
    if (wb.next() != 3)
        errln("Didn't get break before period in \"boo.\"");
    if (wb.current() != 4 && wb.next() != 4)
        errln("Didn't get break at end of string.");
}
 
Example 10
Source File: SpellCheckIterator.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a new spell check iterator.
 *	
 * @param document the document containing the specified partition
 * @param region the region to spell check
 * @param locale the locale to use for spell checking
 * @param breakIterator the break-iterator
 */
public SpellCheckIterator(IDocument document, IRegion region, Locale locale, BreakIterator breakIterator) {
	fOffset= region.getOffset();
	fWordIterator= breakIterator;
	fDelimiter= TextUtilities.getDefaultLineDelimiter(document);

	String content;
	try {

		content= document.get(region.getOffset(), region.getLength());

	} catch (Exception exception) {
		content= ""; //$NON-NLS-1$
	}
	fContent= content;

	fWordIterator.setText(content);
	fPredecessor= fWordIterator.first();
	fSuccessor= fWordIterator.next();

	final BreakIterator iterator= BreakIterator.getSentenceInstance(locale);
	iterator.setText(content);

	int offset= iterator.current();
	while (offset != BreakIterator.DONE) {

		fSentenceBreaks.add(new Integer(offset));
		offset= iterator.next();
	}
}
 
Example 11
Source File: SexpBaseBackwardHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see com.mulgasoft.emacsplus.commands.SexpHandler#getNextPosition(org.eclipse.jface.text.IDocument, java.text.BreakIterator)
 */
@Override
protected int getNextPosition(IDocument document, BreakIterator iter) {
	int pos = iter.current();
	int result = iter.previous();
	if (result != BreakIterator.DONE) {
		result = checkUnder(document,iter,result);			
		result = checkDot(document,pos,result);
	}
	return result;
}
 
Example 12
Source File: SexpBaseForwardHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see com.mulgasoft.emacsplus.commands.SexpHandler#getNextPosition(org.eclipse.jface.text.IDocument, java.text.BreakIterator)
 */
@Override
protected int getNextPosition(IDocument document, BreakIterator iter) {
	int pos = iter.current();
	int result = iter.next();
	if (result != BreakIterator.DONE) {
		result = checkDot(document,pos,result);
		result = checkUnder(document,result);			
	}
	return result;
}
 
Example 13
Source File: CaretSelectionBindImpl.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Assumes that {@code getArea().getLength != 0} is true and {@link BreakIterator#setText(String)} has been called */
private int calculatePositionViaBreakingForwards(int numOfBreaks, BreakIterator breakIterator, int position) {
    breakIterator.following(position);
    for (int i = 1; i < numOfBreaks; i++) {
        breakIterator.next(numOfBreaks);
    }
    return breakIterator.current();
}
 
Example 14
Source File: CodeArea.java    From RichTextFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override // to select words containing underscores
public void selectWord()
{
    if ( getLength() == 0 ) return;

    CaretSelectionBind<?,?,?> csb = getCaretSelectionBind();
    int paragraph = csb.getParagraphIndex();
    int position = csb.getColumnPosition(); 
    
    String paragraphText = getText( paragraph );
    BreakIterator breakIterator = BreakIterator.getWordInstance( getLocale() );
    breakIterator.setText( paragraphText );

    breakIterator.preceding( position );
    int start = breakIterator.current();
    
    while ( start > 0 && paragraphText.charAt( start-1 ) == '_' )
    {
        if ( --start > 0 && ! breakIterator.isBoundary( start-1 ) )
        {
            breakIterator.preceding( start );
            start = breakIterator.current();
        }
    }
    
    breakIterator.following( position );
    int end = breakIterator.current();
    int len = paragraphText.length();
    
    while ( end < len && paragraphText.charAt( end ) == '_' )
    {
        if ( ++end < len && ! breakIterator.isBoundary( end+1 ) )
        {
            breakIterator.following( end );
            end = breakIterator.current();
        }
        // For some reason single digits aren't picked up so ....
        else if ( Character.isDigit( paragraphText.charAt( end ) ) )
        {
            end++;
        }
    }
    
    csb.selectRange( paragraph, start, paragraph, end );
}
 
Example 15
Source File: TestWholeBreakIterator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Asserts that two breakiterators break the text the same way */
public static void assertSameBreaks(CharacterIterator one, CharacterIterator two, BreakIterator expected, BreakIterator actual) {
  expected.setText(one);
  actual.setText(two);

  assertEquals(expected.current(), actual.current());

  // next()
  int v = expected.current();
  while (v != BreakIterator.DONE) {
    assertEquals(v = expected.next(), actual.next());
    assertEquals(expected.current(), actual.current());
  }
  
  // first()
  assertEquals(expected.first(), actual.first());
  assertEquals(expected.current(), actual.current());
  // last()
  assertEquals(expected.last(), actual.last());
  assertEquals(expected.current(), actual.current());
  
  // previous()
  v = expected.current();
  while (v != BreakIterator.DONE) {
    assertEquals(v = expected.previous(), actual.previous());
    assertEquals(expected.current(), actual.current());
  }
  
  // following()
  for (int i = one.getBeginIndex(); i <= one.getEndIndex(); i++) {
    expected.first();
    actual.first();
    assertEquals(expected.following(i), actual.following(i));
    assertEquals(expected.current(), actual.current());
  }
  
  // preceding()
  for (int i = one.getBeginIndex(); i <= one.getEndIndex(); i++) {
    expected.last();
    actual.last();
    assertEquals(expected.preceding(i), actual.preceding(i));
    assertEquals(expected.current(), actual.current());
  }
}
 
Example 16
Source File: LengthGoalBreakIterator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Breaks will be on average {@code targetLength} apart; the closest break to this target (before or after)
 * is chosen. The match will be positioned according to {@code fragmentAlignment} as much as possible.
 */
public static LengthGoalBreakIterator createClosestToLength(BreakIterator baseIter, int targetLength,
                                                            float fragmentAlignment) {
  return new LengthGoalBreakIterator(baseIter, targetLength, fragmentAlignment, false, baseIter.current());
}
 
Example 17
Source File: LengthGoalBreakIterator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Breaks will be at least {@code minLength} apart (to the extent possible),
 * while trying to position the match inside the fragment according to {@code fragmentAlignment}.
 */
public static LengthGoalBreakIterator createMinLength(BreakIterator baseIter, int minLength,
                                                      float fragmentAlignment) {
  return new LengthGoalBreakIterator(baseIter, minLength, fragmentAlignment, true, baseIter.current());
}