Source for gnu.java.text.LineBreakIterator

   1: /* LineBreakIterator.java - Default word BreakIterator.
   2:    Copyright (C) 1999, 2001, 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.text;
  40: 
  41: import java.text.CharacterIterator;
  42: 
  43: /**
  44:  * @author Tom Tromey <tromey@cygnus.com>
  45:  * @date March 22, 1999
  46:  * Written using The Unicode Standard, Version 2.0.
  47:  */
  48: 
  49: public class LineBreakIterator extends BaseBreakIterator
  50: {
  51:   public Object clone ()
  52:   {
  53:     return new LineBreakIterator (this);
  54:   }
  55: 
  56:   public LineBreakIterator ()
  57:   {
  58:   }
  59: 
  60:   private LineBreakIterator (LineBreakIterator other)
  61:   {
  62:     iter = (CharacterIterator) other.iter.clone();
  63:   }
  64: 
  65:   // Some methods to tell us different properties of characters.
  66:   private final boolean isNb (char c)
  67:   {
  68:     return (c == 0x00a0         // NO-BREAK SPACE
  69:             || c == 0x2011      // NON-BREAKING HYPHEN
  70:             || c == 0xfeff);    // ZERO WITH NO-BREAK SPACE
  71:   }
  72:   private final boolean isClose (int type)
  73:   {
  74:     return (type == Character.END_PUNCTUATION
  75:             // Unicode book says "comma, period, ...", which I take to
  76:             // mean "Po" class.
  77:             || type == Character.OTHER_PUNCTUATION);
  78:   }
  79:   private final boolean isIdeo (char c)
  80:   {
  81:     return (c >= 0x3040 && c <= 0x309f         // Hiragana
  82:             || c >= 0x30a0 && c <= 0x30ff      // Katakana
  83:             || c >= 0x4e00 && c <= 0x9fff      // Han
  84:             || c >= 0x3100 && c <= 0x312f);    // Bopomofo
  85:   }
  86: 
  87:   public int next ()
  88:   {
  89:     int end = iter.getEndIndex();
  90:     if (iter.getIndex() == end)
  91:       return DONE;
  92: 
  93:     while (iter.getIndex() < end)
  94:       {
  95:         char c = iter.current();
  96:         int type = Character.getType(c);
  97: 
  98:         char n = iter.next();
  99: 
 100:         if (n == CharacterIterator.DONE
 101:             || type == Character.PARAGRAPH_SEPARATOR
 102:             || type == Character.LINE_SEPARATOR)
 103:           break;
 104: 
 105:         // Handle two cases where we must scan for non-spacing marks.
 106:         int start = iter.getIndex();
 107:         if (type == Character.SPACE_SEPARATOR
 108:             || type == Character.START_PUNCTUATION
 109:             || isIdeo (c))
 110:           {
 111:             while (n != CharacterIterator.DONE
 112:                    && Character.getType(n) == Character.NON_SPACING_MARK)
 113:               n = iter.next();
 114:             if (n == CharacterIterator.DONE)
 115:               break;
 116: 
 117:             if (type == Character.SPACE_SEPARATOR)
 118:               {
 119:                 int nt = Character.getType(n);
 120:                 if (nt != Character.NON_SPACING_MARK
 121:                     && nt != Character.SPACE_SEPARATOR
 122:                     && ! isNb (n))
 123:                   break;
 124:               }
 125:             else if (type == Character.START_PUNCTUATION)
 126:               {
 127:                 if (isIdeo (n))
 128:                   {
 129:                     // Open punctuation followed by non spacing marks
 130:                     // and then ideograph does not have a break in
 131:                     // it.  So skip all this.
 132:                     start = iter.getIndex();
 133:                   }
 134:               }
 135:             else
 136:               {
 137:                 // Ideograph preceded this character.
 138:                 if (isClose (Character.getType(n)))
 139:                   break;
 140:               }
 141:           }
 142:         iter.setIndex(start);
 143:       }
 144: 
 145:     return iter.getIndex();
 146:   }
 147: 
 148:   public int previous ()
 149:   {
 150:     int start = iter.getBeginIndex();
 151:     if (iter.getIndex() == start)
 152:       return DONE;
 153: 
 154:     while (iter.getIndex() >= start)
 155:       {
 156:         char c = iter.previous();
 157:         if (c == CharacterIterator.DONE)
 158:           break;
 159:         int type = Character.getType(c);
 160: 
 161:         char n = iter.previous();
 162:         if (n == CharacterIterator.DONE)
 163:           break;
 164:         iter.next();
 165: 
 166:         int nt = Character.getType(n);
 167:         // Break after paragraph separators.
 168:         if (nt == Character.PARAGRAPH_SEPARATOR
 169:             || nt == Character.LINE_SEPARATOR)
 170:           break;
 171: 
 172:         // Skip non-spacing marks.
 173:         int init = iter.getIndex();
 174:         while (n != CharacterIterator.DONE && nt == Character.NON_SPACING_MARK)
 175:           {
 176:             n = iter.previous();
 177:             nt = Character.getType(n);
 178:           }
 179: 
 180:         if (nt == Character.SPACE_SEPARATOR
 181:             && type != Character.SPACE_SEPARATOR
 182:             && type != Character.NON_SPACING_MARK
 183:             && ! isNb (c))
 184:           break;
 185:         if (! isClose (type) && isIdeo (n))
 186:           break;
 187:         if (isIdeo (c) && nt != Character.START_PUNCTUATION)
 188:           break;
 189:         iter.setIndex(init);
 190:       }
 191: 
 192:     return iter.getIndex();
 193:   }
 194: }