001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.text.translate; 018 019import java.io.IOException; 020import java.io.Writer; 021import java.util.Arrays; 022import java.util.Collections; 023import java.util.EnumSet; 024 025import org.apache.commons.lang3.ArrayUtils; 026import org.apache.commons.lang3.CharUtils; 027 028/** 029 * Translates XML numeric entities of the form &#[xX]?\d+;? to 030 * the specific code point. 031 * 032 * Note that the semicolon is optional. 033 * 034 * @since 1.0 035 */ 036public class NumericEntityUnescaper extends CharSequenceTranslator { 037 038 /** 039 * Enumerates NumericEntityUnescaper options for unescaping. 040 */ 041 public enum OPTION { 042 043 /** 044 * Requires a semicolon. 045 */ 046 semiColonRequired, 047 048 /** 049 * Does not require a semicolon. 050 */ 051 semiColonOptional, 052 053 /** 054 * Throws an exception if a semicolon is missing. 055 */ 056 errorIfNoSemiColon 057 } 058 059 /** Default options. */ 060 private static final EnumSet<OPTION> DEFAULT_OPTIONS = EnumSet 061 .copyOf(Collections.singletonList(OPTION.semiColonRequired)); 062 063 /** EnumSet of OPTIONS, given from the constructor, read-only. */ 064 private final EnumSet<OPTION> options; 065 066 /** 067 * Constructs a new instance. 068 * 069 * The constructor takes a list of options, only one type of which is currently available (whether to allow, error or ignore the semicolon on the end of a 070 * numeric entity to being missing). 071 * <p> 072 * For example, to support numeric entities without a ';': 073 * </p> 074 * 075 * <pre> 076 * new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional) 077 * </pre> 078 * <p> 079 * and to throw an IllegalArgumentException when they're missing: 080 * </p> 081 * 082 * <pre> 083 * new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon) 084 * </pre> 085 * <p> 086 * Note that the default behavior is to ignore them. 087 * </p> 088 * 089 * @param options to apply to this unescaper 090 */ 091 public NumericEntityUnescaper(final OPTION... options) { 092 this.options = ArrayUtils.isEmpty(options) ? DEFAULT_OPTIONS : EnumSet.copyOf(Arrays.asList(options)); 093 } 094 095 /** 096 * Tests whether the passed in option is currently set. 097 * 098 * @param option to check state of. 099 * @return whether the option is set. 100 */ 101 public boolean isSet(final OPTION option) { 102 return options.contains(option); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override 109 public int translate(final CharSequence input, final int index, final Writer writer) throws IOException { 110 final int seqEnd = input.length(); 111 // Uses -2 to ensure there is something after the &# 112 if (input.charAt(index) == '&' && index < seqEnd - 2 && input.charAt(index + 1) == '#') { 113 int start = index + 2; 114 boolean isHex = false; 115 final char firstChar = input.charAt(start); 116 if (firstChar == 'x' || firstChar == 'X') { 117 start++; 118 isHex = true; 119 // Check there's more than just an x after the &# 120 if (start == seqEnd) { 121 return 0; 122 } 123 } 124 int end = start; 125 // Note that this supports character codes without a ; on the end 126 while (end < seqEnd && CharUtils.isHex(input.charAt(end))) { 127 end++; 128 } 129 final boolean semiNext = end != seqEnd && input.charAt(end) == ';'; 130 if (!semiNext) { 131 if (isSet(OPTION.semiColonRequired)) { 132 return 0; 133 } 134 if (isSet(OPTION.errorIfNoSemiColon)) { 135 throw new IllegalArgumentException("Semi-colon required at end of numeric entity"); 136 } 137 } 138 final int entityValue; 139 try { 140 if (isHex) { 141 entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 16); 142 } else { 143 entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 10); 144 } 145 } catch (final NumberFormatException nfe) { 146 return 0; 147 } 148 if (!Character.isValidCodePoint(entityValue)) { 149 return 0; 150 } 151 if (entityValue > 0xFFFF) { 152 final char[] chars = Character.toChars(entityValue); 153 writer.write(chars[0]); 154 writer.write(chars[1]); 155 } else { 156 writer.write(entityValue); 157 } 158 return 2 + end - start + (isHex ? 1 : 0) + (semiNext ? 1 : 0); 159 } 160 return 0; 161 } 162}