| 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.UncheckedIOException; |
| 22 | import java.io.Writer; |
| 23 | import java.util.Locale; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import org.apache.commons.lang3.ArrayUtils; |
| 27 | |
| 28 | /** |
| 29 | * An API for translating text. |
| 30 | * Its core use is to escape and unescape text. Because escaping and unescaping |
| 31 | * is completely contextual, the API does not present two separate signatures. |
| 32 | * |
| 33 | * @since 3.0 |
| 34 | * @deprecated As of 3.6, use Apache Commons Text |
| 35 | * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/CharSequenceTranslator.html"> |
| 36 | * CharSequenceTranslator</a> instead |
| 37 | */ |
| 38 | @Deprecated |
| 39 | public abstract class CharSequenceTranslator { |
| 40 | |
| 41 | static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 42 | |
| 43 | /** |
| 44 | * Returns an upper case hexadecimal {@link String} for the given |
| 45 | * character. |
| 46 | * |
| 47 | * @param codePoint The code point to convert. |
| 48 | * @return An upper case hexadecimal {@link String} |
| 49 | */ |
| 50 | public static String hex(final int codePoint) { |
| 51 | return Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Constructs a new instance. |
| 56 | */ |
| 57 | public CharSequenceTranslator() { |
| 58 | // empty |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Helper for non-Writer usage. |
| 63 | * @param input CharSequence to be translated |
| 64 | * @return String output of translation |
| 65 | */ |
| 66 | public final String translate(final CharSequence input) { |
| 67 | if (input == null) { |
| 68 | return null; |
| 69 | } |
| 70 | try { |
| 71 | final StringWriter writer = new StringWriter(input.length() * 2); |
| 72 | translate(input, writer); |
| 73 | return writer.toString(); |
| 74 | } catch (final IOException ioe) { |
| 75 | // this should never ever happen while writing to a StringWriter |
| 76 | throw new UncheckedIOException(ioe); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Translate a set of code points, represented by an int index into a CharSequence, |
| 82 | * into another set of code points. The number of code points consumed must be returned, |
| 83 | * and the only IOExceptions thrown must be from interacting with the Writer so that |
| 84 | * the top level API may reliably ignore StringWriter IOExceptions. |
| 85 | * |
| 86 | * @param input CharSequence that is being translated |
| 87 | * @param index int representing the current point of translation |
| 88 | * @param out Writer to translate the text to |
| 89 | * @return int count of code points consumed |
| 90 | * @throws IOException if and only if the Writer produces an IOException |
| 91 | */ |
| 92 | public abstract int translate(CharSequence input, int index, Writer out) throws IOException; |
| 93 | |
| 94 | /** |
| 95 | * Translate an input onto a Writer. This is intentionally final as its algorithm is |
| 96 | * tightly coupled with the abstract method of this class. |
| 97 | * |
| 98 | * @param input CharSequence that is being translated |
| 99 | * @param writer Writer to translate the text to |
| 100 | * @throws IOException if and only if the Writer produces an IOException |
| 101 | */ |
| 102 | @SuppressWarnings("resource") // Caller closes writer |
| 103 | public final void translate(final CharSequence input, final Writer writer) throws IOException { |
| 104 | Objects.requireNonNull(writer, "writer"); |
| 105 | if (input == null) { |
| 106 | return; |
| 107 | } |
| 108 | int pos = 0; |
| 109 | final int len = input.length(); |
| 110 | while (pos < len) { |
| 111 | final int consumed = translate(input, pos, writer); |
| 112 | if (consumed == 0) { |
| 113 | // inlined implementation of Character.toChars(Character.codePointAt(input, pos)) |
| 114 | // avoids allocating temp char arrays and duplicate checks |
| 115 | final char c1 = input.charAt(pos); |
| 116 | writer.write(c1); |
| 117 | pos++; |
| 118 | if (Character.isHighSurrogate(c1) && pos < len) { |
| 119 | final char c2 = input.charAt(pos); |
| 120 | if (Character.isLowSurrogate(c2)) { |
| 121 | writer.write(c2); |
| 122 | pos++; |
| 123 | } |
| 124 | } |
| 125 | continue; |
| 126 | } |
| 127 | // contract with translators is that they have to understand code points |
| 128 | // and they just took care of a surrogate pair |
| 129 | for (int pt = 0; pt < consumed; pt++) { |
| 130 | pos += Character.charCount(Character.codePointAt(input, pos)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Helper method to create a merger of this translator with another set of |
| 137 | * translators. Useful in customizing the standard functionality. |
| 138 | * |
| 139 | * @param translators CharSequenceTranslator array of translators to merge with this one |
| 140 | * @return CharSequenceTranslator merging this translator with the others |
| 141 | */ |
| 142 | public final CharSequenceTranslator with(final CharSequenceTranslator... translators) { |
| 143 | final CharSequenceTranslator[] newArray = new CharSequenceTranslator[translators.length + 1]; |
| 144 | newArray[0] = this; |
| 145 | return new AggregateTranslator(ArrayUtils.arraycopy(translators, 0, newArray, 1, translators.length)); |
| 146 | } |
| 147 | |
| 148 | } |