| 1 | /* AttributedStringIterator.java -- Class to iterate over AttributedString
|
|---|
| 2 | Copyright (C) 1998, 1999 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 java.text;
|
|---|
| 40 |
|
|---|
| 41 | import java.util.Set;
|
|---|
| 42 | import java.util.HashSet;
|
|---|
| 43 | import java.util.Map;
|
|---|
| 44 | import java.util.HashMap;
|
|---|
| 45 | import java.util.Iterator;
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * This class implements the AttributedCharacterIterator interface. It
|
|---|
| 49 | * is used by AttributedString.getIterator().
|
|---|
| 50 | *
|
|---|
| 51 | * @version 0.0
|
|---|
| 52 | *
|
|---|
| 53 | * @author Aaron M. Renn ([email protected])
|
|---|
| 54 | */
|
|---|
| 55 | class AttributedStringIterator implements AttributedCharacterIterator
|
|---|
| 56 | {
|
|---|
| 57 |
|
|---|
| 58 | /*************************************************************************/
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Instance Variables
|
|---|
| 62 | */
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * The character iterator containing the text
|
|---|
| 66 | */
|
|---|
| 67 | private CharacterIterator ci;
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * The list of attributes and ranges
|
|---|
| 71 | */
|
|---|
| 72 | private AttributedString.AttributeRange[] attribs;
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * The list of attributes that the user is interested in. We may,
|
|---|
| 76 | * at our option, not return any other attributes.
|
|---|
| 77 | */
|
|---|
| 78 | private AttributedCharacterIterator.Attribute[] restricts;
|
|---|
| 79 |
|
|---|
| 80 | /*************************************************************************/
|
|---|
| 81 |
|
|---|
| 82 | /*
|
|---|
| 83 | * Constructors
|
|---|
| 84 | */
|
|---|
| 85 |
|
|---|
| 86 | AttributedStringIterator(StringCharacterIterator sci,
|
|---|
| 87 | AttributedString.AttributeRange[] attribs,
|
|---|
| 88 | int begin_index, int end_index,
|
|---|
| 89 | AttributedCharacterIterator.Attribute[] restricts)
|
|---|
| 90 | {
|
|---|
| 91 | this.ci = new StringCharacterIterator(sci, begin_index, end_index);
|
|---|
| 92 | this.attribs = attribs;
|
|---|
| 93 | this.restricts = restricts;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /*************************************************************************/
|
|---|
| 97 |
|
|---|
| 98 | /*
|
|---|
| 99 | * Instance Methods
|
|---|
| 100 | */
|
|---|
| 101 |
|
|---|
| 102 | // First we have a bunch of stupid redirects. If StringCharacterIterator
|
|---|
| 103 | // weren't final, I just would have extended that for this class. Alas, no.
|
|---|
| 104 |
|
|---|
| 105 | public Object
|
|---|
| 106 | clone()
|
|---|
| 107 | {
|
|---|
| 108 | return(ci.clone());
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | public char
|
|---|
| 112 | current()
|
|---|
| 113 | {
|
|---|
| 114 | return(ci.current());
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | public char
|
|---|
| 118 | next()
|
|---|
| 119 | {
|
|---|
| 120 | return(ci.next());
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public char
|
|---|
| 124 | previous()
|
|---|
| 125 | {
|
|---|
| 126 | return(ci.previous());
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | public char
|
|---|
| 130 | first()
|
|---|
| 131 | {
|
|---|
| 132 | return(ci.first());
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | public char
|
|---|
| 136 | last()
|
|---|
| 137 | {
|
|---|
| 138 | return(ci.last());
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public int
|
|---|
| 142 | getIndex()
|
|---|
| 143 | {
|
|---|
| 144 | return(ci.getIndex());
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | public char
|
|---|
| 148 | setIndex(int index)
|
|---|
| 149 | {
|
|---|
| 150 | return(ci.setIndex(index));
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | public int
|
|---|
| 154 | getBeginIndex()
|
|---|
| 155 | {
|
|---|
| 156 | return(ci.getBeginIndex());
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | public int
|
|---|
| 160 | getEndIndex()
|
|---|
| 161 | {
|
|---|
| 162 | return(ci.getEndIndex());
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /*
|
|---|
| 166 | * Here is where the AttributedCharacterIterator methods start.
|
|---|
| 167 | */
|
|---|
| 168 |
|
|---|
| 169 | /*************************************************************************/
|
|---|
| 170 |
|
|---|
| 171 | /**
|
|---|
| 172 | * Returns a list of all the attribute keys that are defined anywhere
|
|---|
| 173 | * on this string.
|
|---|
| 174 | */
|
|---|
| 175 | public Set
|
|---|
| 176 | getAllAttributeKeys()
|
|---|
| 177 | {
|
|---|
| 178 | HashSet s = new HashSet();
|
|---|
| 179 | if (attribs == null)
|
|---|
| 180 | return(s);
|
|---|
| 181 |
|
|---|
| 182 | for (int i = 0; i < attribs.length; i++)
|
|---|
| 183 | {
|
|---|
| 184 | Set key_set = attribs[i].attribs.keySet();
|
|---|
| 185 | Iterator iter = key_set.iterator();
|
|---|
| 186 | while (iter.hasNext())
|
|---|
| 187 | {
|
|---|
| 188 | s.add(iter.next());
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | return(s);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /*************************************************************************/
|
|---|
| 196 |
|
|---|
| 197 | /**
|
|---|
| 198 | * Various methods that determine how far the run extends for various
|
|---|
| 199 | * attribute combinations.
|
|---|
| 200 | */
|
|---|
| 201 |
|
|---|
| 202 | public int
|
|---|
| 203 | getRunLimit()
|
|---|
| 204 | {
|
|---|
| 205 | return(getRunLimit(getAttributes().keySet()));
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | public int
|
|---|
| 209 | getRunLimit(AttributedCharacterIterator.Attribute attrib)
|
|---|
| 210 | {
|
|---|
| 211 | HashSet s = new HashSet();
|
|---|
| 212 | s.add(attrib);
|
|---|
| 213 |
|
|---|
| 214 | return(getRunLimit(s));
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | public synchronized int
|
|---|
| 218 | getRunLimit(Set attribute_set)
|
|---|
| 219 | {
|
|---|
| 220 | int orig_index = ci.getIndex();
|
|---|
| 221 | int run_limit;
|
|---|
| 222 |
|
|---|
| 223 | do
|
|---|
| 224 | {
|
|---|
| 225 | run_limit = ci.getIndex();
|
|---|
| 226 |
|
|---|
| 227 | Map attribute_map = getAttributes();
|
|---|
| 228 |
|
|---|
| 229 | boolean found = false;
|
|---|
| 230 | Iterator iter = attribute_set.iterator();
|
|---|
| 231 | while(iter.hasNext())
|
|---|
| 232 | if (!attribute_map.containsKey(iter.next()))
|
|---|
| 233 | {
|
|---|
| 234 | found = true;
|
|---|
| 235 | break;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | if (found)
|
|---|
| 239 | break;
|
|---|
| 240 | }
|
|---|
| 241 | while (ci.next() != CharacterIterator.DONE);
|
|---|
| 242 |
|
|---|
| 243 | boolean hit_end = (ci.previous() == CharacterIterator.DONE);
|
|---|
| 244 |
|
|---|
| 245 | ci.setIndex(orig_index);
|
|---|
| 246 |
|
|---|
| 247 | if (run_limit == orig_index)
|
|---|
| 248 | return(-1); // No characters match the given attributes
|
|---|
| 249 | // else if (!hit_end)
|
|---|
| 250 | // --run_limit;
|
|---|
| 251 |
|
|---|
| 252 | return(run_limit);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /*************************************************************************/
|
|---|
| 256 |
|
|---|
| 257 | /**
|
|---|
| 258 | * Various methods that determine where the run begins for various
|
|---|
| 259 | * attribute combinations.
|
|---|
| 260 | */
|
|---|
| 261 |
|
|---|
| 262 | public int
|
|---|
| 263 | getRunStart()
|
|---|
| 264 | {
|
|---|
| 265 | return(getRunStart(getAttributes().keySet()));
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | public int
|
|---|
| 269 | getRunStart(AttributedCharacterIterator.Attribute attrib)
|
|---|
| 270 | {
|
|---|
| 271 | HashSet s = new HashSet();
|
|---|
| 272 | s.add(attrib);
|
|---|
| 273 |
|
|---|
| 274 | return(getRunStart(s));
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | public int
|
|---|
| 278 | getRunStart(Set attribute_set)
|
|---|
| 279 | {
|
|---|
| 280 | int orig_index = ci.getIndex();
|
|---|
| 281 | int run_start;
|
|---|
| 282 |
|
|---|
| 283 | do
|
|---|
| 284 | {
|
|---|
| 285 | run_start = ci.getIndex();
|
|---|
| 286 |
|
|---|
| 287 | Map attribute_map = getAttributes();
|
|---|
| 288 |
|
|---|
| 289 | Iterator iter = attribute_set.iterator();
|
|---|
| 290 | while(iter.hasNext())
|
|---|
| 291 | if (!attribute_map.containsKey(iter.next()))
|
|---|
| 292 | break;
|
|---|
| 293 |
|
|---|
| 294 | if (iter.hasNext())
|
|---|
| 295 | break;
|
|---|
| 296 | }
|
|---|
| 297 | while (ci.previous() != CharacterIterator.DONE);
|
|---|
| 298 |
|
|---|
| 299 | boolean hit_beginning = (ci.previous() == CharacterIterator.DONE);
|
|---|
| 300 |
|
|---|
| 301 | ci.setIndex(orig_index);
|
|---|
| 302 |
|
|---|
| 303 | if (run_start == orig_index)
|
|---|
| 304 | return(-1); // No characters match the given attributes
|
|---|
| 305 | else if (!hit_beginning)
|
|---|
| 306 | ++run_start;
|
|---|
| 307 |
|
|---|
| 308 | return(run_start);
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /*************************************************************************/
|
|---|
| 312 |
|
|---|
| 313 | public Object
|
|---|
| 314 | getAttribute(AttributedCharacterIterator.Attribute attrib)
|
|---|
| 315 | {
|
|---|
| 316 | if (attribs == null)
|
|---|
| 317 | return(null);
|
|---|
| 318 |
|
|---|
| 319 | for (int i = 0; i < attribs.length; i++)
|
|---|
| 320 | {
|
|---|
| 321 | Set key_set = attribs[i].attribs.keySet();
|
|---|
| 322 | Iterator iter = key_set.iterator();
|
|---|
| 323 | while (iter.hasNext())
|
|---|
| 324 | {
|
|---|
| 325 | Object obj = iter.next();
|
|---|
| 326 |
|
|---|
| 327 | // Check for attribute match and range match
|
|---|
| 328 | if (obj.equals(attrib))
|
|---|
| 329 | if ((ci.getIndex() >= attribs[i].begin_index) &&
|
|---|
| 330 | (ci.getIndex() <= attribs[i].end_index))
|
|---|
| 331 | return(attribs[i].attribs.get(obj));
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | return(null);
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /*************************************************************************/
|
|---|
| 339 |
|
|---|
| 340 | /**
|
|---|
| 341 | * Return a list of all the attributes and values defined for this
|
|---|
| 342 | * character
|
|---|
| 343 | */
|
|---|
| 344 | public Map
|
|---|
| 345 | getAttributes()
|
|---|
| 346 | {
|
|---|
| 347 | HashMap m = new HashMap();
|
|---|
| 348 | if (attribs == null)
|
|---|
| 349 | return(m);
|
|---|
| 350 |
|
|---|
| 351 | for (int i = 0; i < attribs.length; i++)
|
|---|
| 352 | {
|
|---|
| 353 | if ((ci.getIndex() >= attribs[i].begin_index) &&
|
|---|
| 354 | (ci.getIndex() <= attribs[i].end_index))
|
|---|
| 355 | m.putAll(attribs[i].attribs);
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | return(m);
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | } // class AttributedStringIterator
|
|---|
| 362 |
|
|---|