Java Code Examples for java.text.BreakIterator#following()
The following examples show how to use
java.text.BreakIterator#following() .
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: TextComponent.java From JDKSourceCode1.8 with MIT License | 6 votes |
/** * Needed to unify forward and backward searching. * The method assumes that s is the text assigned to words. */ private int findWordLimit(int index, BreakIterator words, boolean direction, String s) { // Fix for 4256660 and 4256661. // Words iterator is different from character and sentence iterators // in that end of one word is not necessarily start of another word. // Please see java.text.BreakIterator JavaDoc. The code below is // based on nextWordStartAfter example from BreakIterator.java. int last = (direction == NEXT) ? words.following(index) : words.preceding(index); int current = (direction == NEXT) ? words.next() : words.previous(); while (current != BreakIterator.DONE) { for (int p = Math.min(last, current); p < Math.max(last, current); p++) { if (Character.isLetter(s.charAt(p))) { return last; } } last = current; current = (direction == NEXT) ? words.next() : words.previous(); } return BreakIterator.DONE; }
Example 2
Source File: TextComponent.java From Bytecoder with Apache License 2.0 | 6 votes |
/** * Needed to unify forward and backward searching. * The method assumes that s is the text assigned to words. */ private int findWordLimit(int index, BreakIterator words, boolean direction, String s) { // Fix for 4256660 and 4256661. // Words iterator is different from character and sentence iterators // in that end of one word is not necessarily start of another word. // Please see java.text.BreakIterator JavaDoc. The code below is // based on nextWordStartAfter example from BreakIterator.java. int last = (direction == NEXT) ? words.following(index) : words.preceding(index); int current = (direction == NEXT) ? words.next() : words.previous(); while (current != BreakIterator.DONE) { for (int p = Math.min(last, current); p < Math.max(last, current); p++) { if (Character.isLetter(s.charAt(p))) { return last; } } last = current; current = (direction == NEXT) ? words.next() : words.previous(); } return BreakIterator.DONE; }
Example 3
Source File: BreakIteratorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private void testFollowing(BreakIterator bi, String text, int[] boundaries) { logln("testFollowing():"); int p = 2; int i = 0; try { for (i = 0; i <= text.length(); i++) { // change to <= when new BI code goes in if (i == boundaries[p]) ++p; int b = bi.following(i); logln("bi.following(" + i + ") -> " + b); if (b != boundaries[p]) errln("Wrong result from following() for " + i + ": expected " + boundaries[p] + ", got " + b); } } catch (IllegalArgumentException illargExp) { errln("IllegalArgumentException caught from following() for offset: " + i); } }
Example 4
Source File: TextComponent.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * Needed to unify forward and backward searching. * The method assumes that s is the text assigned to words. */ private int findWordLimit(int index, BreakIterator words, boolean direction, String s) { // Fix for 4256660 and 4256661. // Words iterator is different from character and sentence iterators // in that end of one word is not necessarily start of another word. // Please see java.text.BreakIterator JavaDoc. The code below is // based on nextWordStartAfter example from BreakIterator.java. int last = (direction == NEXT) ? words.following(index) : words.preceding(index); int current = (direction == NEXT) ? words.next() : words.previous(); while (current != BreakIterator.DONE) { for (int p = Math.min(last, current); p < Math.max(last, current); p++) { if (Character.isLetter(s.charAt(p))) { return last; } } last = current; current = (direction == NEXT) ? words.next() : words.previous(); } return BreakIterator.DONE; }
Example 5
Source File: BreakIteratorTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private void testFollowing(BreakIterator bi, String text, int[] boundaries) { logln("testFollowing():"); int p = 2; int i = 0; try { for (i = 0; i <= text.length(); i++) { // change to <= when new BI code goes in if (i == boundaries[p]) ++p; int b = bi.following(i); logln("bi.following(" + i + ") -> " + b); if (b != boundaries[p]) errln("Wrong result from following() for " + i + ": expected " + boundaries[p] + ", got " + b); } } catch (IllegalArgumentException illargExp) { errln("IllegalArgumentException caught from following() for offset: " + i); } }
Example 6
Source File: TextComponent.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * Needed to unify forward and backward searching. * The method assumes that s is the text assigned to words. */ private int findWordLimit(int index, BreakIterator words, boolean direction, String s) { // Fix for 4256660 and 4256661. // Words iterator is different from character and sentence iterators // in that end of one word is not necessarily start of another word. // Please see java.text.BreakIterator JavaDoc. The code below is // based on nextWordStartAfter example from BreakIterator.java. int last = (direction == NEXT) ? words.following(index) : words.preceding(index); int current = (direction == NEXT) ? words.next() : words.previous(); while (current != BreakIterator.DONE) { for (int p = Math.min(last, current); p < Math.max(last, current); p++) { if (Character.isLetter(s.charAt(p))) { return last; } } last = current; current = (direction == NEXT) ? words.next() : words.previous(); } return BreakIterator.DONE; }
Example 7
Source File: TextComponent.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
/** * Needed to unify forward and backward searching. * The method assumes that s is the text assigned to words. */ private int findWordLimit(int index, BreakIterator words, boolean direction, String s) { // Fix for 4256660 and 4256661. // Words iterator is different from character and sentence iterators // in that end of one word is not necessarily start of another word. // Please see java.text.BreakIterator JavaDoc. The code below is // based on nextWordStartAfter example from BreakIterator.java. int last = (direction == NEXT) ? words.following(index) : words.preceding(index); int current = (direction == NEXT) ? words.next() : words.previous(); while (current != BreakIterator.DONE) { for (int p = Math.min(last, current); p < Math.max(last, current); p++) { if (Character.isLetter(s.charAt(p))) { return last; } } last = current; current = (direction == NEXT) ? words.next() : words.previous(); } return BreakIterator.DONE; }
Example 8
Source File: MainPanel.java From java-swing-tips with MIT License | 5 votes |
public static int getWordStart(JTextComponent c, int offs) throws BadLocationException { Element line = Optional.ofNullable(Utilities.getParagraphElement(c, offs)) .orElseThrow(() -> new BadLocationException("No word at " + offs, offs)); Document doc = c.getDocument(); int lineStart = line.getStartOffset(); int lineEnd = Math.min(line.getEndOffset(), doc.getLength()); int offs2 = offs; Segment seg = SegmentCache.getSharedSegment(); doc.getText(lineStart, lineEnd - lineStart, seg); if (seg.count > 0) { BreakIterator words = BreakIterator.getWordInstance(c.getLocale()); words.setText(seg); int wordPosition = seg.offset + offs - lineStart; if (wordPosition >= words.last()) { wordPosition = words.last() - 1; words.following(wordPosition); offs2 = lineStart + words.previous() - seg.offset; } else { words.following(wordPosition); offs2 = lineStart + words.previous() - seg.offset; for (int i = offs; i > offs2; i--) { char ch = seg.charAt(i - seg.offset); if (ch == '_' || ch == '-') { offs2 = i + 1; break; } } } } SegmentCache.releaseSharedSegment(seg); return offs2; }
Example 9
Source File: MainPanel.java From java-swing-tips with MIT License | 5 votes |
public static int getWordEnd(JTextComponent c, int offs) throws BadLocationException { Element line = Optional.ofNullable(Utilities.getParagraphElement(c, offs)) .orElseThrow(() -> new BadLocationException("No word at " + offs, offs)); Document doc = c.getDocument(); int lineStart = line.getStartOffset(); int lineEnd = Math.min(line.getEndOffset(), doc.getLength()); int offs2 = offs; Segment seg = SegmentCache.getSharedSegment(); doc.getText(lineStart, lineEnd - lineStart, seg); if (seg.count > 0) { BreakIterator words = BreakIterator.getWordInstance(c.getLocale()); words.setText(seg); int wordPosition = offs - lineStart + seg.offset; if (wordPosition >= words.last()) { wordPosition = words.last() - 1; } offs2 = lineStart + words.following(wordPosition) - seg.offset; for (int i = offs; i < offs2; i++) { char ch = seg.charAt(i - seg.offset); if (ch == '_' || ch == '-') { offs2 = i; break; } } } SegmentCache.releaseSharedSegment(seg); return offs2; }
Example 10
Source File: CommandExecutionUtils.java From APICloud-Studio with GNU General Public License v3.0 | 5 votes |
/** * Tries to find the word at the given offset. * * @param line * the line * @param offset * the offset * @return the word or <code>null</code> if none */ protected static IRegion findWordRegion(String line, int offset) { BreakIterator breakIter = BreakIterator.getWordInstance(); breakIter.setText(line); int start = breakIter.preceding(offset); if (start == BreakIterator.DONE) start = 0; int end = breakIter.following(offset); if (end == BreakIterator.DONE) end = line.length(); if (breakIter.isBoundary(offset)) { if (end - offset > offset - start) { start = offset; } else { end = offset; } } if (end == start) { return new Region(start, 0); } return new Region(start, end - start); }
Example 11
Source File: AccessibleHTML.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * Returns the Segment at <code>index</code> representing either * the paragraph or sentence as identified by <code>part</code>, or * null if a valid paragraph/sentence can't be found. The offset * will point to the start of the word/sentence in the array, and * the modelOffset will point to the location of the word/sentence * in the model. */ private IndexedSegment getSegmentAt(int part, int index) throws BadLocationException { IndexedSegment seg = getParagraphElementText(index); if (seg == null) { return null; } BreakIterator iterator; switch (part) { case AccessibleText.WORD: iterator = BreakIterator.getWordInstance(getLocale()); break; case AccessibleText.SENTENCE: iterator = BreakIterator.getSentenceInstance(getLocale()); break; default: return null; } seg.first(); iterator.setText(seg); int end = iterator.following(index - seg.modelOffset + seg.offset); if (end == BreakIterator.DONE) { return null; } if (end > seg.offset + seg.count) { return null; } int begin = iterator.previous(); if (begin == BreakIterator.DONE || begin >= seg.offset + seg.count) { return null; } seg.modelOffset = seg.modelOffset + begin - seg.offset; seg.offset = begin; seg.count = end - begin; return seg; }
Example 12
Source File: AccessibleHTML.java From Java8CN with Apache License 2.0 | 5 votes |
/** * Returns the Segment at <code>index</code> representing either * the paragraph or sentence as identified by <code>part</code>, or * null if a valid paragraph/sentence can't be found. The offset * will point to the start of the word/sentence in the array, and * the modelOffset will point to the location of the word/sentence * in the model. */ private IndexedSegment getSegmentAt(int part, int index) throws BadLocationException { IndexedSegment seg = getParagraphElementText(index); if (seg == null) { return null; } BreakIterator iterator; switch (part) { case AccessibleText.WORD: iterator = BreakIterator.getWordInstance(getLocale()); break; case AccessibleText.SENTENCE: iterator = BreakIterator.getSentenceInstance(getLocale()); break; default: return null; } seg.first(); iterator.setText(seg); int end = iterator.following(index - seg.modelOffset + seg.offset); if (end == BreakIterator.DONE) { return null; } if (end > seg.offset + seg.count) { return null; } int begin = iterator.previous(); if (begin == BreakIterator.DONE || begin >= seg.offset + seg.count) { return null; } seg.modelOffset = seg.modelOffset + begin - seg.offset; seg.offset = begin; seg.count = end - begin; return seg; }
Example 13
Source File: TestSplittingBreakIterator.java From lucene-solr with Apache License 2.0 | 5 votes |
private void testFollowing(BreakIterator bi, String text, String boundaries, List<Integer> indexes) { String message = "Text: " + text; for (Integer index : indexes) { int got = bi.following(index); if (index == boundaries.length()) { assertEquals(message, BreakIterator.DONE, got); assertEquals(boundaries.lastIndexOf('^'), bi.current()); continue; } assertEquals(message + " index:" + index, boundaries.indexOf('^', index + 1), got); } }
Example 14
Source File: AccessibleHTML.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
/** * Returns the Segment at <code>index</code> representing either * the paragraph or sentence as identified by <code>part</code>, or * null if a valid paragraph/sentence can't be found. The offset * will point to the start of the word/sentence in the array, and * the modelOffset will point to the location of the word/sentence * in the model. */ private IndexedSegment getSegmentAt(int part, int index) throws BadLocationException { IndexedSegment seg = getParagraphElementText(index); if (seg == null) { return null; } BreakIterator iterator; switch (part) { case AccessibleText.WORD: iterator = BreakIterator.getWordInstance(getLocale()); break; case AccessibleText.SENTENCE: iterator = BreakIterator.getSentenceInstance(getLocale()); break; default: return null; } seg.first(); iterator.setText(seg); int end = iterator.following(index - seg.modelOffset + seg.offset); if (end == BreakIterator.DONE) { return null; } if (end > seg.offset + seg.count) { return null; } int begin = iterator.previous(); if (begin == BreakIterator.DONE || begin >= seg.offset + seg.count) { return null; } seg.modelOffset = seg.modelOffset + begin - seg.offset; seg.offset = begin; seg.count = end - begin; return seg; }
Example 15
Source File: AccessibleHTML.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Returns the Segment at <code>index</code> representing either * the paragraph or sentence as identified by <code>part</code>, or * null if a valid paragraph/sentence can't be found. The offset * will point to the start of the word/sentence in the array, and * the modelOffset will point to the location of the word/sentence * in the model. */ private IndexedSegment getSegmentAt(int part, int index) throws BadLocationException { IndexedSegment seg = getParagraphElementText(index); if (seg == null) { return null; } BreakIterator iterator; switch (part) { case AccessibleText.WORD: iterator = BreakIterator.getWordInstance(getLocale()); break; case AccessibleText.SENTENCE: iterator = BreakIterator.getSentenceInstance(getLocale()); break; default: return null; } seg.first(); iterator.setText(seg); int end = iterator.following(index - seg.modelOffset + seg.offset); if (end == BreakIterator.DONE) { return null; } if (end > seg.offset + seg.count) { return null; } int begin = iterator.previous(); if (begin == BreakIterator.DONE || begin >= seg.offset + seg.count) { return null; } seg.modelOffset = seg.modelOffset + begin - seg.offset; seg.offset = begin; seg.count = end - begin; return seg; }
Example 16
Source File: AbstractWordAwareDoubleClickStrategy.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Override protected IRegion findWord(IDocument document, int offset) { try { IRegion line = document.getLineInformationOfOffset(offset); if (offset == line.getOffset() + line.getLength()) return null; BreakIterator breakIter = createBreakIterator(); CharacterIterator characterIterator = new DocumentCharacterIterator(document); breakIter.setText(characterIterator); int start = breakIter.preceding(offset); if (start == BreakIterator.DONE) start = line.getOffset(); int end = breakIter.following(offset); if (end == BreakIterator.DONE) end = line.getOffset() + line.getLength(); if (breakIter.isBoundary(offset)) { if (end - offset > offset - start) start = offset; else end = offset; } if (end == start) return null; return new Region(start, end - start); } catch (BadLocationException e) { return null; } }
Example 17
Source File: CaretSelectionBindImpl.java From RichTextFX with BSD 2-Clause "Simplified" License | 5 votes |
/** 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 18
Source File: CodeArea.java From RichTextFX with BSD 2-Clause "Simplified" License | 4 votes |
@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 19
Source File: TextUtilities.java From ccu-historian with GNU General Public License v3.0 | 4 votes |
/** * Returns the character index of the next line break. If the next * character is wider than <code>width</code> this method will return * <code>start</code> - the caller should check for this case. * * @param text the text (<code>null</code> not permitted). * @param start the start index. * @param width the target display width. * @param iterator the word break iterator. * @param measurer the text measurer. * * @return The index of the next line break. */ private static int nextLineBreak(String text, int start, float width, BreakIterator iterator, TextMeasurer measurer) { // this method is (loosely) based on code in JFreeReport's // TextParagraph class int current = start; int end; float x = 0.0f; boolean firstWord = true; int newline = text.indexOf('\n', start); if (newline < 0) { newline = Integer.MAX_VALUE; } while (((end = iterator.following(current)) != BreakIterator.DONE)) { x += measurer.getStringWidth(text, current, end); if (x > width) { if (firstWord) { while (measurer.getStringWidth(text, start, end) > width) { end--; if (end <= start) { return end; } } return end; } else { end = iterator.previous(); return end; } } else { if (end > newline) { return newline; } } // we found at least one word that fits ... firstWord = false; current = end; } return BreakIterator.DONE; }
Example 20
Source File: ImeContainer.java From mongol-library with MIT License | 4 votes |
private int getNextWordBoundary(CharSequence text, int selectionEnd) { BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text.toString()); int next = boundary.following(selectionEnd); return (next == BreakIterator.DONE) ? selectionEnd : next; }