KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > TransliteratorIDParser


1 /*
2 **********************************************************************
3 * Copyright (c) 2002-2006, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 01/14/2002 aliu Creation.
8 **********************************************************************
9 */

10
11 package com.ibm.icu.text;
12
13 import com.ibm.icu.util.CaseInsensitiveString;
14 import com.ibm.icu.impl.Utility;
15 import java.text.ParsePosition JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 /**
20  * Parsing component for transliterator IDs. This class contains only
21  * static members; it cannot be instantiated. Methods in this class
22  * parse various ID formats, including the following:
23  *
24  * A basic ID, which contains source, target, and variant, but no
25  * filter and no explicit inverse. Examples include
26  * "Latin-Greek/UNGEGN" and "Null".
27  *
28  * A single ID, which is a basic ID plus optional filter and optional
29  * explicit inverse. Examples include "[a-zA-Z] Latin-Greek" and
30  * "Lower (Upper)".
31  *
32  * A compound ID, which is a sequence of one or more single IDs,
33  * separated by semicolons, with optional forward and reverse global
34  * filters. The global filters are UnicodeSet patterns prepended or
35  * appended to the IDs, separated by semicolons. An appended filter
36  * must be enclosed in parentheses and applies in the reverse
37  * direction.
38  *
39  * @author Alan Liu
40  */

