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 * @version $Id: CharSequenceTranslator.java 1436770 2013-01-22 07:09:45Z ggregory $
31 */
32 public abstract class CharSequenceTranslator {
33
34 /**
35 * Translate a set of codepoints, represented by an int index into a CharSequence,
36 * into another set of codepoints. The number of codepoints consumed must be returned,
37 * and the only IOExceptions thrown must be from interacting with the Writer so that
38 * the top level API may reliable ignore StringWriter IOExceptions.
39 *
40 * @param input CharSequence that is being translated
41 * @param index int representing the current point of translation
42 * @param out Writer to translate the text to
43 * @return int count of codepoints consumed
44 * @throws IOException if and only if the Writer produces an IOException
45 */
46 public abstract int translate(CharSequence input, int index, Writer out) throws IOException;
47
48 /**
49 * Helper for non-Writer usage.
50 * @param input CharSequence to be translated
51 * @return String output of translation
52 */
53 public final String translate(final CharSequence input) {
54 if (input == null) {
55 return null;
56 }
57 try {
58 final StringWriter writer = new StringWriter(input.length() * 2);
59 translate(input, writer);
60 return writer.toString();
61 } catch (final IOException ioe) {
62 // this should never ever happen while writing to a StringWriter
63 throw new RuntimeException(ioe);
64 }
65 }
66
67 /**
68 * Translate an input onto a Writer. This is intentionally final as its algorithm is
69 * tightly coupled with the abstract method of this class.
70 *
71 * @param input CharSequence that is being translated
72 * @param out Writer to translate the text to
73 * @throws IOException if and only if the Writer produces an IOException
74 */
75 public final void translate(final CharSequence input, final Writer out) throws IOException {
76 if (out == null) {
77 throw new IllegalArgumentException("The Writer must not be null");
78 }
79 if (input == null) {
80 return;
81 }
82 int pos = 0;
83 final int len = input.length();
84 while (pos < len) {
85 final int consumed = translate(input, pos, out);
86 if (consumed == 0) {
87 final char[] c = Character.toChars(Character.codePointAt(input, pos));
88 out.write(c);
89 pos+= c.length;
90 continue;
91 }
92 // // contract with translators is that they have to understand codepoints
93 // // and they just took care of a surrogate pair
94 for (int pt = 0; pt < consumed; pt++) {
95 pos += Character.charCount(Character.codePointAt(input, pt));
96 }
97 }
98 }
99
100 /**
101 * Helper method to create a merger of this translator with another set of
102 * translators. Useful in customizing the standard functionality.
103 *
104 * @param translators CharSequenceTranslator array of translators to merge with this one
105 * @return CharSequenceTranslator merging this translator with the others
106 */
107 public final CharSequenceTranslator with(final CharSequenceTranslator... translators) {
108 final CharSequenceTranslator[] newArray = new CharSequenceTranslator[translators.length + 1];
109 newArray[0] = this;
110 System.arraycopy(translators, 0, newArray, 1, translators.length);
111 return new AggregateTranslator(newArray);
112 }
113
114 /**
115 * <p>Returns an upper case hexadecimal <code>String</code> for the given
116 * character.</p>
117 *
118 * @param codepoint The codepoint to convert.
119 * @return An upper case hexadecimal <code>String</code>
120 */
121 public static String hex(final int codepoint) {
122 return Integer.toHexString(codepoint).toUpperCase(Locale.ENGLISH);
123 }
124
125 }