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