43 |
import java.util.HashMap; |
import java.util.HashMap; |
44 |
import java.util.Vector; |
import java.util.Vector; |
45 |
|
|
46 |
|
|
47 |
|
/** |
48 |
|
* This class should not be put public and it is only intended to the |
49 |
|
* classes of the java.text package. Its aim is to build a segmented |
50 |
|
* character iterator by appending strings and adding attributes to |
51 |
|
* portions of strings. The code intends to do some optimization |
52 |
|
* concerning memory consumption and attribute access but at the |
53 |
|
* end it is only an AttributedCharacterIterator. |
54 |
|
* |
55 |
|
* @author Guilhem Lavaux <guilhem@kaffe.org> |
56 |
|
* @date November 22, 2003 |
57 |
|
*/ |
58 |
class FormatCharacterIterator implements AttributedCharacterIterator |
class FormatCharacterIterator implements AttributedCharacterIterator |
59 |
{ |
{ |
60 |
private String formattedString; |
private String formattedString; |
63 |
private int[] ranges; |
private int[] ranges; |
64 |
private HashMap[] attributes; |
private HashMap[] attributes; |
65 |
|
|
66 |
|
/** |
67 |
|
* This constructor builds an empty iterated strings. The attributes |
68 |
|
* are empty and so is the string. However you may append strings |
69 |
|
* and attributes to this iterator. |
70 |
|
*/ |
71 |
FormatCharacterIterator() |
FormatCharacterIterator() |
72 |
{ |
{ |
73 |
formattedString = ""; |
formattedString = ""; |
75 |
attributes = new HashMap[0]; |
attributes = new HashMap[0]; |
76 |
} |
} |
77 |
|
|
78 |
|
/** |
79 |
|
* This constructor take a string <code>s</code>, a set of ranges |
80 |
|
* and the corresponding attributes. This is used to build an iterator. |
81 |
|
* The array <code>ranges</code> should be formatted as follow: |
82 |
|
* each element of <code>ranges</code> specifies the index in the string |
83 |
|
* until which the corresponding map of attributes at the same position |
84 |
|
* is applied. For example, if you have: |
85 |
|
* <pre> |
86 |
|
* s = "hello"; |
87 |
|
* ranges = new int[] { 2, 6 }; |
88 |
|
* attributes = new HashMap[2]; |
89 |
|
* </pre> |
90 |
|
* <code>"he"</code> will have the attributes <code>attributes[0]</code>, |
91 |
|
* <code>"llo"</code> the <code>attributes[1]</code>. |
92 |
|
*/ |
93 |
FormatCharacterIterator (String s, int[] ranges, HashMap[] attributes) |
FormatCharacterIterator (String s, int[] ranges, HashMap[] attributes) |
94 |
{ |
{ |
95 |
formattedString = s; |
formattedString = s; |
97 |
this.attributes = attributes; |
this.attributes = attributes; |
98 |
} |
} |
99 |
|
|
100 |
/* |
/* |
101 |
* ----------------------------------- |
* The following methods are inherited from AttributedCharacterIterator, |
102 |
* AttributedCharacterIterator methods |
* and thus are already documented. |
|
* ----------------------------------- |
|
103 |
*/ |
*/ |
104 |
|
|
105 |
public Set getAllAttributeKeys() |
public Set getAllAttributeKeys() |
106 |
{ |
{ |
107 |
if (attributes != null && attributes[attributeIndex] != null) |
if (attributes != null && attributes[attributeIndex] != null) |
224 |
} |
} |
225 |
|
|
226 |
/* |
/* |
227 |
* --------------------------------- |
* The following methods are inherited from CharacterIterator and thus |
228 |
* CharacterIterator methods |
* are already documented. |
|
* --------------------------------- |
|
229 |
*/ |
*/ |
230 |
|
|
231 |
public char current() |
public char current() |
232 |
{ |
{ |
233 |
return formattedString.charAt (charIndex); |
return formattedString.charAt (charIndex); |
316 |
return formattedString.charAt (charIndex); |
return formattedString.charAt (charIndex); |
317 |
} |
} |
318 |
|
|
319 |
|
/** |
320 |
|
* This method merge the specified attributes and ranges with the |
321 |
|
* internal tables. This method is in charge of the optimization |
322 |
|
* of tables. Two following sets of attributes are never the same. |
323 |
|
* |
324 |
|
* @see #FormatCharacterIterator() |
325 |
|
* |
326 |
|
* @param attributes the new array attributes to apply to the string. |
327 |
|
*/ |
328 |
protected void mergeAttributes (HashMap[] attributes, int[] ranges) |
protected void mergeAttributes (HashMap[] attributes, int[] ranges) |
329 |
{ |
{ |
330 |
Vector new_ranges = new Vector(); |
Vector new_ranges = new Vector(); |
390 |
|
|
391 |
} |
} |
392 |
|
|
393 |
|
/** |
394 |
|
* This method appends to the internal attributed string the attributed |
395 |
|
* string contained in the specified iterator. |
396 |
|
* |
397 |
|
* @param iterator the iterator which contains the attributed string to |
398 |
|
* append to this iterator. |
399 |
|
*/ |
400 |
protected void append (AttributedCharacterIterator iterator) |
protected void append (AttributedCharacterIterator iterator) |
401 |
{ |
{ |
402 |
char c = iterator.first(); |
char c = iterator.first(); |
431 |
ranges = new_ranges; |
ranges = new_ranges; |
432 |
} |
} |
433 |
|
|
434 |
|
/** |
435 |
|
* This method appends an attributed string which attributes are specified |
436 |
|
* directly in the calling parameters. |
437 |
|
* |
438 |
|
* @param text The string to append. |
439 |
|
* @param local_attributes The attributes to put on this string in the |
440 |
|
* iterator. If it is <code>null</code> the string will simply have no |
441 |
|
* attributes. |
442 |
|
*/ |
443 |
protected void append (String text, HashMap local_attributes) |
protected void append (String text, HashMap local_attributes) |
444 |
{ |
{ |
445 |
int[] new_ranges = new int[ranges.length+1]; |
int[] new_ranges = new int[ranges.length+1]; |
455 |
attributes = new_attributes; |
attributes = new_attributes; |
456 |
} |
} |
457 |
|
|
458 |
|
/** |
459 |
|
* This method appends a string without attributes. It is completely |
460 |
|
* equivalent to call {@link #append(String,HashMap)} with local_attributes |
461 |
|
* equal to <code>null</code>. |
462 |
|
* |
463 |
|
* @param text The string to append to the iterator. |
464 |
|
*/ |
465 |
protected void append (String text) |
protected void append (String text) |
466 |
{ |
{ |
467 |
append (text, null); |
append (text, null); |