Coverage Report - org.apache.commons.lang3.text.translate.NumericEntityEscaper
 
Classes in this File Line Coverage Branch Coverage Complexity
NumericEntityEscaper
100%
20/20
100%
10/10
2
 
 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  
 
 22  
 /**
 23  
  * Translates codepoints to their XML numeric entity escaped value.
 24  
  *
 25  
  * @since 3.0
 26  
  * @version $Id: NumericEntityEscaper.java 1436768 2013-01-22 07:07:42Z ggregory $
 27  
  */
 28  
 public class NumericEntityEscaper extends CodePointTranslator {
 29  
 
 30  
     private final int below;
 31  
     private final int above;
 32  
     private final boolean between;
 33  
 
 34  
     /**
 35  
      * <p>Constructs a <code>NumericEntityEscaper</code> for the specified range. This is
 36  
      * the underlying method for the other constructors/builders. The <code>below</code>
 37  
      * and <code>above</code> boundaries are inclusive when <code>between</code> is
 38  
      * <code>true</code> and exclusive when it is <code>false</code>. </p>
 39  
      *
 40  
      * @param below int value representing the lowest codepoint boundary
 41  
      * @param above int value representing the highest codepoint boundary
 42  
      * @param between whether to escape between the boundaries or outside them
 43  
      */
 44  17
     private NumericEntityEscaper(final int below, final int above, final boolean between) {
 45  17
         this.below = below;
 46  17
         this.above = above;
 47  17
         this.between = between;
 48  17
     }
 49  
 
 50  
     /**
 51  
      * <p>Constructs a <code>NumericEntityEscaper</code> for all characters. </p>
 52  
      */
 53  
     public NumericEntityEscaper() {
 54  1
         this(0, Integer.MAX_VALUE, true);
 55  1
     }
 56  
 
 57  
     /**
 58  
      * <p>Constructs a <code>NumericEntityEscaper</code> below the specified value (exclusive). </p>
 59  
      *
 60  
      * @param codepoint below which to escape
 61  
      * @return the newly created {@code NumericEntityEscaper} instance
 62  
      */
 63  
     public static NumericEntityEscaper below(final int codepoint) {
 64  2
         return outsideOf(codepoint, Integer.MAX_VALUE);
 65  
     }
 66  
 
 67  
     /**
 68  
      * <p>Constructs a <code>NumericEntityEscaper</code> above the specified value (exclusive). </p>
 69  
      *
 70  
      * @param codepoint above which to escape
 71  
      * @return the newly created {@code NumericEntityEscaper} instance
 72  
      */
 73  
     public static NumericEntityEscaper above(final int codepoint) {
 74  2
         return outsideOf(0, codepoint);
 75  
     }
 76  
 
 77  
     /**
 78  
      * <p>Constructs a <code>NumericEntityEscaper</code> between the specified values (inclusive). </p>
 79  
      *
 80  
      * @param codepointLow above which to escape
 81  
      * @param codepointHigh below which to escape
 82  
      * @return the newly created {@code NumericEntityEscaper} instance
 83  
      */
 84  
     public static NumericEntityEscaper between(final int codepointLow, final int codepointHigh) {
 85  12
         return new NumericEntityEscaper(codepointLow, codepointHigh, true);
 86  
     }
 87  
 
 88  
     /**
 89  
      * <p>Constructs a <code>NumericEntityEscaper</code> outside of the specified values (exclusive). </p>
 90  
      *
 91  
      * @param codepointLow below which to escape
 92  
      * @param codepointHigh above which to escape
 93  
      * @return the newly created {@code NumericEntityEscaper} instance
 94  
      */
 95  
     public static NumericEntityEscaper outsideOf(final int codepointLow, final int codepointHigh) {
 96  4
         return new NumericEntityEscaper(codepointLow, codepointHigh, false);
 97  
     }
 98  
 
 99  
     /**
 100  
      * {@inheritDoc}
 101  
      */
 102  
     @Override
 103  
     public boolean translate(final int codepoint, final Writer out) throws IOException {
 104  461
         if(between) {
 105  372
             if (codepoint < below || codepoint > above) {
 106  349
                 return false;
 107  
             }
 108  
         } else {
 109  89
             if (codepoint >= below && codepoint <= above) {
 110  76
                 return false;
 111  
             }
 112  
         }
 113  
 
 114  36
         out.write("&#");
 115  36
         out.write(Integer.toString(codepoint, 10));
 116  36
         out.write(';');
 117  36
         return true;
 118  
     }
 119  
 }