1:
37:
38:
39: package ;
40:
41: import ;
42:
43:
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:
66: private final boolean isNb (char c)
67: {
68: return (c == 0x00a0
69: || c == 0x2011
70: || c == 0xfeff);
71: }
72: private final boolean isClose (int type)
73: {
74: return (type == Character.END_PUNCTUATION
75:
76:
77: || type == Character.OTHER_PUNCTUATION);
78: }
79: private final boolean isIdeo (char c)
80: {
81: return (c >= 0x3040 && c <= 0x309f
82: || c >= 0x30a0 && c <= 0x30ff
83: || c >= 0x4e00 && c <= 0x9fff
84: || c >= 0x3100 && c <= 0x312f);
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:
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:
130:
131:
132: start = iter.getIndex();
133: }
134: }
135: else
136: {
137:
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:
168: if (nt == Character.PARAGRAPH_SEPARATOR
169: || nt == Character.LINE_SEPARATOR)
170: break;
171:
172:
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: }