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