2 **********************************************************************
3 * Copyright (c) 2002-2011, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 01/14/2002 aliu Creation.
8 **********************************************************************
11 package com.ibm.icu.text;
13 import java.text.ParsePosition;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
20 import com.ibm.icu.impl.PatternProps;
21 import com.ibm.icu.impl.Utility;
22 import com.ibm.icu.util.CaseInsensitiveString;
25 * Parsing component for transliterator IDs. This class contains only
26 * static members; it cannot be instantiated. Methods in this class
27 * parse various ID formats, including the following:
29 * A basic ID, which contains source, target, and variant, but no
30 * filter and no explicit inverse. Examples include
31 * "Latin-Greek/UNGEGN" and "Null".
33 * A single ID, which is a basic ID plus optional filter and optional
34 * explicit inverse. Examples include "[a-zA-Z] Latin-Greek" and
37 * A compound ID, which is a sequence of one or more single IDs,
38 * separated by semicolons, with optional forward and reverse global
39 * filters. The global filters are UnicodeSet patterns prepended or
40 * appended to the IDs, separated by semicolons. An appended filter
41 * must be enclosed in parentheses and applies in the reverse
46 class TransliteratorIDParser {
48 private static final char ID_DELIM = ';';
50 private static final char TARGET_SEP = '-';
52 private static final char VARIANT_SEP = '/';
54 private static final char OPEN_REV = '(';
56 private static final char CLOSE_REV = ')';
58 private static final String ANY = "Any";
60 private static final int FORWARD = Transliterator.FORWARD;
62 private static final int REVERSE = Transliterator.REVERSE;
64 private static final Map<CaseInsensitiveString, String> SPECIAL_INVERSES =
65 Collections.synchronizedMap(new HashMap<CaseInsensitiveString, String>());
68 * A structure containing the parsed data of a filtered ID, that
69 * is, a basic ID optionally with a filter.
71 * 'source' and 'target' will always be non-null. The 'variant'
72 * will be non-null only if a non-empty variant was parsed.
74 * 'sawSource' is true if there was an explicit source in the
75 * parsed id. If there was no explicit source, then an implied
76 * source of ANY is returned and 'sawSource' is set to false.
78 * 'filter' is the parsed filter pattern, or null if there was no
81 private static class Specs {
82 public String source; // not null
83 public String target; // not null
84 public String variant; // may be null
85 public String filter; // may be null
86 public boolean sawSource;
87 Specs(String s, String t, String v, boolean sawS, String f) {
97 * A structure containing the canonicalized data of a filtered ID,
98 * that is, a basic ID optionally with a filter.
100 * 'canonID' is always non-null. It may be the empty string "".
101 * It is the id that should be assigned to the created
102 * transliterator. It _cannot_ be instantiated directly.
104 * 'basicID' is always non-null and non-empty. It is always of
105 * the form S-T or S-T/V. It is designed to be fed to low-level
106 * instantiation code that only understands these two formats.
108 * 'filter' may be null, if there is none, or non-null and
111 static class SingleID {
112 public String canonID;
113 public String basicID;
114 public String filter;
115 SingleID(String c, String b, String f) {
120 SingleID(String c, String b) {
123 Transliterator getInstance() {
125 if (basicID == null || basicID.length() == 0) {
126 t = Transliterator.getBasicInstance("Any-Null", canonID);
128 t = Transliterator.getBasicInstance(basicID, canonID);
131 if (filter != null) {
132 t.setFilter(new UnicodeSet(filter));
140 * Parse a filter ID, that is, an ID of the general form
141 * "[f1] s1-t1/v1", with the filters optional, and the variants optional.
142 * @param id the id to be parsed
143 * @param pos INPUT-OUTPUT parameter. On input, the position of
144 * the first character to parse. On output, the position after
145 * the last character parsed.
146 * @return a SingleID object or null if the parse fails
148 public static SingleID parseFilterID(String id, int[] pos) {
151 Specs specs = parseFilterID(id, pos, true);
157 // Assemble return results
158 SingleID single = specsToID(specs, FORWARD);
159 single.filter = specs.filter;
164 * Parse a single ID, that is, an ID of the general form
165 * "[f1] s1-t1/v1 ([f2] s2-t3/v2)", with the parenthesized element
166 * optional, the filters optional, and the variants optional.
167 * @param id the id to be parsed
168 * @param pos INPUT-OUTPUT parameter. On input, the position of
169 * the first character to parse. On output, the position after
170 * the last character parsed.
171 * @param dir the direction. If the direction is REVERSE then the
172 * SingleID is constructed for the reverse direction.
173 * @return a SingleID object or null
175 public static SingleID parseSingleID(String id, int[] pos, int dir) {
179 // The ID will be of the form A, A(), A(B), or (B), where
180 // A and B are filter IDs.
183 boolean sawParen = false;
185 // On the first pass, look for (B) or (). If this fails, then
186 // on the second pass, look for A, A(B), or A().
187 for (int pass=1; pass<=2; ++pass) {
189 specsA = parseFilterID(id, pos, true);
190 if (specsA == null) {
195 if (Utility.parseChar(id, pos, OPEN_REV)) {
197 if (!Utility.parseChar(id, pos, CLOSE_REV)) {
198 specsB = parseFilterID(id, pos, true);
199 // Must close with a ')'
200 if (specsB == null || !Utility.parseChar(id, pos, CLOSE_REV)) {
209 // Assemble return results
212 if (dir == FORWARD) {
213 single = specsToID(specsA, FORWARD);
214 single.canonID = single.canonID +
215 OPEN_REV + specsToID(specsB, FORWARD).canonID + CLOSE_REV;
216 if (specsA != null) {
217 single.filter = specsA.filter;
220 single = specsToID(specsB, FORWARD);
221 single.canonID = single.canonID +
222 OPEN_REV + specsToID(specsA, FORWARD).canonID + CLOSE_REV;
223 if (specsB != null) {
224 single.filter = specsB.filter;
228 // assert(specsA != null);
229 if (dir == FORWARD) {
230 single = specsToID(specsA, FORWARD);
232 single = specsToSpecialInverse(specsA);
233 if (single == null) {
234 single = specsToID(specsA, REVERSE);
237 single.filter = specsA.filter;
244 * Parse a global filter of the form "[f]" or "([f])", depending
246 * @param id the pattern the parse
247 * @param pos INPUT-OUTPUT parameter. On input, the position of
248 * the first character to parse. On output, the position after
249 * the last character parsed.
250 * @param dir the direction.
251 * @param withParens INPUT-OUTPUT parameter. On entry, if
252 * withParens[0] is 0, then parens are disallowed. If it is 1,
253 * then parens are requires. If it is -1, then parens are
254 * optional, and the return result will be set to 0 or 1.
255 * @param canonID OUTPUT parameter. The pattern for the filter
256 * added to the canonID, either at the end, if dir is FORWARD, or
257 * at the start, if dir is REVERSE. The pattern will be enclosed
258 * in parentheses if appropriate, and will be suffixed with an
259 * ID_DELIM character. May be null.
260 * @return a UnicodeSet object or null. A non-null results
261 * indicates a successful parse, regardless of whether the filter
262 * applies to the given direction. The caller should discard it
263 * if withParens != (dir == REVERSE).
265 public static UnicodeSet parseGlobalFilter(String id, int[] pos, int dir,
267 StringBuffer canonID) {
268 UnicodeSet filter = null;
271 if (withParens[0] == -1) {
272 withParens[0] = Utility.parseChar(id, pos, OPEN_REV) ? 1 : 0;
273 } else if (withParens[0] == 1) {
274 if (!Utility.parseChar(id, pos, OPEN_REV)) {
280 pos[0] = PatternProps.skipWhiteSpace(id, pos[0]);
282 if (UnicodeSet.resemblesPattern(id, pos[0])) {
283 ParsePosition ppos = new ParsePosition(pos[0]);
285 filter = new UnicodeSet(id, ppos, null);
286 } catch (IllegalArgumentException e) {
291 String pattern = id.substring(pos[0], ppos.getIndex());
292 pos[0] = ppos.getIndex();
294 if (withParens[0] == 1 && !Utility.parseChar(id, pos, CLOSE_REV)) {
299 // In the forward direction, append the pattern to the
300 // canonID. In the reverse, insert it at zero, and invert
301 // the presence of parens ("A" <-> "(A)").
302 if (canonID != null) {
303 if (dir == FORWARD) {
304 if (withParens[0] == 1) {
305 pattern = String.valueOf(OPEN_REV) + pattern + CLOSE_REV;
307 canonID.append(pattern + ID_DELIM);
309 if (withParens[0] == 0) {
310 pattern = String.valueOf(OPEN_REV) + pattern + CLOSE_REV;
312 canonID.insert(0, pattern + ID_DELIM);
321 * Parse a compound ID, consisting of an optional forward global
322 * filter, a separator, one or more single IDs delimited by
323 * separators, an an optional reverse global filter. The
324 * separator is a semicolon. The global filters are UnicodeSet
325 * patterns. The reverse global filter must be enclosed in
327 * @param id the pattern the parse
328 * @param dir the direction.
329 * @param canonID OUTPUT parameter that receives the canonical ID,
330 * consisting of canonical IDs for all elements, as returned by
331 * parseSingleID(), separated by semicolons. Previous contents
333 * @param list OUTPUT parameter that receives a list of SingleID
334 * objects representing the parsed IDs. Previous contents are
336 * @param globalFilter OUTPUT parameter that receives a pointer to
337 * a newly created global filter for this ID in this direction, or
338 * null if there is none.
339 * @return true if the parse succeeds, that is, if the entire
340 * id is consumed without syntax error.
342 public static boolean parseCompoundID(String id, int dir,
343 StringBuffer canonID,
345 UnicodeSet[] globalFilter) {
346 int[] pos = new int[] { 0 };
347 int[] withParens = new int[1];
350 globalFilter[0] = null;
351 canonID.setLength(0);
353 // Parse leading global filter, if any
354 withParens[0] = 0; // parens disallowed
355 filter = parseGlobalFilter(id, pos, dir, withParens, canonID);
356 if (filter != null) {
357 if (!Utility.parseChar(id, pos, ID_DELIM)) {
358 // Not a global filter; backup and resume
359 canonID.setLength(0);
362 if (dir == FORWARD) {
363 globalFilter[0] = filter;
367 boolean sawDelimiter = true;
369 SingleID single = parseSingleID(id, pos, dir);
370 if (single == null) {
373 if (dir == FORWARD) {
378 if (!Utility.parseChar(id, pos, ID_DELIM)) {
379 sawDelimiter = false;
384 if (list.size() == 0) {
388 // Construct canonical ID
389 for (int i=0; i<list.size(); ++i) {
390 SingleID single = list.get(i);
391 canonID.append(single.canonID);
392 if (i != (list.size()-1)) {
393 canonID.append(ID_DELIM);
397 // Parse trailing global filter, if any, and only if we saw
398 // a trailing delimiter after the IDs.
400 withParens[0] = 1; // parens required
401 filter = parseGlobalFilter(id, pos, dir, withParens, canonID);
402 if (filter != null) {
403 // Don't require trailing ';', but parse it if present
404 Utility.parseChar(id, pos, ID_DELIM);
406 if (dir == REVERSE) {
407 globalFilter[0] = filter;
412 // Trailing unparsed text is a syntax error
413 pos[0] = PatternProps.skipWhiteSpace(id, pos[0]);
414 if (pos[0] != id.length()) {
422 * Returns the list of Transliterator objects for the
423 * given list of SingleID objects.
425 * @param ids list vector of SingleID objects.
426 * @return Actual transliterators for the list of SingleIDs
428 static List<Transliterator> instantiateList(List<SingleID> ids) {
430 List<Transliterator> translits = new ArrayList<Transliterator>();
431 for (SingleID single : ids) {
432 if (single.basicID.length() == 0) {
435 t = single.getInstance();
437 throw new IllegalArgumentException("Illegal ID " + single.canonID);
442 // An empty list is equivalent to a Null transliterator.
443 if (translits.size() == 0) {
444 t = Transliterator.getBasicInstance("Any-Null", null);
446 // Should never happen
447 throw new IllegalArgumentException("Internal error; cannot instantiate Any-Null");
455 * Parse an ID into pieces. Take IDs of the form T, T/V, S-T,
456 * S-T/V, or S/V-T. If the source is missing, return a source of
458 * @param id the id string, in any of several forms
459 * @return an array of 4 strings: source, target, variant, and
460 * isSourcePresent. If the source is not present, ANY will be
461 * given as the source, and isSourcePresent will be null. Otherwise
462 * isSourcePresent will be non-null. The target may be empty if the
463 * id is not well-formed. The variant may be empty.
465 public static String[] IDtoSTV(String id) {
467 String target = null;
470 int sep = id.indexOf(TARGET_SEP);
471 int var = id.indexOf(VARIANT_SEP);
475 boolean isSourcePresent = false;
478 // Form: T/V or T (or /V)
479 target = id.substring(0, var);
480 variant = id.substring(var);
481 } else if (sep < var) {
482 // Form: S-T/V or S-T (or -T/V or -T)
484 source = id.substring(0, sep);
485 isSourcePresent = true;
487 target = id.substring(++sep, var);
488 variant = id.substring(var);
490 // Form: (S/V-T or /V-T)
492 source = id.substring(0, var);
493 isSourcePresent = true;
495 variant = id.substring(var, sep++);
496 target = id.substring(sep);
499 if (variant.length() > 0) {
500 variant = variant.substring(1);
503 return new String[] { source, target, variant,
504 isSourcePresent ? "" : null };
508 * Given source, target, and variant strings, concatenate them into a
509 * full ID. If the source is empty, then "Any" will be used for the
510 * source, so the ID will always be of the form s-t/v or s-t.
512 public static String STVtoID(String source,
515 StringBuilder id = new StringBuilder(source);
516 if (id.length() == 0) {
519 id.append(TARGET_SEP).append(target);
520 if (variant != null && variant.length() != 0) {
521 id.append(VARIANT_SEP).append(variant);
523 return id.toString();
527 * Register two targets as being inverses of one another. For
528 * example, calling registerSpecialInverse("NFC", "NFD", true) causes
529 * Transliterator to form the following inverse relationships:
534 * Any-NFD => Any-NFC</pre>
536 * (Without the special inverse registration, the inverse of NFC
537 * would be NFC-Any.) Note that NFD is shorthand for Any-NFD, but
538 * that the presence or absence of "Any-" is preserved.
540 * <p>The relationship is symmetrical; registering (a, b) is
541 * equivalent to registering (b, a).
543 * <p>The relevant IDs must still be registered separately as
544 * factories or classes.
546 * <p>Only the targets are specified. Special inverses always
547 * have the form Any-Target1 <=> Any-Target2. The target should
548 * have canonical casing (the casing desired to be produced when
549 * an inverse is formed) and should contain no whitespace or other
550 * extraneous characters.
552 * @param target the target against which to register the inverse
553 * @param inverseTarget the inverse of target, that is
554 * Any-target.getInverse() => Any-inverseTarget
555 * @param bidirectional if true, register the reverse relation
556 * as well, that is, Any-inverseTarget.getInverse() => Any-target
558 public static void registerSpecialInverse(String target,
559 String inverseTarget,
560 boolean bidirectional) {
561 SPECIAL_INVERSES.put(new CaseInsensitiveString(target), inverseTarget);
562 if (bidirectional && !target.equalsIgnoreCase(inverseTarget)) {
563 SPECIAL_INVERSES.put(new CaseInsensitiveString(inverseTarget), target);
567 //----------------------------------------------------------------
568 // Private implementation
569 //----------------------------------------------------------------
572 * Parse an ID into component pieces. Take IDs of the form T,
573 * T/V, S-T, S-T/V, or S/V-T. If the source is missing, return a
575 * @param id the id string, in any of several forms
576 * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the
577 * offset of the first character to parse in id. On output,
578 * pos[0] is the offset after the last parsed character. If the
579 * parse failed, pos[0] will be unchanged.
580 * @param allowFilter if true, a UnicodeSet pattern is allowed
581 * at any location between specs or delimiters, and is returned
582 * as the fifth string in the array.
583 * @return a Specs object, or null if the parse failed. If
584 * neither source nor target was seen in the parsed id, then the
585 * parse fails. If allowFilter is true, then the parsed filter
586 * pattern is returned in the Specs object, otherwise the returned
587 * filter reference is null. If the parse fails for any reason
590 private static Specs parseFilterID(String id, int[] pos,
591 boolean allowFilter) {
593 String source = null;
594 String target = null;
595 String variant = null;
596 String filter = null;
601 // This loop parses one of the following things with each
602 // pass: a filter, a delimiter character (either '-' or '/'),
603 // or a spec (source, target, or variant).
605 pos[0] = PatternProps.skipWhiteSpace(id, pos[0]);
606 if (pos[0] == id.length()) {
611 if (allowFilter && filter == null &&
612 UnicodeSet.resemblesPattern(id, pos[0])) {
614 ParsePosition ppos = new ParsePosition(pos[0]);
615 // Parse the set to get the position.
616 new UnicodeSet(id, ppos, null);
617 filter = id.substring(pos[0], ppos.getIndex());
618 pos[0] = ppos.getIndex();
622 if (delimiter == 0) {
623 char c = id.charAt(pos[0]);
624 if ((c == TARGET_SEP && target == null) ||
625 (c == VARIANT_SEP && variant == null)) {
632 // We are about to try to parse a spec with no delimiter
633 // when we can no longer do so (we can only do so at the
635 if (delimiter == 0 && specCount > 0) {
639 String spec = Utility.parseUnicodeIdentifier(id, pos);
641 // Note that if there was a trailing delimiter, we
642 // consume it. So Foo-, Foo/, Foo-Bar/, and Foo/Bar-
662 // A spec with no prior character is either source or target,
663 // depending on whether an explicit "-target" was seen.
665 if (target == null) {
672 // Must have either source or target
673 if (source == null && target == null) {
678 // Empty source or target defaults to ANY
679 boolean sawSource = true;
680 if (source == null) {
684 if (target == null) {
688 return new Specs(source, target, variant, sawSource, filter);
692 * Givens a Spec object, convert it to a SingleID object. The
693 * Spec object is a more unprocessed parse result. The SingleID
694 * object contains information about canonical and basic IDs.
695 * @return a SingleID; never returns null. Returned object always
696 * has 'filter' field of null.
698 private static SingleID specsToID(Specs specs, int dir) {
701 String basicPrefix = "";
703 StringBuilder buf = new StringBuilder();
704 if (dir == FORWARD) {
705 if (specs.sawSource) {
706 buf.append(specs.source).append(TARGET_SEP);
708 basicPrefix = specs.source + TARGET_SEP;
710 buf.append(specs.target);
712 buf.append(specs.target).append(TARGET_SEP).append(specs.source);
714 if (specs.variant != null) {
715 buf.append(VARIANT_SEP).append(specs.variant);
717 basicID = basicPrefix + buf.toString();
718 if (specs.filter != null) {
719 buf.insert(0, specs.filter);
721 canonID = buf.toString();
723 return new SingleID(canonID, basicID);
727 * Given a Specs object, return a SingleID representing the
728 * special inverse of that ID. If there is no special inverse
730 * @return a SingleID or null. Returned object always has
731 * 'filter' field of null.
733 private static SingleID specsToSpecialInverse(Specs specs) {
734 if (!specs.source.equalsIgnoreCase(ANY)) {
737 String inverseTarget = SPECIAL_INVERSES.get(new CaseInsensitiveString(specs.target));
738 if (inverseTarget != null) {
739 // If the original ID contained "Any-" then make the
740 // special inverse "Any-Foo"; otherwise make it "Foo".
741 // So "Any-NFC" => "Any-NFD" but "NFC" => "NFD".
742 StringBuilder buf = new StringBuilder();
743 if (specs.filter != null) {
744 buf.append(specs.filter);
746 if (specs.sawSource) {
747 buf.append(ANY).append(TARGET_SEP);
749 buf.append(inverseTarget);
751 String basicID = ANY + TARGET_SEP + inverseTarget;
753 if (specs.variant != null) {
754 buf.append(VARIANT_SEP).append(specs.variant);
755 basicID = basicID + VARIANT_SEP + specs.variant;
757 return new SingleID(buf.toString(), basicID);