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.Writer;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.EnumSet;
24
25 /**
26 * Translate XML numeric entities of the form &#[xX]?\d+;? to
27 * the specific code point.
28 *
29 * Note that the semicolon is optional.
30 *
31 * @since 3.0
32 * @deprecated As of 3.6, use Apache Commons Text
33 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/NumericEntityUnescaper.html">
34 * NumericEntityUnescaper</a> instead
35 */
36 @Deprecated
37 public class NumericEntityUnescaper extends CharSequenceTranslator {
38
39 /** Enumerates NumericEntityUnescaper options for unescaping. */
40 public enum OPTION {
41
42 /**
43 * Require a semicolon.
44 */
45 semiColonRequired,
46
47 /**
48 * Do not require a semicolon.
49 */
50 semiColonOptional,
51
52 /**
53 * Throw an exception if a semicolon is missing.
54 */
55 errorIfNoSemiColon
56 }
57
58 // TODO?: Create an OptionsSet class to hide some of the conditional logic below
59 private final EnumSet<OPTION> options;
60
61 /**
62 * Create a UnicodeUnescaper.
63 *
64 * The constructor takes a list of options, only one type of which is currently
65 * available (whether to allow, error or ignore the semicolon on the end of a
66 * numeric entity to being missing).
67 *
68 * For example, to support numeric entities without a ';':
69 * new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional)
70 * and to throw an IllegalArgumentException when they're missing:
71 * new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon)
72 *
73 * Note that the default behavior is to ignore them.
74 *
75 * @param options to apply to this unescaper
76 */
77 public NumericEntityUnescaper(final OPTION... options) {
78 if (options.length > 0) {
79 this.options = EnumSet.copyOf(Arrays.asList(options));
80 } else {
81 this.options = EnumSet.copyOf(Collections.singletonList(OPTION.semiColonRequired));
82 }
83 }
84
85 /**
86 * Whether the passed in option is currently set.
87 *
88 * @param option to check state of
89 * @return whether the option is set
90 */
91 public boolean isSet(final OPTION option) {
92 return options != null && options.contains(option);
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
100 final int seqEnd = input.length();
101 // Uses -2 to ensure there is something after the &#
102 if (input.charAt(index) == '&' && index < seqEnd - 2 && input.charAt(index + 1) == '#') {
103 int start = index + 2;
104 boolean isHex = false;
105
106 final char firstChar = input.charAt(start);
107 if (firstChar == 'x' || firstChar == 'X') {
108 start++;
109 isHex = true;
110
111 // Check there's more than just an x after the &#
112 if (start == seqEnd) {
113 return 0;
114 }
115 }
116
117 int end = start;
118 // Note that this supports character codes without a ; on the end
119 while (end < seqEnd && ( input.charAt(end) >= '0' && input.charAt(end) <= '9' ||
120 input.charAt(end) >= 'a' && input.charAt(end) <= 'f' ||
121 input.charAt(end) >= 'A' && input.charAt(end) <= 'F' ) ) {
122 end++;
123 }
124
125 final boolean semiNext = end != seqEnd && input.charAt(end) == ';';
126
127 if (!semiNext) {
128 if (isSet(OPTION.semiColonRequired)) {
129 return 0;
130 }
131 if (isSet(OPTION.errorIfNoSemiColon)) {
132 throw new IllegalArgumentException("Semi-colon required at end of numeric entity");
133 }
134 }
135
136 final int entityValue;
137 try {
138 if (isHex) {
139 entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 16);
140 } else {
141 entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 10);
142 }
143 } catch (final NumberFormatException nfe) {
144 return 0;
145 }
146
147 if (entityValue > 0xFFFF) {
148 final char[] chars = Character.toChars(entityValue);
149 out.write(chars[0]);
150 out.write(chars[1]);
151 } else {
152 out.write(entityValue);
153 }
154
155 return 2 + end - start + (isHex ? 1 : 0) + (semiNext ? 1 : 0);
156 }
157 return 0;
158 }
159 }