41 class TransliteratorIDParser {
42
43     private static final char ID_DELIM = ';';
44
45     private static final char TARGET_SEP = '-';
46
47     private static final char VARIANT_SEP = '/';
48
49     private static final char OPEN_REV = '(';
50
51     private static final char CLOSE_REV = ')';
52
53     private static final String JavaDoc ANY = "Any";
54
55     private static final int FORWARD = Transliterator.FORWARD;
56
57     private static final int REVERSE = Transliterator.REVERSE;
58
59     private static final Hashtable JavaDoc SPECIAL_INVERSES = new Hashtable JavaDoc();
60
61     /**
62      * A structure containing the parsed data of a filtered ID, that
63      * is, a basic ID optionally with a filter.
64      *
65      * 'source' and 'target' will always be non-null. The 'variant'
66      * will be non-null only if a non-empty variant was parsed.
67      *
68      * 'sawSource' is true if there was an explicit source in the
69      * parsed id. If there was no explicit source, then an implied
70      * source of ANY is returned and 'sawSource' is set to false.
71      *
72      * 'filter' is the parsed filter pattern, or null if there was no
73      * filter.
74      */

75     private static class Specs {
76         public String JavaDoc source; // not null
77
public String JavaDoc target; // not null
78
public String JavaDoc variant; // may be null
79
public String JavaDoc filter; // may be null
80
public boolean sawSource;
81         Specs(String JavaDoc s, String JavaDoc t, String JavaDoc v, boolean sawS, String JavaDoc f) {
82             source = s;
83             target = t;
84             variant = v;
85             sawSource = sawS;
86             filter = f;
87         }
88     }
89
90     /**
91      * A structure containing the canonicalized data of a filtered ID,
92      * that is, a basic ID optionally with a filter.
93      *
94      * 'canonID' is always non-null. It may be the empty string "".
95      * It is the id that should be assigned to the created
96      * transliterator. It _cannot_ be instantiated directly.
97      *
98      * 'basicID' is always non-null and non-empty. It is always of
99      * the form S-T or S-T/V. It is designed to be fed to low-level
100      * instantiation code that only understands these two formats.
101      *
102      * 'filter' may be null, if there is none, or non-null and
103      * non-empty.
104      */

105     static class SingleID {
106         public String JavaDoc canonID;
107         public String JavaDoc basicID;
108         public String JavaDoc filter;
109         SingleID(String JavaDoc c, String JavaDoc b, String JavaDoc f) {
110             canonID = c;
111             basicID = b;
112             filter = f;
113         }
114         SingleID(String JavaDoc c, String JavaDoc b) {
115             this(c, b, null);
116         }
117         Transliterator getInstance() {
118             Transliterator t;
119             if (basicID == null || basicID.length() == 0) {
120                 t = Transliterator.getBasicInstance("Any-Null", canonID);
121             } else {
122                 t = Transliterator.getBasicInstance(basicID, canonID);
123             }
124             if (t != null) {
125                 if (filter != null) {
126                     t.setFilter(new UnicodeSet(filter));
127                 }
128             }
129             return t;
130         }
131     }
132
133     /**
134      * Parse a filter ID, that is, an ID of the general form
135      * "[f1] s1-t1/v1", with the filters optional, and the variants optional.
136      * @param id the id to be parsed
137      * @param pos INPUT-OUTPUT parameter. On input, the position of
138      * the first character to parse. On output, the position after
139      * the last character parsed.
140      * @return a SingleID object or null if the parse fails
141      */

142     public static SingleID parseFilterID(String JavaDoc id, int[] pos) {
143
144         int start = pos[0];
145         Specs specs = parseFilterID(id, pos, true);
146         if (specs == null) {
147             pos[0] = start;
148             return null;
149         }
150
151         // Assemble return results
152
SingleID single = specsToID(specs, FORWARD);
153         single.filter = specs.filter;
154         return single;
155     }
156
157     /**
158      * Parse a single ID, that is, an ID of the general form
159      * "[f1] s1-t1/v1 ([f2] s2-t3/v2)", with the parenthesized element
160      * optional, the filters optional, and the variants optional.
161      * @param id the id to be parsed
162      * @param pos INPUT-OUTPUT parameter. On input, the position of
163      * the first character to parse. On output, the position after
164      * the last character parsed.
165      * @param dir the direction. If the direction is REVERSE then the
166      * SingleID is constructed for the reverse direction.
167      * @return a SingleID object or null
168      */

169     public static SingleID parseSingleID(String JavaDoc id, int[] pos, int dir) {
170
171         int start = pos[0];
172
173         // The ID will be of the form A, A(), A(B), or (B), where
174
// A and B are filter IDs.
175
Specs specsA = null;
176         Specs specsB = null;
177         boolean sawParen = false;
178
179         // On the first pass, look for (B) or (). If this fails, then
180
// on the second pass, look for A, A(B), or A().
181
for (int pass=1; pass<=2; ++pass) {
182             if (pass == 2) {
183                 specsA = parseFilterID(id, pos, true);
184                 if (specsA == null) {
185                     pos[0] = start;
186                     return null;
187                 }
188             }
189             if (Utility.parseChar(id, pos, OPEN_REV)) {
190                 sawParen = true;
191                 if (!Utility.parseChar(id, pos, CLOSE_REV)) {
192                     specsB = parseFilterID(id, pos, true);
193                     // Must close with a ')'
194
if (specsB == null || !Utility.parseChar(id, pos, CLOSE_REV)) {
195                         pos[0] = start;
196                         return null;
197                     }
198                 }
199                 break;
200             }
201         }
202
203         // Assemble return results
204
SingleID single;
205         if (sawParen) {
206             if (dir == FORWARD) {
207                 single = specsToID(specsA, FORWARD);
208                 single.canonID = single.canonID +
209                     OPEN_REV + specsToID(specsB, FORWARD).canonID + CLOSE_REV;
210                 if (specsA != null) {
211                     single.filter = specsA.filter;
212                 }
213             } else {
214                 single = specsToID(specsB, FORWARD);
215                 single.canonID = single.canonID +
216                     OPEN_REV + specsToID(specsA, FORWARD).canonID + CLOSE_REV;
217                 if (specsB != null) {
218                     single.filter = specsB.filter;
219                 }
220             }
221         } else {
222             // assert(specsA != null);
223
if (dir == FORWARD) {
224                 single = specsToID(specsA, FORWARD);
225             } else {
226                 single = specsToSpecialInverse(specsA);
227                 if (single == null) {
228                     single = specsToID(specsA, REVERSE);
229                 }
230             }
231             single.filter = specsA.filter;
232         }
233
234         return single;
235     }
236
237     /**
238      * Parse a global filter of the form "[f]" or "([f])", depending
239      * on 'withParens'.
240      * @param id the pattern the parse
241      * @param pos INPUT-OUTPUT parameter. On input, the position of
242      * the first character to parse. On output, the position after
243      * the last character parsed.
244      * @param dir the direction.
245      * @param withParens INPUT-OUTPUT parameter. On entry, if
246      * withParens[0] is 0, then parens are disallowed. If it is 1,
247      * then parens are requires. If it is -1, then parens are
248      * optional, and the return result will be set to 0 or 1.
249      * @param canonID OUTPUT parameter. The pattern for the filter
250      * added to the canonID, either at the end, if dir is FORWARD, or
251      * at the start, if dir is REVERSE. The pattern will be enclosed
252      * in parentheses if appropriate, and will be suffixed with an
253      * ID_DELIM character. May be null.
254      * @return a UnicodeSet object or null. A non-null results
255      * indicates a successful parse, regardless of whether the filter
256      * applies to the given direction. The caller should discard it
257      * if withParens != (dir == REVERSE).
258      */

259     public static UnicodeSet parseGlobalFilter(String JavaDoc id, int[] pos, int dir,
260                                                int[] withParens,
261                                                StringBuffer JavaDoc canonID) {
262         UnicodeSet filter = null;
263         int start = pos[0];
264
265         if (withParens[0] == -1) {
266             withParens[0] = Utility.parseChar(id, pos, OPEN_REV) ? 1 : 0;
267         } else if (withParens[0] == 1) {
268             if (!Utility.parseChar(id, pos, OPEN_REV)) {
269                 pos[0] = start;
270                 return null;
271             }
272         }
273         
274         Utility.skipWhitespace(id, pos);
275
276         if (UnicodeSet.resemblesPattern(id, pos[0])) {
277             ParsePosition JavaDoc ppos = new ParsePosition JavaDoc(pos[0]);
278             try {
279                 filter = new UnicodeSet(id, ppos, null);
280             } catch (IllegalArgumentException JavaDoc e) {
281                 pos[0] = start;
282                 return null;
283             }
284
285             String JavaDoc pattern = id.substring(pos[0], ppos.getIndex());
286             pos[0] = ppos.getIndex();
287
288             if (withParens[0] == 1 && !Utility.parseChar(id, pos, CLOSE_REV)) {
289                 pos[0] = start;
290                 return null;
291             }
292
293             // In the forward direction, append the pattern to the
294
// canonID. In the reverse, insert it at zero, and invert
295
// the presence of parens ("A" <-> "(A)").
296
if (canonID != null) {
297                 if (dir == FORWARD) {
298                     if (withParens[0] == 1) {
299                         pattern = String.valueOf(OPEN_REV) + pattern + CLOSE_REV;
300                     }
301                     canonID.append(pattern + ID_DELIM);
302                 } else {
303                     if (withParens[0] == 0) {
304                         pattern = String.valueOf(OPEN_REV) + pattern + CLOSE_REV;
305                     }
306                     canonID.insert(0, pattern + ID_DELIM);
307                 }
308             }
309         }
310
311         return filter;
312     }
313
314     /**
315      * Parse a compound ID, consisting of an optional forward global
316      * filter, a separator, one or more single IDs delimited by
317      * separators, an an optional reverse global filter. The
318      * separator is a semicolon. The global filters are UnicodeSet
319      * patterns. The reverse global filter must be enclosed in
320      * parentheses.
321      * @param id the pattern the parse
322      * @param dir the direction.
323      * @param canonID OUTPUT parameter that receives the canonical ID,
324      * consisting of canonical IDs for all elements, as returned by
325      * parseSingleID(), separated by semicolons. Previous contents
326      * are discarded.
327      * @param list OUTPUT parameter that receives a list of SingleID
328      * objects representing the parsed IDs. Previous contents are
329      * discarded.
330      * @param globalFilter OUTPUT parameter that receives a pointer to
331      * a newly created global filter for this ID in this direction, or
332      * null if there is none.
333      * @return true if the parse succeeds, that is, if the entire
334      * id is consumed without syntax error.
335      */

336     public static boolean parseCompoundID(String JavaDoc id, int dir,
337                                           StringBuffer JavaDoc canonID,
338                                           Vector JavaDoc list,
339                                           UnicodeSet[] globalFilter) {
340         int[] pos = new int[] { 0 };
341         int[] withParens = new int[1];
342         list.removeAllElements();
343         UnicodeSet filter;
344         globalFilter[0] = null;
345         canonID.setLength(0);
346
347         // Parse leading global filter, if any
348
withParens[0] = 0; // parens disallowed
349
filter = parseGlobalFilter(id, pos, dir, withParens, canonID);
350         if (filter != null) {
351             if (!Utility.parseChar(id, pos, ID_DELIM)) {
352                 // Not a global filter; backup and resume
353
canonID.setLength(0);
354                 pos[0] = 0;
355             }
356             if (dir == FORWARD) {
357                 globalFilter[0] = filter;
358             }
359         }
360
361         boolean sawDelimiter = true;
362         for (;;) {
363             SingleID single = parseSingleID(id, pos, dir);
364             if (single == null) {
365                 break;
366             }
367             if (dir == FORWARD) {
368                 list.addElement(single);
369             } else {
370                 list.insertElementAt(single, 0);
371             }
372             if (!Utility.parseChar(id, pos, ID_DELIM)) {
373                 sawDelimiter = false;
374                 break;
375             }
376         }
377
378         if (list.size() == 0) {
379             return false;
380         }
381
382         // Construct canonical ID
383
for (int i=0; i<list.size(); ++i) {
384             SingleID single = (SingleID) list.elementAt(i);
385             canonID.append(single.canonID);
386             if (i != (list.size()-1)) {
387                 canonID.append(ID_DELIM);
388             }
389         }
390
391         // Parse trailing global filter, if any, and only if we saw
392
// a trailing delimiter after the IDs.
393
if (sawDelimiter) {
394             withParens[0] = 1; // parens required
395
filter = parseGlobalFilter(id, pos, dir, withParens, canonID);
396             if (filter != null) {
397                 // Don't require trailing ';', but parse it if present
398
Utility.parseChar(id, pos, ID_DELIM);
399                 
400                 if (dir == REVERSE) {
401                     globalFilter[0] = filter;
402                 }
403             }
404         }
405
406         // Trailing unparsed text is a syntax error
407
Utility.skipWhitespace(id, pos[0]);
408         if (pos[0] != id.length()) {
409             return false;
410         }
411
412         return true;
413     }
414
415     /**
416      * Convert the elements of the 'list' vector, which are SingleID
417      * objects, into actual Transliterator objects. In the course of
418      * this, some (or all) entries may be removed. If all entries
419      * are removed, the Null transliterator will be added.
420      *
421      * Delete entries with empty basicIDs; these are generated by
422      * elements like "(A)" in the forward direction, or "A()" in
423      * the reverse. THIS MAY RESULT IN AN EMPTY VECTOR. Convert
424      * SingleID entries to actual transliterators.
425      *
426      * @param list vector of SingleID objects. On exit, vector
427      * of one or more Transliterators.
428      */

429     public static void instantiateList(Vector JavaDoc list) {
430         Transliterator t;
431         for (int i=0; i<=list.size(); ) { // [sic]: i<=list.size()
432
// We run the loop too long by one, so we can
433
// do an insert after the last element
434
if (i==list.size()) {
435                 break;
436             }
437             
438             SingleID single = (SingleID) list.elementAt(i);
439             if (single.basicID.length() == 0) {
440                 list.removeElementAt(i);
441             } else {
442                 t = single.getInstance();
443                 if (t == null) {
444                     t = single.getInstance();
445                     throw new IllegalArgumentException JavaDoc("Illegal ID " + single.canonID);
446                 }
447                 list.setElementAt(t, i);
448                 ++i;
449             }
450         }
451         
452         // An empty list is equivalent to a Null transliterator.
453
if (list.size() == 0) {
454             t = Transliterator.getBasicInstance("Any-Null", null);
455             if (t == null) {
456                 // Should never happen
457
throw new IllegalArgumentException JavaDoc("Internal error; cannot instantiate Any-Null");
458             }
459             list.addElement(t);
460         }
461     }
462
463     /**
464      * Parse an ID into pieces. Take IDs of the form T, T/V, S-T,
465      * S-T/V, or S/V-T. If the source is missing, return a source of
466      * ANY.
467      * @param id the id string, in any of several forms
468      * @return an array of 4 strings: source, target, variant, and
469      * isSourcePresent. If the source is not present, ANY will be
470      * given as the source, and isSourcePresent will be null. Otherwise
471      * isSourcePresent will be non-null. The target may be empty if the
472      * id is not well-formed. The variant may be empty.
473      */

474     public static String JavaDoc[] IDtoSTV(String JavaDoc id) {
475         String JavaDoc source = ANY;
476         String JavaDoc target = null;
477         String JavaDoc variant = "";
478         
479         int sep = id.indexOf(TARGET_SEP);
480         int var = id.indexOf(VARIANT_SEP);
481         if (var < 0) {
482             var = id.length();
483         }
484         boolean isSourcePresent = false;
485         
486         if (sep < 0) {
487             // Form: T/V or T (or /V)
488
target = id.substring(0, var);
489             variant = id.substring(var);
490         } else if (sep < var) {
491             // Form: S-T/V or S-T (or -T/V or -T)
492
if (sep > 0) {
493                 source = id.substring(0, sep);
494               isSourcePresent = true;
495             }
496             target = id.substring(++sep, var);
497             variant = id.substring(var);
498         } else {
499             // Form: (S/V-T or /V-T)
500
if (var > 0) {
501                 source = id.substring(0, var);
502                 isSourcePresent = true;
503             }
504             variant = id.substring(var, sep++);
505             target = id.substring(sep);
506         }
507
508         if (variant.length() > 0) {
509             variant = variant.substring(1);
510         }
511         
512         return new String JavaDoc[] { source, target, variant,
513                               isSourcePresent ? "" : null };
514     }
515
516     /**
517      * Given source, target, and variant strings, concatenate them into a
518      * full ID. If the source is empty, then "Any" will be used for the
519      * source, so the ID will always be of the form s-t/v or s-t.
520      */

521     public static String JavaDoc STVtoID(String JavaDoc source,
522                                  String JavaDoc target,
523                                  String JavaDoc variant) {
524         StringBuffer JavaDoc id = new StringBuffer JavaDoc(source);
525         if (id.length() == 0) {
526             id.append(ANY);
527         }
528         id.append(TARGET_SEP).append(target);
529         if (variant != null && variant.length() != 0) {
530             id.append(VARIANT_SEP).append(variant);
531         }
532         return id.toString();
533     }
534
535     /**
536      * Register two targets as being inverses of one another. For
537      * example, calling registerSpecialInverse("NFC", "NFD", true) causes
538      * Transliterator to form the following inverse relationships:
539      *
540      * <pre>NFC => NFD
541      * Any-NFC => Any-NFD
542      * NFD => NFC
543      * Any-NFD => Any-NFC</pre>
544      *
545      * (Without the special inverse registration, the inverse of NFC
546      * would be NFC-Any.) Note that NFD is shorthand for Any-NFD, but
547      * that the presence or absence of "Any-" is preserved.
548      *
549      * <p>The relationship is symmetrical; registering (a, b) is
550      * equivalent to registering (b, a).
551      *
552      * <p>The relevant IDs must still be registered separately as
553      * factories or classes.
554      *
555      * <p>Only the targets are specified. Special inverses always
556      * have the form Any-Target1 <=> Any-Target2. The target should
557      * have canonical casing (the casing desired to be produced when
558      * an inverse is formed) and should contain no whitespace or other
559      * extraneous characters.
560      *
561      * @param target the target against which to register the inverse
562      * @param inverseTarget the inverse of target, that is
563      * Any-target.getInverse() => Any-inverseTarget
564      * @param bidirectional if true, register the reverse relation
565      * as well, that is, Any-inverseTarget.getInverse() => Any-target
566      */

567     public static void registerSpecialInverse(String JavaDoc target,
568                                               String JavaDoc inverseTarget,
569                                               boolean bidirectional) {
570         SPECIAL_INVERSES.put(new CaseInsensitiveString(target), inverseTarget);
571         if (bidirectional && !target.equalsIgnoreCase(inverseTarget)) {
572             SPECIAL_INVERSES.put(new CaseInsensitiveString(inverseTarget), target);
573         }
574     }
575
576     //----------------------------------------------------------------
577
// Private implementation
578
//----------------------------------------------------------------
579

580     /**
581      * Parse an ID into component pieces. Take IDs of the form T,
582      * T/V, S-T, S-T/V, or S/V-T. If the source is missing, return a
583      * source of ANY.
584      * @param id the id string, in any of several forms
585      * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the
586      * offset of the first character to parse in id. On output,
587      * pos[0] is the offset after the last parsed character. If the
588      * parse failed, pos[0] will be unchanged.
589      * @param allowFilter if true, a UnicodeSet pattern is allowed
590      * at any location between specs or delimiters, and is returned
591      * as the fifth string in the array.
592      * @return a Specs object, or null if the parse failed. If
593      * neither source nor target was seen in the parsed id, then the
594      * parse fails. If allowFilter is true, then the parsed filter
595      * pattern is returned in the Specs object, otherwise the returned
596      * filter reference is null. If the parse fails for any reason
597      * null is returned.
598      */

599     private static Specs parseFilterID(String JavaDoc id, int[] pos,
600                                        boolean allowFilter) {
601         String JavaDoc first = null;
602         String JavaDoc source = null;
603         String JavaDoc target = null;
604         String JavaDoc variant = null;
605         String JavaDoc filter = null;
606         char delimiter = 0;
607         int specCount = 0;
608         int start = pos[0];
609
610         // This loop parses one of the following things with each
611
// pass: a filter, a delimiter character (either '-' or '/'),
612
// or a spec (source, target, or variant).
613
for (;;) {
614             Utility.skipWhitespace(id, pos);
615             if (pos[0] == id.length()) {
616                 break;
617             }
618
619             // Parse filters
620
if (allowFilter && filter == null &&
621                 UnicodeSet.resemblesPattern(id, pos[0])) {
622
623                 ParsePosition JavaDoc ppos = new ParsePosition JavaDoc(pos[0]);
624                 UnicodeSet set = new UnicodeSet(id, ppos, null);
625                 filter = id.substring(pos[0], ppos.getIndex());
626                 pos[0] = ppos.getIndex();
627                 continue;
628             }
629
630             if (delimiter == 0) {
631                 char c = id.charAt(pos[0]);
632                 if ((c == TARGET_SEP && target == null) ||
633                     (c == VARIANT_SEP && variant == null)) {
634                     delimiter = c;
635                     ++pos[0];
636                     continue;
637                 }
638             }
639
640             // We are about to try to parse a spec with no delimiter
641
// when we can no longer do so (we can only do so at the
642
// start); break.
643
if (delimiter == 0 && specCount > 0) {
644                 break;
645             }
646
647             String JavaDoc spec = Utility.parseUnicodeIdentifier(id, pos);
648             if (spec == null) {
649                 // Note that if there was a trailing delimiter, we
650
// consume it. So Foo-, Foo/, Foo-Bar/, and Foo/Bar-
651
// are legal.
652
break;
653             }
654
655             switch (delimiter) {
656             case 0:
657                 first = spec;
658                 break;
659             case TARGET_SEP:
660                 target = spec;
661                 break;
662             case VARIANT_SEP:
663                 variant = spec;
664                 break;
665             }
666             ++specCount;
667             delimiter = 0;
668         }
669
670         // A spec with no prior character is either source or target,
671
// depending on whether an explicit "-target" was seen.
672
if (first != null) {
673             if (target == null) {
674                 target = first;
675             } else {
676                 source = first;
677             }
678         }
679
680         // Must have either source or target
681
if (source == null && target == null) {
682             pos[0] = start;
683             return null;
684         }
685
686         // Empty source or target defaults to ANY
687
boolean sawSource = true;
688         if (source == null) {
689             source = ANY;
690             sawSource = false;
691         }
692         if (target == null) {
693             target = ANY;
694         }
695
696         return new Specs(source, target, variant, sawSource, filter);
697     }
698
699     /**
700      * Givens a Spec object, convert it to a SingleID object. The
701      * Spec object is a more unprocessed parse result. The SingleID
702      * object contains information about canonical and basic IDs.
703      * @return a SingleID; never returns null. Returned object always
704      * has 'filter' field of null.
705      */

706     private static SingleID specsToID(Specs specs, int dir) {
707         String JavaDoc canonID = "";
708         String JavaDoc basicID = "";
709         String JavaDoc basicPrefix = "";
710         if (specs != null) {
711             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
712             if (dir == FORWARD) {
713                 if (specs.sawSource) {
714                     buf.append(specs.source).append(TARGET_SEP);
715                 } else {
716                     basicPrefix = specs.source + TARGET_SEP;
717                 }
718                 buf.append(specs.target);
719             } else {
720                 buf.append(specs.target).append(TARGET_SEP).append(specs.source);
721             }
722             if (specs.variant != null) {
723                 buf.append(VARIANT_SEP).append(specs.variant);
724             }
725             basicID = basicPrefix + buf.toString();
726             if (specs.filter != null) {
727                 buf.insert(0, specs.filter);
728             }
729             canonID = buf.toString();
730         }
731         return new SingleID(canonID, basicID);
732     }
733
734     /**
735      * Given a Specs object, return a SingleID representing the
736      * special inverse of that ID. If there is no special inverse
737      * then return null.
738      * @return a SingleID or null. Returned object always has
739      * 'filter' field of null.
740      */

741     private static SingleID specsToSpecialInverse(Specs specs) {
742         if (!specs.source.equalsIgnoreCase(ANY)) {
743             return null;
744         }
745         String JavaDoc inverseTarget = (String JavaDoc) SPECIAL_INVERSES.get(
746             new CaseInsensitiveString(specs.target));
747         if (inverseTarget != null) {
748             // If the original ID contained "Any-" then make the
749
// special inverse "Any-Foo"; otherwise make it "Foo".
750
// So "Any-NFC" => "Any-NFD" but "NFC" => "NFD".
751
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
752             if (specs.filter != null) {
753                 buf.append(specs.filter);
754             }
755             if (specs.sawSource) {
756                 buf.append(ANY).append(TARGET_SEP);
757             }
758             buf.append(inverseTarget);
759
760             String JavaDoc basicID = ANY + TARGET_SEP + inverseTarget;
761
762             if (specs.variant != null) {
763                 buf.append(VARIANT_SEP).append(specs.variant);
764                 basicID = basicID + VARIANT_SEP + specs.variant;
765             }
766             return new SingleID(buf.toString(), basicID);
767         }
768         return null;
769     }
770 }
771
772 //eof
773
Popular Tags