NumericEntityEscaper.java

  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.text.translate;

  18. import java.io.IOException;
  19. import java.io.Writer;

  20. /**
  21.  * Translates codepoints to their XML numeric entity escaped value.
  22.  *
  23.  * @since 1.0
  24.  */
  25. public class NumericEntityEscaper extends CodePointTranslator {

  26.     private final int below;
  27.     private final int above;
  28.     private final boolean between;

  29.     /**
  30.      * <p>Constructs a <code>NumericEntityEscaper</code> for the specified range. This is
  31.      * the underlying method for the other constructors/builders. The <code>below</code>
  32.      * and <code>above</code> boundaries are inclusive when <code>between</code> is
  33.      * <code>true</code> and exclusive when it is <code>false</code>. </p>
  34.      *
  35.      * @param below int value representing the lowest codepoint boundary
  36.      * @param above int value representing the highest codepoint boundary
  37.      * @param between whether to escape between the boundaries or outside them
  38.      */
  39.     private NumericEntityEscaper(final int below, final int above, final boolean between) {
  40.         this.below = below;
  41.         this.above = above;
  42.         this.between = between;
  43.     }

  44.     /**
  45.      * <p>Constructs a <code>NumericEntityEscaper</code> for all characters. </p>
  46.      */
  47.     public NumericEntityEscaper() {
  48.         this(0, Integer.MAX_VALUE, true);
  49.     }

  50.     /**
  51.      * <p>Constructs a <code>NumericEntityEscaper</code> below the specified value (exclusive). </p>
  52.      *
  53.      * @param codepoint below which to escape
  54.      * @return the newly created {@code NumericEntityEscaper} instance
  55.      */
  56.     public static NumericEntityEscaper below(final int codepoint) {
  57.         return outsideOf(codepoint, Integer.MAX_VALUE);
  58.     }

  59.     /**
  60.      * <p>Constructs a <code>NumericEntityEscaper</code> above the specified value (exclusive). </p>
  61.      *
  62.      * @param codepoint above which to escape
  63.      * @return the newly created {@code NumericEntityEscaper} instance
  64.      */
  65.     public static NumericEntityEscaper above(final int codepoint) {
  66.         return outsideOf(0, codepoint);
  67.     }

  68.     /**
  69.      * <p>Constructs a <code>NumericEntityEscaper</code> between the specified values (inclusive). </p>
  70.      *
  71.      * @param codepointLow above which to escape
  72.      * @param codepointHigh below which to escape
  73.      * @return the newly created {@code NumericEntityEscaper} instance
  74.      */
  75.     public static NumericEntityEscaper between(final int codepointLow, final int codepointHigh) {
  76.         return new NumericEntityEscaper(codepointLow, codepointHigh, true);
  77.     }

  78.     /**
  79.      * <p>Constructs a <code>NumericEntityEscaper</code> outside of the specified values (exclusive). </p>
  80.      *
  81.      * @param codepointLow below which to escape
  82.      * @param codepointHigh above which to escape
  83.      * @return the newly created {@code NumericEntityEscaper} instance
  84.      */
  85.     public static NumericEntityEscaper outsideOf(final int codepointLow, final int codepointHigh) {
  86.         return new NumericEntityEscaper(codepointLow, codepointHigh, false);
  87.     }

  88.     /**
  89.      * {@inheritDoc}
  90.      */
  91.     @Override
  92.     public boolean translate(final int codepoint, final Writer out) throws IOException {
  93.         if(between) {
  94.             if (codepoint < below || codepoint > above) {
  95.                 return false;
  96.             }
  97.         } else {
  98.             if (codepoint >= below && codepoint <= above) {
  99.                 return false;
  100.             }
  101.         }

  102.         out.write("&#");
  103.         out.write(Integer.toString(codepoint, 10));
  104.         out.write(';');
  105.         return true;
  106.     }
  107. }