1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 *   Copyright (C) 1999-2007, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 *   Date        Name        Description
9 *   11/17/99    aliu        Creation.
10 **********************************************************************
11 */
12 #ifndef RBT_H
13 #define RBT_H
14 
15 #include "unicode/utypes.h"
16 
17 #if !UCONFIG_NO_TRANSLITERATION
18 
19 #include "unicode/translit.h"
20 #include "unicode/utypes.h"
21 #include "unicode/parseerr.h"
22 #include "unicode/udata.h"
23 
24 #define U_ICUDATA_TRANSLIT U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "translit"
25 
26 U_NAMESPACE_BEGIN
27 
28 class TransliterationRuleData;
29 
30 /**
31  * <code>RuleBasedTransliterator</code> is a transliterator
32  * built from a set of rules as defined for
33  * Transliterator::createFromRules().
34  * See the C++ class Transliterator documentation for the rule syntax.
35  *
36  * @author Alan Liu
37  * @internal Use transliterator factory methods instead since this class will be removed in that release.
38  */
39 class RuleBasedTransliterator : public Transliterator {
40 private:
41     /**
42      * The data object is immutable, so we can freely share it with
43      * other instances of RBT, as long as we do NOT own this object.
44      *  TODO:  data is no longer immutable.  See bugs #1866, 2155
45      */
46     TransliterationRuleData* fData;
47 
48     /**
49      * If true, we own the data object and must delete it.
50      */
51     UBool isDataOwned;
52 
53 public:
54 
55     /**
56      * Constructs a new transliterator from the given rules.
57      * @param rules rules, separated by ';'
58      * @param direction either FORWARD or REVERSE.
59      * @exception IllegalArgumentException if rules are malformed.
60      * @internal Use transliterator factory methods instead since this class will be removed in that release.
61      */
62     RuleBasedTransliterator(const UnicodeString& id,
63                             const UnicodeString& rules,
64                             UTransDirection direction,
65                             UnicodeFilter* adoptedFilter,
66                             UParseError& parseError,
67                             UErrorCode& status);
68 
69     /**
70      * Constructs a new transliterator from the given rules.
71      * @param rules rules, separated by ';'
72      * @param direction either FORWARD or REVERSE.
73      * @exception IllegalArgumentException if rules are malformed.
74      * @internal Use transliterator factory methods instead since this class will be removed in that release.
75      */
76     /*RuleBasedTransliterator(const UnicodeString& id,
77                             const UnicodeString& rules,
78                             UTransDirection direction,
79                             UnicodeFilter* adoptedFilter,
80                             UErrorCode& status);*/
81 
82     /**
83      * Convenience constructor with no filter.
84      * @internal Use transliterator factory methods instead since this class will be removed in that release.
85      */
86     /*RuleBasedTransliterator(const UnicodeString& id,
87                             const UnicodeString& rules,
88                             UTransDirection direction,
89                             UErrorCode& status);*/
90 
91     /**
92      * Convenience constructor with no filter and FORWARD direction.
93      * @internal Use transliterator factory methods instead since this class will be removed in that release.
94      */
95     /*RuleBasedTransliterator(const UnicodeString& id,
96                             const UnicodeString& rules,
97                             UErrorCode& status);*/
98 
99     /**
100      * Convenience constructor with FORWARD direction.
101      * @internal Use transliterator factory methods instead since this class will be removed in that release.
102      */
103     /*RuleBasedTransliterator(const UnicodeString& id,
104                             const UnicodeString& rules,
105                             UnicodeFilter* adoptedFilter,
106                             UErrorCode& status);*/
107 private:
108 
109      friend class TransliteratorRegistry; // to access TransliterationRuleData convenience ctor
110     /**
111      * Convenience constructor.
112      * @param id            the id for the transliterator.
113      * @param theData       the rule data for the transliterator.
114      * @param adoptedFilter the filter for the transliterator
115      */
116     RuleBasedTransliterator(const UnicodeString& id,
117                             const TransliterationRuleData* theData,
118                             UnicodeFilter* adoptedFilter = 0);
119 
120 
121     friend class Transliterator; // to access following ct
122 
123     /**
124      * Internal constructor.
125      * @param id            the id for the transliterator.
126      * @param theData       the rule data for the transliterator.
127      * @param isDataAdopted determine who will own the 'data' object. True, the caller should not delete 'data'.
128      */
129     RuleBasedTransliterator(const UnicodeString& id,
130                             TransliterationRuleData* data,
131                             UBool isDataAdopted);
132 
133 public:
134 
135     /**
136      * Copy constructor.
137      * @internal Use transliterator factory methods instead since this class will be removed in that release.
138      */
139     RuleBasedTransliterator(const RuleBasedTransliterator&);
140 
141     virtual ~RuleBasedTransliterator();
142 
143     /**
144      * Implement Transliterator API.
145      * @internal Use transliterator factory methods instead since this class will be removed in that release.
146      */
147     virtual RuleBasedTransliterator* clone() const;
148 
149 protected:
150     /**
151      * Implements {@link Transliterator#handleTransliterate}.
152      * @internal Use transliterator factory methods instead since this class will be removed in that release.
153      */
154     virtual void handleTransliterate(Replaceable& text, UTransPosition& offsets,
155                                      UBool isIncremental) const;
156 
157 public:
158     /**
159      * Return a representation of this transliterator as source rules.
160      * These rules will produce an equivalent transliterator if used
161      * to construct a new transliterator.
162      * @param result the string to receive the rules.  Previous
163      * contents will be deleted.
164      * @param escapeUnprintable if true then convert unprintable
165      * character to their hex escape representations, \uxxxx or
166      * \Uxxxxxxxx.  Unprintable characters are those other than
167      * U+000A, U+0020..U+007E.
168      * @internal Use transliterator factory methods instead since this class will be removed in that release.
169      */
170     virtual UnicodeString& toRules(UnicodeString& result,
171                                    UBool escapeUnprintable) const;
172 
173 protected:
174     /**
175      * Implement Transliterator framework
176      */
177     virtual void handleGetSourceSet(UnicodeSet& result) const;
178 
179 public:
180     /**
181      * Override Transliterator framework
182      */
183     virtual UnicodeSet& getTargetSet(UnicodeSet& result) const;
184 
185     /**
186      * Return the class ID for this class.  This is useful only for
187      * comparing to a return value from getDynamicClassID().  For example:
188      * <pre>
189      * .      Base* polymorphic_pointer = createPolymorphicObject();
190      * .      if (polymorphic_pointer->getDynamicClassID() ==
191      * .          Derived::getStaticClassID()) ...
192      * </pre>
193      * @return          The class ID for all objects of this class.
194      * @internal Use transliterator factory methods instead since this class will be removed in that release.
195      */
196     U_I18N_API static UClassID U_EXPORT2 getStaticClassID(void);
197 
198     /**
199      * Returns a unique class ID <b>polymorphically</b>.  This method
200      * is to implement a simple version of RTTI, since not all C++
201      * compilers support genuine RTTI.  Polymorphic operator==() and
202      * clone() methods call this method.
203      *
204      * @return The class ID for this object. All objects of a given
205      * class have the same class ID.  Objects of other classes have
206      * different class IDs.
207      */
208     virtual UClassID getDynamicClassID(void) const;
209 
210 private:
211 
212     void _construct(const UnicodeString& rules,
213                     UTransDirection direction,
214                     UParseError& parseError,
215                     UErrorCode& status);
216 };
217 
218 
219 U_NAMESPACE_END
220 
221 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
222 
223 #endif
224