View Javadoc
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  
19  import java.io.IOException;
20  import java.io.Writer;
21  
22  import org.apache.commons.lang3.Range;
23  
24  /**
25   * Translates code points to their XML numeric entity escaped value.
26   *
27   * @since 1.0
28   */
29  public class NumericEntityEscaper extends CodePointTranslator {
30  
31      /**
32       * Constructs a {@code NumericEntityEscaper} above the specified value (exclusive).
33       *
34       * @param codePoint above which to escape
35       * @return The newly created {@code NumericEntityEscaper} instance
36       */
37      public static NumericEntityEscaper above(final int codePoint) {
38          return outsideOf(0, codePoint);
39      }
40  
41      /**
42       * Constructs a {@code NumericEntityEscaper} below the specified value (exclusive).
43       *
44       * @param codePoint below which to escape
45       * @return The newly created {@code NumericEntityEscaper} instance
46       */
47      public static NumericEntityEscaper below(final int codePoint) {
48          return outsideOf(codePoint, Integer.MAX_VALUE);
49      }
50  
51      /**
52       * Constructs a {@code NumericEntityEscaper} between the specified values (inclusive).
53       *
54       * @param codePointLow above which to escape
55       * @param codePointHigh below which to escape
56       * @return The newly created {@code NumericEntityEscaper} instance
57       */
58      public static NumericEntityEscaper between(final int codePointLow, final int codePointHigh) {
59          return new NumericEntityEscaper(codePointLow, codePointHigh, true);
60      }
61  
62      /**
63       * Constructs a {@code NumericEntityEscaper} outside of the specified values (exclusive).
64       *
65       * @param codePointLow below which to escape
66       * @param codePointHigh above which to escape
67       * @return The newly created {@code NumericEntityEscaper} instance
68       */
69      public static NumericEntityEscaper outsideOf(final int codePointLow, final int codePointHigh) {
70          return new NumericEntityEscaper(codePointLow, codePointHigh, false);
71      }
72  
73      /** Whether to escape between the boundaries or outside them. */
74      private final boolean between;
75  
76      /** Range from lowest code point to highest code point. */
77      private final Range<Integer> range;
78  
79      /**
80       * Constructs a {@code NumericEntityEscaper} for all characters.
81       */
82      public NumericEntityEscaper() {
83          this(0, Integer.MAX_VALUE, true);
84      }
85  
86      /**
87       * Constructs a {@code NumericEntityEscaper} for the specified range. This is
88       * the underlying method for the other constructors/builders. The {@code below}
89       * and {@code above} boundaries are inclusive when {@code between} is
90       * {@code true} and exclusive when it is {@code false}.
91       *
92       * @param below int value representing the lowest code point boundary
93       * @param above int value representing the highest code point boundary
94       * @param between whether to escape between the boundaries or outside them
95       */
96      private NumericEntityEscaper(final int below, final int above, final boolean between) {
97          this.range = Range.of(below, above);
98          this.between = between;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public boolean translate(final int codePoint, final Writer writer) throws IOException {
106         if (this.between != this.range.contains(codePoint)) {
107             return false;
108         }
109         writer.write("&#");
110         writer.write(Integer.toString(codePoint, 10));
111         writer.write(';');
112         return true;
113     }
114 }