View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.text.translate;
18  
19  import java.io.IOException;
20  import java.io.StringWriter;
21  import java.io.Writer;
22  import java.util.Locale;
23  
24  /**
25   * An API for translating text.
26   * Its core use is to escape and unescape text. Because escaping and unescaping
27   * is completely contextual, the API does not present two separate signatures.
28   *
29   * @since 3.0
30   * @deprecated as of 3.6, use commons-text
31   * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/CharSequenceTranslator.html">
32   * CharSequenceTranslator</a> instead
33   */
34  @Deprecated
35  public abstract class CharSequenceTranslator {
36  
37      static final char[] HEX_DIGITS = new char[] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
38  
39      /**
40       * Translate a set of codepoints, represented by an int index into a CharSequence,
41       * into another set of codepoints. The number of codepoints consumed must be returned,
42       * and the only IOExceptions thrown must be from interacting with the Writer so that
43       * the top level API may reliably ignore StringWriter IOExceptions.
44       *
45       * @param input CharSequence that is being translated
46       * @param index int representing the current point of translation
47       * @param out Writer to translate the text to
48       * @return int count of codepoints consumed
49       * @throws IOException if and only if the Writer produces an IOException
50       */
51      public abstract int translate(CharSequence input, int index, Writer out) throws IOException;
52  
53      /**
54       * Helper for non-Writer usage.
55       * @param input CharSequence to be translated
56       * @return String output of translation
57       */
58      public final String translate(final CharSequence input) {
59          if (input == null) {
60              return null;
61          }
62          try {
63              final StringWriter writer = new StringWriter(input.length() * 2);
64              translate(input, writer);
65              return writer.toString();
66          } catch (final IOException ioe) {
67              // this should never ever happen while writing to a StringWriter
68              throw new RuntimeException(ioe);
69          }
70      }
71  
72      /**
73       * Translate an input onto a Writer. This is intentionally final as its algorithm is
74       * tightly coupled with the abstract method of this class.
75       *
76       * @param input CharSequence that is being translated
77       * @param out Writer to translate the text to
78       * @throws IOException if and only if the Writer produces an IOException
79       */
80      public final void translate(final CharSequence input, final Writer out) throws IOException {
81          if (out == null) {
82              throw new IllegalArgumentException("The Writer must not be null");
83          }
84          if (input == null) {
85              return;
86          }
87          int pos = 0;
88          final int len = input.length();
89          while (pos < len) {
90              final int consumed = translate(input, pos, out);
91              if (consumed == 0) {
92                  // inlined implementation of Character.toChars(Character.codePointAt(input, pos))
93                  // avoids allocating temp char arrays and duplicate checks
94                  final char c1 = input.charAt(pos);
95                  out.write(c1);
96                  pos++;
97                  if (Character.isHighSurrogate(c1) && pos < len) {
98                      final char c2 = input.charAt(pos);
99                      if (Character.isLowSurrogate(c2)) {
100                       out.write(c2);
101                       pos++;
102                     }
103                 }
104                 continue;
105             }
106             // contract with translators is that they have to understand codepoints
107             // and they just took care of a surrogate pair
108             for (int pt = 0; pt < consumed; pt++) {
109                 pos += Character.charCount(Character.codePointAt(input, pos));
110             }
111         }
112     }
113 
114     /**
115      * Helper method to create a merger of this translator with another set of
116      * translators. Useful in customizing the standard functionality.
117      *
118      * @param translators CharSequenceTranslator array of translators to merge with this one
119      * @return CharSequenceTranslator merging this translator with the others
120      */
121     public final CharSequenceTranslator with(final CharSequenceTranslator... translators) {
122         final CharSequenceTranslator[] newArray = new CharSequenceTranslator[translators.length + 1];
123         newArray[0] = this;
124         System.arraycopy(translators, 0, newArray, 1, translators.length);
125         return new AggregateTranslator(newArray);
126     }
127 
128     /**
129      * <p>Returns an upper case hexadecimal <code>String</code> for the given
130      * character.</p>
131      *
132      * @param codepoint The codepoint to convert.
133      * @return An upper case hexadecimal <code>String</code>
134      */
135     public static String hex(final int codepoint) {
136         return Integer.toHexString(codepoint).toUpperCase(Locale.ENGLISH);
137     }
138 
139 }